1 fbaf9fba 2021-04-25 op /* $OpenBSD: strsep.c,v 1.8 2015/08/31 02:53:57 guenther Exp $ */
4 fbaf9fba 2021-04-25 op * Copyright (c) 1990, 1993
5 fbaf9fba 2021-04-25 op * The Regents of the University of California. All rights reserved.
7 fbaf9fba 2021-04-25 op * Redistribution and use in source and binary forms, with or without
8 fbaf9fba 2021-04-25 op * modification, are permitted provided that the following conditions
10 fbaf9fba 2021-04-25 op * 1. Redistributions of source code must retain the above copyright
11 fbaf9fba 2021-04-25 op * notice, this list of conditions and the following disclaimer.
12 fbaf9fba 2021-04-25 op * 2. Redistributions in binary form must reproduce the above copyright
13 fbaf9fba 2021-04-25 op * notice, this list of conditions and the following disclaimer in the
14 fbaf9fba 2021-04-25 op * documentation and/or other materials provided with the distribution.
15 fbaf9fba 2021-04-25 op * 3. Neither the name of the University nor the names of its contributors
16 fbaf9fba 2021-04-25 op * may be used to endorse or promote products derived from this software
17 fbaf9fba 2021-04-25 op * without specific prior written permission.
19 fbaf9fba 2021-04-25 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 fbaf9fba 2021-04-25 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 fbaf9fba 2021-04-25 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 fbaf9fba 2021-04-25 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 fbaf9fba 2021-04-25 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 fbaf9fba 2021-04-25 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 fbaf9fba 2021-04-25 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 fbaf9fba 2021-04-25 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 fbaf9fba 2021-04-25 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 fbaf9fba 2021-04-25 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 fbaf9fba 2021-04-25 op * SUCH DAMAGE.
32 fbaf9fba 2021-04-25 op #include <string.h>
34 fbaf9fba 2021-04-25 op #include "compat.h"
37 fbaf9fba 2021-04-25 op * Get next token from string *stringp, where tokens are possibly-empty
38 fbaf9fba 2021-04-25 op * strings separated by characters from delim.
40 fbaf9fba 2021-04-25 op * Writes NULs into the string at *stringp to end tokens.
41 fbaf9fba 2021-04-25 op * delim need not remain constant from call to call.
42 fbaf9fba 2021-04-25 op * On return, *stringp points past the last NUL written (if there might
43 fbaf9fba 2021-04-25 op * be further tokens), or is NULL (if there are definitely no more tokens).
45 fbaf9fba 2021-04-25 op * If *stringp is NULL, strsep returns NULL.
48 fbaf9fba 2021-04-25 op strsep(char **stringp, const char *delim)
51 fbaf9fba 2021-04-25 op const char *spanp;
55 fbaf9fba 2021-04-25 op if ((s = *stringp) == NULL)
56 fbaf9fba 2021-04-25 op return (NULL);
57 fbaf9fba 2021-04-25 op for (tok = s;;) {
59 fbaf9fba 2021-04-25 op spanp = delim;
61 fbaf9fba 2021-04-25 op if ((sc = *spanp++) == c) {
69 fbaf9fba 2021-04-25 op } while (sc != 0);
71 fbaf9fba 2021-04-25 op /* NOTREACHED */