xref: /onnv-gate/usr/src/lib/libc/port/gen/strsep.c (revision 7478:f2ea9cb5143d)
1*7478SVladimir.Kotal@Sun.COM /*
2*7478SVladimir.Kotal@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3*7478SVladimir.Kotal@Sun.COM  * Use is subject to license terms.
4*7478SVladimir.Kotal@Sun.COM  */
5*7478SVladimir.Kotal@Sun.COM 
6*7478SVladimir.Kotal@Sun.COM /*	$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $	*/
7*7478SVladimir.Kotal@Sun.COM 
8*7478SVladimir.Kotal@Sun.COM /*
9*7478SVladimir.Kotal@Sun.COM  * Copyright (c) 1990, 1993
10*7478SVladimir.Kotal@Sun.COM  *	The Regents of the University of California.  All rights reserved.
11*7478SVladimir.Kotal@Sun.COM  *
12*7478SVladimir.Kotal@Sun.COM  * Redistribution and use in source and binary forms, with or without
13*7478SVladimir.Kotal@Sun.COM  * modification, are permitted provided that the following conditions
14*7478SVladimir.Kotal@Sun.COM  * are met:
15*7478SVladimir.Kotal@Sun.COM  * 1. Redistributions of source code must retain the above copyright
16*7478SVladimir.Kotal@Sun.COM  *    notice, this list of conditions and the following disclaimer.
17*7478SVladimir.Kotal@Sun.COM  * 2. Redistributions in binary form must reproduce the above copyright
18*7478SVladimir.Kotal@Sun.COM  *    notice, this list of conditions and the following disclaimer in the
19*7478SVladimir.Kotal@Sun.COM  *    documentation and/or other materials provided with the distribution.
20*7478SVladimir.Kotal@Sun.COM  * 3. All advertising materials mentioning features or use of this software
21*7478SVladimir.Kotal@Sun.COM  *    must display the following acknowledgement:
22*7478SVladimir.Kotal@Sun.COM  *	This product includes software developed by the University of
23*7478SVladimir.Kotal@Sun.COM  *	California, Berkeley and its contributors.
24*7478SVladimir.Kotal@Sun.COM  * 4. Neither the name of the University nor the names of its contributors
25*7478SVladimir.Kotal@Sun.COM  *    may be used to endorse or promote products derived from this software
26*7478SVladimir.Kotal@Sun.COM  *    without specific prior written permission.
27*7478SVladimir.Kotal@Sun.COM  *
28*7478SVladimir.Kotal@Sun.COM  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29*7478SVladimir.Kotal@Sun.COM  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30*7478SVladimir.Kotal@Sun.COM  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31*7478SVladimir.Kotal@Sun.COM  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32*7478SVladimir.Kotal@Sun.COM  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*7478SVladimir.Kotal@Sun.COM  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*7478SVladimir.Kotal@Sun.COM  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*7478SVladimir.Kotal@Sun.COM  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36*7478SVladimir.Kotal@Sun.COM  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37*7478SVladimir.Kotal@Sun.COM  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38*7478SVladimir.Kotal@Sun.COM  * SUCH DAMAGE.
39*7478SVladimir.Kotal@Sun.COM  */
40*7478SVladimir.Kotal@Sun.COM 
41*7478SVladimir.Kotal@Sun.COM #include "lint.h"
42*7478SVladimir.Kotal@Sun.COM #include <string.h>
43*7478SVladimir.Kotal@Sun.COM #include <stdio.h>
44*7478SVladimir.Kotal@Sun.COM 
45*7478SVladimir.Kotal@Sun.COM #if defined(LIBC_SCCS) && !defined(lint)
46*7478SVladimir.Kotal@Sun.COM #if 0
47*7478SVladimir.Kotal@Sun.COM static char sccsid[] = "@(#)strsep.c	8.1 (Berkeley) 6/4/93";
48*7478SVladimir.Kotal@Sun.COM #else
49*7478SVladimir.Kotal@Sun.COM static char *rcsid =
50*7478SVladimir.Kotal@Sun.COM 	"$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $";
51*7478SVladimir.Kotal@Sun.COM #endif
52*7478SVladimir.Kotal@Sun.COM #endif /* LIBC_SCCS and not lint */
53*7478SVladimir.Kotal@Sun.COM 
54*7478SVladimir.Kotal@Sun.COM /*
55*7478SVladimir.Kotal@Sun.COM  * Get next token from string *stringp, where tokens are possibly-empty
56*7478SVladimir.Kotal@Sun.COM  * strings separated by characters from delim.
57*7478SVladimir.Kotal@Sun.COM  *
58*7478SVladimir.Kotal@Sun.COM  * Writes NULs into the string at *stringp to end tokens.
59*7478SVladimir.Kotal@Sun.COM  * delim need not remain constant from call to call.
60*7478SVladimir.Kotal@Sun.COM  * On return, *stringp points past the last NUL written (if there might
61*7478SVladimir.Kotal@Sun.COM  * be further tokens), or is NULL (if there are definitely no more tokens).
62*7478SVladimir.Kotal@Sun.COM  *
63*7478SVladimir.Kotal@Sun.COM  * If *stringp is NULL, strsep returns NULL.
64*7478SVladimir.Kotal@Sun.COM  */
65*7478SVladimir.Kotal@Sun.COM char *
strsep(char ** stringp,const char * delim)66*7478SVladimir.Kotal@Sun.COM strsep(char **stringp, const char *delim)
67*7478SVladimir.Kotal@Sun.COM {
68*7478SVladimir.Kotal@Sun.COM 	register char *s;
69*7478SVladimir.Kotal@Sun.COM 	register const char *spanp;
70*7478SVladimir.Kotal@Sun.COM 	register int c, sc;
71*7478SVladimir.Kotal@Sun.COM 	char *tok;
72*7478SVladimir.Kotal@Sun.COM 
73*7478SVladimir.Kotal@Sun.COM 	if ((s = *stringp) == NULL)
74*7478SVladimir.Kotal@Sun.COM 		return (NULL);
75*7478SVladimir.Kotal@Sun.COM 	for (tok = s; ; ) {
76*7478SVladimir.Kotal@Sun.COM 		c = *s++;
77*7478SVladimir.Kotal@Sun.COM 		spanp = delim;
78*7478SVladimir.Kotal@Sun.COM 		do {
79*7478SVladimir.Kotal@Sun.COM 			if ((sc = *spanp++) == c) {
80*7478SVladimir.Kotal@Sun.COM 				if (c == 0)
81*7478SVladimir.Kotal@Sun.COM 					s = NULL;
82*7478SVladimir.Kotal@Sun.COM 				else
83*7478SVladimir.Kotal@Sun.COM 					s[-1] = 0;
84*7478SVladimir.Kotal@Sun.COM 				*stringp = s;
85*7478SVladimir.Kotal@Sun.COM 				return (tok);
86*7478SVladimir.Kotal@Sun.COM 			}
87*7478SVladimir.Kotal@Sun.COM 		} while (sc != 0);
88*7478SVladimir.Kotal@Sun.COM 	}
89*7478SVladimir.Kotal@Sun.COM 	/* NOTREACHED */
90*7478SVladimir.Kotal@Sun.COM }
91