maint: copy bootstrap from Gnulib
[debian/gzip] / util.c
diff --git a/util.c b/util.c
index 4713e0ae702e179badec6e7c43faafaceaf05512..8e0f35f24284e261a1719ba6a4dfcc40324a6ae7 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,6 +1,6 @@
 /* util.c -- utility functions for gzip support
 
-   Copyright (C) 1997-1999, 2001-2002, 2006, 2009-2013 Free Software
+   Copyright (C) 1997-1999, 2001-2002, 2006, 2009-2017 Free Software
    Foundation, Inc.
    Copyright (C) 1992-1993 Jean-loup Gailly
 
 #include <config.h>
 #include <ctype.h>
 #include <errno.h>
-
-#include "tailor.h"
-
 #include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <errno.h>
 
+#include "tailor.h"
 #include "gzip.h"
+#include <dirname.h>
 #include <xalloc.h>
 
 #ifndef CHAR_BIT
@@ -301,18 +300,7 @@ char *
 gzip_base_name (fname)
     char *fname;
 {
-    char *p;
-
-    if ((p = strrchr(fname, PATH_SEP))  != NULL) fname = p+1;
-#ifdef PATH_SEP2
-    if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
-#endif
-#ifdef PATH_SEP3
-    if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
-#endif
-#ifdef SUFFIX_SEP
-    if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
-#endif
+    fname = last_component (fname);
     if (casemap('A') == 'a') strlwr(fname);
     return fname;
 }
@@ -361,11 +349,15 @@ void make_simple_name(name)
     } while (p != name);
 }
 
-/* ========================================================================
- * Add an environment variable (if any) before argv, and update argc.
- * Return the expanded environment variable to be freed later, or NULL
- * if no options were added to argv.
- */
+/* Convert the value of the environment variable ENVVAR_NAME
+   to a newly allocated argument vector, and set *ARGCP and *ARGVP
+   to the number of arguments and to the vector, respectively.
+   Make the new vector's zeroth element equal to the old **ARGVP.
+   Return a pointer to the newly allocated string storage.
+
+   If the vector would be empty, do not allocate storage,
+   do not set *ARGCP and *ARGVP, and return NULL.  */
+
 #define SEPARATOR      " \t"   /* separators in env variable */
 
 char *add_envopt(
@@ -376,7 +368,6 @@ char *add_envopt(
     char *p;             /* running pointer through env variable */
     char **oargv;        /* runs through old argv array */
     char **nargv;        /* runs through new argv array */
-    int         oargc = *argcp; /* old argc */
     int  nargc = 0;      /* number of arguments in env variable */
     char *env_val;
 
@@ -396,7 +387,7 @@ char *add_envopt(
         free(env_val);
         return NULL;
     }
-    *argcp += nargc;
+    *argcp = nargc + 1;
     /* Allocate the new argv array, with an extra element just in case
      * the original arg list did not end with a NULL.
      */
@@ -405,9 +396,7 @@ char *add_envopt(
     *argvp = nargv;
 
     /* Copy the program name first */
-    if (oargc-- < 0)
-      gzip_error ("argc<=0");
-    *(nargv++) = *(oargv++);
+    *nargv++ = *oargv;
 
     /* Then copy the environment args */
     for (p = env_val; nargc > 0; nargc--) {
@@ -416,8 +405,6 @@ char *add_envopt(
         while (*p++) ;                      /* skip over word */
     }
 
-    /* Finally copy the old args and add a NULL (usual convention) */
-    while (oargc--) *(nargv++) = *(oargv++);
     *nargv = NULL;
     return env_val;
 }