Re: [PATCH] Lacking strtok_r
On Fri, Mar 28, 2008 at 08:33:00AM -0700, Dan Fandrich wrote:
> On Fri, Mar 28, 2008 at 12:36:58PM +0100, Alain Bench wrote:
> > ...due to recursive calls to strtok(). We'd need a replacement
> > strtok_r() for systems where it lacks.
>
> Hmmm, I didn't notice the recursion--that explains the change to
> strtok_r. I'll see what I can do about that.
Attached is a patch that replaces strtok_r when it's not available. It
comes from glibc 2.6.1 (like the strsep replacement) and uses the same
autoconf hooks.
It would be a good idea now to replace all strtok calls with strtok_r so
the same types of bugs that occurred in the past don't accidentally
recur as changes are made in the future.
>>> Dan
--
http://www.MoveAnnouncer.com The web change of address service
Let webmasters know that your web site has moved
--- configure.ac.orig Fri Mar 28 13:20:24 2008
+++ configure.ac Fri Mar 28 13:18:29 2008
@@ -335,7 +335,7 @@
AC_CHECK_FUNCS(fgetpos memmove setegid srand48 strerror)
-AC_REPLACE_FUNCS([setenv strcasecmp strdup strsep])
+AC_REPLACE_FUNCS([setenv strcasecmp strdup strsep strtok_r])
AC_CHECK_FUNC(getopt)
if test $ac_cv_func_getopt = yes; then
--- /dev/null Tue Apr 25 01:53:54 1995
+++ strtok_r.c Fri Mar 28 13:19:37 2008
@@ -0,0 +1,63 @@
+/* Reentrant string tokenizer. Generic version.
+ Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <string.h>
+
+/* Taken from glibc 2.6.1 */
+
+/* Parse S into tokens separated by characters in DELIM.
+ If S is NULL, the saved pointer in SAVE_PTR is used as
+ the next starting point. For example:
+ char s[] = "-abc-=-def";
+ char *sp;
+ x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
+ x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
+ x = strtok_r(NULL, "=", &sp); // x = NULL
+ // s = "abc\0-def\0"
+*/
+char *
+strtok_r (char *s, const char *delim, char **save_ptr)
+{
+ char *token;
+
+ if (s == NULL)
+ s = *save_ptr;
+
+ /* Scan leading delimiters. */
+ s += strspn (s, delim);
+ if (*s == '\0')
+ {
+ *save_ptr = s;
+ return NULL;
+ }
+
+ /* Find the end of the token. */
+ token = s;
+ s = strpbrk (token, delim);
+ if (s == NULL)
+ /* This token finishes the string. */
+ *save_ptr = strchr (token, '\0');
+ else
+ {
+ /* Terminate the token and make *SAVE_PTR point past it. */
+ *s = '\0';
+ *save_ptr = s + 1;
+ }
+ return token;
+}