Blame


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