[klibc] [klibc:master] readlink: Add -f option

klibc-bot for Ben Hutchings ben at decadent.org.uk
Tue Jan 5 17:54:06 PST 2016


Commit-ID:  4d9db8a092aee0dfaebb65e0b4f054a40d92cbd9
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=4d9db8a092aee0dfaebb65e0b4f054a40d92cbd9
Author:     Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Wed, 6 Jan 2016 01:09:16 +0000
Committer:  H. Peter Anvin <hpa at linux.intel.com>
CommitDate: Tue, 5 Jan 2016 17:48:48 -0800

[klibc] readlink: Add -f option

This is needed to support mounting non-root filesystems in
initramfs-tools.

Signed-off-by: Ben Hutchings <ben at decadent.org.uk>
Signed-off-by: H. Peter Anvin <hpa at linux.intel.com>

---
 usr/utils/readlink.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c
index 4e3cfcb..ffb0b1f 100644
--- a/usr/utils/readlink.c
+++ b/usr/utils/readlink.c
@@ -7,24 +7,45 @@ const char *progname;
 
 static __noreturn usage(void)
 {
-	fprintf(stderr, "Usage: %s link...\n", progname);
+	fprintf(stderr, "Usage: %s [-f] link...\n", progname);
 	exit(1);
 }
 
 int main(int argc, char *argv[])
 {
+	int c, f_flag = 0;
 	const char *name;
 	char link_name[PATH_MAX];
 	int rv;
 	int i;
 
-	progname = *argv++;
+	progname = argv[0];
 
-	if (argc < 2)
+	do {
+		c = getopt(argc, argv, "f");
+		if (c == EOF)
+			break;
+		switch (c) {
+		case 'f':
+			f_flag = 1;
+			break;
+
+		case '?':
+			fprintf(stderr, "%s: invalid option -%c\n",
+				progname, optopt);
+			usage();
+		}
+	} while (1);
+
+	if (optind == argc)
 		usage();
 
+	argv += optind;
 	while ((name = *argv++)) {
-		rv = readlink(name, link_name, sizeof link_name - 1);
+		if (f_flag)
+			rv = realpath(name, link_name) ? strlen(link_name) : -1;
+		else
+			rv = readlink(name, link_name, sizeof link_name - 1);
 		if (rv < 0) {
 			perror(name);
 			exit(1);


More information about the klibc mailing list