4 * Utility functions that are used by both the client and server.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 typedef unsigned char byte; /* Byte type */
36 static byte dtable[256]; /* base64 encode / decode table */
39 char *safestrncpy(char *dest, const char *src, size_t n)
41 if (dest == NULL || src == NULL) {
42 fprintf(stderr, "safestrncpy: NULL argument\n");
45 strncpy(dest, src, n);
52 #ifndef HAVE_STRNCASECMP
53 int strncasecmp(char *lstr, char *rstr, int len)
58 lc=tolower(lstr[pos]);
59 rc=tolower(rstr[pos]);
60 if ((lc==0)&&(rc==0)) return(0);
61 if (lc<rc) return(-1);
72 * num_tokens() - discover number of parameters/tokens in a string
74 int num_tokens(char *source, char tok) {
78 if (source == NULL) return(0);
79 for (a=0; a<strlen(source); ++a) {
80 if (source[a]==tok) ++count;
86 * extract_token() - a smarter string tokenizer
88 void extract_token(char *dest, char *source, int parmnum, char separator)
98 if (strlen(source)==0) {
102 for (i=0; i<strlen(source); ++i) {
103 if (source[i]==separator) {
106 else if (curr_parm == parmnum) {
108 dest[len++] = source[i];
116 * remove_token() - a tokenizer that kills, maims, and destroys
118 void remove_token(char *source, int parmnum, char separator)
130 if (strlen(source)==0) {
134 for (i=0; i<strlen(source); ++i) {
135 if ( (start < 0) && (curr_parm == parmnum) ) {
139 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
143 if (source[i]==separator) {
148 if (end < 0) end = strlen(source);
150 printf("%d .. %d\n", start, end);
152 strcpy(&source[start], &source[end]);
159 * extract_int() - extract an int parm w/o supplying a buffer
161 int extract_int(char *source, int parmnum)
165 extract_token(buf, source, parmnum, '|');
170 * extract_long() - extract an long parm w/o supplying a buffer
172 long extract_long(char *source, long int parmnum)
176 extract_token(buf, source, parmnum, '|');
183 * decode_base64() and encode_base64() are adaptations of code by
184 * John Walker, found in full in the file "base64.c" included with this
185 * distribution. The difference between those functions and these is that
186 * these are intended to encode/decode small string buffers, and those are
187 * intended to encode/decode entire MIME parts.
190 void encode_base64(char *dest, char *source)
192 int i, hiteof = FALSE;
196 /* Fill dtable with character encodings. */
198 for (i = 0; i < 26; i++) {
200 dtable[26 + i] = 'a' + i;
202 for (i = 0; i < 10; i++) {
203 dtable[52 + i] = '0' + i;
209 byte igroup[3], ogroup[4];
212 igroup[0] = igroup[1] = igroup[2] = 0;
213 for (n = 0; n < 3; n++) {
219 igroup[n] = (byte) c;
222 ogroup[0] = dtable[igroup[0] >> 2];
223 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
224 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
225 ogroup[3] = dtable[igroup[2] & 0x3F];
227 /* Replace characters in output stream with "=" pad
228 characters if fewer than three characters were
229 read from the end of the input stream. */
237 for (i = 0; i < 4; i++) {
238 dest[dpos++] = ogroup[i];
247 * Convert base64-encoded to binary. Returns the length of the decoded data.
248 * It will stop after reading 'length' bytes.
250 int decode_base64(char *dest, char *source, size_t length)
256 for (i = 0; i < 255; i++) {
259 for (i = 'A'; i <= 'Z'; i++) {
260 dtable[i] = 0 + (i - 'A');
262 for (i = 'a'; i <= 'z'; i++) {
263 dtable[i] = 26 + (i - 'a');
265 for (i = '0'; i <= '9'; i++) {
266 dtable[i] = 52 + (i - '0');
272 /*CONSTANTCONDITION*/
274 byte a[4], b[4], o[3];
276 for (i = 0; i < 4; i++) {
277 if (spos >= length) {
288 if (dtable[c] & 0x80) {
289 /* Ignoring errors: discard invalid character. */
294 b[i] = (byte) dtable[c];
296 o[0] = (b[0] << 2) | (b[1] >> 4);
297 o[1] = (b[1] << 4) | (b[2] >> 2);
298 o[2] = (b[2] << 6) | b[3];
299 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
300 if (i>=1) dest[dpos++] = o[0];
301 if (i>=2) dest[dpos++] = o[1];
302 if (i>=3) dest[dpos++] = o[2];
313 * Strip leading and trailing spaces from a string
315 void striplt(char *buf)
317 while ((strlen(buf) > 0) && (isspace(buf[0])))
318 strcpy(buf, &buf[1]);
319 while (isspace(buf[strlen(buf) - 1]))
320 buf[strlen(buf) - 1] = 0;
328 * Return the number of occurances of character ch in string st
330 int haschar(char *st, int ch)
334 for (a = 0; a < strlen(st); ++a)
344 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
346 int collapsed_strcmp(char *s1, char *s2) {
350 c1 = malloc(strlen(s1)+1);
351 c2 = malloc(strlen(s2)+1);
356 for (i=0; i<strlen(s1); ++i) {
357 if (isalnum(s1[i])) {
358 c1[pos] = tolower(s1[i]);
364 for (i=0; i<strlen(s2); ++i) {
365 if (isalnum(s2[i])) {
366 c2[pos] = tolower(s2[i]);
371 ret = strcmp(c1, c2);
380 * Format a date/time stamp for output
382 void fmt_date(char *buf, time_t thetime) {
386 char *ascmonths[] = {
387 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
388 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
392 tm = localtime(&thetime);
395 if (hour == 0) hour = 12;
396 else if (hour > 12) hour = hour - 12;
398 sprintf(buf, "%s %d %4d %d:%02d%s",
399 ascmonths[tm->tm_mon],
404 ( (tm->tm_hour >= 12) ? "pm" : "am" )
411 * Determine whether the specified message number is contained within the
414 int is_msg_in_mset(char *mset, long msgnum) {
417 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
421 * Now set it for all specified messages.
423 num_sets = num_tokens(mset, ',');
424 for (s=0; s<num_sets; ++s) {
425 extract_token(setstr, mset, s, ',');
427 extract_token(lostr, setstr, 0, ':');
428 if (num_tokens(setstr, ':') >= 2) {
429 extract_token(histr, setstr, 1, ':');
430 if (!strcmp(histr, "*")) {
431 sprintf(histr, "%ld", LONG_MAX);
435 strcpy(histr, lostr);
440 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
448 * Utility function to "readline" from memory
449 * (returns new pointer)
451 char *memreadline(char *start, char *buf, int maxlen)
455 int len = 0; /* tally our own length to avoid strlen() delays */
458 memset(buf, 0, maxlen);
462 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
463 buf[strlen(buf) + 1] = 0;
464 buf[strlen(buf)] = ch;
467 if ((ch == 10) || (ch == 0)) {
475 * Strip a boundarized substring out of a string (for example, remove
476 * parentheses and anything inside them).
478 void stripout(char *str, char leftboundary, char rightboundary) {
483 for (a = 0; a < strlen(str); ++a) {
484 if (str[a] == leftboundary) lb = a;
485 if (str[a] == rightboundary) rb = a;
488 if ( (lb > 0) && (rb > lb) ) {
489 strcpy(&str[lb - 1], &str[rb + 1]);
496 * Reduce a string down to a boundarized substring (for example, remove
497 * parentheses and anything outside them).
499 void stripallbut(char *str, char leftboundary, char rightboundary) {
502 for (a = 0; a < strlen(str); ++ a) {
503 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
506 for (a = 0; a < strlen(str); ++ a) {
507 if (str[a] == rightboundary) str[a] = 0;