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 CtdlEncodeBase64() are adaptations of code by
258 * John Walker, found in full in the file "base64.c" included with this
259 * distribution. We are moving in the direction of eventually discarding
260 * the separate executables, and using the ones in our code exclusively.
263 void CtdlEncodeBase64(char *dest, char *source, int sourcelen)
265 int i, hiteof = FALSE;
269 /* Fill dtable with character encodings. */
271 for (i = 0; i < 26; i++) {
273 dtable[26 + i] = 'a' + i;
275 for (i = 0; i < 10; i++) {
276 dtable[52 + i] = '0' + i;
282 byte igroup[3], ogroup[4];
285 igroup[0] = igroup[1] = igroup[2] = 0;
286 for (n = 0; n < 3; n++) {
287 if (spos >= sourcelen) {
292 igroup[n] = (byte) c;
295 ogroup[0] = dtable[igroup[0] >> 2];
296 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
297 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
298 ogroup[3] = dtable[igroup[2] & 0x3F];
300 /* Replace characters in output stream with "=" pad
301 characters if fewer than three characters were
302 read from the end of the input stream. */
310 for (i = 0; i < 4; i++) {
311 dest[dpos++] = ogroup[i];
320 * Convert base64-encoded to binary. Returns the length of the decoded data.
321 * It will stop after reading 'length' bytes.
323 int CtdlDecodeBase64(char *dest, char *source, size_t length)
329 for (i = 0; i < 255; i++) {
332 for (i = 'A'; i <= 'Z'; i++) {
333 dtable[i] = 0 + (i - 'A');
335 for (i = 'a'; i <= 'z'; i++) {
336 dtable[i] = 26 + (i - 'a');
338 for (i = '0'; i <= '9'; i++) {
339 dtable[i] = 52 + (i - '0');
345 /*CONSTANTCONDITION*/
347 byte a[4], b[4], o[3];
349 for (i = 0; i < 4; i++) {
350 if (spos >= length) {
361 if (dtable[c] & 0x80) {
362 /* Ignoring errors: discard invalid character. */
367 b[i] = (byte) dtable[c];
369 o[0] = (b[0] << 2) | (b[1] >> 4);
370 o[1] = (b[1] << 4) | (b[2] >> 2);
371 o[2] = (b[2] << 6) | b[3];
372 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
373 if (i>=1) dest[dpos++] = o[0];
374 if (i>=2) dest[dpos++] = o[1];
375 if (i>=3) dest[dpos++] = o[2];
386 * Strip leading and trailing spaces from a string
388 void striplt(char *buf)
390 while ((strlen(buf) > 0) && (isspace(buf[0])))
391 strcpy(buf, &buf[1]);
392 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
393 buf[strlen(buf) - 1] = 0;
401 * Return the number of occurances of character ch in string st
403 int haschar(const char *st, int ch)
407 for (a = 0; a < strlen(st); ++a)
417 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
419 int collapsed_strcmp(char *s1, char *s2) {
423 c1 = malloc(strlen(s1)+1);
424 c2 = malloc(strlen(s2)+1);
429 for (i=0; i<strlen(s1); ++i) {
430 if (isalnum(s1[i])) {
431 c1[pos] = tolower(s1[i]);
437 for (i=0; i<strlen(s2); ++i) {
438 if (isalnum(s2[i])) {
439 c2[pos] = tolower(s2[i]);
444 ret = strcmp(c1, c2);
453 * Format a date/time stamp for output
454 * seconds is whether to print the seconds
456 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
460 char *ascmonths[] = {
461 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
462 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
466 tm = localtime(&thetime);
469 if (hour == 0) hour = 12;
470 else if (hour > 12) hour = hour - 12;
473 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
474 ascmonths[tm->tm_mon],
480 ( (tm->tm_hour >= 12) ? "pm" : "am" )
483 snprintf(buf, n, "%s %d %4d %d:%02d%s",
484 ascmonths[tm->tm_mon],
489 ( (tm->tm_hour >= 12) ? "pm" : "am" )
497 * Determine whether the specified message number is contained within the
500 int is_msg_in_mset(char *mset, long msgnum) {
503 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
507 * Now set it for all specified messages.
509 num_sets = num_tokens(mset, ',');
510 for (s=0; s<num_sets; ++s) {
511 extract_token(setstr, mset, s, ',');
513 extract_token(lostr, setstr, 0, ':');
514 if (num_tokens(setstr, ':') >= 2) {
515 extract_token(histr, setstr, 1, ':');
516 if (!strcmp(histr, "*")) {
517 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
521 strcpy(histr, lostr);
526 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
534 * Utility function to "readline" from memory
535 * (returns new pointer)
537 char *memreadline(char *start, char *buf, int maxlen)
541 int len = 0; /* tally our own length to avoid strlen() delays */
544 memset(buf, 0, maxlen);
548 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
549 buf[strlen(buf) + 1] = 0;
550 buf[strlen(buf)] = ch;
553 if ((ch == 10) || (ch == 0)) {
561 * Strip a boundarized substring out of a string (for example, remove
562 * parentheses and anything inside them).
564 void stripout(char *str, char leftboundary, char rightboundary) {
569 for (a = 0; a < strlen(str); ++a) {
570 if (str[a] == leftboundary) lb = a;
571 if (str[a] == rightboundary) rb = a;
574 if ( (lb > 0) && (rb > lb) ) {
575 strcpy(&str[lb - 1], &str[rb + 1]);
582 * Reduce a string down to a boundarized substring (for example, remove
583 * parentheses and anything outside them).
585 void stripallbut(char *str, char leftboundary, char rightboundary) {
588 for (a = 0; a < strlen(str); ++ a) {
589 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
592 for (a = 0; a < strlen(str); ++ a) {
593 if (str[a] == rightboundary) str[a] = 0;
598 char *myfgets(char *s, int size, FILE *stream) {
599 char *ret = fgets(s, size, stream);
603 nl = strchr(s, '\n');
613 * Escape a string for feeding out as a URL.
614 * Output buffer must be big enough to handle escape expansion!
616 void urlesc(char *outbuf, char *strbuf)
619 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
623 for (a = 0; a < strlen(strbuf); ++a) {
625 for (b = 0; b < strlen(ec); ++b) {
626 if (strbuf[a] == ec[b])
631 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
633 sprintf(&outbuf[b], "%c", strbuf[a]);
639 * Citadelian replacement for tmpnam()
641 char *CtdlTempFileName(char *prefix1, int prefix2) {
643 static char buf[SIZ];
645 sprintf(buf, "/tmp/Citadel-%s-%d-%04x-%04x",
657 * Citadelian replacement for tmpfile()
659 FILE *CtdlTempFile(void) {
663 strcpy(filename, tmpnam(NULL));
664 fp = fopen(filename, "w+b");