* if the token extractor fails to do its work, rather return '0' than some random...
[citadel.git] / libcitadel / lib / tools.c
1 /*
2  * A basic toolset containing miscellaneous functions for string manipluation,
3  * encoding/decoding, and a bunch of other stuff.
4  */
5
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <errno.h>
16 #include <limits.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include "libcitadel.h"
30
31
32 #define TRUE  1
33 #define FALSE 0
34
35 typedef unsigned char byte;           /* Byte type */
36 static byte dtable[256] = "\0";       /* base64 decode table */
37 static byte etable[256] = "\0";       /* base64 encode table */
38
39 char *safestrncpy(char *dest, const char *src, size_t n)
40 {
41         int i = 0;
42
43         if (dest == NULL || src == NULL) {
44                 fprintf(stderr, "safestrncpy: NULL argument\n");
45                 abort();
46         }
47
48         do {
49                 dest[i] = src[i];
50                 if (dest[i] == 0) return(dest);
51                 ++i;
52         } while (i<n);
53         dest[n - 1] = 0;
54         return dest;
55 }
56
57
58
59 /*
60  * num_tokens()  -  discover number of parameters/tokens in a string
61  */
62 int num_tokens(const char *source, char tok)
63 {
64         int count = 1;
65         const char *ptr = source;
66
67         if (source == NULL) {
68                 return (0);
69         }
70
71         while (*ptr != '\0') {
72                 if (*ptr++ == tok) {
73                         ++count;
74                 }
75         }
76         
77         return (count);
78 }
79
80 //extern void cit_backtrace(void);
81
82
83 /*
84  * extract_token() - a string tokenizer
85  * returns -1 if not found, or length of token.
86  */
87 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
88 {
89         const char *s;                  //* source * /
90         int len = 0;                    //* running total length of extracted string * /
91         int current_token = 0;          //* token currently being processed * /
92
93         s = source;
94
95         if (dest == NULL) {
96                 return(-1);
97         }
98
99         //cit_backtrace();
100         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
101         dest[0] = 0;
102
103         if (s == NULL) {
104                 return(-1);
105         }
106         
107         maxlen--;
108
109         while (*s) {
110                 if (*s == separator) {
111                         ++current_token;
112                 }
113                 if ( (current_token == parmnum) && 
114                      (*s != separator) && 
115                      (len < maxlen) ) {
116                         dest[len] = *s;
117                         ++len;
118                 }
119                 else if ((current_token > parmnum) || (len >= maxlen)) {
120                         break;
121                 }
122                 ++s;
123         }
124
125         dest[len] = '\0';
126         if (current_token < parmnum) {
127                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
128                 return(-1);
129         }
130         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
131         return(len);
132 }
133 //*/
134
135
136 /*
137  * extract_token() - a string tokenizer
138  * /
139 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
140 {
141         char *d;                // dest
142         const char *s;          // source
143         int count = 0;
144         int len = 0;
145
146         
147         //cit_backtrace();
148         //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
149         strcpy(dest, "");
150
151         //  Locate desired parameter 
152         s = source;
153         while (count < parmnum) {
154                 //  End of string, bail!
155                 if (!*s) {
156                         s = NULL;
157                         break;
158                 }
159                 if (*s == separator) {
160                         count++;
161                 }
162                 s++;
163         }
164         if (!s) {
165                 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
166                 return -1;              // Parameter not found
167         }
168         
169         for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
170                 *d = *s;
171         }
172         *d = 0;
173         //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
174         return 0;
175 }
176 */
177
178
179 /*
180  * remove_token() - a tokenizer that kills, maims, and destroys
181  */
182 void remove_token(char *source, int parmnum, char separator)
183 {
184         char *d, *s;            /* dest, source */
185         int count = 0;
186
187         /* Find desired parameter */
188         d = source;
189         while (count < parmnum) {
190                 /* End of string, bail! */
191                 if (!*d) {
192                         d = NULL;
193                         break;
194                 }
195                 if (*d == separator) {
196                         count++;
197                 }
198                 d++;
199         }
200         if (!d) return;         /* Parameter not found */
201
202         /* Find next parameter */
203         s = d;
204         while (*s && *s != separator) {
205                 s++;
206         }
207
208         /* Hack and slash */
209         if (*s)
210                 strcpy(d, ++s);
211         else if (d == source)
212                 *d = 0;
213         else
214                 *--d = 0;
215         /*
216         while (*s) {
217                 *d++ = *s++;
218         }
219         *d = 0;
220         */
221 }
222
223
224 /*
225  * extract_int()  -  extract an int parm w/o supplying a buffer
226  */
227 int extract_int(const char *source, int parmnum)
228 {
229         char buf[32];
230         
231         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
232                 return(atoi(buf));
233         else
234                 return 0;
235 }
236
237 /*
238  * extract_long()  -  extract an long parm w/o supplying a buffer
239  */
240 long extract_long(const char *source, int parmnum)
241 {
242         char buf[32];
243         
244         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
245                 return(atol(buf));
246         else
247                 return 0;
248 }
249
250
251 /*
252  * extract_unsigned_long() - extract an unsigned long parm
253  */
254 unsigned long extract_unsigned_long(const char *source, int parmnum)
255 {
256         char buf[32];
257
258         if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
259                 return strtoul(buf, NULL, 10);
260         else 
261                 return 0;
262 }
263
264
265
266 void CtdlInitBase64Table(void)
267 {
268         int i;
269         /*      Fill dtable with character encodings.  */
270         
271         /* Encoder Table */
272         for (i = 0; i < 26; i++) {
273                 etable[i] = 'A' + i;
274                 etable[26 + i] = 'a' + i;
275         }
276         for (i = 0; i < 10; i++) {
277                 etable[52 + i] = '0' + i;
278         }
279         etable[62] = '+';
280         etable[63] = '/';
281         
282         /* Decoder Table */
283         for (i = 0; i < 255; i++) {
284                 dtable[i] = 0x80;
285         }
286         for (i = 'A'; i <= 'Z'; i++) {
287                 dtable[i] = 0 + (i - 'A');
288         }
289         for (i = 'a'; i <= 'z'; i++) {
290                 dtable[i] = 26 + (i - 'a');
291         }
292         for (i = '0'; i <= '9'; i++) {
293                 dtable[i] = 52 + (i - '0');
294         }
295         dtable['+'] = 62;
296         dtable['/'] = 63;
297         dtable['='] = 0;
298 }
299
300
301 /*
302  * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by John Walker.
303  */
304
305 size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
306 {
307         int i, hiteof = FALSE;
308         int spos = 0;
309         int dpos = 0;
310         int thisline = 0;
311
312         /**  Fill dtable with character encodings.  */
313
314         for (i = 0; i < 26; i++) {
315                 dtable[i] = 'A' + i;
316                 dtable[26 + i] = 'a' + i;
317         }
318         for (i = 0; i < 10; i++) {
319                 dtable[52 + i] = '0' + i;
320         }
321         dtable[62] = '+';
322         dtable[63] = '/';
323
324         while (!hiteof) {
325                 byte igroup[3], ogroup[4];
326                 int c, n;
327
328                 igroup[0] = igroup[1] = igroup[2] = 0;
329                 for (n = 0; n < 3; n++) {
330                         if (spos >= sourcelen) {
331                                 hiteof = TRUE;
332                                 break;
333                         }
334                         c = source[spos++];
335                         igroup[n] = (byte) c;
336                 }
337                 if (n > 0) {
338                         ogroup[0] = dtable[igroup[0] >> 2];
339                         ogroup[1] =
340                             dtable[((igroup[0] & 3) << 4) |
341                                    (igroup[1] >> 4)];
342                         ogroup[2] =
343                             dtable[((igroup[1] & 0xF) << 2) |
344                                    (igroup[2] >> 6)];
345                         ogroup[3] = dtable[igroup[2] & 0x3F];
346
347                         /**
348                          * Replace characters in output stream with "=" pad
349                          * characters if fewer than three characters were
350                          * read from the end of the input stream. 
351                          */
352
353                         if (n < 3) {
354                                 ogroup[3] = '=';
355                                 if (n < 2) {
356                                         ogroup[2] = '=';
357                                 }
358                         }
359                         for (i = 0; i < 4; i++) {
360                                 dest[dpos++] = ogroup[i];
361                                 dest[dpos] = 0;
362                         }
363                         thisline += 4;
364                         if ( (linebreaks) && (thisline > 70) ) {
365                                 dest[dpos++] = '\r';
366                                 dest[dpos++] = '\n';
367                                 dest[dpos] = 0;
368                                 thisline = 0;
369                         }
370                 }
371         }
372         if ( (linebreaks) && (thisline > 70) ) {
373                 dest[dpos++] = '\r';
374                 dest[dpos++] = '\n';
375                 dest[dpos] = 0;
376                 thisline = 0;
377         }
378
379         return(dpos);
380 }
381
382
383
384 /* 
385  * Convert base64-encoded to binary.  Returns the length of the decoded data.
386  * It will stop after reading 'length' bytes.
387  */
388 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
389 {
390     int i, c;
391     int dpos = 0;
392     int spos = 0;
393
394
395     /*CONSTANTCONDITION*/
396     while (TRUE) {
397         byte a[4], b[4], o[3];
398
399         for (i = 0; i < 4; i++) {
400             if (spos >= length) {
401                 return(dpos);
402             }
403             c = source[spos++];
404
405             if (c == 0) {
406                 if (i > 0) {
407                     return(dpos);
408                 }
409                 return(dpos);
410             }
411             if (dtable[c] & 0x80) {
412                 /* Ignoring errors: discard invalid character. */
413                 i--;
414                 continue;
415             }
416             a[i] = (byte) c;
417             b[i] = (byte) dtable[c];
418         }
419         o[0] = (b[0] << 2) | (b[1] >> 4);
420         o[1] = (b[1] << 4) | (b[2] >> 2);
421         o[2] = (b[2] << 6) | b[3];
422         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
423         if (i>=1) dest[dpos++] = o[0];
424         if (i>=2) dest[dpos++] = o[1];
425         if (i>=3) dest[dpos++] = o[2];
426         dest[dpos] = 0;
427         if (i < 3) {
428             return(dpos);
429         }
430     }
431 }
432
433
434 /*
435  * if we send out non ascii subjects, we encode it this way.
436  */
437 char *rfc2047encode(char *line, long length)
438 {
439         char *AlreadyEncoded;
440         char *result;
441         long end;
442 #define UTF8_HEADER "=?UTF-8?B?"
443
444         /* check if we're already done */
445         AlreadyEncoded = strstr(line, "=?");
446         if ((AlreadyEncoded != NULL) &&
447             ((strstr(AlreadyEncoded, "?B?") != NULL)||
448              (strstr(AlreadyEncoded, "?Q?") != NULL)))
449         {
450                 return strdup(line);
451         }
452
453         result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
454         strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
455         CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
456         end = strlen (result);
457         result[end]='?';
458         result[end+1]='=';
459         result[end+2]='\0';
460         return result;
461 }
462
463
464 /*
465  * Strip leading and trailing spaces from a string
466  */
467 void striplt(char *buf)
468 {
469         size_t len;
470         int a;
471
472         if (buf==NULL) return;
473         if (IsEmptyStr(buf)) return;
474         len = strlen(buf);
475         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
476                 buf[--len] = 0;
477         if (IsEmptyStr(buf)) return;
478         a = 0;
479         while ((!IsEmptyStr(buf)) && (isspace(buf[a])))
480                 a++;
481         if (a > 0)
482                 memmove(buf, &buf[a], len - a + 1);
483 }
484
485
486
487
488
489 /**
490  * \brief check for the presence of a character within a string (returns count)
491  * \param st the string to examine
492  * \param ch the char to search
493  * \return the position inside of st
494  */
495 int haschar(const char *st,int ch)
496 {
497         const char *ptr;
498         int b;
499         b = 0;
500         ptr = st;
501         while (!IsEmptyStr(ptr))
502         {
503                 if (*ptr == ch)
504                         ++b;
505                 ptr ++;
506         }
507         return (b);
508 }
509
510
511
512
513
514 /*
515  * Format a date/time stamp for output 
516  * seconds is whether to print the seconds
517  */
518 void fmt_date(char *buf, size_t n, time_t thetime, int seconds) {
519         struct tm tm;
520         int hour;
521
522         /* Month strings for date conversions ... this needs to be localized eventually */
523         char *fmt_date_months[12] = {
524                 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
525         };
526
527         strcpy(buf, "");
528         localtime_r(&thetime, &tm);
529
530         hour = tm.tm_hour;
531         if (hour == 0)  hour = 12;
532         else if (hour > 12) hour = hour - 12;
533
534         if (seconds) {
535                 snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
536                         fmt_date_months[tm.tm_mon],
537                         tm.tm_mday,
538                         tm.tm_year + 1900,
539                         hour,
540                         tm.tm_min,
541                         tm.tm_sec,
542                         ( (tm.tm_hour >= 12) ? "pm" : "am" )
543                 );
544         } else {
545                 snprintf(buf, n, "%s %d %4d %d:%02d%s",
546                         fmt_date_months[tm.tm_mon],
547                         tm.tm_mday,
548                         tm.tm_year + 1900,
549                         hour,
550                         tm.tm_min,
551                         ( (tm.tm_hour >= 12) ? "pm" : "am" )
552                 );
553         }
554 }
555
556
557
558 /*
559  * Determine whether the specified message number is contained within the
560  * specified sequence set.
561  */
562 int is_msg_in_sequence_set(char *mset, long msgnum) {
563         int num_sets;
564         int s;
565         char setstr[128], lostr[128], histr[128];
566         long lo, hi;
567
568         num_sets = num_tokens(mset, ',');
569         for (s=0; s<num_sets; ++s) {
570                 extract_token(setstr, mset, s, ',', sizeof setstr);
571
572                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
573                 if (num_tokens(setstr, ':') >= 2) {
574                         extract_token(histr, setstr, 1, ':', sizeof histr);
575                         if (!strcmp(histr, "*")) {
576                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
577                         }
578                 } 
579                 else {
580                         strcpy(histr, lostr);
581                 }
582                 lo = atol(lostr);
583                 hi = atol(histr);
584
585                 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
586         }
587
588         return(0);
589 }
590
591 /** 
592  * \brief Utility function to "readline" from memory
593  * \param start Location in memory from which we are reading.
594  * \param buf the buffer to place the string in.
595  * \param maxlen Size of string buffer
596  * \return Pointer to the source memory right after we stopped reading.
597  */
598 char *memreadline(char *start, char *buf, int maxlen)
599 {
600         char ch;
601         char *ptr;
602         int len = 0;            /**< tally our own length to avoid strlen() delays */
603
604         ptr = start;
605
606         while (1) {
607                 ch = *ptr++;
608                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
609                         buf[len++] = ch;
610                 }
611                 if ((ch == 10) || (ch == 0)) {
612                         buf[len] = 0;
613                         return ptr;
614                 }
615         }
616 }
617
618
619 /** 
620  * \brief Utility function to "readline" from memory
621  * \param start Location in memory from which we are reading.
622  * \param buf the buffer to place the string in.
623  * \param maxlen Size of string buffer
624  * \param retlen the length of the returned string
625  * \return Pointer to the source memory right after we stopped reading.
626  */
627 char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen)
628 {
629         char ch;
630         char *ptr;
631         int len = 0;            /**< tally our own length to avoid strlen() delays */
632
633         ptr = start;
634
635         while (1) {
636                 ch = *ptr++;
637                 if ((len + 1 < (maxlen)) && (ch != 13) && (ch != 10)) {
638                         buf[len++] = ch;
639                 }
640                 if ((ch == 10) || (ch == 0)) {
641                         buf[len] = 0;
642                         *retlen = len;
643                         return ptr;
644                 }
645         }
646 }
647
648
649
650
651 /*
652  * Strip a boundarized substring out of a string (for example, remove
653  * parentheses and anything inside them).
654  */
655 void stripout(char *str, char leftboundary, char rightboundary) {
656         int a;
657         int lb = (-1);
658         int rb = (-1);
659
660         for (a = 0; a < strlen(str); ++a) {
661                 if (str[a] == leftboundary) lb = a;
662                 if (str[a] == rightboundary) rb = a;
663         }
664
665         if ( (lb > 0) && (rb > lb) ) {
666                 strcpy(&str[lb - 1], &str[rb + 1]);
667         }
668
669         else if ( (lb == 0) && (rb > lb) ) {
670                 strcpy(str, &str[rb + 1]);
671         }
672
673 }
674
675
676 /*
677  * Reduce a string down to a boundarized substring (for example, remove
678  * parentheses and anything outside them).
679  */
680 void stripallbut(char *str, char leftboundary, char rightboundary) {
681         int a;
682
683         for (a = 0; a < strlen(str); ++ a) {
684                 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
685         }
686
687         for (a = 0; a < strlen(str); ++ a) {
688                 if (str[a] == rightboundary) str[a] = 0;
689         }
690
691 }
692
693 char *myfgets(char *s, int size, FILE *stream) {
694         char *ret = fgets(s, size, stream);
695         char *nl;
696
697         if (ret != NULL) {
698                 nl = strchr(s, '\n');
699
700                 if (nl != NULL)
701                         *nl = 0;
702         }
703
704         return ret;
705 }
706
707 /** 
708  * \brief Escape a string for feeding out as a URL.
709  * \param outbuf the output buffer
710  * \param oblen the size of outbuf to sanitize
711  * \param strbuf the input buffer
712  */
713 void urlesc(char *outbuf, size_t oblen, char *strbuf)
714 {
715         int a, b, c, len, eclen, olen;
716         char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
717
718         strcpy(outbuf, "");
719         len = strlen(strbuf);
720         eclen = strlen(ec);
721         olen = 0;
722         for (a = 0; a < len; ++a) {
723                 c = 0;
724                 for (b = 0; b < eclen; ++b) {
725                         if (strbuf[a] == ec[b])
726                                 c = 1;
727                 }
728                 if (c == 1) {
729                         snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
730                         olen += 3;
731                 }
732                 else 
733                         outbuf[olen ++] = strbuf[a];
734         }
735         outbuf[olen] = '\0';
736 }
737
738
739
740 /*
741  * In our world, we want strcpy() to be able to work with overlapping strings.
742  */
743 #ifdef strcpy
744 #undef strcpy
745 #endif
746 char *strcpy(char *dest, const char *src) {
747         memmove(dest, src, (strlen(src) + 1) );
748         return(dest);
749 }
750
751
752 /*
753  * Generate a new, globally unique UID parameter for a calendar etc. object
754  */
755 void generate_uuid(char *buf) {
756         static int seq = 0;
757
758         sprintf(buf, "%lx-%lx-%x",
759                 time(NULL),
760                 (long)getpid(),
761                 (seq++)
762         );
763 }
764
765 /*
766  * bmstrcasestr() -- case-insensitive substring search
767  *
768  * This uses the Boyer-Moore search algorithm and is therefore quite fast.
769  * The code is roughly based on the strstr() replacement from 'tin' written
770  * by Urs Jannsen.
771  */
772 char *bmstrcasestr(char *text, char *pattern) {
773
774         register unsigned char *p, *t;
775         register int i, j, *delta;
776         register size_t p1;
777         int deltaspace[256];
778         size_t textlen;
779         size_t patlen;
780
781         textlen = strlen (text);
782         patlen = strlen (pattern);
783
784         /* algorithm fails if pattern is empty */
785         if ((p1 = patlen) == 0)
786                 return (text);
787
788         /* code below fails (whenever i is unsigned) if pattern too long */
789         if (p1 > textlen)
790                 return (NULL);
791
792         /* set up deltas */
793         delta = deltaspace;
794         for (i = 0; i <= 255; i++)
795                 delta[i] = p1;
796         for (p = (unsigned char *) pattern, i = p1; --i > 0;)
797                 delta[tolower(*p++)] = i;
798
799         /*
800          * From now on, we want patlen - 1.
801          * In the loop below, p points to the end of the pattern,
802          * t points to the end of the text to be tested against the
803          * pattern, and i counts the amount of text remaining, not
804          * including the part to be tested.
805          */
806         p1--;
807         p = (unsigned char *) pattern + p1;
808         t = (unsigned char *) text + p1;
809         i = textlen - patlen;
810         while(1) {
811                 if (tolower(p[0]) == tolower(t[0])) {
812                         if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
813                                 return ((char *)t - p1);
814                         }
815                 }
816                 j = delta[tolower(t[0])];
817                 if (i < j)
818                         break;
819                 i -= j;
820                 t += j;
821         }
822         return (NULL);
823 }
824
825
826
827 /*
828  * Local replacement for controversial C library function that generates
829  * names for temporary files.  Included to shut up compiler warnings.
830  */
831 void CtdlMakeTempFileName(char *name, int len) {
832         int i = 0;
833
834         while (i++, i < 100) {
835                 snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
836                         (long)getpid(),
837                         rand()
838                 );
839                 if (!access(name, F_OK)) {
840                         return;
841                 }
842         }
843 }
844
845
846
847 /*
848  * Determine whether the specified message number is contained within the specified set.
849  * Returns nonzero if the specified message number is in the specified message set string.
850  */
851 int is_msg_in_mset(char *mset, long msgnum) {
852         int num_sets;
853         int s;
854         char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
855         long lo, hi;
856
857         /*
858          * Now set it for all specified messages.
859          */
860         num_sets = num_tokens(mset, ',');
861         for (s=0; s<num_sets; ++s) {
862                 extract_token(setstr, mset, s, ',', sizeof setstr);
863
864                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
865                 if (num_tokens(setstr, ':') >= 2) {
866                         extract_token(histr, setstr, 1, ':', sizeof histr);
867                         if (!strcmp(histr, "*")) {
868                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
869                         }
870                 }
871                 else {
872                         strcpy(histr, lostr);
873                 }
874                 lo = atol(lostr);
875                 hi = atol(histr);
876
877                 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
878         }
879
880         return(0);
881 }
882
883
884 /*
885  * \brief searches for a  paternn within asearch string
886  * \param search the string to search 
887  * \param patn the pattern to find in string
888  * \returns position in string
889  */
890 int pattern2(char *search, char *patn)
891 {
892         int a;
893         int len, plen;
894         len = strlen (search);
895         plen = strlen (patn);
896         for (a = 0; a < len; ++a) {
897                 if (!strncasecmp(&search[a], patn, plen))
898                         return (a);
899         }
900         return (-1);
901 }
902
903
904 /**
905  * \brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
906  * \param buf the string to modify
907  * \param len length of the string. 
908  */
909 void stripltlen(char *buf, int *len)
910 {
911         int delta = 0;
912         if (*len == 0) return;
913         while ((*len > delta) && (isspace(buf[delta]))){
914                 delta ++;
915         }
916         memmove (buf, &buf[delta], *len - delta + 1);
917         (*len) -=delta;
918
919         if (*len == 0) return;
920         while (isspace(buf[(*len) - 1])){
921                 buf[--(*len)] = '\0';
922         }
923 }
924