home *** CD-ROM | disk | FTP | other *** search
- /* str.c -- string functions missing from BSD
- Copyright (C) 1989 David MacKenzie
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- #define NULL 0
-
- char *index ();
-
- /* Return the length of the span of characters at the start of `string'
- that are members of `class'. */
-
- int
- strspn (string, class)
- char *string;
- char *class;
- {
- int count;
-
- for (count = 0; string[count]; ++count)
- if (!index (class, string[count]))
- break;
- return count;
- }
-
- /* Return the length of the span of characters at the start of `string'
- that are non-members of `class'. */
-
- int
- strcspn (string, class)
- char *string;
- char *class;
- {
- int count;
-
- for (count = 0; string[count]; ++count)
- if (index (class, string[count]))
- break;
- return count;
- }
-
- /* Return the next token in `string', delimited by one or more members of
- the set `separators'. If `string' is NULL, use the same string as in the
- last call. */
-
- char *
- strtok (string, separators)
- char *string;
- char *separators;
- {
- static char *pos = NULL; /* Current location in the string. */
- int token_length;
-
- if (string)
- pos = string;
- pos += strspn (pos, separators); /* Skip initial separators. */
- token_length = strcspn (pos, separators); /* Find token length. */
- if (token_length == 0)
- return NULL; /* No more tokens; pos is on a 0. */
- separators = pos; /* Re-use separators to save start of token. */
- pos += token_length; /* Move onto the 0. */
- if (*pos) /* If not the last token, */
- *pos++ = 0; /* null terminate the token. */
- return separators;
- }
-