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