merge the formfield patch from ooo-build
[ooovba.git] / dmake / dbug / malloc / string.c
blob84b42ed62a8472cf9a492c89743ed00ac292387e
1 /*
2 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
3 * You may copy, distribute, and use this software as long as this
4 * copyright statement is not removed.
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include "malloc.h"
12 #ifndef lint
13 static
14 char rcs_hdr[] = "$Id: string.c,v 1.2 2006-07-25 10:10:03 rt Exp $";
15 #endif
17 int malloc_checking = 0;
19 char *
20 strcat(str1,str2)
21 register char * str1;
22 register char * str2;
24 char * rtn;
25 int len;
27 /*
28 * check pointers agains malloc region. The malloc* functions
29 * will properly handle the case where a pointer does not
30 * point into malloc space.
32 malloc_checking = 1;
34 len = strlen(str2);
35 malloc_check_str("strcat", str2);
37 len += strlen(str1) + 1;
38 malloc_checking = 0;
40 malloc_check_data("strcat", str1, len);
42 rtn = str1;
44 while( *str1 )
46 str1++;
49 while( (*str1 = *str2) != '\0' )
51 str1++;
52 str2++;
55 return(rtn);
58 char *
59 strdup(str1)
60 register char * str1;
62 char * malloc();
63 char * rtn;
64 register char * str2;
66 malloc_check_str("strdup", str1);
68 rtn = str2 = malloc((unsigned)strlen(str1)+1);
70 if( rtn != (char *) 0)
72 while( (*str2 = *str1) != '\0' )
74 str1++;
75 str2++;
79 return(rtn);
82 char *
83 strncat(str1,str2,len)
84 register char * str1;
85 register char * str2;
86 register int len;
88 int len1;
89 int len2;
90 char * rtn;
92 malloc_check_strn("strncat", str2, len);
94 malloc_checking = 1;
96 len2 = strlen(str2) + 1;
97 len1 = strlen(str1);
99 malloc_checking = 0;
102 if( (len+1) < len2 )
104 len1 += len + 1;
106 else
108 len1 += len2;
110 malloc_check_data("strncat", str1, len1);
112 rtn = str1;
114 while( *str1 )
116 str1++;
119 while( len-- && ((*str1++ = *str2++) != '\0') )
123 if( ! len )
125 *str1 = '\0';
128 return(rtn);
132 strcmp(str1,str2)
133 register char * str1;
134 register char * str2;
136 malloc_check_str("strcmp", str1);
137 malloc_check_str("strcmp", str2);
139 while( *str1 && (*str1 == *str2) )
141 str1++;
142 str2++;
147 * in order to deal with the case of a negative last char of either
148 * string when the other string has a null
150 if( (*str2 == '\0') && (*str1 == '\0') )
152 return(0);
154 else if( *str2 == '\0' )
156 return(1);
158 else if( *str1 == '\0' )
160 return(-1);
163 return( *str1 - *str2 );
167 strncmp(str1,str2,len)
168 register char * str1;
169 register char * str2;
170 register int len;
172 malloc_check_strn("strncmp", str1, len);
173 malloc_check_strn("strncmp", str2, len);
175 while( --len >= 0 && *str1 && (*str1 == *str2) )
177 str1++;
178 str2++;
181 if( len < 0 )
183 return(0);
186 * in order to deal with the case of a negative last char of either
187 * string when the other string has a null
189 if( (*str2 == '\0') && (*str1 == '\0') )
191 return(0);
193 else if( *str2 == '\0' )
195 return(1);
197 else if( *str1 == '\0' )
199 return(-1);
202 return( *str1 - *str2 );
205 char *
206 strcpy(str1,str2)
207 register char * str1;
208 register char * str2;
210 char * rtn;
211 int len;
213 malloc_checking = 1;
214 len = strlen(str2) + 1;
215 malloc_checking = 0;
217 malloc_check_data("strcpy", str1, len);
218 malloc_check_data("strcpy", str2, len);
220 rtn = str1;
222 while( (*str1++ = *str2++) != '\0')
226 return(rtn);
229 char *
230 strncpy(str1,str2,len)
231 register char * str1;
232 register char * str2;
233 register int len;
235 extern int malloc_checking;
236 char * rtn;
238 malloc_check_data("strncpy", str1, len);
239 malloc_check_strn("strncpy", str2, len);
241 rtn = str1;
243 while((len-- > 0) && (*str1++ = *str2++) != '\0')
246 while( (len-- > 0) )
248 *str1++ = '\0';
251 return(rtn);
255 strlen(str1)
256 register char * str1;
258 register char * s;
260 if(! malloc_checking )
262 malloc_check_str("strlen", str1);
265 for( s = str1; *s; s++)
269 return( s - str1 );
272 char *
273 strchr(str1,c)
274 register char * str1;
275 register int c;
277 malloc_check_str("strchr", str1);
279 while( *str1 && (*str1 != (char) c) )
281 str1++;
284 if(*str1 != (char) c)
286 str1 = (char *) 0;
289 return(str1);
292 char *
293 strrchr(str1,c)
294 register char * str1;
295 register int c;
297 register char * rtn = (char *) 0;
299 malloc_check_str("strrchr", str1);
301 while( *str1 )
303 if(*str1 == (char) c )
305 rtn = str1;
307 str1++;
310 if( *str1 == (char) c)
312 rtn = str1;
315 return(rtn);
318 char *
319 index(str1,c)
320 char * str1;
321 char c;
323 return( strchr(str1,c) );
326 char *
327 rindex(str1,c)
328 char * str1;
329 char c;
331 return( strrchr(str1,c) );
334 char *
335 strpbrk(str1,str2)
336 register char * str1;
337 register char * str2;
339 register char * tmp;
341 malloc_check_str("strpbrk", str1);
342 malloc_check_str("strpbrk", str2);
344 while(*str1)
346 for( tmp=str2; *tmp && *tmp != *str1; tmp++)
349 if( *tmp )
351 break;
353 str1++;
356 if( ! *str1 )
358 str1 = (char *) 0;
361 return(str1);
365 strspn(str1,str2)
366 register char * str1;
367 register char * str2;
369 register char * tmp;
370 char * orig = str1;
372 malloc_check_str("strspn", str1);
373 malloc_check_str("strspn", str2);
375 while(*str1)
377 for( tmp=str2; *tmp && *tmp != *str1; tmp++)
380 if(! *tmp )
382 break;
384 str1++;
387 return( (int) (str1 - orig) );
391 strcspn(str1,str2)
392 register char * str1;
393 register char * str2;
395 register char * tmp;
396 char * orig = str1;
398 malloc_check_str("strcspn", str1);
399 malloc_check_str("strcspn", str2);
401 while(*str1)
403 for( tmp=str2; *tmp && *tmp != *str1; tmp++)
406 if( *tmp )
408 break;
410 str1++;
413 return( (int) (str1 - orig) );
417 * strtok() source taken from that posted to comp.lang.c by Chris Torek
418 * in Jan 1990.
422 * Copyright (c) 1989 The Regents of the University of California.
423 * All rights reserved.
425 * Redistribution and use in source and binary forms are permitted
426 * provided that the above copyright notice and this paragraph are
427 * duplicated in all such forms and that any documentation,
428 * advertising materials, and other materials related to such
429 * distribution and use acknowledge that the software was developed
430 * by the University of California, Berkeley. The name of the
431 * University may not be used to endorse or promote products derived
432 * from this software without specific prior written permission.
433 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
434 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
435 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
439 * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
440 * where tokens are nonempty strings separated by runs of
441 * chars from delim. Writes NULs into s to end tokens. delim need not
442 * remain constant from call to call.
444 * Modified by cpc: changed variable names to conform with naming
445 * conventions used in rest of code. Added malloc pointer
446 * check calls.
448 char *
449 strtok(str1, str2)
450 char * str1;
451 char * str2;
453 static char * last;
454 char * strtoken();
456 if( str1 )
458 malloc_check_str("strtok", str1);
459 last = str1;
461 malloc_check_str("strtok", str2);
463 return (strtoken(&last, str2, 1));
468 * Get next token from string *stringp, where tokens are (possibly empty)
469 * strings separated by characters from delim. Tokens are separated
470 * by exactly one delimiter iff the skip parameter is false; otherwise
471 * they are separated by runs of characters from delim, because we
472 * skip over any initial `delim' characters.
474 * Writes NULs into the string at *stringp to end tokens.
475 * delim will usually, but need not, remain constant from call to call.
476 * On return, *stringp points past the last NUL written (if there might
477 * be further tokens), or is NULL (if there are definitely no more tokens).
479 * If *stringp is NULL, strtoken returns NULL.
481 char *
482 strtoken(stringp, delim, skip)
483 register char **stringp;
484 register char *delim;
485 int skip;
487 register char *s;
488 register char *spanp;
489 register int c, sc;
490 char *tok;
492 if ((s = *stringp) == NULL)
493 return (NULL);
495 if (skip) {
497 * Skip (span) leading delimiters (s += strspn(s, delim)).
499 cont:
500 c = *s;
501 for (spanp = delim; (sc = *spanp++) != 0;) {
502 if (c == sc) {
503 s++;
504 goto cont;
507 if (c == 0) { /* no token found */
508 *stringp = NULL;
509 return (NULL);
514 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
515 * Note that delim must have one NUL; we stop if we see that, too.
517 for (tok = s;;) {
518 c = *s++;
519 spanp = delim;
520 do {
521 if ((sc = *spanp++) == c) {
522 if (c == 0)
523 s = NULL;
524 else
525 s[-1] = 0;
526 *stringp = s;
527 return (tok);
529 } while (sc != 0);
531 /* NOTREACHED */