xref: /freebsd/contrib/libpcap/missing/strtok_r.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1ada6f083SXin LI /*-
2ada6f083SXin LI  * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
3ada6f083SXin LI  *
4ada6f083SXin LI  * strtok_r, from Berkeley strtok
5ada6f083SXin LI  * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6ada6f083SXin LI  *
7ada6f083SXin LI  * Copyright (c) 1988, 1993
8ada6f083SXin LI  *	The Regents of the University of California.  All rights reserved.
9ada6f083SXin LI  *
10ada6f083SXin LI  * Redistribution and use in source and binary forms, with or without
11ada6f083SXin LI  * modification, are permitted provided that the following conditions
12ada6f083SXin LI  * are met:
13ada6f083SXin LI  * 1. Redistributions of source code must retain the above copyright
14ada6f083SXin LI  *    notices, this list of conditions and the following disclaimer.
15ada6f083SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
16ada6f083SXin LI  *    notices, this list of conditions and the following disclaimer in the
17ada6f083SXin LI  *    documentation and/or other materials provided with the distribution.
18ada6f083SXin LI  * 3. Neither the name of the University nor the names of its contributors
19ada6f083SXin LI  *    may be used to endorse or promote products derived from this software
20ada6f083SXin LI  *    without specific prior written permission.
21ada6f083SXin LI  *
22ada6f083SXin LI  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23ada6f083SXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24ada6f083SXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25ada6f083SXin LI  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
26ada6f083SXin LI  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27ada6f083SXin LI  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28ada6f083SXin LI  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29ada6f083SXin LI  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30ada6f083SXin LI  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31ada6f083SXin LI  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32ada6f083SXin LI  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33ada6f083SXin LI  *
34ada6f083SXin LI  * From: @(#)strtok.c	8.1 (Berkeley) 6/4/93
35ada6f083SXin LI  */
36ada6f083SXin LI 
37b00ab754SHans Petter Selasky #include <config.h>
38ada6f083SXin LI 
39ada6f083SXin LI #include "portability.h"
40ada6f083SXin LI 
41ada6f083SXin LI char *
pcapint_strtok_r(char * s,const char * delim,char ** last)42*afdbf109SJoseph Mingrone pcapint_strtok_r(char *s, const char *delim, char **last)
43ada6f083SXin LI {
44ada6f083SXin LI 	char *spanp, *tok;
45ada6f083SXin LI 	int c, sc;
46ada6f083SXin LI 
47ada6f083SXin LI 	if (s == NULL && (s = *last) == NULL)
48ada6f083SXin LI 		return (NULL);
49ada6f083SXin LI 
50ada6f083SXin LI 	/*
51ada6f083SXin LI 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
52ada6f083SXin LI 	 */
53ada6f083SXin LI cont:
54ada6f083SXin LI 	c = *s++;
55ada6f083SXin LI 	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
56ada6f083SXin LI 		if (c == sc)
57ada6f083SXin LI 			goto cont;
58ada6f083SXin LI 	}
59ada6f083SXin LI 
60ada6f083SXin LI 	if (c == 0) {		/* no non-delimiter characters */
61ada6f083SXin LI 		*last = NULL;
62ada6f083SXin LI 		return (NULL);
63ada6f083SXin LI 	}
64ada6f083SXin LI 	tok = s - 1;
65ada6f083SXin LI 
66ada6f083SXin LI 	/*
67ada6f083SXin LI 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
68ada6f083SXin LI 	 * Note that delim must have one NUL; we stop if we see that, too.
69ada6f083SXin LI 	 */
70ada6f083SXin LI 	for (;;) {
71ada6f083SXin LI 		c = *s++;
72ada6f083SXin LI 		spanp = (char *)delim;
73ada6f083SXin LI 		do {
74ada6f083SXin LI 			if ((sc = *spanp++) == c) {
75ada6f083SXin LI 				if (c == 0)
76ada6f083SXin LI 					s = NULL;
77ada6f083SXin LI 				else
78ada6f083SXin LI 					s[-1] = '\0';
79ada6f083SXin LI 				*last = s;
80ada6f083SXin LI 				return (tok);
81ada6f083SXin LI 			}
82ada6f083SXin LI 		} while (sc != 0);
83ada6f083SXin LI 	}
84ada6f083SXin LI 	/* NOTREACHED */
85ada6f083SXin LI }
86