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