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