Initial revision
[citadel.git] / citadel / support.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <ctype.h>
4 #include <stdio.h>
5 #include <pthread.h>
6 #include "citadel.h"
7 #include "server.h"
8 #include "proto.h"
9
10 /*
11  * struncmp()  -  case-insensitive version of strncmp()
12  *                citadel.h will #define a strucmp() based on this
13  */
14 int struncmp(char *lstr, char *rstr, int len)
15 {
16         int pos = 0;
17         char lc,rc;
18         while (pos<len) {
19                 lc=tolower(lstr[pos]);
20                 rc=tolower(rstr[pos]);
21                 if ((lc==0)&&(rc==0)) return(0);
22                 if (lc<rc) return(-1);
23                 if (lc>rc) return(1);
24                 pos=pos+1;
25                 }
26         return(0);
27         }
28
29
30 /*
31  * strproc()  -  make a string 'nice'
32  */
33 void strproc(char *string)
34 {
35         int a;
36
37         if (strlen(string)==0) return;
38
39         /* Convert non-printable characters to blanks */
40         for (a=0; a<strlen(string); ++a) {
41                 if (string[a]<32) string[a]=32;
42                 if (string[a]>126) string[a]=32;
43                 }
44
45         /* Remove leading and trailing blanks */
46         while( (string[0]<33) && (strlen(string)>0) )
47                 strcpy(string,&string[1]);
48         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
49                 string[strlen(string)-1]=0;
50
51         /* Remove double blanks */
52         for (a=0; a<strlen(string); ++a) {
53                 if ((string[a]==32)&&(string[a+1]==32)) {
54                         strcpy(&string[a],&string[a+1]);
55                         a=0;
56                         }
57                 }
58
59         /* remove characters which would interfere with the network */
60         for (a=0; a<strlen(string); ++a) {
61                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
62                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
63                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
64                 while (string[a]==',') strcpy(&string[a],&string[a+1]);
65                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
66                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
67                 }
68
69         }
70
71
72
73 /*
74  * num_parms()  -  discover number of parameters...
75  */
76 int num_parms(char *source)
77 {
78         int a;
79         int count = 1;
80
81         for (a=0; a<strlen(source); ++a) 
82                 if (source[a]=='|') ++count;
83         return(count);
84         }
85
86 /*
87  * extract()  -  extract a parameter from a series of "|" separated...
88  */
89 void extract(char *dest, char *source, int parmnum)
90 {
91         char buf[256];
92         int count = 0;
93         int n;
94
95         if (strlen(source)==0) {
96                 strcpy(dest,"");
97                 return;
98                 }
99
100         n = num_parms(source);
101
102         if (parmnum >= n) {
103                 strcpy(dest,"");
104                 return;
105                 }
106         strcpy(buf,source);
107         if ( (parmnum == 0) && (n == 1) ) {
108                 strcpy(dest,buf);
109                 for (n=0; n<strlen(dest); ++n)
110                         if (dest[n]=='|') dest[n] = 0;
111                 return;
112                 }
113
114         while (count++ < parmnum) do {
115                 strcpy(buf,&buf[1]);
116                 } while( (strlen(buf)>0) && (buf[0]!='|') );
117         if (buf[0]=='|') strcpy(buf,&buf[1]);
118         for (count = 0; count<strlen(buf); ++count)
119                 if (buf[count] == '|') buf[count] = 0;
120         strcpy(dest,buf);
121         }
122
123 /*
124  * extract_int()  -  extract an int parm w/o supplying a buffer
125  */
126 int extract_int(char *source, int parmnum)
127 {
128         char buf[256];
129         
130         extract(buf,source,parmnum);
131         return(atoi(buf));
132         }
133
134 /*
135  * extract_long()  -  extract an long parm w/o supplying a buffer
136  */
137 long extract_long(char *source, long int parmnum)
138 {
139         char buf[256];
140         
141         extract(buf,source,parmnum);
142         return(atol(buf));
143         }
144
145
146
147 /*
148  * get a line of text from a file
149  * ignores lines starting with #
150  */
151 int getstring(FILE *fp, char *string)
152 {
153         int a,c;
154         do {
155                 strcpy(string,"");
156                 a=0;
157                 do {
158                         c=getc(fp);
159                         if (c<0) {
160                                 string[a]=0;
161                                 return(-1);
162                                 }
163                         string[a++]=c;
164                         } while(c!=10);
165                         string[a-1]=0;
166                 } while(string[0]=='#');
167         return(strlen(string));
168         }
169
170
171 /*
172  * sort message pointers
173  */
174 void sort_fullroom(struct fullroom *frptr)
175 {
176         int a,b;
177         long hold;
178         for (a=MSGSPERRM-2; a>=0; --a) {
179                 for (b=0; b<=a; ++b) {
180                         if ((frptr->FRnum[b]) > (frptr->FRnum[b+1])) {
181                                 hold = frptr->FRnum[b];
182                                 frptr->FRnum[b] = frptr->FRnum[b+1];
183                                 frptr->FRnum[b+1] = hold;
184                                 }
185                         }
186                 }
187         }
188
189 /*
190  * pattern2()  -  searches for patn within search string, returns pos 
191  */ 
192 int pattern2(char *search, char *patn)
193 {
194         int a;
195         for (a=0; a<strlen(search); ++a) {
196                 if (!struncmp(&search[a],patn,strlen(patn))) return(a);
197                 }
198         return(-1);
199         }
200
201
202 /*
203  * mesg_locate()  -  locate a message or help file, case insensitive
204  */
205 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
206 {
207         int a;
208         char buf[256];
209         FILE *ls;
210
211         for (a=0; a<numdirs; ++a) {
212                 sprintf(buf,"cd %s; exec ls",dirs[a]);
213                 ls = (FILE *) popen(buf,"r");
214                 if (ls != NULL) {
215                         while(fgets(buf,255,ls)!=NULL) {
216                                 while (isspace(buf[strlen(buf)-1]))
217                                         buf[strlen(buf)-1] = 0;
218                                 if (!strucmp(buf,searchfor)) {
219                                         pclose(ls);
220                                         sprintf(targ,"%s/%s",dirs[a],buf);
221                                         return;
222                                         }
223                                 }
224                         pclose(ls);
225                         }
226                 }
227         strcpy(targ,"");
228         }
229
230
231 #ifdef NO_STRERROR
232 /*
233  * replacement strerror() for systems that don't have it
234  */
235 char *strerror(e)
236 int e; {
237         static char buf[32];
238
239         sprintf(buf,"errno = %d",e);
240         return(buf);
241         }
242 #endif
243