xref: /freebsd/contrib/tcpdump/missing/strsep.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
15b0fe478SBruce M Simpson /*-
25b0fe478SBruce M Simpson  * Copyright (c) 1990, 1993
35b0fe478SBruce M Simpson  *	The Regents of the University of California.  All rights reserved.
45b0fe478SBruce M Simpson  *
55b0fe478SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
65b0fe478SBruce M Simpson  * modification, are permitted provided that the following conditions
75b0fe478SBruce M Simpson  * are met:
85b0fe478SBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
95b0fe478SBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
105b0fe478SBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
115b0fe478SBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
125b0fe478SBruce M Simpson  *    documentation and/or other materials provided with the distribution.
135b0fe478SBruce M Simpson  * 3. All advertising materials mentioning features or use of this software
145b0fe478SBruce M Simpson  *    must display the following acknowledgement:
155b0fe478SBruce M Simpson  *	This product includes software developed by the University of
165b0fe478SBruce M Simpson  *	California, Berkeley and its contributors.
175b0fe478SBruce M Simpson  * 4. Neither the name of the University nor the names of its contributors
185b0fe478SBruce M Simpson  *    may be used to endorse or promote products derived from this software
195b0fe478SBruce M Simpson  *    without specific prior written permission.
205b0fe478SBruce M Simpson  *
215b0fe478SBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225b0fe478SBruce M Simpson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235b0fe478SBruce M Simpson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245b0fe478SBruce M Simpson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255b0fe478SBruce M Simpson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265b0fe478SBruce M Simpson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275b0fe478SBruce M Simpson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285b0fe478SBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295b0fe478SBruce M Simpson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305b0fe478SBruce M Simpson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315b0fe478SBruce M Simpson  * SUCH DAMAGE.
325b0fe478SBruce M Simpson  */
335b0fe478SBruce M Simpson 
345b0fe478SBruce M Simpson #include <config.h>
355b0fe478SBruce M Simpson 
363340d773SGleb Smirnoff #include <netdissect-stdinc.h>
375b0fe478SBruce M Simpson 
385b0fe478SBruce M Simpson #include <string.h>
395b0fe478SBruce M Simpson 
403340d773SGleb Smirnoff #include "netdissect.h"
418bdc5a62SPatrick Kelsey 
425b0fe478SBruce M Simpson /*
435b0fe478SBruce M Simpson  * Get next token from string *stringp, where tokens are possibly-empty
445b0fe478SBruce M Simpson  * strings separated by characters from delim.
455b0fe478SBruce M Simpson  *
465b0fe478SBruce M Simpson  * Writes NULs into the string at *stringp to end tokens.
475b0fe478SBruce M Simpson  * delim need not remain constant from call to call.
485b0fe478SBruce M Simpson  * On return, *stringp points past the last NUL written (if there might
495b0fe478SBruce M Simpson  * be further tokens), or is NULL (if there are definitely no more tokens).
505b0fe478SBruce M Simpson  *
515b0fe478SBruce M Simpson  * If *stringp is NULL, strsep returns NULL.
525b0fe478SBruce M Simpson  */
535b0fe478SBruce M Simpson char *
strsep(char ** stringp,const char * delim)545b0fe478SBruce M Simpson strsep(char **stringp, const char *delim)
555b0fe478SBruce M Simpson {
56*ee67461eSJoseph Mingrone 	char *s;
57*ee67461eSJoseph Mingrone 	const char *spanp;
58*ee67461eSJoseph Mingrone 	int c, sc;
595b0fe478SBruce M Simpson 	char *tok;
605b0fe478SBruce M Simpson 
615b0fe478SBruce M Simpson 	if ((s = *stringp) == NULL)
625b0fe478SBruce M Simpson 		return (NULL);
635b0fe478SBruce M Simpson 	for (tok = s;;) {
645b0fe478SBruce M Simpson 		c = *s++;
655b0fe478SBruce M Simpson 		spanp = delim;
665b0fe478SBruce M Simpson 		do {
675b0fe478SBruce M Simpson 			if ((sc = *spanp++) == c) {
685b0fe478SBruce M Simpson 				if (c == 0)
695b0fe478SBruce M Simpson 					s = NULL;
705b0fe478SBruce M Simpson 				else
715b0fe478SBruce M Simpson 					s[-1] = 0;
725b0fe478SBruce M Simpson 				*stringp = s;
735b0fe478SBruce M Simpson 				return (tok);
745b0fe478SBruce M Simpson 			}
755b0fe478SBruce M Simpson 		} while (sc != 0);
765b0fe478SBruce M Simpson 	}
775b0fe478SBruce M Simpson 	/* NOTREACHED */
785b0fe478SBruce M Simpson }
79