delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2005/01/02/07:18:00

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
From: <ams AT ludd DOT ltu DOT se>
Message-Id: <200501021216.j02CGuF6000124@speedy.ludd.ltu.se>
Subject: Re: More complaints from tests/libclink/check
In-Reply-To: <01c4f053$Blat.v2.2.2$e18729e0@zahav.net.il> "from Eli Zaretskii
at Jan 2, 2005 00:46:30 am"
To: djgpp-workers AT delorie DOT com
Date: Sun, 2 Jan 2005 13:16:56 +0100 (CET)
X-Mailer: ELM [version 2.4ME+ PL78 (25)]
MIME-Version: 1.0
X-ltu-MailScanner-Information: Please contact the ISP for more information
X-ltu-MailScanner: Found to be clean
X-MailScanner-From: ams AT ludd DOT ltu DOT se
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019