/[imapfilter]/imapfilter/misc.c
ViewVC logotype

Annotation of /imapfilter/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Sat Feb 22 16:06:41 2003 UTC (21 years, 1 month ago) by lefcha
Branch: MAIN
CVS Tags: release-0_9
Branch point for: release-0_9-patches
Changes since 1.5: +65 -58 lines
File MIME type: text/plain
Coding style to KNF and some code cleanup.

1 lefcha 1.1 #include <stdio.h>
2     #include <ctype.h>
3     #include <string.h>
4    
5    
6     /*
7     * An implementation of strstr() with case-insensitivity.
8     */
9 lefcha 1.6 char *
10     strcasestr(const char *haystack, const char *needle)
11 lefcha 1.1 {
12 lefcha 1.6 char *h, *n, *c;
13     size_t hl, nl;
14 lefcha 1.1
15 lefcha 1.6 c = (char *)haystack;
16 lefcha 1.5 n = (char *)needle;
17 lefcha 1.6 hl = strlen(haystack);
18     nl = strlen(needle);
19 lefcha 1.1
20 lefcha 1.6 while (hl >= nl) {
21     while (tolower(*c) != tolower(*needle)) {
22     c++;
23     hl--;
24     if (hl < nl)
25     return NULL;
26     }
27    
28     h = c;
29     n = (char *)needle;
30    
31     while (tolower(*h) == tolower(*n)) {
32     h++;
33     n++;
34    
35     if (*n == '\0')
36     return c;
37     }
38     c++;
39     hl--;
40 lefcha 1.1 }
41    
42 lefcha 1.6 return NULL;
43 lefcha 1.1 }
44    
45    
46     /*
47 lefcha 1.2 * Convert an unsigned long integer to a string.
48 lefcha 1.1 */
49 lefcha 1.6 char *
50     ultostr(unsigned long int num, int base)
51 lefcha 1.1 {
52 lefcha 1.6 unsigned int d;
53     static char s[65];
54     char *c;
55 lefcha 1.1
56 lefcha 1.6 if (base < 2 || base > 36)
57     return NULL;
58 lefcha 1.1
59 lefcha 1.6 c = s + 64;
60     *c = '\0';
61 lefcha 1.1
62 lefcha 1.6 do {
63     d = num % base;
64     num /= base;
65    
66     if (d <= 9)
67     *--c = '0' + d;
68     else
69     *--c = 'a' + d - 10;
70     } while (c >= s && num != 0);
71 lefcha 1.1
72 lefcha 1.6 return c;
73 lefcha 1.1 }
74 lefcha 1.3
75    
76     /*
77     * Copies at most size characters from the string pointed by src to the
78     * array pointed by dest, always NULL terminating (unless size == 0).
79     * Returns pointer to dest.
80     */
81 lefcha 1.6 char *
82     xstrncpy(char *dest, const char *src, size_t size)
83 lefcha 1.3 {
84 lefcha 1.6 char *d;
85     const char *s;
86     size_t n;
87    
88     d = dest;
89     s = src;
90     n = size;
91    
92     while (n != 0) {
93     if ((*d++ = *s++) == '\0')
94     break;
95     n--;
96     }
97 lefcha 1.3
98 lefcha 1.6 if (n == 0 && size != 0)
99     *d = '\0';
100 lefcha 1.3
101 lefcha 1.6 return dest;
102 lefcha 1.3 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26