xref: /freebsd/contrib/tcpdump/missing/strsep.c (revision 5b0fe47811aa43b75fc69dbf7338cace232a4d48)
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 #if defined(LIBC_SCCS) && !defined(lint)
355b0fe478SBruce M Simpson static const char rcsid[] =
365b0fe478SBruce M Simpson     "@(#) $Header: /tcpdump/master/tcpdump/missing/strsep.c,v 1.3 2003/03/25 08:33:48 guy Exp $ (LBL)";
375b0fe478SBruce M Simpson #endif /* LIBC_SCCS and not lint */
385b0fe478SBruce M Simpson 
395b0fe478SBruce M Simpson #ifdef HAVE_CONFIG_H
405b0fe478SBruce M Simpson #include <config.h>
415b0fe478SBruce M Simpson #endif
425b0fe478SBruce M Simpson 
435b0fe478SBruce M Simpson #include <tcpdump-stdinc.h>
445b0fe478SBruce M Simpson 
455b0fe478SBruce M Simpson #include <string.h>
465b0fe478SBruce M Simpson 
475b0fe478SBruce M Simpson /*
485b0fe478SBruce M Simpson  * Get next token from string *stringp, where tokens are possibly-empty
495b0fe478SBruce M Simpson  * strings separated by characters from delim.
505b0fe478SBruce M Simpson  *
515b0fe478SBruce M Simpson  * Writes NULs into the string at *stringp to end tokens.
525b0fe478SBruce M Simpson  * delim need not remain constant from call to call.
535b0fe478SBruce M Simpson  * On return, *stringp points past the last NUL written (if there might
545b0fe478SBruce M Simpson  * be further tokens), or is NULL (if there are definitely no more tokens).
555b0fe478SBruce M Simpson  *
565b0fe478SBruce M Simpson  * If *stringp is NULL, strsep returns NULL.
575b0fe478SBruce M Simpson  */
585b0fe478SBruce M Simpson char *
595b0fe478SBruce M Simpson strsep(char **stringp, const char *delim)
605b0fe478SBruce M Simpson {
615b0fe478SBruce M Simpson 	register char *s;
625b0fe478SBruce M Simpson 	register const char *spanp;
635b0fe478SBruce M Simpson 	register int c, sc;
645b0fe478SBruce M Simpson 	char *tok;
655b0fe478SBruce M Simpson 
665b0fe478SBruce M Simpson 	if ((s = *stringp) == NULL)
675b0fe478SBruce M Simpson 		return (NULL);
685b0fe478SBruce M Simpson 	for (tok = s;;) {
695b0fe478SBruce M Simpson 		c = *s++;
705b0fe478SBruce M Simpson 		spanp = delim;
715b0fe478SBruce M Simpson 		do {
725b0fe478SBruce M Simpson 			if ((sc = *spanp++) == c) {
735b0fe478SBruce M Simpson 				if (c == 0)
745b0fe478SBruce M Simpson 					s = NULL;
755b0fe478SBruce M Simpson 				else
765b0fe478SBruce M Simpson 					s[-1] = 0;
775b0fe478SBruce M Simpson 				*stringp = s;
785b0fe478SBruce M Simpson 				return (tok);
795b0fe478SBruce M Simpson 			}
805b0fe478SBruce M Simpson 		} while (sc != 0);
815b0fe478SBruce M Simpson 	}
825b0fe478SBruce M Simpson 	/* NOTREACHED */
835b0fe478SBruce M Simpson }
84