git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 2/3] path: add a find_basename() portability function

From
David Aguilar <davvid@gmail.com>
Date
May 30, 2009, 02:18 UTC
Message-ID
<1243649890-4522-2-git-send-email-davvid@gmail.com>
In-Reply-To
<1243649890-4522-1-git-send-email-davvid@gmail.com>

find_basename() is a simpler version of basename(). It maintains constness and thus does not require you to pass in a copy. It is only concerned with finding the last directory separator in a string and returning a pointer.

This function was written because Windows does not have basename(). It is called "find_basename()" to avoid name collisions and to provide a hint that it is not quite the same thing as POSIX basename().

Signed-off-by: David Aguilar <davvid@gmail.com>
---
As suggested by Peff.
 cache.h |    1 +
 path.c  |   14 ++++++++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index b8503ad..08b6f42 100644
--- a/cache.h
+++ b/cache.h
@@ -640,6 +640,7 @@ static inline int is_absolute_path(const char *path)
 	return path[0] == '/' || has_dos_drive_prefix(path);
 }
 int is_directory(const char *);
+const char *find_basename(const char *path);
 const char *make_absolute_path(const char *path);
 const char *make_nonrelative_path(const char *path);
 const char *make_relative_path(const char *abs, const char *base);
diff --git a/path.c b/path.c
index 8a0a674..7a2fe14 100644
--- a/path.c
+++ b/path.c
@@ -358,6 +358,20 @@ int set_shared_perm(const char *path, int mode)
 	return 0;
 }
 
+/* return the basename of a path */
+const char *find_basename(const char *path)
+{
+	const char *basename = path + strlen(path) - 1;
+	while(*basename && basename > path) {
+		basename--;
+		if (is_dir_sep(*basename)) {
+			basename++;
+			break;
+		}
+	}
+	return basename;
+}
+
 const char *make_relative_path(const char *abs, const char *base)
 {
 	static char buf[PATH_MAX + 1];
-- 
1.6.3.1.178.g4daa97
Previous: David AguilarNext: David Aguilar
Message 2 of 5 in “compat: add a mkstemps() compatibility function”
  1. 1/3 compat: add a mkstemps() compatibility functionDavid Aguilar, May 30, 2009
  2. 2/3 path: add a find_basename() portability functionDavid Aguilar, May 30, 2009
  3. 3/3 diff: generate pretty filenames in prep_temp_blob()David Aguilar, May 30, 2009
  4. Jeff KingMay 30, 2009
  5. David AguilarMay 30, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.