xref: /csrg-svn/lib/libc/string/strsep.c (revision 61193)
142252Smarc /*-
2*61193Sbostic  * Copyright (c) 1990, 1993
3*61193Sbostic  *	The Regents of the University of California.  All rights reserved.
435538Sbostic  *
542252Smarc  * %sccs.include.redist.c%
635538Sbostic  */
735538Sbostic 
846144Sbostic #include <sys/cdefs.h>
942252Smarc #include <string.h>
1042252Smarc #include <stdio.h>
1142252Smarc 
1235538Sbostic #if defined(LIBC_SCCS) && !defined(lint)
13*61193Sbostic static char sccsid[] = "@(#)strsep.c	8.1 (Berkeley) 06/04/93";
1435538Sbostic #endif /* LIBC_SCCS and not lint */
1535538Sbostic 
1642252Smarc /*
1755814Sbostic  * Get next token from string *stringp, where tokens are possibly-empty
1842252Smarc  * strings separated by characters from delim.
1942252Smarc  *
2042252Smarc  * Writes NULs into the string at *stringp to end tokens.
2142252Smarc  * delim need not remain constant from call to call.
2242252Smarc  * On return, *stringp points past the last NUL written (if there might
2342252Smarc  * be further tokens), or is NULL (if there are definitely no more tokens).
2442252Smarc  *
2555814Sbostic  * If *stringp is NULL, strsep returns NULL.
2642252Smarc  */
2735538Sbostic char *
strsep(stringp,delim)2842252Smarc strsep(stringp, delim)
2942252Smarc 	register char **stringp;
3042252Smarc 	register const char *delim;
3135538Sbostic {
3242252Smarc 	register char *s;
3342252Smarc 	register const char *spanp;
3435538Sbostic 	register int c, sc;
3535538Sbostic 	char *tok;
3635538Sbostic 
3742252Smarc 	if ((s = *stringp) == NULL)
3842252Smarc 		return (NULL);
3942252Smarc 	for (tok = s;;) {
4042252Smarc 		c = *s++;
4135538Sbostic 		spanp = delim;
4235538Sbostic 		do {
4335538Sbostic 			if ((sc = *spanp++) == c) {
4442252Smarc 				if (c == 0)
4542252Smarc 					s = NULL;
4642252Smarc 				else
4742252Smarc 					s[-1] = 0;
4842252Smarc 				*stringp = s;
4942252Smarc 				return (tok);
5035538Sbostic 			}
5142252Smarc 		} while (sc != 0);
5235538Sbostic 	}
5335538Sbostic 	/* NOTREACHED */
5435538Sbostic }
55