00001 /* 00002 * Copyright (c) 1998 Softweyr LLC. All rights reserved. 00003 * 00004 * strtok_r, from Berkeley strtok 00005 * Oct 13, 1998 by Wes Peters <wes@softweyr.com> 00006 * 00007 * Copyright (c) 1988, 1993 00008 * The Regents of the University of California. All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notices, this list of conditions and the following disclaimer. 00016 * 00017 * 2. Redistributions in binary form must reproduce the above copyright 00018 * notices, this list of conditions and the following disclaimer in the 00019 * documentation and/or other materials provided with the distribution. 00020 * 00021 * 3. All advertising materials mentioning features or use of this software 00022 * must display the following acknowledgement: 00023 * 00024 * This product includes software developed by Softweyr LLC, the 00025 * University of California, Berkeley, and its contributors. 00026 * 00027 * 4. Neither the name of the University nor the names of its contributors 00028 * may be used to endorse or promote products derived from this software 00029 * without specific prior written permission. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS 00032 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00033 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00034 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE 00035 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00036 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00037 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00038 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00039 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00040 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00041 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00042 */ 00043 00044 #include <u/libu_conf.h> 00045 00046 #ifndef HAVE_STRTOK_R 00047 #include <stddef.h> 00048 #include <string.h> 00049 00050 char * 00051 strtok_r(char *s, const char *delim, char **last) 00052 { 00053 char *spanp; 00054 int c, sc; 00055 char *tok; 00056 00057 if (s == NULL && (s = *last) == NULL) 00058 { 00059 return NULL; 00060 } 00061 00062 /* 00063 * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 00064 */ 00065 cont: 00066 c = *s++; 00067 for (spanp = (char *)delim; (sc = *spanp++) != 0; ) 00068 { 00069 if (c == sc) 00070 { 00071 goto cont; 00072 } 00073 } 00074 00075 if (c == 0) /* no non-delimiter characters */ 00076 { 00077 *last = NULL; 00078 return NULL; 00079 } 00080 tok = s - 1; 00081 00082 /* 00083 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 00084 * Note that delim must have one NUL; we stop if we see that, too. 00085 */ 00086 for (;;) 00087 { 00088 c = *s++; 00089 spanp = (char *)delim; 00090 do 00091 { 00092 if ((sc = *spanp++) == c) 00093 { 00094 if (c == 0) 00095 { 00096 s = NULL; 00097 } 00098 else 00099 { 00100 char *w = s - 1; 00101 *w = '\0'; 00102 } 00103 *last = s; 00104 return tok; 00105 } 00106 } 00107 while (sc != 0); 00108 } 00109 /* NOTREACHED */ 00110 } 00111 00112 #else 00113 char *strtok_r(char *s, const char *delim, char **last); 00114 #endif