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