xref: /csrg-svn/lib/libc/string/strsep.c (revision 35538)
1*35538Sbostic /*
2*35538Sbostic  * Copyright (c) 1988 The Regents of the University of California.
3*35538Sbostic  * All rights reserved.
4*35538Sbostic  *
5*35538Sbostic  * Redistribution and use in source and binary forms are permitted
6*35538Sbostic  * provided that the above copyright notice and this paragraph are
7*35538Sbostic  * duplicated in all such forms and that any documentation,
8*35538Sbostic  * advertising materials, and other materials related to such
9*35538Sbostic  * distribution and use acknowledge that the software was developed
10*35538Sbostic  * by the University of California, Berkeley.  The name of the
11*35538Sbostic  * University may not be used to endorse or promote products derived
12*35538Sbostic  * from this software without specific prior written permission.
13*35538Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35538Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35538Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*35538Sbostic  */
17*35538Sbostic 
18*35538Sbostic #if defined(LIBC_SCCS) && !defined(lint)
19*35538Sbostic static char sccsid[] = "@(#)strsep.c	5.1 (Berkeley) 09/19/88";
20*35538Sbostic #endif /* LIBC_SCCS and not lint */
21*35538Sbostic 
22*35538Sbostic #include <stdio.h>
23*35538Sbostic 
24*35538Sbostic char *
25*35538Sbostic strsep(s, delim)
26*35538Sbostic 	register char *s, *delim;
27*35538Sbostic {
28*35538Sbostic 	register char *spanp;
29*35538Sbostic 	register int c, sc;
30*35538Sbostic 	static char *last;
31*35538Sbostic 	char *tok;
32*35538Sbostic 
33*35538Sbostic 	if (s == NULL && (s = last) == NULL)
34*35538Sbostic 		return(NULL);
35*35538Sbostic 
36*35538Sbostic 	/*
37*35538Sbostic 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
38*35538Sbostic 	 * Note that delim must have one NUL; we stop if we see that, too.
39*35538Sbostic 	 */
40*35538Sbostic 	for (tok = s;; ++s) {
41*35538Sbostic 		c = *s;
42*35538Sbostic 		spanp = delim;
43*35538Sbostic 		do {
44*35538Sbostic 			if ((sc = *spanp++) == c) {
45*35538Sbostic 				if (c == 0) {
46*35538Sbostic 					last = NULL;
47*35538Sbostic 					return(tok == s ? NULL : tok);
48*35538Sbostic 				}
49*35538Sbostic 				*s++ = '\0';
50*35538Sbostic 				last = s;
51*35538Sbostic 				return(tok);
52*35538Sbostic 			}
53*35538Sbostic 		} while (sc);
54*35538Sbostic 	}
55*35538Sbostic 	/* NOTREACHED */
56*35538Sbostic }
57