]> code.citadel.org Git - citadel.git/blob - webcit/tools.c
0bf0dcc24fe95348908b5d28c378a023d08012ac
[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
29
30 char *ascmonths[] = {
31         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
32         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
33 };
34
35 char *ascdays[] = {
36         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
37 };
38
39
40 char *safestrncpy(char *dest, const char *src, size_t n)
41 {
42         if (dest == NULL || src == NULL) {
43                 fprintf(stderr, "safestrncpy: NULL argument\n");
44                 abort();
45         }
46         strncpy(dest, src, n);
47         dest[n - 1] = 0;
48         return dest;
49 }
50
51
52 /*
53  * num_parms()  -  discover number of parameters...
54  */
55 int num_parms(char *source)
56 {
57         int a;
58         int count = 1;
59
60         for (a = 0; a < strlen(source); ++a)
61                 if (source[a] == '|')
62                         ++count;
63         return (count);
64 }
65
66 /*
67  * extract()  -  extract a parameter from a series of "|" separated...
68  */
69 void extract(char *dest, char *source, int parmnum)
70 {
71         char buf[256];
72         int count = 0;
73         int n;
74
75         if (strlen(source) == 0) {
76                 strcpy(dest, "");
77                 return;
78         }
79         n = num_parms(source);
80
81         if (parmnum >= n) {
82                 strcpy(dest, "");
83                 return;
84         }
85         strcpy(buf, source);
86         if ((parmnum == 0) && (n == 1)) {
87                 strcpy(dest, buf);
88                 for (n = 0; n < strlen(dest); ++n)
89                         if (dest[n] == '|')
90                                 dest[n] = 0;
91                 return;
92         }
93         while (count++ < parmnum)
94                 do {
95                         strcpy(buf, &buf[1]);
96                 } while ((strlen(buf) > 0) && (buf[0] != '|'));
97         if (buf[0] == '|')
98                 strcpy(buf, &buf[1]);
99         for (count = 0; count < strlen(buf); ++count)
100                 if (buf[count] == '|')
101                         buf[count] = 0;
102         strcpy(dest, buf);
103 }
104
105 /*
106  * extract_int()  -  extract an int parm w/o supplying a buffer
107  */
108 int extract_int(char *source, int parmnum)
109 {
110         char buf[256];
111
112         extract(buf, source, parmnum);
113         return (atoi(buf));
114 }
115
116 /*
117  * extract_long()  -  extract an long parm w/o supplying a buffer
118  */
119 long extract_long(char *source, long int parmnum)
120 {
121         char buf[256];
122
123         extract(buf, source, parmnum);
124         return (atol(buf));
125 }
126
127
128 /*
129  * check for the presence of a character within a string (returns count)
130  */
131 int haschar(st, ch)
132 char st[];
133 char ch;
134 {
135         int a, b;
136         b = 0;
137         for (a = 0; a < strlen(st); ++a)
138                 if (st[a] == ch)
139                         ++b;
140         return (b);
141 }
142
143
144 /*
145  * Format a date/time stamp for output 
146  */
147 void fmt_date(char *buf, time_t thetime) {
148         struct tm *tm;
149
150         strcpy(buf, "");
151         tm = localtime(&thetime);
152
153         sprintf(buf, "%s %d %d %2d:%02d%s",
154                 ascmonths[tm->tm_mon],
155                 tm->tm_mday,
156                 tm->tm_year + 1900,
157                 ( (tm->tm_hour > 12) ? (tm->tm_hour - 12) : (tm->tm_hour) ),
158                 tm->tm_min,
159                 ( (tm->tm_hour > 12) ? "pm" : "am" )
160         );
161 }
162
163
164
165
166 /*
167  * Format a date/time stamp to the format used in HTTP headers
168  */
169 void httpdate(char *buf, time_t thetime) {
170         struct tm *tm;
171
172         strcpy(buf, "");
173         tm = localtime(&thetime);
174
175         sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
176                 ascdays[tm->tm_wday],
177                 tm->tm_mday,
178                 ascmonths[tm->tm_mon],
179                 tm->tm_year + 1900,
180                 tm->tm_hour,
181                 tm->tm_min,
182                 tm->tm_sec
183         );
184 }
185
186
187
188
189
190 /*
191  * Utility function to "readline" from memory
192  * (returns new pointer)
193  */
194 char *memreadline(char *start, char *buf, int maxlen)
195 {
196         char ch;
197         char *ptr;
198         int len = 0;    /* tally our own length to avoid strlen() delays */
199
200         ptr = start;
201         memset(buf, 0, maxlen);
202
203         while (1) {
204                 ch = *ptr++;
205                 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
206                         buf[strlen(buf) + 1] = 0;
207                         buf[strlen(buf)] = ch;
208                         ++len;
209                 }
210                 if ((ch == 10) || (ch == 0)) {
211                         return ptr;
212                 }
213         }
214 }
215
216
217
218