4 * Utility functions that are used by both the client and server.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
36 int err_printf(char *fmt, ...);
37 int err_printf(char *fmt, ...)
42 retval = fprintf(stderr, fmt, ap);
51 typedef unsigned char byte; /* Byte type */
52 static byte dtable[256]; /* base64 encode / decode table */
55 char *safestrncpy(char *dest, const char *src, size_t n)
57 if (dest == NULL || src == NULL) {
58 err_printf("safestrncpy: NULL argument\n");
61 strncpy(dest, src, n);
68 #ifndef HAVE_STRNCASECMP
69 int strncasecmp(char *lstr, char *rstr, int len)
74 lc=tolower(lstr[pos]);
75 rc=tolower(rstr[pos]);
76 if ((lc==0)&&(rc==0)) return(0);
77 if (lc<rc) return(-1);
88 * num_tokens() - discover number of parameters/tokens in a string
90 int num_tokens(char *source, char tok) {
94 if (source == NULL) return(0);
95 for (a=0; a<strlen(source); ++a) {
96 if (source[a]==tok) ++count;
102 * extract_token() - a smarter string tokenizer
104 void extract_token(char *dest, char *source, int parmnum, char separator)
114 if (strlen(source)==0) {
118 for (i=0; i<strlen(source); ++i) {
119 if (source[i]==separator) {
122 else if (curr_parm == parmnum) {
124 dest[len++] = source[i];
132 * remove_token() - a tokenizer that kills, maims, and destroys
134 void remove_token(char *source, int parmnum, char separator)
146 if (strlen(source)==0) {
150 for (i=0; i<strlen(source); ++i) {
151 if ( (start < 0) && (curr_parm == parmnum) ) {
155 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
159 if (source[i]==separator) {
164 if (end < 0) end = strlen(source);
165 strcpy(&source[start], &source[end]);
172 * extract_int() - extract an int parm w/o supplying a buffer
174 int extract_int(char *source, int parmnum)
178 extract_token(buf, source, parmnum, '|');
183 * extract_long() - extract an long parm w/o supplying a buffer
185 long extract_long(char *source, long int parmnum)
189 extract_token(buf, source, parmnum, '|');
196 * decode_base64() and encode_base64() are adaptations of code by
197 * John Walker, found in full in the file "base64.c" included with this
198 * distribution. The difference between those functions and these is that
199 * these are intended to encode/decode small string buffers, and those are
200 * intended to encode/decode entire MIME parts.
203 void encode_base64(char *dest, char *source)
205 int i, hiteof = FALSE;
209 /* Fill dtable with character encodings. */
211 for (i = 0; i < 26; i++) {
213 dtable[26 + i] = 'a' + i;
215 for (i = 0; i < 10; i++) {
216 dtable[52 + i] = '0' + i;
222 byte igroup[3], ogroup[4];
225 igroup[0] = igroup[1] = igroup[2] = 0;
226 for (n = 0; n < 3; n++) {
232 igroup[n] = (byte) c;
235 ogroup[0] = dtable[igroup[0] >> 2];
236 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
237 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
238 ogroup[3] = dtable[igroup[2] & 0x3F];
240 /* Replace characters in output stream with "=" pad
241 characters if fewer than three characters were
242 read from the end of the input stream. */
250 for (i = 0; i < 4; i++) {
251 dest[dpos++] = ogroup[i];
260 * Convert base64-encoded to binary. Returns the length of the decoded data.
261 * It will stop after reading 'length' bytes.
263 int decode_base64(char *dest, char *source, size_t length)
269 for (i = 0; i < 255; i++) {
272 for (i = 'A'; i <= 'Z'; i++) {
273 dtable[i] = 0 + (i - 'A');
275 for (i = 'a'; i <= 'z'; i++) {
276 dtable[i] = 26 + (i - 'a');
278 for (i = '0'; i <= '9'; i++) {
279 dtable[i] = 52 + (i - '0');
285 /*CONSTANTCONDITION*/
287 byte a[4], b[4], o[3];
289 for (i = 0; i < 4; i++) {
290 if (spos >= length) {
301 if (dtable[c] & 0x80) {
302 /* Ignoring errors: discard invalid character. */
307 b[i] = (byte) dtable[c];
309 o[0] = (b[0] << 2) | (b[1] >> 4);
310 o[1] = (b[1] << 4) | (b[2] >> 2);
311 o[2] = (b[2] << 6) | b[3];
312 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
313 if (i>=1) dest[dpos++] = o[0];
314 if (i>=2) dest[dpos++] = o[1];
315 if (i>=3) dest[dpos++] = o[2];
326 * Strip leading and trailing spaces from a string
328 void striplt(char *buf)
330 while ((strlen(buf) > 0) && (isspace(buf[0])))
331 strcpy(buf, &buf[1]);
332 while (isspace(buf[strlen(buf) - 1]))
333 buf[strlen(buf) - 1] = 0;
341 * Return the number of occurances of character ch in string st
343 int haschar(const char *st, int ch)
347 for (a = 0; a < strlen(st); ++a)
357 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
359 int collapsed_strcmp(char *s1, char *s2) {
363 c1 = malloc(strlen(s1)+1);
364 c2 = malloc(strlen(s2)+1);
369 for (i=0; i<strlen(s1); ++i) {
370 if (isalnum(s1[i])) {
371 c1[pos] = tolower(s1[i]);
377 for (i=0; i<strlen(s2); ++i) {
378 if (isalnum(s2[i])) {
379 c2[pos] = tolower(s2[i]);
384 ret = strcmp(c1, c2);
393 * Format a date/time stamp for output
394 * seconds is whether to print the seconds
396 void fmt_date(char *buf, time_t thetime, int seconds) {
400 char *ascmonths[] = {
401 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
402 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
406 tm = localtime(&thetime);
409 if (hour == 0) hour = 12;
410 else if (hour > 12) hour = hour - 12;
413 sprintf(buf, "%s %d %4d %d:%02d:%02d%s",
414 ascmonths[tm->tm_mon],
420 ( (tm->tm_hour >= 12) ? "pm" : "am" )
423 sprintf(buf, "%s %d %4d %d:%02d%s",
424 ascmonths[tm->tm_mon],
429 ( (tm->tm_hour >= 12) ? "pm" : "am" )
437 * Determine whether the specified message number is contained within the
440 int is_msg_in_mset(char *mset, long msgnum) {
443 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
447 * Now set it for all specified messages.
449 num_sets = num_tokens(mset, ',');
450 for (s=0; s<num_sets; ++s) {
451 extract_token(setstr, mset, s, ',');
453 extract_token(lostr, setstr, 0, ':');
454 if (num_tokens(setstr, ':') >= 2) {
455 extract_token(histr, setstr, 1, ':');
456 if (!strcmp(histr, "*")) {
457 sprintf(histr, "%ld", LONG_MAX);
461 strcpy(histr, lostr);
466 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
474 * Utility function to "readline" from memory
475 * (returns new pointer)
477 char *memreadline(char *start, char *buf, int maxlen)
481 int len = 0; /* tally our own length to avoid strlen() delays */
484 memset(buf, 0, maxlen);
488 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
489 buf[strlen(buf) + 1] = 0;
490 buf[strlen(buf)] = ch;
493 if ((ch == 10) || (ch == 0)) {
501 * Strip a boundarized substring out of a string (for example, remove
502 * parentheses and anything inside them).
504 void stripout(char *str, char leftboundary, char rightboundary) {
509 for (a = 0; a < strlen(str); ++a) {
510 if (str[a] == leftboundary) lb = a;
511 if (str[a] == rightboundary) rb = a;
514 if ( (lb > 0) && (rb > lb) ) {
515 strcpy(&str[lb - 1], &str[rb + 1]);
522 * Reduce a string down to a boundarized substring (for example, remove
523 * parentheses and anything outside them).
525 void stripallbut(char *str, char leftboundary, char rightboundary) {
528 for (a = 0; a < strlen(str); ++ a) {
529 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
532 for (a = 0; a < strlen(str); ++ a) {
533 if (str[a] == rightboundary) str[a] = 0;