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(const 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, const char *source, unsigned long parmnum, char separator)
93 const char *s; /* source */
98 /* Locate desired parameter */
100 while (count < parmnum) {
101 /* End of string, bail! */
106 if (*s == separator) {
111 if (!s) return; /* Parameter not found */
113 for (d = dest; *s && *s != separator; s++, d++) {
121 * extract_token() - a smarter string tokenizer
123 void extract_token_old(char *dest, const char *source, unsigned long parmnum, char separator)
133 if (strlen(source)==0) {
137 for (i=0; i<strlen(source); ++i) {
138 if (source[i]==separator) {
141 else if (curr_parm == parmnum) {
143 dest[len++] = source[i];
150 /* remove_token_fast() - a tokenizer that kills, maims, and destroys fast */
151 void remove_token(char *source, unsigned long parmnum, char separator)
153 char *d, *s; /* dest, source */
156 /* Find desired parameter */
158 while (count < parmnum) {
159 /* End of string, bail! */
164 if (*d == separator) {
169 if (!d) return; /* Parameter not found */
171 /* Find next parameter */
173 while (*s && *s != separator) {
180 else if (d == source)
194 * remove_token() - a tokenizer that kills, maims, and destroys
196 void remove_token_old(char *source, int parmnum, char separator)
208 if (strlen(source)==0) {
212 for (i=0; i<strlen(source); ++i) {
213 if ( (start < 0) && (curr_parm == parmnum) ) {
217 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
221 if (source[i]==separator) {
226 if (end < 0) end = strlen(source);
227 strcpy(&source[start], &source[end]);
234 * extract_int() - extract an int parm w/o supplying a buffer
236 int extract_int(const char *source, unsigned long parmnum)
240 extract_token(buf, source, parmnum, '|');
245 * extract_long() - extract an long parm w/o supplying a buffer
247 long extract_long(const char *source, unsigned long parmnum)
251 extract_token(buf, source, parmnum, '|');
257 * extract_unsigned_long() - extract an unsigned long parm
259 unsigned long extract_unsigned_long(const char *source, unsigned long parmnum)
263 extract_token(buf, source, parmnum, '|');
264 return strtoul(buf, NULL, 10);
269 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
270 * John Walker, found in full in the file "base64.c" included with this
271 * distribution. We are moving in the direction of eventually discarding
272 * the separate executables, and using the ones in our code exclusively.
275 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
277 int i, hiteof = FALSE;
281 /* Fill dtable with character encodings. */
283 for (i = 0; i < 26; i++) {
285 dtable[26 + i] = 'a' + i;
287 for (i = 0; i < 10; i++) {
288 dtable[52 + i] = '0' + i;
294 byte igroup[3], ogroup[4];
297 igroup[0] = igroup[1] = igroup[2] = 0;
298 for (n = 0; n < 3; n++) {
299 if (spos >= sourcelen) {
304 igroup[n] = (byte) c;
307 ogroup[0] = dtable[igroup[0] >> 2];
308 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
309 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
310 ogroup[3] = dtable[igroup[2] & 0x3F];
312 /* Replace characters in output stream with "=" pad
313 characters if fewer than three characters were
314 read from the end of the input stream. */
322 for (i = 0; i < 4; i++) {
323 dest[dpos++] = ogroup[i];
332 * Convert base64-encoded to binary. Returns the length of the decoded data.
333 * It will stop after reading 'length' bytes.
335 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
341 for (i = 0; i < 255; i++) {
344 for (i = 'A'; i <= 'Z'; i++) {
345 dtable[i] = 0 + (i - 'A');
347 for (i = 'a'; i <= 'z'; i++) {
348 dtable[i] = 26 + (i - 'a');
350 for (i = '0'; i <= '9'; i++) {
351 dtable[i] = 52 + (i - '0');
357 /*CONSTANTCONDITION*/
359 byte a[4], b[4], o[3];
361 for (i = 0; i < 4; i++) {
362 if (spos >= length) {
373 if (dtable[c] & 0x80) {
374 /* Ignoring errors: discard invalid character. */
379 b[i] = (byte) dtable[c];
381 o[0] = (b[0] << 2) | (b[1] >> 4);
382 o[1] = (b[1] << 4) | (b[2] >> 2);
383 o[2] = (b[2] << 6) | b[3];
384 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
385 if (i>=1) dest[dpos++] = o[0];
386 if (i>=2) dest[dpos++] = o[1];
387 if (i>=3) dest[dpos++] = o[2];
398 * Strip leading and trailing spaces from a string
400 void striplt(char *buf)
402 while ((strlen(buf) > 0) && (isspace(buf[0])))
403 strcpy(buf, &buf[1]);
404 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
405 buf[strlen(buf) - 1] = 0;
413 * Return the number of occurances of character ch in string st
415 int haschar(const char *st, int ch)
419 for (a = 0; a < strlen(st); ++a)
429 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
431 int collapsed_strcmp(char *s1, char *s2) {
435 c1 = malloc(strlen(s1)+1);
436 c2 = malloc(strlen(s2)+1);
441 for (i=0; i<strlen(s1); ++i) {
442 if (isalnum(s1[i])) {
443 c1[pos] = tolower(s1[i]);
449 for (i=0; i<strlen(s2); ++i) {
450 if (isalnum(s2[i])) {
451 c2[pos] = tolower(s2[i]);
456 ret = strcmp(c1, c2);
465 * Format a date/time stamp for output
466 * seconds is whether to print the seconds
468 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
472 char *ascmonths[] = {
473 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
474 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
478 tm = localtime(&thetime);
481 if (hour == 0) hour = 12;
482 else if (hour > 12) hour = hour - 12;
485 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
486 ascmonths[tm->tm_mon],
492 ( (tm->tm_hour >= 12) ? "pm" : "am" )
495 snprintf(buf, n, "%s %d %4d %d:%02d%s",
496 ascmonths[tm->tm_mon],
501 ( (tm->tm_hour >= 12) ? "pm" : "am" )
509 * Determine whether the specified message number is contained within the
512 int is_msg_in_mset(char *mset, long msgnum) {
515 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
519 * Now set it for all specified messages.
521 num_sets = num_tokens(mset, ',');
522 for (s=0; s<num_sets; ++s) {
523 extract_token(setstr, mset, s, ',');
525 extract_token(lostr, setstr, 0, ':');
526 if (num_tokens(setstr, ':') >= 2) {
527 extract_token(histr, setstr, 1, ':');
528 if (!strcmp(histr, "*")) {
529 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
533 strcpy(histr, lostr);
538 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
546 * Utility function to "readline" from memory
547 * (returns new pointer)
549 char *memreadline(char *start, char *buf, int maxlen)
553 int len = 0; /* tally our own length to avoid strlen() delays */
556 memset(buf, 0, maxlen);
560 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
561 buf[strlen(buf) + 1] = 0;
562 buf[strlen(buf)] = ch;
565 if ((ch == 10) || (ch == 0)) {
573 * Strip a boundarized substring out of a string (for example, remove
574 * parentheses and anything inside them).
576 void stripout(char *str, char leftboundary, char rightboundary) {
581 for (a = 0; a < strlen(str); ++a) {
582 if (str[a] == leftboundary) lb = a;
583 if (str[a] == rightboundary) rb = a;
586 if ( (lb > 0) && (rb > lb) ) {
587 strcpy(&str[lb - 1], &str[rb + 1]);
594 * Reduce a string down to a boundarized substring (for example, remove
595 * parentheses and anything outside them).
597 void stripallbut(char *str, char leftboundary, char rightboundary) {
600 for (a = 0; a < strlen(str); ++ a) {
601 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
604 for (a = 0; a < strlen(str); ++ a) {
605 if (str[a] == rightboundary) str[a] = 0;
610 char *myfgets(char *s, int size, FILE *stream) {
611 char *ret = fgets(s, size, stream);
615 nl = strchr(s, '\n');
625 * Escape a string for feeding out as a URL.
626 * Output buffer must be big enough to handle escape expansion!
628 void urlesc(char *outbuf, char *strbuf)
631 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
635 for (a = 0; a < strlen(strbuf); ++a) {
637 for (b = 0; b < strlen(ec); ++b) {
638 if (strbuf[a] == ec[b])
643 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
645 sprintf(&outbuf[b], "%c", strbuf[a]);
651 * Citadelian replacement for tmpnam()
653 char *CtdlTempFileName(char *prefix1, int prefix2) {
655 static char buf[SIZ];
657 sprintf(buf, "/tmp/Citadel-%s-%d-%04x-%04x",
669 * Citadelian replacement for tmpfile()
671 FILE *CtdlTempFile(void) {
675 strcpy(filename, tmpnam(NULL));
676 fp = fopen(filename, "w+b");