str.c revision 1.4 1 /*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)str.c 5.5 (Berkeley) 6/27/91";*/
36 static char rcsid[] = "$Id: str.c,v 1.4 1993/08/01 19:00:29 mycroft Exp $";
37 #endif /* not lint */
38
39 /*
40 * tc.str.c: Short string package
41 * This has been a lesson of how to write buggy code!
42 */
43 #ifdef SHORT_STRINGS
44
45 #if __STDC__
46 # include <stdarg.h>
47 #else
48 # include <varargs.h>
49 #endif
50
51 #include "csh.h"
52 #include "extern.h"
53
54 Char **
55 blk2short(src)
56 register char **src;
57 {
58 size_t n;
59 register Char **sdst, **dst;
60
61 /*
62 * Count
63 */
64 for (n = 0; src[n] != NULL; n++);
65 sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
66
67 for (; *src != NULL; src++)
68 *dst++ = SAVE(*src);
69 *dst = NULL;
70 return (sdst);
71 }
72
73 char **
74 short2blk(src)
75 register Char **src;
76 {
77 size_t n;
78 register char **sdst, **dst;
79
80 /*
81 * Count
82 */
83 for (n = 0; src[n] != NULL; n++);
84 sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
85
86 for (; *src != NULL; src++)
87 *dst++ = strsave(short2str(*src));
88 *dst = NULL;
89 return (sdst);
90 }
91
92 #define MALLOC_INCR 1024
93 Char *
94 str2short(src)
95 register char *src;
96 {
97 static Char *sdst;
98 static size_t dstsize = 0;
99 register Char *dst, *edst;
100
101 if (src == NULL)
102 return (NULL);
103
104 if (sdst == (NULL)) {
105 dstsize = MALLOC_INCR;
106 sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
107 }
108
109 dst = sdst;
110 edst = &dst[dstsize];
111 while (*src) {
112 *dst++ = (Char) ((unsigned char) *src++);
113 if (dst == edst) {
114 dstsize += MALLOC_INCR;
115 sdst = (Char *) xrealloc((ptr_t) sdst,
116 (size_t) dstsize * sizeof(Char));
117 edst = &sdst[dstsize];
118 dst = &edst[-MALLOC_INCR];
119 }
120 }
121 *dst = 0;
122 return (sdst);
123 }
124
125 char *
126 short2qstr(src)
127 register Char *src;
128 {
129 static char *sdst = NULL;
130 static size_t dstsize = 0;
131 register char *dst, *edst;
132
133 if (src == NULL)
134 return (NULL);
135
136 if (sdst == NULL) {
137 dstsize = MALLOC_INCR;
138 sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
139 }
140 dst = sdst;
141 edst = &dst[dstsize];
142 while (*src) {
143 if (*src & QUOTE) {
144 *dst++ = '\\';
145 if (dst == edst) {
146 dstsize += MALLOC_INCR;
147 sdst = (char *) xrealloc((ptr_t) sdst,
148 (size_t) dstsize * sizeof(char));
149 edst = &sdst[dstsize];
150 dst = &edst[-MALLOC_INCR];
151 }
152 }
153 *dst++ = (char) *src++;
154 if (dst == edst) {
155 dstsize += MALLOC_INCR;
156 sdst = (char *) xrealloc((ptr_t) sdst,
157 (size_t) dstsize * sizeof(char));
158 edst = &sdst[dstsize];
159 dst = &edst[-MALLOC_INCR];
160 }
161 }
162 *dst = 0;
163 return (sdst);
164 }
165 char *
166 short2str(src)
167 register Char *src;
168 {
169 static char *sdst = NULL;
170 static size_t dstsize = 0;
171 register char *dst, *edst;
172
173 if (src == NULL)
174 return (NULL);
175
176 if (sdst == NULL) {
177 dstsize = MALLOC_INCR;
178 sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
179 }
180 dst = sdst;
181 edst = &dst[dstsize];
182 while (*src) {
183 *dst++ = (char) *src++;
184 if (dst == edst) {
185 dstsize += MALLOC_INCR;
186 sdst = (char *) xrealloc((ptr_t) sdst,
187 (size_t) dstsize * sizeof(char));
188 edst = &sdst[dstsize];
189 dst = &edst[-MALLOC_INCR];
190 }
191 }
192 *dst = 0;
193 return (sdst);
194 }
195
196 Char *
197 s_strcpy(dst, src)
198 register Char *dst, *src;
199 {
200 register Char *sdst;
201
202 sdst = dst;
203 while (*dst++ = *src++);
204 return (sdst);
205 }
206
207 Char *
208 s_strncpy(dst, src, n)
209 register Char *dst, *src;
210 register size_t n;
211 {
212 register Char *sdst;
213
214 if (n == 0)
215 return(dst);
216
217 sdst = dst;
218 do
219 if ((*dst++ = *src++) == '\0') {
220 while (--n != 0)
221 *dst++ = '\0';
222 return(sdst);
223 }
224 while (--n != 0);
225 return (sdst);
226 }
227
228 Char *
229 s_strcat(dst, src)
230 register Char *dst, *src;
231 {
232 register short *sdst;
233
234 sdst = dst;
235 while (*dst++);
236 --dst;
237 while (*dst++ = *src++);
238 return (sdst);
239 }
240
241 #ifdef NOTUSED
242 Char *
243 s_strncat(dst, src, n)
244 register Char *dst, *src;
245 register size_t n;
246 {
247 register Char *sdst;
248
249 if (n == 0)
250 return (dst);
251
252 sdst = dst;
253
254 while (*dst++);
255 --dst;
256
257 do
258 if ((*dst++ = *src++) == '\0')
259 return(sdst);
260 while (--n != 0);
261
262 *dst = '\0';
263 return (sdst);
264 }
265
266 #endif
267
268 Char *
269 s_strchr(str, ch)
270 register Char *str;
271 int ch;
272 {
273 do
274 if (*str == ch)
275 return (str);
276 while (*str++);
277 return (NULL);
278 }
279
280 Char *
281 s_strrchr(str, ch)
282 register Char *str;
283 int ch;
284 {
285 register Char *rstr;
286
287 rstr = NULL;
288 do
289 if (*str == ch)
290 rstr = str;
291 while (*str++);
292 return (rstr);
293 }
294
295 size_t
296 s_strlen(str)
297 register Char *str;
298 {
299 register size_t n;
300
301 for (n = 0; *str++; n++);
302 return (n);
303 }
304
305 int
306 s_strcmp(str1, str2)
307 register Char *str1, *str2;
308 {
309 for (; *str1 && *str1 == *str2; str1++, str2++);
310 /*
311 * The following case analysis is necessary so that characters which look
312 * negative collate low against normal characters but high against the
313 * end-of-string NUL.
314 */
315 if (*str1 == '\0' && *str2 == '\0')
316 return (0);
317 else if (*str1 == '\0')
318 return (-1);
319 else if (*str2 == '\0')
320 return (1);
321 else
322 return (*str1 - *str2);
323 }
324
325 int
326 s_strncmp(str1, str2, n)
327 register Char *str1, *str2;
328 register size_t n;
329 {
330 if (n == 0)
331 return (0);
332 do {
333 if (*str1 == '\0' || *str1 != *str2)
334 break;
335 str1++, str2++;
336 } while (--n != 0);
337 /*
338 * The following case analysis is necessary so that characters which look
339 * negative collate low against normal characters but high against the
340 * end-of-string NUL.
341 */
342 if (*str1 == '\0' && *str2 == '\0')
343 return (0);
344 else if (*str1 == '\0')
345 return (-1);
346 else if (*str2 == '\0')
347 return (1);
348 else
349 return (*str1 - *str2);
350 return(0);
351 }
352
353 Char *
354 s_strsave(s)
355 register Char *s;
356 {
357 Char *n;
358 register Char *p;
359
360 if (s == 0)
361 s = STRNULL;
362 for (p = s; *p++;);
363 n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
364 while (*p++ = *s++);
365 return (n);
366 }
367
368 Char *
369 s_strspl(cp, dp)
370 Char *cp, *dp;
371 {
372 Char *ep;
373 register Char *p, *q;
374
375 if (!cp)
376 cp = STRNULL;
377 if (!dp)
378 dp = STRNULL;
379 for (p = cp; *p++;);
380 for (q = dp; *q++;);
381 ep = (Char *) xmalloc((size_t)
382 (((p - cp) + (q - dp) - 1) * sizeof(Char)));
383 for (p = ep, q = cp; *p++ = *q++;);
384 for (p--, q = dp; *p++ = *q++;);
385 return (ep);
386 }
387
388 Char *
389 s_strend(cp)
390 register Char *cp;
391 {
392 if (!cp)
393 return (cp);
394 while (*cp)
395 cp++;
396 return (cp);
397 }
398
399 #ifdef NOTUSED
400 Char *
401 s_strstr(s, t)
402 register Char *s, *t;
403 {
404 do {
405 register Char *ss = s;
406 register Char *tt = t;
407
408 do
409 if (*tt == '\0')
410 return (s);
411 while (*ss++ == *tt++);
412 } while (*s++ != '\0');
413 return (NULL);
414 }
415 #endif
416
417 #endif /* SHORT_STRINGS */
418