+ Add sys_tmpnam() and sys_basename()
authorcarl <cecodere@yahoo.ca>
Sun, 17 Mar 2019 14:23:56 +0000 (22:23 +0800)
committercarl <cecodere@yahoo.ca>
Sun, 17 Mar 2019 14:46:31 +0000 (22:46 +0800)
modules/src/system/basename.c [new file with mode: 0644]
modules/src/system/build.lua
modules/src/system/system.h

diff --git a/modules/src/system/basename.c b/modules/src/system/basename.c
new file mode 100644 (file)
index 0000000..21736bb
--- /dev/null
@@ -0,0 +1,57 @@
+/*  Copyright (c) 2019. See the file License in
+ *  the root directory for more information.
+ *
+ *  Contains path related utilities.
+ */
+#include <string.h>
+
+
+void sys_basename(char *str, register char *dst)
+{
+       register char *p1 = str;
+       register char *p2 = p1;
+       register char *end;
+       register char *start;
+
+       int len = strlen(str);
+       /* Point to the end of the string. */
+       p1 = p1 + len - 1;
+       end = p1;
+
+       while ((*p1 == '/') || (*p1 == '\\'))
+       {
+               if (p1 == str)
+               {
+                       dst[0] = *p1;
+                       dst[1] = '\0';
+                       return;
+               }
+               p1--;
+       }
+       /* Only a volume specification */
+       if (*p1 == ':')
+       {
+               strcpy(dst,str);
+               return;
+       }
+       /* Do a reverse search. */
+       p2 = p1;
+       len = 0;
+       while (p2 != str)
+       {
+               if ((*p1 == '/') || (*p1 == '\\') || (*p1 == ':'))
+               {
+                       strncpy(dst,p2,len);
+                       dst[len] = '\0';
+                       return;
+               }
+               p2 = p1;
+               len++;
+               p1--;
+       }
+   /* Only a pathname */
+   strcpy(dst,str);
+}
+
+
+
index b97130d..b409f69 100644 (file)
@@ -2,7 +2,7 @@ clibrary {
        name = "lib",
        srcs = {
                "./access.c", "./break.c", "./chmode.c", "./close.c",
-               "./create.c", "./filesize.c",
+               "./create.c", "./filesize.c","./basename.c","./tmpnam.c",
                --"./lock.c",
                "./modtime.c", "./open.c", "./read.c", "./remove.c",
                "./rename.c", "./seek.c", "./stop.c", "./system.c",
index 6d394d0..598ceb0 100644 (file)
@@ -67,4 +67,19 @@ time_t sys_modtime(char *);
 /* return value for sys_break */
 #define ILL_BREAK      ((char *)0)
 
+
+/* Extract the base name from a full path specification
+ * in "str" and returns it in "dst".
+ *
+ * "dst" should be large enough to receive the copied
+ * data.
+ *
+ * Supports both DOS and UNIX style paths.
+ * */
+void sys_basename(const char *str, register char *dst);
+
+/* Creates a temporary filename. This has
+ * the same semantics as ISO C90 tmpnam() */
+char* sys_tmpnam(char *buffer);
+
 #endif /* __SYSTEM_INCLUDED__ */