On Sat, Sep 08, 2018 at 10:13:35AM +0200, Martijn van Duren wrote:
> On 09/08/18 04:57, Michael Mikonos wrote:
> > Hello,
> > 
> > The function strsave() in csh(1) is practically strdup(3).
> > The only difference is memory allocation failure results in
> > calling the stderror() error handler, which will later exit.
> > This patch makes the code (IMO) clearer by removing two loops.
> > xmalloc() behaves the same as xreallocarray() in terms of
> > calling stderror(). Does this look OK?
> > 
> > - Michael
> > 
> Why not use strdup(3) altogether then? This way it's even more
> clear what's intended. Maybe we should even rename the function
> to xstrdup?
> 
> martijn@

Your patch was better. Here is a version with the function renamed
and const added to the param list to match strdup(3).


Index: extern.h
===================================================================
RCS file: /cvs/src/bin/csh/extern.h,v
retrieving revision 1.26
diff -u -p -r1.26 extern.h
--- extern.h    22 Jul 2017 09:37:21 -0000      1.26
+++ extern.h    14 Sep 2018 14:18:23 -0000
@@ -201,7 +201,7 @@ int   prefix(Char *, Char *);
 Char   **saveblk(Char **);
 Char    *strip(Char *);
 Char    *quote(Char *);
-char    *strsave(char *);
+char    *xstrdup(const char *);
 char    *strspl(char *, char *);
 void     udvar(Char *);
 
Index: error.c
===================================================================
RCS file: /cvs/src/bin/csh/error.c,v
retrieving revision 1.14
diff -u -p -r1.14 error.c
--- error.c     8 Sep 2018 01:28:39 -0000       1.14
+++ error.c     14 Sep 2018 14:18:23 -0000
@@ -289,7 +289,7 @@ seterror(int id, ...)
        vsnprintf(berr, sizeof(berr), errorlist[id], va);
        va_end(va);
 
-       seterr = strsave(berr);
+       seterr = xstrdup(berr);
     }
 }
 
Index: misc.c
===================================================================
RCS file: /cvs/src/bin/csh/misc.c,v
retrieving revision 1.20
diff -u -p -r1.20 misc.c
--- misc.c      20 Jun 2017 16:44:06 -0000      1.20
+++ misc.c      14 Sep 2018 14:18:23 -0000
@@ -34,6 +34,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <string.h>
 
 #include "csh.h"
 #include "extern.h"
@@ -53,18 +54,16 @@ any(char *s, int c)
 }
 
 char   *
-strsave(char *s)
+xstrdup(const char *s)
 {
-    char   *n;
-    char *p;
+    char *n;
 
     if (s == NULL)
        s = "";
-    for (p = s; *p++;)
-       continue;
-    n = p = xreallocarray(NULL, (p - s), sizeof(char));
-    while ((*p++ = *s++) != '\0')
-       continue;
+    if ((n = strdup(s)) == NULL) {
+       child++;
+       stderror(ERR_NOMEM);
+    }
     return (n);
 }
 
Index: str.c
===================================================================
RCS file: /cvs/src/bin/csh/str.c,v
retrieving revision 1.19
diff -u -p -r1.19 str.c
--- str.c       26 Oct 2015 16:31:09 -0000      1.19
+++ str.c       14 Sep 2018 14:18:23 -0000
@@ -77,7 +77,7 @@ short2blk(Char **src)
     sdst = dst = xreallocarray(NULL, n + 1, sizeof(char *));
 
     for (; *src != NULL; src++)
-       *dst++ = strsave(short2str(*src));
+       *dst++ = xstrdup(short2str(*src));
     *dst = NULL;
     return (sdst);
 }

Reply via email to