- CONFIG_FEATURE_READLINK_FOLLOW readlink -f patch from Colin Watson <cjwatson@debian...
authorNed Ludd <solar@gentoo.org>
Wed, 8 Dec 2004 16:47:28 +0000 (16:47 -0000)
committerNed Ludd <solar@gentoo.org>
Wed, 8 Dec 2004 16:47:28 +0000 (16:47 -0000)
debianutils/Config.in
debianutils/readlink.c
include/usage.h

index 7cf7cadb5b94e98575a52209b66c1e429065d303..f13d56ec7206e8cebac326b7fcc6866f8c3c68fc 100644 (file)
@@ -24,6 +24,13 @@ config CONFIG_READLINK
          This program reads a symbolic link and returns the name
          of the file it points to
 
+config CONFIG_FEATURE_READLINK_FOLLOW
+       bool "  Enable canonicalization by following all symlinks (-f)"
+       default n
+       depends on CONFIG_READLINK
+       help
+         Enable the readlink option (-f).
+
 config CONFIG_RUN_PARTS
        bool "run-parts"
        default n
index d8d7e8c2d27c891974c8da6373b1d3ae66ec510b..90927bb7436eda3d6b8dd1f9e18a282a5301c547 100644 (file)
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <getopt.h>
 #include "busybox.h"
 
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+# define READLINK_FOLLOW       "f"
+# define READLINK_FLAG_f       (1 << 0)
+#else
+# define READLINK_FOLLOW       ""
+#endif
+
+static const char readlink_options[] = READLINK_FOLLOW;
+
 int readlink_main(int argc, char **argv)
 {
        char *buf = NULL;
+       unsigned long opt = bb_getopt_ulflags(argc, argv, readlink_options);
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+       RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);
+#endif
 
        /* no options, no getopt */
 
-       if (argc != 2)
+       if (optind + 1 != argc)
                bb_show_usage();
 
-       buf = xreadlink(argv[1]);
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+       if (opt & READLINK_FLAG_f) {
+               buf = realpath(argv[optind], resolved_path);
+       } else
+#endif
+               buf = xreadlink(argv[optind]);
+
        if (!buf)
                return EXIT_FAILURE;
        puts(buf);
index 377eb10e70371899afeddb8f2391c9130130a5b1..d928a10a302cbbe7860222569455a0500dd8c577 100644 (file)
        "\t-s\tSet the system date and time (default).\n" \
        "\t-p\tPrint the date and time."
 
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+#define USAGE_READLINK_FOLLOW(a) a
+#else
+#define USAGE_READLINK_FOLLOW(a)
+#endif
+
 #define readlink_trivial_usage \
-       ""
+       USAGE_READLINK_FOLLOW("[-f] ") "FILE"
 #define readlink_full_usage \
-       "Displays the value of a symbolic link."
+       "Displays the value of a symbolic link." \
+       USAGE_READLINK_FOLLOW("\n\nOptions:\n" \
+       "\t-f\tcanonicalize by following all symlinks")
 
 #define realpath_trivial_usage \
        "pathname  ..."