xref: /original-bsd/lib/libc/string/strsep.c (revision 6d57652c)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #include <sys/cdefs.h>
9 #include <string.h>
10 #include <stdio.h>
11 
12 #if defined(LIBC_SCCS) && !defined(lint)
13 static const char sccsid[] = "@(#)strsep.c	5.4 (Berkeley) 01/26/91";
14 #endif /* LIBC_SCCS and not lint */
15 
16 /*
17  * Get next token from string *stringp, where tokens are nonempty
18  * strings separated by characters from delim.
19  *
20  * Writes NULs into the string at *stringp to end tokens.
21  * delim need not remain constant from call to call.
22  * On return, *stringp points past the last NUL written (if there might
23  * be further tokens), or is NULL (if there are definitely no more tokens).
24  *
25  * If *stringp is NULL, strtoken returns NULL.
26  */
27 char *
28 strsep(stringp, delim)
29 	register char **stringp;
30 	register const char *delim;
31 {
32 	register char *s;
33 	register const char *spanp;
34 	register int c, sc;
35 	char *tok;
36 
37 	if ((s = *stringp) == NULL)
38 		return (NULL);
39 	for (tok = s;;) {
40 		c = *s++;
41 		spanp = delim;
42 		do {
43 			if ((sc = *spanp++) == c) {
44 				if (c == 0)
45 					s = NULL;
46 				else
47 					s[-1] = 0;
48 				*stringp = s;
49 				return (tok);
50 			}
51 		} while (sc != 0);
52 	}
53 	/* NOTREACHED */
54 }
55