4 * Utility functions that are used by both the client and server.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
38 typedef unsigned char byte; /* Byte type */
39 static byte dtable[256]; /* base64 encode / decode table */
42 char *safestrncpy(char *dest, const char *src, size_t n)
44 if (dest == NULL || src == NULL) {
45 fprintf(stderr, "safestrncpy: NULL argument\n");
48 strncpy(dest, src, n);
55 #ifndef HAVE_STRNCASECMP
56 int strncasecmp(char *lstr, char *rstr, int len)
61 lc=tolower(lstr[pos]);
62 rc=tolower(rstr[pos]);
63 if ((lc==0)&&(rc==0)) return(0);
64 if (lc<rc) return(-1);
75 * num_tokens() - discover number of parameters/tokens in a string
77 int num_tokens(char *source, char tok) {
81 if (source == NULL) return(0);
82 for (a=0; a<strlen(source); ++a) {
83 if (source[a]==tok) ++count;
89 /* extract_token_fast() - a smarter string tokenizer */
90 void extract_token(char *dest, char *source, int parmnum, char separator)
92 char *d, *s; /* dest, source */
97 /* Locate desired parameter */
99 while (count < parmnum) {
100 /* End of string, bail! */
105 if (*s == separator) {
110 if (!s) return; /* Parameter not found */
112 for (d = dest; *s && *s != separator; s++, d++) {
120 * extract_token() - a smarter string tokenizer
122 void extract_token_old(char *dest, char *source, int parmnum, char separator)
132 if (strlen(source)==0) {
136 for (i=0; i<strlen(source); ++i) {
137 if (source[i]==separator) {
140 else if (curr_parm == parmnum) {
142 dest[len++] = source[i];
149 /* remove_token_fast() - a tokenizer that kills, maims, and destroys fast */
150 void remove_token(char *source, int parmnum, char separator)
152 char *d, *s; /* dest, source */
155 /* Find desired parameter */
157 while (count < parmnum) {
158 /* End of string, bail! */
163 if (*d == separator) {
168 if (!d) return; /* Parameter not found */
170 /* Find next parameter */
172 while (*s && *s != separator) {
179 else if (d == source)
193 * remove_token() - a tokenizer that kills, maims, and destroys
195 void remove_token_old(char *source, int parmnum, char separator)
207 if (strlen(source)==0) {
211 for (i=0; i<strlen(source); ++i) {
212 if ( (start < 0) && (curr_parm == parmnum) ) {
216 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
220 if (source[i]==separator) {
225 if (end < 0) end = strlen(source);
226 strcpy(&source[start], &source[end]);
233 * extract_int() - extract an int parm w/o supplying a buffer
235 int extract_int(char *source, int parmnum)
239 extract_token(buf, source, parmnum, '|');
244 * extract_long() - extract an long parm w/o supplying a buffer
246 long extract_long(char *source, long int parmnum)
250 extract_token(buf, source, parmnum, '|');
257 * CtdlDecodeBase64() and encode_base64() are adaptations of code by
258 * John Walker, found in full in the file "base64.c" included with this
259 * distribution. The difference between those functions and these is that
260 * these are intended to encode/decode small string buffers, and those are
261 * intended to encode/decode entire MIME parts.
264 void encode_base64(char *dest, char *source)
266 int i, hiteof = FALSE;
270 /* Fill dtable with character encodings. */
272 for (i = 0; i < 26; i++) {
274 dtable[26 + i] = 'a' + i;
276 for (i = 0; i < 10; i++) {
277 dtable[52 + i] = '0' + i;
283 byte igroup[3], ogroup[4];
286 igroup[0] = igroup[1] = igroup[2] = 0;
287 for (n = 0; n < 3; n++) {
293 igroup[n] = (byte) c;
296 ogroup[0] = dtable[igroup[0] >> 2];
297 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
298 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
299 ogroup[3] = dtable[igroup[2] & 0x3F];
301 /* Replace characters in output stream with "=" pad
302 characters if fewer than three characters were
303 read from the end of the input stream. */
311 for (i = 0; i < 4; i++) {
312 dest[dpos++] = ogroup[i];
321 * Convert base64-encoded to binary. Returns the length of the decoded data.
322 * It will stop after reading 'length' bytes.
324 int CtdlDecodeBase64(char *dest, char *source, size_t length)
330 for (i = 0; i < 255; i++) {
333 for (i = 'A'; i <= 'Z'; i++) {
334 dtable[i] = 0 + (i - 'A');
336 for (i = 'a'; i <= 'z'; i++) {
337 dtable[i] = 26 + (i - 'a');
339 for (i = '0'; i <= '9'; i++) {
340 dtable[i] = 52 + (i - '0');
346 /*CONSTANTCONDITION*/
348 byte a[4], b[4], o[3];
350 for (i = 0; i < 4; i++) {
351 if (spos >= length) {
362 if (dtable[c] & 0x80) {
363 /* Ignoring errors: discard invalid character. */
368 b[i] = (byte) dtable[c];
370 o[0] = (b[0] << 2) | (b[1] >> 4);
371 o[1] = (b[1] << 4) | (b[2] >> 2);
372 o[2] = (b[2] << 6) | b[3];
373 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
374 if (i>=1) dest[dpos++] = o[0];
375 if (i>=2) dest[dpos++] = o[1];
376 if (i>=3) dest[dpos++] = o[2];
387 * Strip leading and trailing spaces from a string
389 void striplt(char *buf)
391 while ((strlen(buf) > 0) && (isspace(buf[0])))
392 strcpy(buf, &buf[1]);
393 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
394 buf[strlen(buf) - 1] = 0;
402 * Return the number of occurances of character ch in string st
404 int haschar(const char *st, int ch)
408 for (a = 0; a < strlen(st); ++a)
418 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
420 int collapsed_strcmp(char *s1, char *s2) {
424 c1 = malloc(strlen(s1)+1);
425 c2 = malloc(strlen(s2)+1);
430 for (i=0; i<strlen(s1); ++i) {
431 if (isalnum(s1[i])) {
432 c1[pos] = tolower(s1[i]);
438 for (i=0; i<strlen(s2); ++i) {
439 if (isalnum(s2[i])) {
440 c2[pos] = tolower(s2[i]);
445 ret = strcmp(c1, c2);
454 * Format a date/time stamp for output
455 * seconds is whether to print the seconds
457 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
461 char *ascmonths[] = {
462 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
463 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
467 tm = localtime(&thetime);
470 if (hour == 0) hour = 12;
471 else if (hour > 12) hour = hour - 12;
474 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
475 ascmonths[tm->tm_mon],
481 ( (tm->tm_hour >= 12) ? "pm" : "am" )
484 snprintf(buf, n, "%s %d %4d %d:%02d%s",
485 ascmonths[tm->tm_mon],
490 ( (tm->tm_hour >= 12) ? "pm" : "am" )
498 * Determine whether the specified message number is contained within the
501 int is_msg_in_mset(char *mset, long msgnum) {
504 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
508 * Now set it for all specified messages.
510 num_sets = num_tokens(mset, ',');
511 for (s=0; s<num_sets; ++s) {
512 extract_token(setstr, mset, s, ',');
514 extract_token(lostr, setstr, 0, ':');
515 if (num_tokens(setstr, ':') >= 2) {
516 extract_token(histr, setstr, 1, ':');
517 if (!strcmp(histr, "*")) {
518 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
522 strcpy(histr, lostr);
527 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
535 * Utility function to "readline" from memory
536 * (returns new pointer)
538 char *memreadline(char *start, char *buf, int maxlen)
542 int len = 0; /* tally our own length to avoid strlen() delays */
545 memset(buf, 0, maxlen);
549 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
550 buf[strlen(buf) + 1] = 0;
551 buf[strlen(buf)] = ch;
554 if ((ch == 10) || (ch == 0)) {
562 * Strip a boundarized substring out of a string (for example, remove
563 * parentheses and anything inside them).
565 void stripout(char *str, char leftboundary, char rightboundary) {
570 for (a = 0; a < strlen(str); ++a) {
571 if (str[a] == leftboundary) lb = a;
572 if (str[a] == rightboundary) rb = a;
575 if ( (lb > 0) && (rb > lb) ) {
576 strcpy(&str[lb - 1], &str[rb + 1]);
583 * Reduce a string down to a boundarized substring (for example, remove
584 * parentheses and anything outside them).
586 void stripallbut(char *str, char leftboundary, char rightboundary) {
589 for (a = 0; a < strlen(str); ++ a) {
590 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
593 for (a = 0; a < strlen(str); ++ a) {
594 if (str[a] == rightboundary) str[a] = 0;
599 char *myfgets(char *s, int size, FILE *stream) {
600 char *ret = fgets(s, size, stream);
604 nl = strchr(s, '\n');
614 * Escape a string for feeding out as a URL.
615 * Output buffer must be big enough to handle escape expansion!
617 void urlesc(char *outbuf, char *strbuf)
620 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
624 for (a = 0; a < strlen(strbuf); ++a) {
626 for (b = 0; b < strlen(ec); ++b) {
627 if (strbuf[a] == ec[b])
632 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
634 sprintf(&outbuf[b], "%c", strbuf[a]);
640 * Citadelian replacement for tmpnam()
642 char *CtdlTempFileName(char *prefix1, int prefix2) {
644 static char buf[SIZ];
646 sprintf(buf, "/tmp/Citadel-%s-%d-%04x-%04x",
658 * Citadelian replacement for tmpfile()
660 FILE *CtdlTempFile(void) {
664 strcpy(filename, tmpnam(NULL));
665 fp = fopen(filename, "w+b");