* Brought over yet another new version of the MIME parser from Citadel
[citadel.git] / webcit / tools.c
1 /*
2  * tools.c -- Miscellaneous routines 
3  */
4
5
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <sys/time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30
31 char *ascmonths[] = {
32         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
33         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
34 };
35
36 char *ascdays[] = {
37         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
38 };
39
40
41 char *safestrncpy(char *dest, const char *src, size_t n)
42 {
43         if (dest == NULL || src == NULL) {
44                 lprintf(1, "safestrncpy: NULL argument\n");
45                 abort();
46         }
47         strncpy(dest, src, n);
48         dest[n - 1] = 0;
49         return dest;
50 }
51
52
53
54 /*
55  * num_tokens()  -  discover number of parameters/tokens in a string
56  */
57 int num_tokens(char *source, char tok) {
58         int a;
59         int count = 1;
60
61         if (source == NULL) return(0);
62         for (a=0; a<strlen(source); ++a) {
63                 if (source[a]==tok) ++count;
64         }
65         return(count);
66 }
67
68 /*
69  * extract_token()  -  a smarter string tokenizer
70  */
71 void extract_token(char *dest, char *source, int parmnum, char separator) 
72 {
73         int i;
74         int len;
75         int curr_parm;
76
77         strcpy(dest,"");
78         len = 0;
79         curr_parm = 0;
80
81         if (strlen(source)==0) {
82                 return;
83                 }
84
85         for (i=0; i<strlen(source); ++i) {
86                 if (source[i]==separator) {
87                         ++curr_parm;
88                 }
89                 else if (curr_parm == parmnum) {
90                         dest[len+1] = 0;
91                         dest[len++] = source[i];
92                 }
93         }
94 }
95
96
97
98 /*
99  * remove_token()  -  a tokenizer that kills, maims, and destroys
100  */
101 void remove_token(char *source, int parmnum, char separator)
102 {
103         int i;
104         int len;
105         int curr_parm;
106         int start, end;
107
108         len = 0;
109         curr_parm = 0;
110         start = (-1);
111         end = (-1);
112
113         if (strlen(source)==0) {
114                 return;
115                 }
116
117         for (i=0; i<strlen(source); ++i) {
118                 if ( (start < 0) && (curr_parm == parmnum) ) {
119                         start = i;
120                 }
121
122                 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
123                         end = i;
124                 }
125
126                 if (source[i]==separator) {
127                         ++curr_parm;
128                 }
129         }
130
131         if (end < 0) end = strlen(source);
132
133         strcpy(&source[start], &source[end]);
134 }
135
136
137
138
139 /*
140  * extract_int()  -  extract an int parm w/o supplying a buffer
141  */
142 int extract_int(char *source, int parmnum)
143 {
144         char buf[SIZ];
145         
146         extract_token(buf, source, parmnum, '|');
147         return(atoi(buf));
148 }
149
150 /*
151  * extract_long()  -  extract an long parm w/o supplying a buffer
152  */
153 long extract_long(char *source, long int parmnum)
154 {
155         char buf[SIZ];
156         
157         extract_token(buf, source, parmnum, '|');
158         return(atol(buf));
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 /*
176  * check for the presence of a character within a string (returns count)
177  */
178 int haschar(st, ch)
179 char st[];
180 char ch;
181 {
182         int a, b;
183         b = 0;
184         for (a = 0; a < strlen(st); ++a)
185                 if (st[a] == ch)
186                         ++b;
187         return (b);
188 }
189
190
191 /*
192  * Format a date/time stamp for output 
193  */
194 void fmt_date(char *buf, time_t thetime) {
195         struct tm *tm;
196
197         strcpy(buf, "");
198         tm = localtime(&thetime);
199
200         sprintf(buf, "%s %d %d %2d:%02d%s",
201                 ascmonths[tm->tm_mon],
202                 tm->tm_mday,
203                 tm->tm_year + 1900,
204                 ( (tm->tm_hour > 12) ? (tm->tm_hour - 12) : (tm->tm_hour) ),
205                 tm->tm_min,
206                 ( (tm->tm_hour > 12) ? "pm" : "am" )
207         );
208 }
209
210
211
212
213 /*
214  * Format a date/time stamp to the format used in HTTP headers
215  */
216 void httpdate(char *buf, time_t thetime) {
217         struct tm *tm;
218
219         strcpy(buf, "");
220         tm = localtime(&thetime);
221
222         sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
223                 ascdays[tm->tm_wday],
224                 tm->tm_mday,
225                 ascmonths[tm->tm_mon],
226                 tm->tm_year + 1900,
227                 tm->tm_hour,
228                 tm->tm_min,
229                 tm->tm_sec
230         );
231 }
232
233
234
235
236
237 /*
238  * Utility function to "readline" from memory
239  * (returns new pointer)
240  */
241 char *memreadline(char *start, char *buf, int maxlen)
242 {
243         char ch;
244         char *ptr;
245         int len = 0;    /* tally our own length to avoid strlen() delays */
246
247         ptr = start;
248         memset(buf, 0, maxlen);
249
250         while (1) {
251                 ch = *ptr++;
252                 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
253                         buf[strlen(buf) + 1] = 0;
254                         buf[strlen(buf)] = ch;
255                         ++len;
256                 }
257                 if ((ch == 10) || (ch == 0)) {
258                         return ptr;
259                 }
260         }
261 }
262
263
264
265 /*
266  * pattern2()  -  searches for patn within search string, returns pos
267  */
268 int pattern2(char *search, char *patn)
269 {
270         int a;
271         for (a=0; a<strlen(search); ++a) {
272                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
273                 }
274         return(-1);
275         }
276
277
278 /*
279  * Strip leading and trailing spaces from a string
280  */
281 void striplt(char *buf)
282 {
283         while ((strlen(buf) > 0) && (isspace(buf[0])))
284                 strcpy(buf, &buf[1]);
285         while (isspace(buf[strlen(buf) - 1]))
286                 buf[strlen(buf) - 1] = 0;
287 }
288
289
290
291