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