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