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.
14 char rcs_hdr
[] = "$Id: string.c,v 1.2 2006-07-25 10:10:03 rt Exp $";
17 int malloc_checking
= 0;
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.
35 malloc_check_str("strcat", str2
);
37 len
+= strlen(str1
) + 1;
40 malloc_check_data("strcat", str1
, len
);
49 while( (*str1
= *str2
) != '\0' )
66 malloc_check_str("strdup", str1
);
68 rtn
= str2
= malloc((unsigned)strlen(str1
)+1);
70 if( rtn
!= (char *) 0)
72 while( (*str2
= *str1
) != '\0' )
83 strncat(str1
,str2
,len
)
92 malloc_check_strn("strncat", str2
, len
);
96 len2
= strlen(str2
) + 1;
110 malloc_check_data("strncat", str1
, len1
);
119 while( len
-- && ((*str1
++ = *str2
++) != '\0') )
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
) )
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') )
154 else if( *str2
== '\0' )
158 else if( *str1
== '\0' )
163 return( *str1
- *str2
);
167 strncmp(str1
,str2
,len
)
168 register char * str1
;
169 register char * str2
;
172 malloc_check_strn("strncmp", str1
, len
);
173 malloc_check_strn("strncmp", str2
, len
);
175 while( --len
>= 0 && *str1
&& (*str1
== *str2
) )
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') )
193 else if( *str2
== '\0' )
197 else if( *str1
== '\0' )
202 return( *str1
- *str2
);
207 register char * str1
;
208 register char * str2
;
214 len
= strlen(str2
) + 1;
217 malloc_check_data("strcpy", str1
, len
);
218 malloc_check_data("strcpy", str2
, len
);
222 while( (*str1
++ = *str2
++) != '\0')
230 strncpy(str1
,str2
,len
)
231 register char * str1
;
232 register char * str2
;
235 extern int malloc_checking
;
238 malloc_check_data("strncpy", str1
, len
);
239 malloc_check_strn("strncpy", str2
, len
);
243 while((len
-- > 0) && (*str1
++ = *str2
++) != '\0')
256 register char * str1
;
260 if(! malloc_checking
)
262 malloc_check_str("strlen", str1
);
265 for( s
= str1
; *s
; s
++)
274 register char * str1
;
277 malloc_check_str("strchr", str1
);
279 while( *str1
&& (*str1
!= (char) c
) )
284 if(*str1
!= (char) c
)
294 register char * str1
;
297 register char * rtn
= (char *) 0;
299 malloc_check_str("strrchr", str1
);
303 if(*str1
== (char) c
)
310 if( *str1
== (char) c
)
323 return( strchr(str1
,c
) );
331 return( strrchr(str1
,c
) );
336 register char * str1
;
337 register char * str2
;
341 malloc_check_str("strpbrk", str1
);
342 malloc_check_str("strpbrk", str2
);
346 for( tmp
=str2
; *tmp
&& *tmp
!= *str1
; tmp
++)
366 register char * str1
;
367 register char * str2
;
372 malloc_check_str("strspn", str1
);
373 malloc_check_str("strspn", str2
);
377 for( tmp
=str2
; *tmp
&& *tmp
!= *str1
; tmp
++)
387 return( (int) (str1
- orig
) );
392 register char * str1
;
393 register char * str2
;
398 malloc_check_str("strcspn", str1
);
399 malloc_check_str("strcspn", str2
);
403 for( tmp
=str2
; *tmp
&& *tmp
!= *str1
; tmp
++)
413 return( (int) (str1
- orig
) );
417 * strtok() source taken from that posted to comp.lang.c by Chris Torek
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
458 malloc_check_str("strtok", 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.
482 strtoken(stringp
, delim
, skip
)
483 register char **stringp
;
484 register char *delim
;
488 register char *spanp
;
492 if ((s
= *stringp
) == NULL
)
497 * Skip (span) leading delimiters (s += strspn(s, delim)).
501 for (spanp
= delim
; (sc
= *spanp
++) != 0;) {
507 if (c
== 0) { /* no token found */
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.
521 if ((sc
= *spanp
++) == c
) {