libgen: basename and dirname [PATCH]
Jeff Johnston
jjohnstn@redhat.com
Fri Apr 8 00:01:00 GMT 2005
Shaun,
These functions aren't appropriate to add in stdlib. They're trivial enough
that if you want to add them in libc/unix, ok, but you need to add libgen.h
header file and you also have to provide licensing info in the files that
document you wrote the files plus the permissions you are granting.
-- Jeff J.
Shaun Jackman wrote:
> 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);
> +}
More information about the Newlib
mailing list