xref: /csrg-svn/lib/libc/string/strsep.c (revision 46144)
142252Smarc /*-
242252Smarc  * Copyright (c) 1990 The Regents of the University of California.
335538Sbostic  * All rights reserved.
435538Sbostic  *
542252Smarc  * %sccs.include.redist.c%
635538Sbostic  */
735538Sbostic 
8*46144Sbostic #include <sys/cdefs.h>
942252Smarc #include <string.h>
1042252Smarc #include <stdio.h>
1142252Smarc 
1235538Sbostic #if defined(LIBC_SCCS) && !defined(lint)
13*46144Sbostic static const char sccsid[] = "@(#)strsep.c	5.4 (Berkeley) 01/26/91";
1435538Sbostic #endif /* LIBC_SCCS and not lint */
1535538Sbostic 
1642252Smarc /*
1742252Smarc  * Get next token from string *stringp, where tokens are nonempty
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  *
2542252Smarc  * If *stringp is NULL, strtoken returns NULL.
2642252Smarc  */
2735538Sbostic char *
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