Blame


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