xref: /freebsd/contrib/mandoc/compat_strsep.c (revision c1c95add8c80843ba15d784f95c361d795b1f593)
1*c1c95addSBrooks Davis /*	$Id: compat_strsep.c,v 1.6 2022/06/21 10:34:14 schwarze Exp $	*/
26d38604fSBaptiste Daroussin /*	$OpenBSD: strsep.c,v 1.8 2015/08/31 02:53:57 guenther Exp $	*/
361d06d6bSBaptiste Daroussin 
461d06d6bSBaptiste Daroussin /*-
561d06d6bSBaptiste Daroussin  * Copyright (c) 1990, 1993
661d06d6bSBaptiste Daroussin  *	The Regents of the University of California.  All rights reserved.
761d06d6bSBaptiste Daroussin  *
861d06d6bSBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
961d06d6bSBaptiste Daroussin  * modification, are permitted provided that the following conditions
1061d06d6bSBaptiste Daroussin  * are met:
1161d06d6bSBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
1261d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
1361d06d6bSBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
1461d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
1561d06d6bSBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
1661d06d6bSBaptiste Daroussin  * 3. Neither the name of the University nor the names of its contributors
1761d06d6bSBaptiste Daroussin  *    may be used to endorse or promote products derived from this software
1861d06d6bSBaptiste Daroussin  *    without specific prior written permission.
1961d06d6bSBaptiste Daroussin  *
2061d06d6bSBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2161d06d6bSBaptiste Daroussin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261d06d6bSBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361d06d6bSBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2461d06d6bSBaptiste Daroussin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2561d06d6bSBaptiste Daroussin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2661d06d6bSBaptiste Daroussin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2761d06d6bSBaptiste Daroussin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2861d06d6bSBaptiste Daroussin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2961d06d6bSBaptiste Daroussin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3061d06d6bSBaptiste Daroussin  * SUCH DAMAGE.
3161d06d6bSBaptiste Daroussin  */
326d38604fSBaptiste Daroussin #include "config.h"
3361d06d6bSBaptiste Daroussin 
34*c1c95addSBrooks Davis #include <stddef.h>
35*c1c95addSBrooks Davis 
3661d06d6bSBaptiste Daroussin /*
3761d06d6bSBaptiste Daroussin  * Get next token from string *stringp, where tokens are possibly-empty
3861d06d6bSBaptiste Daroussin  * strings separated by characters from delim.
3961d06d6bSBaptiste Daroussin  *
4061d06d6bSBaptiste Daroussin  * Writes NULs into the string at *stringp to end tokens.
4161d06d6bSBaptiste Daroussin  * delim need not remain constant from call to call.
4261d06d6bSBaptiste Daroussin  * On return, *stringp points past the last NUL written (if there might
4361d06d6bSBaptiste Daroussin  * be further tokens), or is NULL (if there are definitely no more tokens).
4461d06d6bSBaptiste Daroussin  *
4561d06d6bSBaptiste Daroussin  * If *stringp is NULL, strsep returns NULL.
4661d06d6bSBaptiste Daroussin  */
4761d06d6bSBaptiste Daroussin char *
strsep(char ** stringp,const char * delim)4861d06d6bSBaptiste Daroussin strsep(char **stringp, const char *delim)
4961d06d6bSBaptiste Daroussin {
5061d06d6bSBaptiste Daroussin 	char *s;
5161d06d6bSBaptiste Daroussin 	const char *spanp;
5261d06d6bSBaptiste Daroussin 	int c, sc;
5361d06d6bSBaptiste Daroussin 	char *tok;
5461d06d6bSBaptiste Daroussin 
5561d06d6bSBaptiste Daroussin 	if ((s = *stringp) == NULL)
5661d06d6bSBaptiste Daroussin 		return (NULL);
5761d06d6bSBaptiste Daroussin 	for (tok = s;;) {
5861d06d6bSBaptiste Daroussin 		c = *s++;
5961d06d6bSBaptiste Daroussin 		spanp = delim;
6061d06d6bSBaptiste Daroussin 		do {
6161d06d6bSBaptiste Daroussin 			if ((sc = *spanp++) == c) {
6261d06d6bSBaptiste Daroussin 				if (c == 0)
6361d06d6bSBaptiste Daroussin 					s = NULL;
6461d06d6bSBaptiste Daroussin 				else
6561d06d6bSBaptiste Daroussin 					s[-1] = 0;
6661d06d6bSBaptiste Daroussin 				*stringp = s;
6761d06d6bSBaptiste Daroussin 				return (tok);
6861d06d6bSBaptiste Daroussin 			}
6961d06d6bSBaptiste Daroussin 		} while (sc != 0);
7061d06d6bSBaptiste Daroussin 	}
7161d06d6bSBaptiste Daroussin 	/* NOTREACHED */
7261d06d6bSBaptiste Daroussin }
73