Blame


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