readlink: support --, -n always

POSIX will be standardizing readlink (just the -n option) and realpath
(just -E and -e options):
https://www.austingroupbugs.net/view.php?id=1457

Change things for readlink so that the POSIX-mandated -n and -- work
even when disabling the non-standard (and partially non-working) -f
when FEATURE_READLINK_FOLLOW is clear.

POSIX also wants readlink to be verbose by default (if the argument is
not a symlink, readlink must output a diagnostic); I did NOT address
that one, because I'm waiting to see what the GNU Coreutils folks do:
https://lists.gnu.org/archive/html/bug-coreutils/2023-03/msg00035.html

Partial fix for https://bugs.busybox.net/show_bug.cgi?id=15466

function                                             old     new   delta
packed_usage                                       34538   34557     +19

Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Eric Blake
2023-03-24 09:58:14 -05:00
committed by Denys Vlasenko
parent 2ffd8986e2
commit d2b81b3dc2

View File

@@ -25,12 +25,14 @@
//kbuild:lib-$(CONFIG_READLINK) += readlink.o
//usage:#define readlink_trivial_usage
//usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ") "FILE"
//usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ")
//usage: IF_NOT_FEATURE_READLINK_FOLLOW("[-n] ")
//usage: "FILE"
//usage:#define readlink_full_usage "\n\n"
//usage: "Display the value of a symlink"
//usage: IF_FEATURE_READLINK_FOLLOW( "\n"
//usage: "\n -f Canonicalize by following all symlinks"
//usage: "Display the value of a symlink" "\n"
//usage: "\n -n Don't add newline"
//usage: IF_FEATURE_READLINK_FOLLOW(
//usage: "\n -f Canonicalize by following all symlinks"
//usage: "\n -v Verbose"
//usage: )
@@ -67,25 +69,18 @@ int readlink_main(int argc UNUSED_PARAM, char **argv)
{
char *buf;
char *fname;
unsigned opt;
IF_FEATURE_READLINK_FOLLOW(
unsigned opt;
/* We need exactly one non-option argument. */
opt = getopt32(argv, "^" "fnvsq" "\0" "=1");
fname = argv[optind];
)
IF_NOT_FEATURE_READLINK_FOLLOW(
const unsigned opt = 0;
if (argc != 2) bb_show_usage();
fname = argv[1];
)
opt = getopt32(argv, "^" "n" IF_FEATURE_READLINK_FOLLOW("fvsq")
"\0" "=1");
fname = argv[optind];
/* compat: coreutils readlink reports errors silently via exit code */
if (!(opt & 4)) /* not -v */
logmode = LOGMODE_NONE;
/* NOFORK: only one alloc is allowed; must free */
if (opt & 1) { /* -f */
if (opt & 2) { /* -f */
buf = xmalloc_realpath_coreutils(fname);
} else {
buf = xmalloc_readlink_or_warn(fname);
@@ -93,7 +88,7 @@ int readlink_main(int argc UNUSED_PARAM, char **argv)
if (!buf)
return EXIT_FAILURE;
printf((opt & 2) ? "%s" : "%s\n", buf);
printf((opt & 1) ? "%s" : "%s\n", buf);
free(buf);
fflush_stdout_and_exit_SUCCESS();