net-snmp 5.7
strtok_r.c
00001 
00002 /*      $NetBSD: strtok_r.c,v 1.9 2003/08/07 16:43:53 agc Exp $ */
00003 
00004 /*
00005  * Copyright (c) 1988 Regents of the University of California.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the University nor the names of its contributors
00017  *    may be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  */
00032 #include <net-snmp/net-snmp-config.h>
00033 
00034 #ifndef WIN32
00035 #include <sys/cdefs.h>
00036 #endif
00037 
00038 #include <string.h>
00039 
00040 #include <net-snmp/library/system.h>
00041 
00042 /*
00043  * thread-safe version of strtok
00044  */
00045 char *
00046 strtok_r(char *s, const char *delim, char **lasts)
00047 {
00048         const char *spanp;
00049         int c, sc;
00050         char *tok;
00051 
00052         /* s may be NULL */
00053         /*netsnmp_assert(delim != NULL);*/
00054         /*netsnmp_assert(lasts != NULL);*/
00055 
00056         if (s == NULL && (s = *lasts) == NULL)
00057                 return (NULL);
00058 
00059         /*
00060          * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
00061          */
00062 cont:
00063         c = *s++;
00064         for (spanp = delim; (sc = *spanp++) != 0;) {
00065                 if (c == sc)
00066                         goto cont;
00067         }
00068 
00069         if (c == 0) {           /* no non-delimiter characters */
00070                 *lasts = NULL;
00071                 return (NULL);
00072         }
00073         tok = s - 1;
00074 
00075         /*
00076          * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
00077          * Note that delim must have one NUL; we stop if we see that, too.
00078          */
00079         for (;;) {
00080                 c = *s++;
00081                 spanp = delim;
00082                 do {
00083                         if ((sc = *spanp++) == c) {
00084                                 if (c == 0)
00085                                         s = NULL;
00086                                 else
00087                                         s[-1] = 0;
00088                                 *lasts = s;
00089                                 return (tok);
00090                         }
00091                 } while (sc != 0);
00092         }
00093         /* NOTREACHED */
00094 }