From a61e8f28c7946c96e605d622cd867cef866be47b Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Thu, 25 Aug 2005 11:33:43 +0000 Subject: [PATCH] Added basename(3) --- include/libgen.h | 9 +++++++++ lib/other/Makefile | 4 ++++ lib/other/basename.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 include/libgen.h create mode 100644 lib/other/basename.c diff --git a/include/libgen.h b/include/libgen.h new file mode 100644 index 000000000..5aa3705a0 --- /dev/null +++ b/include/libgen.h @@ -0,0 +1,9 @@ +/* +libgen.h +*/ + +#include + +/* Open Group Base Specifications Issue 6 (not complete) */ +_PROTOTYPE( char *basename, (char *_path) ); + diff --git a/lib/other/Makefile b/lib/other/Makefile index 1d3d81f13..c1dc8b544 100755 --- a/lib/other/Makefile +++ b/lib/other/Makefile @@ -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 diff --git a/lib/other/basename.c b/lib/other/basename.c new file mode 100644 index 000000000..5de8f7af5 --- /dev/null +++ b/lib/other/basename.c @@ -0,0 +1,30 @@ +/* +basename.c +*/ + +#include +#include + +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; +} -- 2.44.0