mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/3] libgen: implement posix_basename
Date: Wed, 19 Apr 2017 10:06:10 +0200	[thread overview]
Message-ID: <20170419080612.19797-1-s.hauer@pengutronix.de> (raw)

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

             reply	other threads:[~2017-04-19  8:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-19  8:06 Sascha Hauer [this message]
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

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=20170419080612.19797-1-s.hauer@pengutronix.de \
    --to=s.hauer@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