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/missing/strtok_r.h> 00045 00046 #ifndef HAVE_STRTOK_R 00047 00048 #include <stdio.h> 00049 #include <stddef.h> 00050 #include <string.h> 00051 00052 char *strtok_r(char *s, const char *delim, char **last) 00053 { 00054 char *spanp; 00055 int c, sc; 00056 char *tok; 00057 00058 if (s == NULL && (s = *last) == NULL) 00059 { 00060 return NULL; 00061 } 00062 00063 /* 00064 * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 00065 */ 00066 cont: 00067 c = *s++; 00068 for (spanp = (char *)delim; (sc = *spanp++) != 0; ) 00069 { 00070 if (c == sc) 00071 { 00072 goto cont; 00073 } 00074 } 00075 00076 if (c == 0) /* no non-delimiter characters */ 00077 { 00078 *last = NULL; 00079 return NULL; 00080 } 00081 tok = s - 1; 00082 00083 /* 00084 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 00085 * Note that delim must have one NUL; we stop if we see that, too. 00086 */ 00087 for (;;) 00088 { 00089 c = *s++; 00090 spanp = (char *)delim; 00091 do 00092 { 00093 if ((sc = *spanp++) == c) 00094 { 00095 if (c == 0) 00096 { 00097 s = NULL; 00098 } 00099 else 00100 { 00101 char *w = s - 1; 00102 *w = '\0'; 00103 } 00104 *last = s; 00105 return tok; 00106 } 00107 } 00108 while (sc != 0); 00109 } 00110 /* NOTREACHED */ 00111 } 00112 00113 #else /* HAVE_STRTOK_R */ 00114 char *strtok_r(char *s, const char *delim, char **last); 00115 #endif /* !HAVE_STRTOK_R */