]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include: add remove_entry() to env.h
authorSami Kerola <kerolasa@iki.fi>
Sun, 12 Apr 2020 07:53:32 +0000 (08:53 +0100)
committerSami Kerola <kerolasa@iki.fi>
Mon, 13 Apr 2020 11:14:08 +0000 (12:14 +0100)
A function to remove an command-line option argument, or environment
variable.

Requested-by: Karel Zak <kzak@redhat.com>
Reference: https://github.com/karelzak/util-linux/pull/1003#discussion_r403988092
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/env.h
lib/env.c
text-utils/more.c

index db6f95932dc9b5ab5c7cca7cd99329bc6786eee1..6cee8e698aa46bfb9ac4a5e46d1311f989514dc4 100644 (file)
@@ -18,5 +18,11 @@ static inline void xsetenv(char const *name, char const *val, int overwrite)
                err(XSETENV_EXIT_CODE, _("failed to set the %s environment variable"), name);
 }
 
+static inline int remote_entry(char **argv, int remove, int last)
+{
+       memmove(argv + remove, argv + remove + 1, sizeof(char *) * (last - remove));
+       return last - 1;
+}
+
 #endif /* UTIL_LINUX_ENV_H */
 
index ea7d457827854966b1ccfd7de7fdb294d3f15290..009130db4128cead31195c7788cf923e7ea108e4 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -56,13 +56,15 @@ sanitize_env(void)
         char **envp = environ;
         char * const *bad;
         char **cur;
-        char **move;
+        int last = 0;
+
+        for (cur = envp; *cur; cur++)
+                last++;
 
         for (cur = envp; *cur; cur++) {
                 for (bad = forbid; *bad; bad++) {
                         if (strncmp(*cur, *bad, strlen(*bad)) == 0) {
-                                for (move = cur; *move; move++)
-                                        *move = *(move + 1);
+                                last = remote_entry(envp, cur - envp, last);
                                 cur--;
                                 break;
                         }
@@ -75,8 +77,7 @@ sanitize_env(void)
                                 continue;
                         if (!strchr(*cur, '/'))
                                 continue;  /* OK */
-                        for (move = cur; *move; move++)
-                                *move = *(move + 1);
+                        last = remote_entry(envp, cur - envp, last);
                         cur--;
                         break;
                 }
index b4029444fc9ce52b98bbef5443ed95007efcce9e..bbced5c38aea2d4d8ecc7655b48212c13f8f028a 100644 (file)
@@ -74,6 +74,7 @@
 # include <term.h>
 #endif
 
+#include "env.h"
 #include "strutils.h"
 #include "nls.h"
 #include "xalloc.h"
@@ -251,8 +252,7 @@ static void argscan(struct more_control *ctl, int as_argc, char **as_argv)
                        }
                }
                if (move) {
-                       as_argc--;
-                       memmove(as_argv + opt, as_argv + opt + 1, sizeof(char *) * (as_argc - opt));
+                       as_argc = remote_entry(as_argv, opt, as_argc);
                        opt--;
                }
        }