[BusyBox] [PATCH] readlink -f

Colin Watson cjwatson at debian.org
Mon Nov 15 17:03:48 UTC 2004


On Thu, Oct 28, 2004 at 07:29:00PM +0400, Vladimir N. Oleynik wrote:
> Colin,
> 
> if use libbb/have bb_getopt_ulflags() this patch is simplyfied
> 
> >+	unsigned char follow_flag = 0;
> 
> +	int follow_flag = bb_getopt_ulflags(argc, argv, readlink_options);
> 
> This code a block do not require now:
> 
> >+	int opt;
> >+
> >+	while ((opt = getopt(argc, argv, readlink_options)) != -1) {
> >+		switch (opt) {
> >+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
> >+			case 'f':
> >+				follow_flag = 1;
> >+				break;
> >+#endif
> >+			default:
> >+				bb_show_usage();
> >+		}
> >+	}

Thanks. The attached patch incorporates Vladimir's comment, and
furthermore updates readlink's usage message. It supersedes my previous
readlink-follow.patch. readlink-tests.patch still applies, and I've
included it again here for convenience.

Can somebody please apply if appropriate?

Thanks,

-- 
Colin Watson                                       [cjwatson at debian.org]
-------------- next part --------------
Index: debianutils/Config.in
===================================================================
RCS file: /var/cvs/busybox/debianutils/Config.in,v
retrieving revision 1.7
diff -p -u -r1.7 Config.in
--- debianutils/Config.in	15 Mar 2004 08:28:24 -0000	1.7
+++ debianutils/Config.in	15 Nov 2004 17:00:45 -0000
@@ -24,6 +24,12 @@ 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
+	help
+	  Enable the readlink option (-f).
+
 config CONFIG_RUN_PARTS
 	bool "run-parts"
 	default n
Index: debianutils/readlink.c
===================================================================
RCS file: /var/cvs/busybox/debianutils/readlink.c,v
retrieving revision 1.2
diff -p -u -r1.2 readlink.c
--- debianutils/readlink.c	19 Mar 2003 09:11:41 -0000	1.2
+++ debianutils/readlink.c	15 Nov 2004 17:00:45 -0000
@@ -23,18 +23,38 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <getopt.h>
 #include "busybox.h"
 
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+# define READLINK_FOLLOW	"f"
+#else
+# define READLINK_FOLLOW	""
+#endif
+
+static const char readlink_options[] = READLINK_FOLLOW;
+
 int readlink_main(int argc, char **argv)
 {
 	char *buf = NULL;
+#ifdef CONFIG_FEATURE_READLINK_FOLLOW
+	unsigned long follow_flag =
+		bb_getopt_ulflags(argc, argv, readlink_options);
+	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 (follow_flag) {
+		buf = realpath(argv[optind], resolved_path);
+	} else
+#endif
+		buf = xreadlink(argv[optind]);
+
 	if (!buf)
 		return EXIT_FAILURE;
 	puts(buf);
Index: include/usage.h
===================================================================
RCS file: /var/cvs/busybox/include/usage.h,v
retrieving revision 1.222
diff -p -u -r1.222 usage.h
--- include/usage.h	14 Sep 2004 16:23:56 -0000	1.222
+++ include/usage.h	15 Nov 2004 17:00:46 -0000
@@ -1985,10 +1985,18 @@
 	"\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  ..."
-------------- next part --------------
--- testsuite/readlink/readlink-canonicalizes	1970-01-01 01:00:00.000000000 +0100
+++ testsuite/readlink/readlink-canonicalizes	2004-10-28 12:50:09.760233256 +0100
@@ -0,0 +1,6 @@
+# FEATURE: CONFIG_FEATURE_READLINK_FOLLOW
+mkdir foo
+ln -s foo bar
+touch foo/1
+ln -s 1 foo/2
+test x`pwd`/foo/1 = x`readlink -f bar/2`
--- testsuite/readlink/readlink-works	1970-01-01 01:00:00.000000000 +0100
+++ testsuite/readlink/readlink-works	2004-10-28 12:45:20.833156848 +0100
@@ -0,0 +1,3 @@
+touch foo
+ln -s foo bar
+test xfoo = x`busybox readlink bar`


More information about the busybox mailing list