xref: /netbsd-src/external/bsd/tcpdump/dist/missing/strsep.c (revision c41df9f6167ea7cd2f761f0a97783c8267cb8847)
10f74e101Schristos /*-
20f74e101Schristos  * Copyright (c) 1990, 1993
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that the following conditions
70f74e101Schristos  * are met:
80f74e101Schristos  * 1. Redistributions of source code must retain the above copyright
90f74e101Schristos  *    notice, this list of conditions and the following disclaimer.
100f74e101Schristos  * 2. Redistributions in binary form must reproduce the above copyright
110f74e101Schristos  *    notice, this list of conditions and the following disclaimer in the
120f74e101Schristos  *    documentation and/or other materials provided with the distribution.
130f74e101Schristos  * 3. All advertising materials mentioning features or use of this software
140f74e101Schristos  *    must display the following acknowledgement:
150f74e101Schristos  *	This product includes software developed by the University of
160f74e101Schristos  *	California, Berkeley and its contributors.
170f74e101Schristos  * 4. Neither the name of the University nor the names of its contributors
180f74e101Schristos  *    may be used to endorse or promote products derived from this software
190f74e101Schristos  *    without specific prior written permission.
200f74e101Schristos  *
210f74e101Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220f74e101Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230f74e101Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240f74e101Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250f74e101Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260f74e101Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270f74e101Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280f74e101Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290f74e101Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300f74e101Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310f74e101Schristos  * SUCH DAMAGE.
320f74e101Schristos  */
330f74e101Schristos 
340f74e101Schristos #include <config.h>
350f74e101Schristos 
36784088dfSchristos #include <netdissect-stdinc.h>
370f74e101Schristos 
380f74e101Schristos #include <string.h>
390f74e101Schristos 
40784088dfSchristos #include "netdissect.h"
413d25ea14Schristos 
420f74e101Schristos /*
430f74e101Schristos  * Get next token from string *stringp, where tokens are possibly-empty
440f74e101Schristos  * strings separated by characters from delim.
450f74e101Schristos  *
460f74e101Schristos  * Writes NULs into the string at *stringp to end tokens.
470f74e101Schristos  * delim need not remain constant from call to call.
480f74e101Schristos  * On return, *stringp points past the last NUL written (if there might
490f74e101Schristos  * be further tokens), or is NULL (if there are definitely no more tokens).
500f74e101Schristos  *
510f74e101Schristos  * If *stringp is NULL, strsep returns NULL.
520f74e101Schristos  */
530f74e101Schristos char *
540f74e101Schristos strsep(char **stringp, const char *delim)
550f74e101Schristos {
56*d881c474Schristos 	char *s;
57*d881c474Schristos 	const char *spanp;
58*d881c474Schristos 	int c, sc;
590f74e101Schristos 	char *tok;
600f74e101Schristos 
610f74e101Schristos 	if ((s = *stringp) == NULL)
620f74e101Schristos 		return (NULL);
630f74e101Schristos 	for (tok = s;;) {
640f74e101Schristos 		c = *s++;
650f74e101Schristos 		spanp = delim;
660f74e101Schristos 		do {
670f74e101Schristos 			if ((sc = *spanp++) == c) {
680f74e101Schristos 				if (c == 0)
690f74e101Schristos 					s = NULL;
700f74e101Schristos 				else
710f74e101Schristos 					s[-1] = 0;
720f74e101Schristos 				*stringp = s;
730f74e101Schristos 				return (tok);
740f74e101Schristos 			}
750f74e101Schristos 		} while (sc != 0);
760f74e101Schristos 	}
770f74e101Schristos 	/* NOTREACHED */
780f74e101Schristos }
79