]> Zhao Yanbai Git Server - minix.git/commitdiff
Added basename(3)
authorPhilip Homburg <philip@cs.vu.nl>
Thu, 25 Aug 2005 11:33:43 +0000 (11:33 +0000)
committerPhilip Homburg <philip@cs.vu.nl>
Thu, 25 Aug 2005 11:33:43 +0000 (11:33 +0000)
include/libgen.h [new file with mode: 0644]
lib/other/Makefile
lib/other/basename.c [new file with mode: 0644]

diff --git a/include/libgen.h b/include/libgen.h
new file mode 100644 (file)
index 0000000..5aa3705
--- /dev/null
@@ -0,0 +1,9 @@
+/*
+libgen.h
+*/
+
+#include <ansi.h>
+
+/* Open Group Base Specifications Issue 6 (not complete) */
+_PROTOTYPE( char *basename, (char *_path)                              );
+
index 1d3d81f134df17e872c06323b507f50a85494fa2..c1dc8b5444dc6813925a116695302608a81c5bc9 100755 (executable)
@@ -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 (file)
index 0000000..5de8f7a
--- /dev/null
@@ -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;
+}