2 * tools.c -- Miscellaneous routines
11 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <netinet/in.h>
26 #include "webserver.h"
30 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
31 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
35 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
39 char *safestrncpy(char *dest, const char *src, size_t n)
41 if (dest == NULL || src == NULL) {
42 lprintf(1, "safestrncpy: NULL argument\n");
45 strncpy(dest, src, n);
53 * num_tokens() - discover number of parameters/tokens in a string
55 int num_tokens(char *source, char tok) {
59 if (source == NULL) return(0);
60 for (a=0; a<strlen(source); ++a) {
61 if (source[a]==tok) ++count;
67 * extract_token() - a smarter string tokenizer
69 void extract_token(char *dest, char *source, int parmnum, char separator)
79 if (strlen(source)==0) {
83 for (i=0; i<strlen(source); ++i) {
84 if (source[i]==separator) {
87 else if (curr_parm == parmnum) {
89 dest[len++] = source[i];
97 * remove_token() - a tokenizer that kills, maims, and destroys
99 void remove_token(char *source, int parmnum, char separator)
111 if (strlen(source)==0) {
115 for (i=0; i<strlen(source); ++i) {
116 if ( (start < 0) && (curr_parm == parmnum) ) {
120 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
124 if (source[i]==separator) {
129 if (end < 0) end = strlen(source);
131 strcpy(&source[start], &source[end]);
138 * extract_int() - extract an int parm w/o supplying a buffer
140 int extract_int(char *source, int parmnum)
144 extract_token(buf, source, parmnum, '|');
149 * extract_long() - extract an long parm w/o supplying a buffer
151 long extract_long(char *source, long int parmnum)
155 extract_token(buf, source, parmnum, '|');
174 * check for the presence of a character within a string (returns count)
182 for (a = 0; a < strlen(st); ++a)
190 * Format a date/time stamp for output
192 void fmt_date(char *buf, time_t thetime) {
197 tm = localtime(&thetime);
199 if (hour == 0) hour = 12;
200 else if (hour > 12) hour = hour - 12;
202 sprintf(buf, "%s %d %d %2d:%02d%s",
203 ascmonths[tm->tm_mon],
208 ( (tm->tm_hour > 12) ? "pm" : "am" )
215 * Format TIME ONLY for output
217 void fmt_time(char *buf, time_t thetime) {
222 tm = localtime(&thetime);
224 if (hour == 0) hour = 12;
225 else if (hour > 12) hour = hour - 12;
227 sprintf(buf, "%d:%02d%s",
230 ( (tm->tm_hour > 12) ? "pm" : "am" )
238 * Format a date/time stamp to the format used in HTTP headers
240 void httpdate(char *buf, time_t thetime) {
244 tm = localtime(&thetime);
246 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
247 ascdays[tm->tm_wday],
249 ascmonths[tm->tm_mon],
262 * Utility function to "readline" from memory
263 * (returns new pointer)
265 char *memreadline(char *start, char *buf, int maxlen)
269 int len = 0; /* tally our own length to avoid strlen() delays */
272 memset(buf, 0, maxlen);
276 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
277 buf[strlen(buf) + 1] = 0;
278 buf[strlen(buf)] = ch;
281 if ((ch == 10) || (ch == 0)) {
290 * pattern2() - searches for patn within search string, returns pos
292 int pattern2(char *search, char *patn)
295 for (a=0; a<strlen(search); ++a) {
296 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
303 * Strip leading and trailing spaces from a string
305 void striplt(char *buf)
307 while ((strlen(buf) > 0) && (isspace(buf[0])))
308 strcpy(buf, &buf[1]);
309 while (isspace(buf[strlen(buf) - 1]))
310 buf[strlen(buf) - 1] = 0;
315 * Determine whether the specified message number is contained within the
318 int is_msg_in_mset(char *mset, long msgnum) {
321 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
325 * Now set it for all specified messages.
327 num_sets = num_tokens(mset, ',');
328 for (s=0; s<num_sets; ++s) {
329 extract_token(setstr, mset, s, ',');
331 extract_token(lostr, setstr, 0, ':');
332 if (num_tokens(setstr, ':') >= 2) {
333 extract_token(histr, setstr, 1, ':');
334 if (!strcmp(histr, "*")) {
335 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
339 strcpy(histr, lostr);
344 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
352 * Strip a boundarized substring out of a string (for example, remove
353 * parentheses and anything inside them).
355 * This improved version can strip out *multiple* boundarized substrings.
357 void stripout(char *str, char leftboundary, char rightboundary) {
366 for (a = 0; a < strlen(str); ++a) {
367 if (str[a] == leftboundary) lb = a;
368 if (str[a] == rightboundary) rb = a;
371 if ( (lb > 0) && (rb > lb) ) {
372 strcpy(&str[lb - 1], &str[rb + 1]);
375 } while ( (lb > 0) && (rb > lb) );
382 * Replacement for sleep() that uses select() in order to avoid SIGALRM
384 void sleeeeeeeeeep(int seconds)
390 select(0, NULL, NULL, NULL, &tv);