mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] commands: readlink: print to stdout if variable is missing
Date: Thu, 16 Jan 2025 15:10:57 +0100	[thread overview]
Message-ID: <20250116141057.299989-1-a.fatoum@pengutronix.de> (raw)

For easier interactive use during debugging or when the shell is
exercised by Labgrid, teach the readlink command to directly output to
stdout instead of an environment variable if none was specified.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig    |  6 ++++--
 commands/readlink.c | 39 +++++++++++++++++++++++++++++----------
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 448a6e240cad..ecbcabd0272d 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1084,9 +1084,11 @@ config CMD_READLINK
 	help
 	  Read value of a symbolic link
 
-	  Usage: readlink [-f] FILE VARIABLE
+	  Usage: readlink [-f] FILE [VARIABLE]
 
-	  Read value of a symbolic link and store it into VARIABLE.
+	  Read value of a symbolic link or canonical file name
+	  The value is either stored it into the specified VARIABLE
+	  or printed.
 
 	  Options:
 		  -f	canonicalize by following first symlink
diff --git a/commands/readlink.c b/commands/readlink.c
index 55f8249028e1..a37b6d7512d7 100644
--- a/commands/readlink.c
+++ b/commands/readlink.c
@@ -11,9 +11,19 @@
 #include <malloc.h>
 #include <getopt.h>
 
+static void output_result(const char *var, const char *val)
+{
+	if (var)
+		setenv(var, val);
+	else
+		printf("%s\n", val);
+
+}
+
 static int do_readlink(int argc, char *argv[])
 {
-	char realname[PATH_MAX];
+	const char *var;
+	char *path, realname[PATH_MAX];
 	int canonicalize = 0;
 	int opt;
 
@@ -27,35 +37,44 @@ static int do_readlink(int argc, char *argv[])
 		}
 	}
 
-	if (argc < optind + 2)
+	argv += optind;
+	argc -= optind;
+
+	if (argc == 0 || argc > 2)
 		return COMMAND_ERROR_USAGE;
 
+	path = argv[0];
+	var = argv[1];
+
 	if (canonicalize) {
-		char *buf = canonicalize_path(AT_FDCWD, argv[optind]);
+		char *buf = canonicalize_path(AT_FDCWD, path);
 		struct stat s;
 
 		if (!buf)
 			goto err;
-		if (stat(dirname(argv[optind]), &s) || !S_ISDIR(s.st_mode)) {
+		if (stat(dirname(path), &s) || !S_ISDIR(s.st_mode)) {
 			free(buf);
 			goto err;
 		}
-		setenv(argv[optind + 1], buf);
+		output_result(var, buf);
 		free(buf);
 	} else {
-		if (readlink(argv[optind], realname, PATH_MAX - 1) < 0)
+		if (readlink(path, realname, PATH_MAX - 1) < 0)
 			goto err;
-		setenv(argv[optind + 1], realname);
+		output_result(var, realname);
 	}
 
 	return 0;
 err:
-	unsetenv(argv[optind + 1]);
+	if (var)
+		unsetenv(var);
 	return 1;
 }
 
 BAREBOX_CMD_HELP_START(readlink)
-BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link or canonical file name and store it into VARIABLE.")
+BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link or canonical file name")
+BAREBOX_CMD_HELP_TEXT("The value is either stored it into the specified VARIABLE")
+BAREBOX_CMD_HELP_TEXT("or printed.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT("-f", "canonicalize by following symlinks;")
@@ -65,7 +84,7 @@ BAREBOX_CMD_HELP_END
 BAREBOX_CMD_START(readlink)
 	.cmd		= do_readlink,
 	BAREBOX_CMD_DESC("read value of a symbolic link or canonical file name")
-	BAREBOX_CMD_OPTS("[-f] FILE VARIABLE")
+	BAREBOX_CMD_OPTS("[-f] FILE [VARIABLE]")
 	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
 	BAREBOX_CMD_HELP(cmd_readlink_help)
 BAREBOX_CMD_END
-- 
2.39.5




             reply	other threads:[~2025-01-16 14:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-16 14:10 Ahmad Fatoum [this message]
2025-01-21  8:10 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250116141057.299989-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox