* [PATCH 1/3] libgen: implement posix_basename
@ 2017-04-19 8:06 Sascha Hauer
2017-04-19 8:06 ` [PATCH 2/3] cp: Use posix_basename() on source argument Sascha Hauer
2017-04-19 8:06 ` [PATCH 3/3] commands: basename: Use posix_basename() Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2017-04-19 8:06 UTC (permalink / raw)
To: Barebox List
There are two different versions of basename(): The GNU version and the
POSIX version. The GNU version never modifies its argument and returns
the empty string when path has a trailing slash, and in particular also
when it is "/". The POSIX version modifies its argument and thus works
properly with strings which have a trailing "/".
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/libgen.h | 1 +
lib/libgen.c | 23 +++++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/include/libgen.h b/include/libgen.h
index cd27fd11e3..71f06eb6f6 100644
--- a/include/libgen.h
+++ b/include/libgen.h
@@ -2,6 +2,7 @@
#define __LIBGEN_H
char *basename (char *path);
+char *posix_basename(char *path);
char *dirname (char *path);
#endif /* __LIBGEN_H */
diff --git a/lib/libgen.c b/lib/libgen.c
index 1e43cf3f29..08ef3528af 100644
--- a/lib/libgen.c
+++ b/lib/libgen.c
@@ -37,6 +37,29 @@ char *basename (char *path)
}
EXPORT_SYMBOL(basename);
+/*
+ * There are two different versions of basename(): The GNU version implemented
+ * above and the POSIX version. The GNU version never modifies its argument and
+ * returns the empty string when path has a trailing slash, and in particular
+ * also when it is "/".
+ */
+char *posix_basename(char *path)
+{
+ char *fname;
+
+ fname = path + strlen(path) - 1;
+
+ while (*fname == '/') {
+ if (fname == path)
+ return path;
+ *fname = '\0';
+ fname--;
+ }
+
+ return basename(path);
+}
+EXPORT_SYMBOL(posix_basename);
+
char *dirname (char *path)
{
char *fname;
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/3] cp: Use posix_basename() on source argument
2017-04-19 8:06 [PATCH 1/3] libgen: implement posix_basename Sascha Hauer
@ 2017-04-19 8:06 ` Sascha Hauer
2017-04-19 8:06 ` [PATCH 3/3] commands: basename: Use posix_basename() Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2017-04-19 8:06 UTC (permalink / raw)
To: Barebox List
The GNU basename version does not work with strings which have a
trailing slash. Use posix_basename instead. With this cp -r
can properly work on source arguments which have a trailing slash,
i.e. "cp /dir1/ dst".
Reported-by: Peter Mamonov <pmamonov@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/cp.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/commands/cp.c b/commands/cp.c
index 4f1c068dd6..54934dd64f 100644
--- a/commands/cp.c
+++ b/commands/cp.c
@@ -76,7 +76,8 @@ static int do_cp(int argc, char *argv[])
for (i = optind; i < argc - 1; i++) {
char *dst;
- dst = concat_path_file(argv[argc - 1], basename(argv[i]));
+
+ dst = concat_path_file(argv[argc - 1], posix_basename(argv[i]));
if (recursive)
ret = copy_recursive(argv[i], dst);
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 3/3] commands: basename: Use posix_basename()
2017-04-19 8:06 [PATCH 1/3] libgen: implement posix_basename Sascha Hauer
2017-04-19 8:06 ` [PATCH 2/3] cp: Use posix_basename() on source argument Sascha Hauer
@ 2017-04-19 8:06 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2017-04-19 8:06 UTC (permalink / raw)
To: Barebox List
The Linux shell basename command uses the POSIX basename version,
so the same for barebox.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/basename.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/commands/basename.c b/commands/basename.c
index 028b0e0f24..c1a28e9e1d 100644
--- a/commands/basename.c
+++ b/commands/basename.c
@@ -27,7 +27,7 @@ static int do_basename(int argc, char *argv[])
if (argc != 3)
return COMMAND_ERROR_USAGE;
- setenv(argv[2], basename(argv[1]));
+ setenv(argv[2], posix_basename(argv[1]));
return 0;
}
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-04-19 8:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-19 8:06 [PATCH 1/3] libgen: implement posix_basename Sascha Hauer
2017-04-19 8:06 ` [PATCH 2/3] cp: Use posix_basename() on source argument Sascha Hauer
2017-04-19 8:06 ` [PATCH 3/3] commands: basename: Use posix_basename() Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox