Updated the CtdlUserGoto() API call to also return the oldest and newest message...
[citadel.git] / citadel / support.c
1 /*
2  * Server-side utility functions
3  */
4
5 #include "sysdep.h"
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <libcitadel.h>
10
11 #include "citadel.h"
12 #include "support.h"
13
14 /*
15  * strproc()  -  make a string 'nice'
16  */
17 void strproc(char *string)
18 {
19         int a, b;
20
21         if (string == NULL) return;
22         if (IsEmptyStr(string)) return;
23
24         /* Convert non-printable characters to blanks */
25         for (a=0; !IsEmptyStr(&string[a]); ++a) {
26                 if (string[a]<32) string[a]=32;
27                 if (string[a]>126) string[a]=32;
28         }
29
30         /* a is now the length of our string. */
31         /* Remove leading and trailing blanks */
32         while( (string[a-1]<33) && (!IsEmptyStr(string)) )
33                 string[--a]=0;
34         b = 0;
35         while( (string[b]<33) && (!IsEmptyStr(&string[b])) )
36                 b++;
37         if (b > 0)
38                 memmove(string,&string[b], a - b + 1);
39
40         /* Remove double blanks */
41         for (a=0; !IsEmptyStr(&string[a]); ++a) {
42                 if ((string[a]==32)&&(string[a+1]==32)) {
43                         strcpy(&string[a],&string[a+1]);
44                         a=0;
45                 }
46         }
47
48         /* remove characters which would interfere with the network */
49         for (a=0; !IsEmptyStr(&string[a]); ++a) {
50                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
51                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
52                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
53                 while (string[a]==',') strcpy(&string[a],&string[a+1]);
54                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
55                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
56         }
57
58 }
59
60
61
62 /*
63  * get a line of text from a file
64  * ignores lines starting with #
65  */
66 int getstring(FILE *fp, char *string)
67 {
68         int a,c;
69         do {
70                 strcpy(string,"");
71                 a=0;
72                 do {
73                         c=getc(fp);
74                         if (c<0) {
75                                 string[a]=0;
76                                 return(-1);
77                                 }
78                         string[a++]=c;
79                         } while(c!=10);
80                         string[a-1]=0;
81                 } while(string[0]=='#');
82         return(strlen(string));
83 }
84
85
86
87
88 /*
89  * mesg_locate()  -  locate a message or help file, case insensitive
90  */
91 void mesg_locate(char *targ, size_t n, const char *searchfor,
92                  int numdirs, const char * const *dirs)
93 {
94         int a;
95         char buf[SIZ];
96         struct stat test;
97
98         for (a=0; a<numdirs; ++a) {
99                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
100                 if (!stat(buf, &test)) {
101                         snprintf(targ, n, "%s/%s", dirs[a], searchfor);
102                         return;
103                 }
104         }
105         strcpy(targ,"");
106 }
107
108
109 #ifndef HAVE_STRERROR
110 /*
111  * replacement strerror() for systems that don't have it
112  */
113 char *strerror(int e)
114 {
115         static char buf[32];
116
117         snprintf(buf,sizeof buf,"errno = %d",e);
118         return(buf);
119         }
120 #endif
121