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@

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      8 Sep 2018 08:10:34 -0000
@@ -34,6 +34,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <string.h>
 
 #include "csh.h"
 #include "extern.h"
@@ -55,16 +56,14 @@ any(char *s, int c)
 char   *
 strsave(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);
 }
 

Reply via email to