include: add remove_entry() to env.h

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>
This commit is contained in:
Sami Kerola 2020-04-12 08:53:32 +01:00
parent 3d6fa8da69
commit ed292a08dd
No known key found for this signature in database
GPG Key ID: 0D46FEF7E61DBB46
3 changed files with 14 additions and 7 deletions

View 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 */

View File

@ -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;
}

View 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--;
}
}