summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorEric Blake2023-03-24 09:58:14 -0500
committerDenys Vlasenko2023-04-16 18:01:44 +0200
commitd2b81b3dc2b31d32e1060d3ea8bd998d30a37d8a (patch)
tree4e80a2e7eaf488830ef8a6d22544885a9a038299 /coreutils
parent2ffd8986e21f1782c78e1a9d34f6042af53d876c (diff)
downloadbusybox-d2b81b3dc2b31d32e1060d3ea8bd998d30a37d8a.zip
busybox-d2b81b3dc2b31d32e1060d3ea8bd998d30a37d8a.tar.gz
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>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/readlink.c29
1 files changed, 12 insertions, 17 deletions
diff --git a/coreutils/readlink.c b/coreutils/readlink.c
index b2e8678..0a9aa95 100644
--- a/coreutils/readlink.c
+++ b/coreutils/readlink.c
@@ -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();