* [PATCH] commands: readlink: print to stdout if variable is missing
@ 2025-01-16 14:10 Ahmad Fatoum
2025-01-21 8:10 ` Sascha Hauer
0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-01-16 14:10 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] commands: readlink: print to stdout if variable is missing
2025-01-16 14:10 [PATCH] commands: readlink: print to stdout if variable is missing Ahmad Fatoum
@ 2025-01-21 8:10 ` Sascha Hauer
0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2025-01-21 8:10 UTC (permalink / raw)
To: barebox, Ahmad Fatoum
On Thu, 16 Jan 2025 15:10:57 +0100, Ahmad Fatoum wrote:
> 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.
>
>
Applied, thanks!
[1/1] commands: readlink: print to stdout if variable is missing
https://git.pengutronix.de/cgit/barebox/commit/?id=5e08a007ccb4 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-01-21 8:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-16 14:10 [PATCH] commands: readlink: print to stdout if variable is missing Ahmad Fatoum
2025-01-21 8:10 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox