Mail Archives: djgpp-workers/2005/01/02/07:18:00
According to Eli Zaretskii:
> I don't see how allocation off the heap would solve reentrancy
> problems. Static storage is used there as simple solutions to
> specific problems; replacing that with malloc'ed or automatic storage
> could easily break the functionality.
>
> Let's see your suggested implementations, and talk then specifically.
Here's my strtok_r() (pasted) and all will be clear (I hope!):
New strtok.c:
/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <string.h>
char *
strtok(char *s, const char *delim)
{
static char *last;
return strtok_r(s, delim, &last);
}
diff -u between original strtok.c and strtok_r.c:
--- /x3/cvs/martin/djgpp/djgpp/src/libc/ansi/string/strtok.c.safe Tue Nov 29 10:40:46 1994
+++ /x3/cvs/martin/djgpp/djgpp/src/libc/posix/string/strtok_r.c Fri Dec 31 15:15:42 2004
@@ -1,16 +1,15 @@
+/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <string.h>
char *
-strtok(char *s, const char *delim)
+strtok_r(char *s, const char *delim, char **last)
{
const char *spanp;
int c, sc;
char *tok;
- static char *last;
-
- if (s == NULL && (s = last) == NULL)
+ if (s == NULL && (s = *last) == NULL)
return (NULL);
/*
@@ -24,7 +23,7 @@
}
if (c == 0) { /* no non-delimiter characters */
- last = NULL;
+ *last = NULL;
return (NULL);
}
tok = s - 1;
@@ -42,7 +41,7 @@
s = NULL;
else
s[-1] = 0;
- last = s;
+ *last = s;
return (tok);
}
} while (sc != 0);
In general the POSIX _r() functions add parameters so that they don't
use any global or static storage and let the caller provide the
necessary storage space.
Right,
MartinS
- Raw text -