Blame


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