Changed all of the proprietary strucmp() and struncmp() calls to the more
[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 /*
12  * strproc()  -  make a string 'nice'
13  */
14 void strproc(char *string)
15 {
16         int a;
17
18         if (strlen(string)==0) return;
19
20         /* Convert non-printable characters to blanks */
21         for (a=0; a<strlen(string); ++a) {
22                 if (string[a]<32) string[a]=32;
23                 if (string[a]>126) string[a]=32;
24                 }
25
26         /* Remove leading and trailing blanks */
27         while( (string[0]<33) && (strlen(string)>0) )
28                 strcpy(string,&string[1]);
29         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
30                 string[strlen(string)-1]=0;
31
32         /* Remove double blanks */
33         for (a=0; a<strlen(string); ++a) {
34                 if ((string[a]==32)&&(string[a+1]==32)) {
35                         strcpy(&string[a],&string[a+1]);
36                         a=0;
37                         }
38                 }
39
40         /* remove characters which would interfere with the network */
41         for (a=0; a<strlen(string); ++a) {
42                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
43                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
44                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
45                 while (string[a]==',') strcpy(&string[a],&string[a+1]);
46                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
47                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
48                 }
49
50         }
51
52
53
54 /*
55  * num_parms()  -  discover number of parameters...
56  */
57 int num_parms(char *source)
58 {
59         int a;
60         int count = 1;
61
62         for (a=0; a<strlen(source); ++a) 
63                 if (source[a]=='|') ++count;
64         return(count);
65         }
66
67 /*
68  * extract()  -  extract a parameter from a series of "|" separated...
69  */
70 void extract(char *dest, char *source, int parmnum)
71 {
72         char buf[256];
73         int count = 0;
74         int n;
75
76         if (strlen(source)==0) {
77                 strcpy(dest,"");
78                 return;
79                 }
80
81         n = num_parms(source);
82
83         if (parmnum >= n) {
84                 strcpy(dest,"");
85                 return;
86                 }
87         strcpy(buf,source);
88         if ( (parmnum == 0) && (n == 1) ) {
89                 strcpy(dest,buf);
90                 for (n=0; n<strlen(dest); ++n)
91                         if (dest[n]=='|') dest[n] = 0;
92                 return;
93                 }
94
95         while (count++ < parmnum) do {
96                 strcpy(buf,&buf[1]);
97                 } while( (strlen(buf)>0) && (buf[0]!='|') );
98         if (buf[0]=='|') strcpy(buf,&buf[1]);
99         for (count = 0; count<strlen(buf); ++count)
100                 if (buf[count] == '|') buf[count] = 0;
101         strcpy(dest,buf);
102         }
103
104 /*
105  * extract_int()  -  extract an int parm w/o supplying a buffer
106  */
107 int extract_int(char *source, int parmnum)
108 {
109         char buf[256];
110         
111         extract(buf,source,parmnum);
112         return(atoi(buf));
113         }
114
115 /*
116  * extract_long()  -  extract an long parm w/o supplying a buffer
117  */
118 long extract_long(char *source, long int parmnum)
119 {
120         char buf[256];
121         
122         extract(buf,source,parmnum);
123         return(atol(buf));
124         }
125
126
127
128 /*
129  * get a line of text from a file
130  * ignores lines starting with #
131  */
132 int getstring(FILE *fp, char *string)
133 {
134         int a,c;
135         do {
136                 strcpy(string,"");
137                 a=0;
138                 do {
139                         c=getc(fp);
140                         if (c<0) {
141                                 string[a]=0;
142                                 return(-1);
143                                 }
144                         string[a++]=c;
145                         } while(c!=10);
146                         string[a-1]=0;
147                 } while(string[0]=='#');
148         return(strlen(string));
149         }
150
151
152 /*
153  * pattern2()  -  searches for patn within search string, returns pos 
154  */ 
155 int pattern2(char *search, char *patn)
156 {
157         int a;
158         for (a=0; a<strlen(search); ++a) {
159                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
160                 }
161         return(-1);
162         }
163
164
165 /*
166  * mesg_locate()  -  locate a message or help file, case insensitive
167  */
168 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
169 {
170         int a;
171         char buf[256];
172         FILE *ls;
173
174         for (a=0; a<numdirs; ++a) {
175                 sprintf(buf,"cd %s; exec ls",dirs[a]);
176                 ls = (FILE *) popen(buf,"r");
177                 if (ls != NULL) {
178                         while(fgets(buf,255,ls)!=NULL) {
179                                 while (isspace(buf[strlen(buf)-1]))
180                                         buf[strlen(buf)-1] = 0;
181                                 if (!strcasecmp(buf,searchfor)) {
182                                         pclose(ls);
183                                         sprintf(targ,"%s/%s",dirs[a],buf);
184                                         return;
185                                         }
186                                 }
187                         pclose(ls);
188                         }
189                 }
190         strcpy(targ,"");
191         }
192
193
194 #ifdef NO_STRERROR
195 /*
196  * replacement strerror() for systems that don't have it
197  */
198 char *strerror(e)
199 int e; {
200         static char buf[32];
201
202         sprintf(buf,"errno = %d",e);
203         return(buf);
204         }
205 #endif
206