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

Contents of /imapfilter/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Sat Feb 14 19:14:43 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +3 -3 lines
File MIME type: text/plain
Indentation.

1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4
5
6 /*
7 * An implementation of strstr() with case-insensitivity.
8 */
9 char *
10 strcasestr(const char *haystack, const char *needle)
11 {
12 char *h, *n, *c;
13 size_t hl, nl;
14
15 c = (char *)haystack;
16 n = (char *)needle;
17 hl = strlen(haystack);
18 nl = strlen(needle);
19
20 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 }
41
42 return NULL;
43 }
44
45
46 /*
47 * Convert an unsigned long integer to a string.
48 */
49 char *
50 ultostr(unsigned long int num, int base)
51 {
52 unsigned int d;
53 static char s[65];
54 char *c;
55
56 if (base < 2 || base > 36)
57 return NULL;
58
59 c = s + 64;
60 *c = '\0';
61
62 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
72 return c;
73 }
74
75
76 /*
77 * Copies at most size characters from the string pointed by src to the array
78 * pointed by dest, always NULL terminating (unless size == 0). Returns
79 * pointer to dest.
80 */
81 char *
82 xstrncpy(char *dest, const char *src, size_t size)
83 {
84 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
98 if (n == 0 && size != 0)
99 *d = '\0';
100
101 return dest;
102 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26