4 * Utility functions that are used by both the client and server.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
31 typedef unsigned char byte; /* Byte type */
32 static byte dtable[256]; /* base64 encode / decode table */
35 char *safestrncpy(char *dest, const char *src, size_t n)
37 if (dest == NULL || src == NULL) {
38 fprintf(stderr, "safestrncpy: NULL argument\n");
41 strncpy(dest, src, n);
48 #ifndef HAVE_STRNCASECMP
49 int strncasecmp(char *lstr, char *rstr, int len)
54 lc=tolower(lstr[pos]);
55 rc=tolower(rstr[pos]);
56 if ((lc==0)&&(rc==0)) return(0);
57 if (lc<rc) return(-1);
68 * num_tokens() - discover number of parameters/tokens in a string
70 int num_tokens(char *source, char tok) {
74 for (a=0; a<strlen(source); ++a) {
75 if (source[a]==tok) ++count;
81 * extract_token() - a smarter string tokenizer
83 void extract_token(char *dest, char *source, int parmnum, char separator)
93 if (strlen(source)==0) {
97 for (i=0; i<strlen(source); ++i) {
98 if (source[i]==separator) {
101 else if (curr_parm == parmnum) {
103 dest[len++] = source[i];
111 * remove_token() - a tokenizer that kills, maims, and destroys
113 void remove_token(char *source, int parmnum, char separator)
125 if (strlen(source)==0) {
129 for (i=0; i<strlen(source); ++i) {
130 if ( (start < 0) && (curr_parm == parmnum) ) {
134 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
138 if (source[i]==separator) {
143 if (end < 0) end = strlen(source);
145 printf("%d .. %d\n", start, end);
147 strcpy(&source[start], &source[end]);
154 * extract_int() - extract an int parm w/o supplying a buffer
156 int extract_int(char *source, int parmnum)
160 extract_token(buf, source, parmnum, '|');
165 * extract_long() - extract an long parm w/o supplying a buffer
167 long extract_long(char *source, long int parmnum)
171 extract_token(buf, source, parmnum, '|');
178 * decode_base64() and encode_base64() are adaptations of code by
179 * John Walker, found in full in the file "base64.c" included with this
180 * distribution. The difference between those functions and these is that
181 * these are intended to encode/decode small string buffers, and those are
182 * intended to encode/decode entire MIME parts.
185 void encode_base64(char *dest, char *source)
187 int i, hiteof = FALSE;
191 /* Fill dtable with character encodings. */
193 for (i = 0; i < 26; i++) {
195 dtable[26 + i] = 'a' + i;
197 for (i = 0; i < 10; i++) {
198 dtable[52 + i] = '0' + i;
204 byte igroup[3], ogroup[4];
207 igroup[0] = igroup[1] = igroup[2] = 0;
208 for (n = 0; n < 3; n++) {
214 igroup[n] = (byte) c;
217 ogroup[0] = dtable[igroup[0] >> 2];
218 ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
219 ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
220 ogroup[3] = dtable[igroup[2] & 0x3F];
222 /* Replace characters in output stream with "=" pad
223 characters if fewer than three characters were
224 read from the end of the input stream. */
232 for (i = 0; i < 4; i++) {
233 dest[dpos++] = ogroup[i];
242 void decode_base64(char *dest, char *source)
248 for (i = 0; i < 255; i++) {
251 for (i = 'A'; i <= 'Z'; i++) {
252 dtable[i] = 0 + (i - 'A');
254 for (i = 'a'; i <= 'z'; i++) {
255 dtable[i] = 26 + (i - 'a');
257 for (i = '0'; i <= '9'; i++) {
258 dtable[i] = 52 + (i - '0');
264 /*CONSTANTCONDITION*/
266 byte a[4], b[4], o[3];
268 for (i = 0; i < 4; i++) {
269 int c = source[spos++];
277 if (dtable[c] & 0x80) {
278 /* Ignoring errors: discard invalid character. */
283 b[i] = (byte) dtable[c];
285 o[0] = (b[0] << 2) | (b[1] >> 4);
286 o[1] = (b[1] << 4) | (b[2] >> 2);
287 o[2] = (b[2] << 6) | b[3];
288 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
289 if (i>=1) dest[dpos++] = o[0];
290 if (i>=2) dest[dpos++] = o[1];
291 if (i>=3) dest[dpos++] = o[2];
302 * Strip leading and trailing spaces from a string
304 void striplt(char *buf)
306 while ((strlen(buf) > 0) && (isspace(buf[0])))
307 strcpy(buf, &buf[1]);
308 while (isspace(buf[strlen(buf) - 1]))
309 buf[strlen(buf) - 1] = 0;
317 * Return the number of occurances of character ch in string st
319 int haschar(char *st, int ch)
323 for (a = 0; a < strlen(st); ++a)
333 * Compare two strings, insensitive to case, punctuation, and non-alnum chars
335 int collapsed_strcmp(char *s1, char *s2) {
339 c1 = malloc(strlen(s1)+1);
340 c2 = malloc(strlen(s2)+1);
345 for (i=0; i<strlen(s1); ++i) {
346 if (isalnum(s1[i])) {
347 c1[pos] = tolower(s1[i]);
353 for (i=0; i<strlen(s2); ++i) {
354 if (isalnum(s2[i])) {
355 c2[pos] = tolower(s2[i]);
360 ret = strcmp(c1, c2);
369 * Format a date/time stamp for output
371 void fmt_date(char *buf, time_t thetime) {
375 char *ascmonths[] = {
376 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
377 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
381 tm = localtime(&thetime);
384 if (hour == 0) hour = 12;
385 else if (hour > 12) hour = hour - 12;
387 sprintf(buf, "%s %d %4d %d:%02d%s",
388 ascmonths[tm->tm_mon],
393 ( (tm->tm_hour >= 12) ? "pm" : "am" )
400 * Determine whether the specified message number is contained within the
403 int is_msg_in_mset(char *mset, long msgnum) {
406 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
410 * Now set it for all specified messages.
412 num_sets = num_tokens(mset, ',');
413 for (s=0; s<num_sets; ++s) {
414 extract_token(setstr, mset, s, ',');
416 extract_token(lostr, setstr, 0, ':');
417 if (num_tokens(setstr, ':') >= 2) {
418 extract_token(histr, setstr, 1, ':');
419 if (!strcmp(histr, "*")) {
420 sprintf(histr, "%ld", LONG_MAX);
424 strcpy(histr, lostr);
429 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
437 * Utility function to "readline" from memory
438 * (returns new pointer)
440 char *memreadline(char *start, char *buf, int maxlen)
444 int len = 0; /* tally our own length to avoid strlen() delays */
447 memset(buf, 0, maxlen);
451 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
452 buf[strlen(buf) + 1] = 0;
453 buf[strlen(buf)] = ch;
456 if ((ch == 10) || (ch == 0)) {