sl@0: /* sl@0: * FUNCTION sl@0: * <>---get next token from a string sl@0: * INDEX sl@0: * strtok sl@0: * INDEX sl@0: * strtok_r sl@0: * ANSI_SYNOPSIS sl@0: * #include sl@0: * char *strtok(char *<[source]>, const char *<[delimiters]>) sl@0: * char *strtok_r(char *<[source]>, const char *<[delimiters]>, sl@0: * char **<[lasts]>) sl@0: * TRAD_SYNOPSIS sl@0: * #include sl@0: * char *strtok(<[source]>, <[delimiters]>) sl@0: * char *<[source]>; sl@0: * char *<[delimiters]>; sl@0: * char *strtok_r(<[source]>, <[delimiters]>, <[lasts]>) sl@0: * char *<[source]>; sl@0: * char *<[delimiters]>; sl@0: * char **<[lasts]>; sl@0: * A series of calls to <> break the string starting at sl@0: * <<*<[source]>>> into a sequence of tokens. The tokens are sl@0: * delimited from one another by characters from the string at sl@0: * <<*<[delimiters]>>>, at the outset. sl@0: * The first call to <> normally has a string address as sl@0: * the first argument; subsequent calls can use <> as the sl@0: * first argument, to continue searching the same string. You sl@0: * can continue searching a single string with different sl@0: * delimiters by using a different delimiter string on each call. sl@0: * <> begins by searching for any character not in the sl@0: * <[delimiters]> string: the first such character is the sl@0: * beginning of a token (and its address will be the result of sl@0: * the <> call). <> then continues searching sl@0: * until it finds another delimiter character; it replaces that sl@0: * character by <> and returns. (If <> comes to sl@0: * the end of the <<*<[source]>>> string without finding any more sl@0: * delimiters, the entire remainder of the string is treated as sl@0: * the next token). sl@0: * <> starts its search at <<*<[source]>>>, unless you sl@0: * pass <> as the first argument; if <[source]> is sl@0: * <>, <> continues searching from the end of the sl@0: * last search. Exploiting the <> first argument leads to sl@0: * non-reentrant code. You can easily circumvent this problem by sl@0: * saving the last delimiter address in your application, and sl@0: * always using it to pass a non-null <[source]> argument. sl@0: * RETURNS sl@0: * <> returns a pointer to the next token, or <> if sl@0: * no more tokens can be found. sl@0: * PORTABILITY sl@0: * <> is ANSI C. sl@0: * <> requires no supporting OS subroutines. sl@0: * QUICKREF sl@0: * strtok ansi impure sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: sl@0: /** sl@0: Sequentially truncate string if delimiter is found. sl@0: If string is not NULL, the function scans string for the first occurrence of sl@0: any character included in delimiters. If it is found, the function overwrites sl@0: the delimiter in string by a null-character and returns a pointer to the token, sl@0: i.e. the part of the scanned string previous to the delimiter. sl@0: After a first call to strtok, the function may be called with NULL as string parameter, sl@0: and it will follow by where the last call to strtok found a delimiter. sl@0: @return A pointer to the last token found in string. sl@0: NULL is returned when there are no more tokens to be found. sl@0: @param s Null-terminated string to scan. sl@0: @param delim Null-terminated string containing the separators. sl@0: */ sl@0: EXPORT_C char * sl@0: strtok (register char *s, register const char *delim) sl@0: { sl@0: return strtok_r (s, delim, &(_REENT->_scanpoint)); sl@0: } sl@0: sl@0: /* sl@0: * Copyright (c) 1988 Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * This product includes software developed by the University of sl@0: * California, Berkeley and its contributors. sl@0: * 4. Neither the name of the University nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: */ sl@0: sl@0: EXPORT_C char * sl@0: strtok_r (register char *s, register const char *delim, char **lasts) sl@0: { sl@0: register char *spanp; sl@0: register int c, sc; sl@0: char *tok; sl@0: sl@0: sl@0: if (s == NULL && (s = *lasts) == NULL) sl@0: return (NULL); sl@0: sl@0: /* sl@0: * Skip (span) leading delimiters (s += strspn(s, delim), sort of). sl@0: */ sl@0: cont: sl@0: c = *s++; sl@0: for (spanp = (char *)delim; (sc = *spanp++) != 0;) { sl@0: if (c == sc) sl@0: goto cont; sl@0: } sl@0: sl@0: if (c == 0) { /* no non-delimiter characters */ sl@0: *lasts = NULL; sl@0: return (NULL); sl@0: } sl@0: tok = s - 1; sl@0: sl@0: /* sl@0: * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). sl@0: * Note that delim must have one NUL; we stop if we see that, too. sl@0: */ sl@0: for (;;) { sl@0: c = *s++; sl@0: spanp = (char *)delim; sl@0: do { sl@0: if ((sc = *spanp++) == c) { sl@0: if (c == 0) sl@0: s = NULL; sl@0: else sl@0: s[-1] = 0; sl@0: *lasts = s; sl@0: return (tok); sl@0: } sl@0: } while (sc != 0); sl@0: } sl@0: /* NOTREACHED */ sl@0: }