1 /*
2 * STREXTRA.C
3 *
4 * Written on 30-Jul-90 by jim nutt. Changes on 10-Jul-94 by John Dennis,
5 * Paul Edwards and Andrew Clarke. Released to the public domain.
6 *
7 * A few string handling routines for Msged.
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include "strextra.h"
14
strdel(char * l,int x)15 void strdel(char *l, int x)
16 {
17 int i;
18
19 i = strlen(l);
20
21 if (x > i)
22 {
23 return;
24 }
25
26 x--;
27 memmove(l + x, l + x + 1, i - x + 1);
28 *(l + i) = 0;
29 }
30
31 /* strncmpi(...) -> strncmp(...), ignore case */
32
strncmpi(const char * s,const char * t,size_t x)33 int strncmpi(const char *s, const char *t, size_t x)
34 {
35 long n;
36
37 n = (long)x;
38
39 while (n-- && tolower(*s) == tolower(*t))
40 {
41 if (*s == '\0')
42 {
43 /* equal */
44 return 0;
45 }
46 s++;
47 t++;
48 }
49
50 if (n < 0)
51 {
52 /* maximum hit, equal */
53 return 0;
54 }
55
56 /* fell through, not equal */
57 if (tolower(*s) > tolower(*t))
58 {
59 return 1;
60 }
61 else
62 {
63 return -1;
64 }
65 }
66
67 #if !defined(MSC) && !defined(UNIX) && !defined(_MSC_VER)
68
stricmp(const char * s,const char * t)69 int stricmp(const char *s, const char *t)
70 {
71 while (*s != '\0')
72 {
73 int rc;
74 rc = tolower((unsigned char)*s) - tolower((unsigned char)*t);
75 if (rc != 0)
76 {
77 return rc;
78 }
79 s++;
80 t++;
81 }
82 if (*t != '\0')
83 {
84 return -tolower((unsigned char)*t);
85 }
86 return 0;
87 }
88 #endif
89
90 #ifndef __IBMC__
91
92 #if !(defined(_MSC_VER) && (_MSC_VER >= 1200)) && !defined(UNIX)
strdup(const char * s)93 char *strdup(const char *s)
94 {
95 char *p;
96 p = malloc(strlen(s) + 1);
97 if (p != NULL)
98 {
99 strcpy(p, s);
100 }
101 return p;
102 }
103 #endif
104 #endif
105
106 #if !(defined(_MSC_VER) && (_MSC_VER >= 1200))
107
108 #ifndef __IBMC__
memicmp(const void * s1,const void * s2,size_t n)109 int memicmp(const void *s1, const void *s2, size_t n)
110 #else
111 int memicmp(void *s1, void *s2, size_t n)
112 #endif
113 {
114 size_t x;
115 int ret;
116 for (x = 0; x < n; x++)
117 {
118 ret = (tolower((unsigned char)*(char *)s1) -
119 tolower((unsigned char)*(char *)s2));
120 if (ret != 0)
121 {
122 return (ret);
123 }
124 }
125 return 0;
126 }
127
strlwr(char * s)128 char *strlwr(char *s)
129 {
130 char *p;
131 p = s;
132 while (*p != '\0')
133 {
134 *p = (char)tolower((unsigned char)*p);
135 p++;
136 }
137 return s;
138 }
139
strupr(char * s)140 char *strupr(char *s)
141 {
142 char *p;
143 p = s;
144 while (*p != '\0')
145 {
146 *p = (char)toupper((unsigned char)*p);
147 p++;
148 }
149 return s;
150 }
151
152 #endif
153
154 #if !defined(PACIFIC) && !defined(__HIGHC__)
155
stristr(const char * s1,const char * s2)156 const char *stristr(const char *s1, const char *s2)
157 {
158 const char *pptr, *sptr, *start;
159 size_t slen, plen;
160
161 start = s1;
162 slen = strlen(s1);
163 plen = strlen(s2);
164
165 /* while string length not shorter than pattern length */
166 while (slen >= plen)
167 {
168 /* find start of pattern in string */
169 while (toupper(*start) != toupper(*s2))
170 {
171 start++;
172 slen--;
173
174 /* if pattern longer than string */
175
176 if (slen < plen)
177 {
178 return NULL;
179 }
180 }
181
182 sptr = start;
183 pptr = s2;
184
185 while (toupper(*sptr) == toupper(*pptr))
186 {
187 sptr++;
188 pptr++;
189
190 /* if end of pattern then pattern was found */
191
192 if (*pptr == '\0')
193 {
194 return start;
195 }
196 }
197
198 start++;
199 slen--;
200 }
201
202 return NULL;
203 }
204
205 #endif
206