xref: /csrg-svn/lib/libc/string/strtok.c (revision 61193)
124193Skre /*
2*61193Sbostic  * Copyright (c) 1988, 1993
3*61193Sbostic  *	The Regents of the University of California.  All rights reserved.
434479Sbostic  *
542637Sbostic  * %sccs.include.redist.c%
624193Skre  */
724193Skre 
826538Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61193Sbostic static char sccsid[] = "@(#)strtok.c	8.1 (Berkeley) 06/04/93";
1034479Sbostic #endif /* LIBC_SCCS and not lint */
1124193Skre 
1242181Sbostic #include <stddef.h>
1342181Sbostic #include <string.h>
1435297Sbostic 
1524193Skre char *
strtok(s,delim)1635297Sbostic strtok(s, delim)
1746613Sbostic 	register char *s;
1846613Sbostic 	register const char *delim;
1924193Skre {
2035297Sbostic 	register char *spanp;
2135297Sbostic 	register int c, sc;
2235297Sbostic 	char *tok;
2335297Sbostic 	static char *last;
2424193Skre 
2524193Skre 
2635297Sbostic 	if (s == NULL && (s = last) == NULL)
2735297Sbostic 		return (NULL);
2835297Sbostic 
2935297Sbostic 	/*
3035297Sbostic 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
3135297Sbostic 	 */
3235297Sbostic cont:
3335297Sbostic 	c = *s++;
3446613Sbostic 	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
3535297Sbostic 		if (c == sc)
3635297Sbostic 			goto cont;
3724193Skre 	}
3824193Skre 
3935297Sbostic 	if (c == 0) {		/* no non-delimiter characters */
4035297Sbostic 		last = NULL;
4135297Sbostic 		return (NULL);
4224193Skre 	}
4335297Sbostic 	tok = s - 1;
4424193Skre 
4535297Sbostic 	/*
4635297Sbostic 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
4735297Sbostic 	 * Note that delim must have one NUL; we stop if we see that, too.
4835297Sbostic 	 */
4935297Sbostic 	for (;;) {
5035297Sbostic 		c = *s++;
5146613Sbostic 		spanp = (char *)delim;
5235297Sbostic 		do {
5335297Sbostic 			if ((sc = *spanp++) == c) {
5435297Sbostic 				if (c == 0)
5535297Sbostic 					s = NULL;
5635297Sbostic 				else
5735297Sbostic 					s[-1] = 0;
5835297Sbostic 				last = s;
5935297Sbostic 				return (tok);
6035297Sbostic 			}
6135297Sbostic 		} while (sc != 0);
6224193Skre 	}
6335297Sbostic 	/* NOTREACHED */
6424193Skre }
65