Added basename(3)

This commit is contained in:
Philip Homburg 2005-08-25 11:33:43 +00:00
parent 4c863cf9fc
commit a61e8f28c7
3 changed files with 43 additions and 0 deletions

9
include/libgen.h Normal file
View file

@ -0,0 +1,9 @@
/*
libgen.h
*/
#include <ansi.h>
/* Open Group Base Specifications Issue 6 (not complete) */
_PROTOTYPE( char *basename, (char *_path) );

View file

@ -21,6 +21,7 @@ OBJECTS = \
$(LIBRARY)(_devctl.o) \
$(LIBRARY)(_findproc.o) \
$(LIBRARY)(asynchio.o) \
$(LIBRARY)(basename.o) \
$(LIBRARY)(configfile.o) \
$(LIBRARY)(crypt.o) \
$(LIBRARY)(ctermid.o) \
@ -106,6 +107,9 @@ $(LIBRARY)(_getsysinfo.o): _getsysinfo.c
$(LIBRARY)(asynchio.o): asynchio.c
$(CC1) asynchio.c
$(LIBRARY)(basename.o): basename.c
$(CC1) basename.c
$(LIBRARY)(bcmp.o): bcmp.c
$(CC1) bcmp.c

30
lib/other/basename.c Normal file
View file

@ -0,0 +1,30 @@
/*
basename.c
*/
#include <libgen.h>
#include <string.h>
char *basename(path)
char *path;
{
size_t len;
char *cp;
if (path == NULL)
return ".";
len= strlen(path);
if (len == 0)
return ".";
while (path[len-1] == '/')
{
if (len == 1)
return path; /* just "/" */
len--;
path[len]= '\0';
}
cp= strrchr(path, '/');
if (cp != NULL)
return cp+1;
return path;
}