4 * Utility functions that are used by both the client and server.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
36 typedef unsigned char byte; /* Byte type */
37 static byte dtable[256]; /* base64 encode / decode table */
40 char *safestrncpy(char *dest, const char *src, size_t n)
42 if (dest == NULL || src == NULL) {
43 fprintf(stderr, "safestrncpy: NULL argument\n");
46 strncpy(dest, src, n);
53 #ifndef HAVE_STRNCASECMP
54 int strncasecmp(char *lstr, char *rstr, int len)
59 lc=tolower(lstr[pos]);
60 rc=tolower(rstr[pos]);
61 if ((lc==0)&&(rc==0)) return(0);
62 if (lc<rc) return(-1);
73 * num_tokens() - discover number of parameters/tokens in a string
75 int num_tokens(char *source, char tok) {
79 if (source == NULL) return(0);
80 for (a=0; a<strlen(source); ++a) {
81 if (source[a]==tok) ++count;
87 * extract_token() - a smarter string tokenizer
89 void extract_token(char *dest, char *source, int parmnum, char separator)
99 if (strlen(source)==0) {
103 for (i=0; i<strlen(source); ++i) {
104 if (source[i]==separator) {
107 else if (curr_parm == parmnum) {
109 dest[len++] = source[i];
117 * remove_token() - a tokenizer that kills, maims, and destroys
119 void remove_token(char *source, int parmnum, char separator)
131 if (strlen(source)==0) {
135 for (i=0; i<strlen(source); ++i) {
136 if ( (start < 0) && (curr_parm == parmnum) ) {
140 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
144 if (source[i]==separator) {
149 if (end < 0) end = strlen(source);
150 strcpy(&source[start], &source[end]);
157 * extract_int() - extract an int parm w/o supplying a buffer
159 int extract_int(char *source, int parmnum)
163 extract_token(buf, source, parmnum, '|');
168 * extract_long() - extract an long parm w/o supplying a buffer
170 long extract_long(char *source, long int parmnum)
174 extract_token(buf, source, parmnum, '|');
181 * decode_base64() and encode_base64() are adaptations of code by
182 * John Walker, found in full in the file "base64.c" included with this
183 * distribution. The difference between those functions and these is that
184 * these are intended to encode/decode small string buffers, and those are
185 * intended to encode/decode entire MIME parts.
188 void encode_base64(char *dest, char *source)
190 int i, hiteof = FALSE;
194 /* Fill dtable with character encodings. */
196 for (i = 0; i < 26; i++) {
198 dtable[26 + i] = 'a' + i;
200 for (i = 0; i < 10; i++) {
201 dtable[52 + i] = '0' + i;
207 byte igroup[3], ogroup[4];
210 igroup[0] = igroup[1] = igroup[2] = 0;
211 for (n = 0; n < 3; n++) {
217 igroup[n] = (byte) c;
220 ogroup[0] = dtable[igroup[0] >> 2];
221 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
222 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
223 ogroup[3] = dtable[igroup[2] & 0x3F];
225 /* Replace characters in output stream with "=" pad
226 characters if fewer than three characters were
227 read from the end of the input stream. */
235 for (i = 0; i < 4; i++) {
236 dest[dpos++] = ogroup[i];
245 * Convert base64-encoded to binary. Returns the length of the decoded data.
246 * It will stop after reading 'length' bytes.
248 int decode_base64(char *dest, char *source, size_t length)
254 for (i = 0; i < 255; i++) {
257 for (i = 'A'; i <= 'Z'; i++) {
258 dtable[i] = 0 + (i - 'A');
260 for (i = 'a'; i <= 'z'; i++) {
261 dtable[i] = 26 + (i - 'a');
263 for (i = '0'; i <= '9'; i++) {
264 dtable[i] = 52 + (i - '0');
270 /*CONSTANTCONDITION*/
272 byte a[4], b[4], o[3];
274 for (i = 0; i < 4; i++) {
275 if (spos >= length) {
286 if (dtable[c] & 0x80) {
287 /* Ignoring errors: discard invalid character. */
292 b[i] = (byte) dtable[c];
294 o[0] = (b[0] << 2) | (b[1] >> 4);
295 o[1] = (b[1] << 4) | (b[2] >> 2);
296 o[2] = (b[2] << 6) | b[3];
297 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
298 if (i>=1) dest[dpos++] = o[0];
299 if (i>=2) dest[dpos++] = o[1];
300 if (i>=3) dest[dpos++] = o[2];
311 * Strip leading and trailing spaces from a string
313 void striplt(char *buf)
315 while ((strlen(buf) > 0) && (isspace(buf[0])))
316 strcpy(buf, &buf[1]);
317 while (isspace(buf[strlen(buf) - 1]))
318 buf[strlen(buf) - 1] = 0;
326 * Return the number of occurances of character ch in string st
328 int haschar(const char *st, int ch)
332 for (a = 0; a < strlen(st); ++a)
342 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
344 int collapsed_strcmp(char *s1, char *s2) {
348 c1 = malloc(strlen(s1)+1);
349 c2 = malloc(strlen(s2)+1);
354 for (i=0; i<strlen(s1); ++i) {
355 if (isalnum(s1[i])) {
356 c1[pos] = tolower(s1[i]);
362 for (i=0; i<strlen(s2); ++i) {
363 if (isalnum(s2[i])) {
364 c2[pos] = tolower(s2[i]);
369 ret = strcmp(c1, c2);
378 * Format a date/time stamp for output
379 * seconds is whether to print the seconds
381 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
385 char *ascmonths[] = {
386 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
387 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
391 tm = localtime(&thetime);
394 if (hour == 0) hour = 12;
395 else if (hour > 12) hour = hour - 12;
398 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
399 ascmonths[tm->tm_mon],
405 ( (tm->tm_hour >= 12) ? "pm" : "am" )
408 snprintf(buf, n, "%s %d %4d %d:%02d%s",
409 ascmonths[tm->tm_mon],
414 ( (tm->tm_hour >= 12) ? "pm" : "am" )
422 * Determine whether the specified message number is contained within the
425 int is_msg_in_mset(char *mset, long msgnum) {
428 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
432 * Now set it for all specified messages.
434 num_sets = num_tokens(mset, ',');
435 for (s=0; s<num_sets; ++s) {
436 extract_token(setstr, mset, s, ',');
438 extract_token(lostr, setstr, 0, ':');
439 if (num_tokens(setstr, ':') >= 2) {
440 extract_token(histr, setstr, 1, ':');
441 if (!strcmp(histr, "*")) {
442 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
446 strcpy(histr, lostr);
451 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
459 * Utility function to "readline" from memory
460 * (returns new pointer)
462 char *memreadline(char *start, char *buf, int maxlen)
466 int len = 0; /* tally our own length to avoid strlen() delays */
469 memset(buf, 0, maxlen);
473 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
474 buf[strlen(buf) + 1] = 0;
475 buf[strlen(buf)] = ch;
478 if ((ch == 10) || (ch == 0)) {
486 * Strip a boundarized substring out of a string (for example, remove
487 * parentheses and anything inside them).
489 void stripout(char *str, char leftboundary, char rightboundary) {
494 for (a = 0; a < strlen(str); ++a) {
495 if (str[a] == leftboundary) lb = a;
496 if (str[a] == rightboundary) rb = a;
499 if ( (lb > 0) && (rb > lb) ) {
500 strcpy(&str[lb - 1], &str[rb + 1]);
507 * Reduce a string down to a boundarized substring (for example, remove
508 * parentheses and anything outside them).
510 void stripallbut(char *str, char leftboundary, char rightboundary) {
513 for (a = 0; a < strlen(str); ++ a) {
514 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
517 for (a = 0; a < strlen(str); ++ a) {
518 if (str[a] == rightboundary) str[a] = 0;
523 char *myfgets(char *s, int size, FILE *stream) {
524 char *ret = fgets(s, size, stream);
528 nl = strchr(s, '\n');