Blame


1 fbaf9fba 2021-04-25 op /* $OpenBSD: strsep.c,v 1.8 2015/08/31 02:53:57 guenther Exp $ */
2 fbaf9fba 2021-04-25 op
3 fbaf9fba 2021-04-25 op /*-
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.
6 fbaf9fba 2021-04-25 op *
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
9 fbaf9fba 2021-04-25 op * are met:
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.
18 fbaf9fba 2021-04-25 op *
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.
30 fbaf9fba 2021-04-25 op */
31 fbaf9fba 2021-04-25 op
32 fbaf9fba 2021-04-25 op #include <string.h>
33 fbaf9fba 2021-04-25 op
34 fbaf9fba 2021-04-25 op #include "compat.h"
35 fbaf9fba 2021-04-25 op
36 fbaf9fba 2021-04-25 op /*
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.
39 fbaf9fba 2021-04-25 op *
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).
44 fbaf9fba 2021-04-25 op *
45 fbaf9fba 2021-04-25 op * If *stringp is NULL, strsep returns NULL.
46 fbaf9fba 2021-04-25 op */
47 fbaf9fba 2021-04-25 op char *
48 fbaf9fba 2021-04-25 op strsep(char **stringp, const char *delim)
49 fbaf9fba 2021-04-25 op {
50 fbaf9fba 2021-04-25 op char *s;
51 fbaf9fba 2021-04-25 op const char *spanp;
52 fbaf9fba 2021-04-25 op int c, sc;
53 fbaf9fba 2021-04-25 op char *tok;
54 fbaf9fba 2021-04-25 op
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;;) {
58 fbaf9fba 2021-04-25 op c = *s++;
59 fbaf9fba 2021-04-25 op spanp = delim;
60 fbaf9fba 2021-04-25 op do {
61 fbaf9fba 2021-04-25 op if ((sc = *spanp++) == c) {
62 fbaf9fba 2021-04-25 op if (c == 0)
63 fbaf9fba 2021-04-25 op s = NULL;
64 fbaf9fba 2021-04-25 op else
65 fbaf9fba 2021-04-25 op s[-1] = 0;
66 fbaf9fba 2021-04-25 op *stringp = s;
67 fbaf9fba 2021-04-25 op return (tok);
68 fbaf9fba 2021-04-25 op }
69 fbaf9fba 2021-04-25 op } while (sc != 0);
70 fbaf9fba 2021-04-25 op }
71 fbaf9fba 2021-04-25 op /* NOTREACHED */
72 fbaf9fba 2021-04-25 op }