Better ANSI C compatibility and portability - part 1:
authorcarl <cecodere@yahoo.ca>
Mon, 18 Feb 2019 16:36:48 +0000 (00:36 +0800)
committercarl <cecodere@yahoo.ca>
Mon, 18 Feb 2019 16:54:23 +0000 (00:54 +0800)
+ Addition of function prototypes.
+ Change function definitions to ANSI C style.
+ Initial support for CMake
+ Added support for sys_tmpdir for better portability.

modules/src/system/CMakeLists.txt [new file with mode: 0644]
modules/src/system/build.lua
modules/src/system/chmode.c
modules/src/system/filesize.c
modules/src/system/modtime.c
modules/src/system/remove.c
modules/src/system/stop.c
modules/src/system/system.h
modules/src/system/tmpdir.c [new file with mode: 0644]

diff --git a/modules/src/system/CMakeLists.txt b/modules/src/system/CMakeLists.txt
new file mode 100644 (file)
index 0000000..f41c70f
--- /dev/null
@@ -0,0 +1,36 @@
+cmake_minimum_required (VERSION 2.9)
+project(system)
+
+set(SRC
+ access.c
+ break.c
+ chmode.c
+ close.c
+ create.c
+ filesize.c
+ lock.c
+ modtime.c
+ open.c
+ read.c
+ remove.c
+ rename.c
+ seek.c
+ stop.c
+ system.c
+ write.c
+ tmpdir.c
+ system.h
+)
+
+add_library(${PROJECT_NAME} ${SRC})
+target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "system.h")
+
+install(TARGETS ${PROJECT_NAME} 
+        RUNTIME DESTINATION bin
+        LIBRARY DESTINATION lib
+        ARCHIVE DESTINATION lib
+        PUBLIC_HEADER DESTINATION include
+)
+install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/system.3 DESTINATION man OPTIONAL)
+
index c9eba29..b97130d 100644 (file)
@@ -6,7 +6,7 @@ clibrary {
                --"./lock.c",
                "./modtime.c", "./open.c", "./read.c", "./remove.c",
                "./rename.c", "./seek.c", "./stop.c", "./system.c",
-               --"./unlock.c",
+               --"./unlock.cā€,ā€./tmpdir.cā€,
                "./write.c",
        },
        hdrs = { "./system.h" },
index 0c0c1f5..bed6bcd 100644 (file)
@@ -4,12 +4,10 @@
  */
 /* $Id$ */
 
+#include <sys/stat.h>
 #include "system.h"
 
-int
-sys_chmode(path, mode)
-       char *path;
-       int mode;
+int sys_chmode(char* path,int mode)
 {
-       return chmod(path, mode) == 0;
+       return chmod(path, (mode_t)mode) == 0;
 }
index c3a62e5..b028451 100644 (file)
@@ -8,13 +8,11 @@
 #include <sys/stat.h>
 #include "system.h"
 
-long
-sys_filesize(path)
-       char *path;
+off_t sys_filesize(char* path)
 {
        struct stat st_buf;
 
        if (stat(path, &st_buf) != 0)
                return -1L;
-       return (long) st_buf.st_size;
+       return st_buf.st_size;
 }
index 129c3bc..9fe9455 100644 (file)
@@ -8,13 +8,11 @@
 #include <sys/stat.h>
 #include "system.h"
 
-long
-sys_modtime(path)
-       char *path;
+time_t sys_modtime(char* path)
 {
        struct stat st_buf;
 
        if (stat(path, &st_buf) != 0)
                return -1L;
-       return (long) st_buf.st_mtime;
+       return st_buf.st_mtime;
 }
index 435d67b..82b6fd4 100644 (file)
@@ -4,11 +4,10 @@
  */
 /* $Id$ */
 
+#include <stdio.h>
 #include "system.h"
 
-int
-sys_remove(path)
-       char *path;
+int sys_remove(char* path)
 {
-       return unlink(path) == 0;
+       return remove(path) == 0;
 }
index 05986b1..5e94367 100644 (file)
@@ -7,15 +7,13 @@
 #include <stdlib.h>
 #include "system.h"
 
-void
-sys_stop(how)
-       int how;
+void sys_stop(int how)
 {
        switch(how) {
        case S_END:
-               exit(0);
+               exit(EXIT_SUCCESS);
        case S_EXIT:
-               exit(1);
+               exit(EXIT_FAILURE);
        case S_ABORT:
        default:
                abort();
index f596def..6d394d0 100644 (file)
@@ -6,6 +6,10 @@
 #ifndef __SYSTEM_INCLUDED__
 #define __SYSTEM_INCLUDED__
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+
 struct _sys_fildes {
        int o_fd;       /* UNIX filedescriptor */
        int o_flags;    /* flags for open; 0 if not used */
@@ -40,15 +44,17 @@ int sys_reset(File *);
 int sys_access(char *, int);
 int sys_remove(char *);
 int sys_rename(char *, char *);
-long sys_filesize(char *);
+off_t sys_filesize(char *);
 int sys_chmode(char *, int);
+/* Return the temporary directory location */
+char* sys_gettmpdir(void);
 #if 0
 int sys_lock(char *);
 int sys_unlock(char *);
 #endif
 char *sys_break(int);
 void sys_stop(int);
-long sys_modtime(char *);
+time_t sys_modtime(char *);
 
 /* standard file decsriptors */
 #define STDIN  &_sys_ftab[0]
diff --git a/modules/src/system/tmpdir.c b/modules/src/system/tmpdir.c
new file mode 100644 (file)
index 0000000..d02a91b
--- /dev/null
@@ -0,0 +1,27 @@
+/*  Copyright (c) 2019. See the file "Copyright" in
+ *  the root directory for more information.
+ *
+ *  Created on: 2019-02-09
+ *  
+ */
+#include <stdlib.h>
+
+char* sys_gettmpdir(void)
+{
+       char* result = 0;
+       /* Check the UNIX temporary directory */
+       result = getenv("TMPDIR");
+       if (result != 0)
+               return result;
+       result = getenv("TMP");
+       if (result != 0)
+           return result;
+       /* DOS compatible systems */
+       result = getenv("TEMP");
+       if (result != 0)
+               return result;
+       /* Then try current directory */
+       return ".";
+}
+
+