This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

libgen: basename and dirname [PATCH]


This patch adds the libgen functions, basename and dirname, to newlib.
Although I'm sure these functions have been implemented a few times
over, this implementation is my own. Sorry I don't have the
documentation headers for these files. I expect they're probably drawn
from some source, and so I'll leave it to the person in the know.

Cheers,
Shaun

2005-04-06  Shaun Jackman  <sjackman@gmail.com>

	* libc/stdlib/Makefile.am: Add basename.c and dirname.c to GENERAL_SOURCES.
	* libc/stdlib/basename.c: New file.
	* libc/stdlib/dirname.c: Ditto.

Index: newlib/libc/stdlib/Makefile.am
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/Makefile.am,v
retrieving revision 1.20
diff -u -3 -p -r1.20 Makefile.am
--- newlib/libc/stdlib/Makefile.am	9 Sep 2004 19:46:54 -0000	1.20
+++ newlib/libc/stdlib/Makefile.am	6 Apr 2005 22:56:25 -0000
@@ -19,7 +19,9 @@ GENERAL_SOURCES = \
 	atoff.c		\
 	atoi.c  	\
 	atol.c		\
+	basename.c  \
 	calloc.c	\
+	dirname.c   \
 	div.c  		\
 	dtoa.c 		\
 	dtoastub.c 	\
--- /dev/null	2005-04-04 21:44:30.000000000 -0700
+++ newlib/libc/stdlib/basename.c	2005-04-06 13:30:37.000000000 -0700
@@ -0,0 +1,17 @@
+#include <string.h>
+
+char *basename(char *path)
+{
+	char *p;
+	if( path == NULL || *path == '\0' )
+		return ".";
+	p = path + strlen(path) - 1;
+	while( *p == '/' ) {
+		if( p == path )
+			return path;
+		*p-- = '\0';
+	}
+	while( p >= path && *p != '/' )
+		p--;
+	return p + 1;
+}
--- /dev/null	2005-04-04 21:44:30.000000000 -0700
+++ newlib/libc/stdlib/dirname.c	2005-04-06 13:33:42.000000000 -0700
@@ -0,0 +1,20 @@
+#include <string.h>
+
+char *dirname(char *path)
+{
+	char *p;
+	if( path == NULL || *path == '\0' )
+		return ".";
+	p = path + strlen(path) - 1;
+	while( *p == '/' ) {
+		if( p == path )
+			return path;
+		*p-- = '\0';
+	}
+	while( p >= path && *p != '/' )
+		p--;
+	return
+		p < path ? "." :
+		p == path ? "/" :
+		(*p = '\0', path);
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]