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() - 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++) {
120 /* remove_token() - a tokenizer that kills, maims, and destroys fast */
121 void remove_token(char *source, unsigned long parmnum, char separator)
123 char *d, *s; /* dest, source */
126 /* Find desired parameter */
128 while (count < parmnum) {
129 /* End of string, bail! */
134 if (*d == separator) {
139 if (!d) return; /* Parameter not found */
141 /* Find next parameter */
143 while (*s && *s != separator) {
150 else if (d == source)
164 * extract_int() - extract an int parm w/o supplying a buffer
166 int extract_int(const char *source, unsigned long parmnum)
170 extract_token(buf, source, parmnum, '|');
175 * extract_long() - extract an long parm w/o supplying a buffer
177 long extract_long(const char *source, unsigned long parmnum)
181 extract_token(buf, source, parmnum, '|');
187 * extract_unsigned_long() - extract an unsigned long parm
189 unsigned long extract_unsigned_long(const char *source, unsigned long parmnum)
193 extract_token(buf, source, parmnum, '|');
194 return strtoul(buf, NULL, 10);
199 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
200 * John Walker, found in full in the file "base64.c" included with this
201 * distribution. We are moving in the direction of eventually discarding
202 * the separate executables, and using the ones in our code exclusively.
205 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
207 int i, hiteof = FALSE;
211 /* Fill dtable with character encodings. */
213 for (i = 0; i < 26; i++) {
215 dtable[26 + i] = 'a' + i;
217 for (i = 0; i < 10; i++) {
218 dtable[52 + i] = '0' + i;
224 byte igroup[3], ogroup[4];
227 igroup[0] = igroup[1] = igroup[2] = 0;
228 for (n = 0; n < 3; n++) {
229 if (spos >= sourcelen) {
234 igroup[n] = (byte) c;
237 ogroup[0] = dtable[igroup[0] >> 2];
238 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
239 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
240 ogroup[3] = dtable[igroup[2] & 0x3F];
242 /* Replace characters in output stream with "=" pad
243 characters if fewer than three characters were
244 read from the end of the input stream. */
252 for (i = 0; i < 4; i++) {
253 dest[dpos++] = ogroup[i];
262 * Convert base64-encoded to binary. Returns the length of the decoded data.
263 * It will stop after reading 'length' bytes.
265 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
271 for (i = 0; i < 255; i++) {
274 for (i = 'A'; i <= 'Z'; i++) {
275 dtable[i] = 0 + (i - 'A');
277 for (i = 'a'; i <= 'z'; i++) {
278 dtable[i] = 26 + (i - 'a');
280 for (i = '0'; i <= '9'; i++) {
281 dtable[i] = 52 + (i - '0');
287 /*CONSTANTCONDITION*/
289 byte a[4], b[4], o[3];
291 for (i = 0; i < 4; i++) {
292 if (spos >= length) {
303 if (dtable[c] & 0x80) {
304 /* Ignoring errors: discard invalid character. */
309 b[i] = (byte) dtable[c];
311 o[0] = (b[0] << 2) | (b[1] >> 4);
312 o[1] = (b[1] << 4) | (b[2] >> 2);
313 o[2] = (b[2] << 6) | b[3];
314 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
315 if (i>=1) dest[dpos++] = o[0];
316 if (i>=2) dest[dpos++] = o[1];
317 if (i>=3) dest[dpos++] = o[2];
328 * Strip leading and trailing spaces from a string
330 void striplt(char *buf)
332 while ((strlen(buf) > 0) && (isspace(buf[0])))
333 strcpy(buf, &buf[1]);
334 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
335 buf[strlen(buf) - 1] = 0;
343 * Return the number of occurances of character ch in string st
345 int haschar(const char *st, int ch)
349 for (a = 0; a < strlen(st); ++a)
359 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
361 int collapsed_strcmp(char *s1, char *s2) {
365 c1 = malloc(strlen(s1)+1);
366 c2 = malloc(strlen(s2)+1);
371 for (i=0; i<strlen(s1); ++i) {
372 if (isalnum(s1[i])) {
373 c1[pos] = tolower(s1[i]);
379 for (i=0; i<strlen(s2); ++i) {
380 if (isalnum(s2[i])) {
381 c2[pos] = tolower(s2[i]);
386 ret = strcmp(c1, c2);
395 * Format a date/time stamp for output
396 * seconds is whether to print the seconds
398 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
402 char *ascmonths[] = {
403 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
404 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
408 tm = localtime(&thetime);
411 if (hour == 0) hour = 12;
412 else if (hour > 12) hour = hour - 12;
415 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
416 ascmonths[tm->tm_mon],
422 ( (tm->tm_hour >= 12) ? "pm" : "am" )
425 snprintf(buf, n, "%s %d %4d %d:%02d%s",
426 ascmonths[tm->tm_mon],
431 ( (tm->tm_hour >= 12) ? "pm" : "am" )
439 * Determine whether the specified message number is contained within the
442 int is_msg_in_mset(char *mset, long msgnum) {
445 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
449 * Now set it for all specified messages.
451 num_sets = num_tokens(mset, ',');
452 for (s=0; s<num_sets; ++s) {
453 extract_token(setstr, mset, s, ',');
455 extract_token(lostr, setstr, 0, ':');
456 if (num_tokens(setstr, ':') >= 2) {
457 extract_token(histr, setstr, 1, ':');
458 if (!strcmp(histr, "*")) {
459 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
463 strcpy(histr, lostr);
468 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
476 * Utility function to "readline" from memory
477 * (returns new pointer)
479 char *memreadline(char *start, char *buf, int maxlen)
483 int len = 0; /* tally our own length to avoid strlen() delays */
486 memset(buf, 0, maxlen);
490 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
491 buf[strlen(buf) + 1] = 0;
492 buf[strlen(buf)] = ch;
495 if ((ch == 10) || (ch == 0)) {
503 * Strip a boundarized substring out of a string (for example, remove
504 * parentheses and anything inside them).
506 void stripout(char *str, char leftboundary, char rightboundary) {
511 for (a = 0; a < strlen(str); ++a) {
512 if (str[a] == leftboundary) lb = a;
513 if (str[a] == rightboundary) rb = a;
516 if ( (lb > 0) && (rb > lb) ) {
517 strcpy(&str[lb - 1], &str[rb + 1]);
524 * Reduce a string down to a boundarized substring (for example, remove
525 * parentheses and anything outside them).
527 void stripallbut(char *str, char leftboundary, char rightboundary) {
530 for (a = 0; a < strlen(str); ++ a) {
531 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
534 for (a = 0; a < strlen(str); ++ a) {
535 if (str[a] == rightboundary) str[a] = 0;
540 char *myfgets(char *s, int size, FILE *stream) {
541 char *ret = fgets(s, size, stream);
545 nl = strchr(s, '\n');
555 * Escape a string for feeding out as a URL.
556 * Output buffer must be big enough to handle escape expansion!
558 void urlesc(char *outbuf, char *strbuf)
561 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
565 for (a = 0; a < strlen(strbuf); ++a) {
567 for (b = 0; b < strlen(ec); ++b) {
568 if (strbuf[a] == ec[b])
573 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
575 sprintf(&outbuf[b], "%c", strbuf[a]);
581 * Citadelian replacement for tmpnam()
583 char *CtdlTempFileName(char *prefix1, int prefix2) {
585 static char buf[SIZ];
587 sprintf(buf, "/tmp/Citadel-%s-%d-%04x-%04x",
599 * Citadelian replacement for tmpfile()
601 FILE *CtdlTempFile(void) {
605 strcpy(filename, tmpnam(NULL));
606 fp = fopen(filename, "w+b");