logo

utils-std

Collection of commonly available Unix tools
commit: e5d7e7e39bd832c9ef2303b491443807db984548
parent 2393325139c74193441482603b7c3cc5aac36587
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 21 Apr 2024 13:26:00 +0200

cmd/test: Remove support for shell and switch to access(2)

Only relevant for awkward builtin cases, test(1) isn't setuid
and otherwise could drop privileges.

Also adapted testsuite to work when launched as root as well.

Diffstat:

Mcmd/test.c27++++-----------------------
Mtest-cmd/test.sh2+-
2 files changed, 5 insertions(+), 24 deletions(-)

diff --git a/cmd/test.c b/cmd/test.c @@ -11,13 +11,6 @@ * * This program is in the Public Domain. */ -/* - * Important: This file is used both as a standalone program /bin/test and - * as a builtin for /bin/sh (#define SHELL). - */ - -// For eaccess -#define _GNU_SOURCE #include <sys/types.h> #include <sys/stat.h> @@ -31,10 +24,6 @@ #include <string.h> #include <unistd.h> -#ifdef SHELL -#define main testcmd -#include "bltin/bltin.h" -#else #include <locale.h> static void @@ -46,7 +35,6 @@ error(const char *msg, ...) /*NOTREACHED*/ va_end(ap); } -#endif /* test(1) accepts the following grammar: oexpr ::= aexpr | aexpr "-o" oexpr ; @@ -209,9 +197,7 @@ main(int argc, char **argv) if (--argc <= 0) return 1; -#ifndef SHELL (void)setlocale(LC_CTYPE, ""); -#endif nargc = argc; t_wp = &argv[1]; parenlevel = 0; @@ -373,18 +359,13 @@ filstat(char *nm, enum token mode) switch (mode) { case FILRD: - return (eaccess(nm, R_OK) == 0); + return (access(nm, R_OK) == 0); case FILWR: - return (eaccess(nm, W_OK) == 0); + return (access(nm, W_OK) == 0); case FILEX: - /* XXX work around eaccess(2) false positives for superuser */ - if (eaccess(nm, X_OK) != 0) - return 0; - if (S_ISDIR(s.st_mode) || geteuid() != 0) - return 1; - return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0; + return (access(nm, X_OK) == 0); case FILEXIST: - return (eaccess(nm, F_OK) == 0); + return (access(nm, F_OK) == 0); case FILREG: return S_ISREG(s.st_mode); case FILDIR: diff --git a/test-cmd/test.sh b/test-cmd/test.sh @@ -92,7 +92,7 @@ t 0 'true -o -X' t 0 '\( \( \( a = a \) -o 1 \) -a 1 \) -a true' t 1 '-h /' t 0 '-r /' -t 1 '-w /' +t "$([ "$(id -u)" == 0 ]; echo $?)" '-w /' t 0 '-x /bin/sh' t 0 '-c /dev/null' t 0 '-f /etc/passwd'