2 * tools.c -- Miscellaneous routines
13 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
32 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
33 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
37 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
41 char *safestrncpy(char *dest, const char *src, size_t n)
43 if (dest == NULL || src == NULL) {
44 lprintf(1, "safestrncpy: NULL argument\n");
47 strncpy(dest, src, n);
55 * num_tokens() - discover number of parameters/tokens in a string
57 int num_tokens(char *source, char tok) {
61 if (source == NULL) return(0);
62 for (a=0; a<strlen(source); ++a) {
63 if (source[a]==tok) ++count;
69 * extract_token() - a smarter string tokenizer
71 void extract_token(char *dest, char *source, int parmnum, char separator)
81 if (strlen(source)==0) {
85 for (i=0; i<strlen(source); ++i) {
86 if (source[i]==separator) {
89 else if (curr_parm == parmnum) {
91 dest[len++] = source[i];
99 * remove_token() - a tokenizer that kills, maims, and destroys
101 void remove_token(char *source, int parmnum, char separator)
113 if (strlen(source)==0) {
117 for (i=0; i<strlen(source); ++i) {
118 if ( (start < 0) && (curr_parm == parmnum) ) {
122 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
126 if (source[i]==separator) {
131 if (end < 0) end = strlen(source);
133 strcpy(&source[start], &source[end]);
140 * extract_int() - extract an int parm w/o supplying a buffer
142 int extract_int(char *source, int parmnum)
146 extract_token(buf, source, parmnum, '|');
151 * extract_long() - extract an long parm w/o supplying a buffer
153 long extract_long(char *source, long int parmnum)
157 extract_token(buf, source, parmnum, '|');
176 * check for the presence of a character within a string (returns count)
184 for (a = 0; a < strlen(st); ++a)
192 * Format a date/time stamp for output
194 void fmt_date(char *buf, time_t thetime) {
198 tm = localtime(&thetime);
200 sprintf(buf, "%s %d %d %2d:%02d%s",
201 ascmonths[tm->tm_mon],
204 ( (tm->tm_hour > 12) ? (tm->tm_hour - 12) : (tm->tm_hour) ),
206 ( (tm->tm_hour > 12) ? "pm" : "am" )
214 * Format a date/time stamp to the format used in HTTP headers
216 void httpdate(char *buf, time_t thetime) {
220 tm = localtime(&thetime);
222 sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d",
223 ascdays[tm->tm_wday],
225 ascmonths[tm->tm_mon],
238 * Utility function to "readline" from memory
239 * (returns new pointer)
241 char *memreadline(char *start, char *buf, int maxlen)
245 int len = 0; /* tally our own length to avoid strlen() delays */
248 memset(buf, 0, maxlen);
252 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
253 buf[strlen(buf) + 1] = 0;
254 buf[strlen(buf)] = ch;
257 if ((ch == 10) || (ch == 0)) {
266 * pattern2() - searches for patn within search string, returns pos
268 int pattern2(char *search, char *patn)
271 for (a=0; a<strlen(search); ++a) {
272 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
279 * Strip leading and trailing spaces from a string
281 void striplt(char *buf)
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;