From b4df26e79db04bc7acf691420393d2b431c83295 Mon Sep 17 00:00:00 2001 From: carl Date: Tue, 19 Feb 2019 00:36:48 +0800 Subject: [PATCH] Better ANSI C compatibility and portability - part 1: + 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 | 36 +++++++++++++++++++++++++++++++ modules/src/system/build.lua | 2 +- modules/src/system/chmode.c | 8 +++---- modules/src/system/filesize.c | 6 ++---- modules/src/system/modtime.c | 6 ++---- modules/src/system/remove.c | 7 +++--- modules/src/system/stop.c | 8 +++---- modules/src/system/system.h | 10 +++++++-- modules/src/system/tmpdir.c | 27 +++++++++++++++++++++++ 9 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 modules/src/system/CMakeLists.txt create mode 100644 modules/src/system/tmpdir.c diff --git a/modules/src/system/CMakeLists.txt b/modules/src/system/CMakeLists.txt new file mode 100644 index 000000000..f41c70f0d --- /dev/null +++ b/modules/src/system/CMakeLists.txt @@ -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) + diff --git a/modules/src/system/build.lua b/modules/src/system/build.lua index c9eba2979..b97130d98 100644 --- a/modules/src/system/build.lua +++ b/modules/src/system/build.lua @@ -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" }, diff --git a/modules/src/system/chmode.c b/modules/src/system/chmode.c index 0c0c1f59e..bed6bcd83 100644 --- a/modules/src/system/chmode.c +++ b/modules/src/system/chmode.c @@ -4,12 +4,10 @@ */ /* $Id$ */ +#include #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; } diff --git a/modules/src/system/filesize.c b/modules/src/system/filesize.c index c3a62e568..b02845113 100644 --- a/modules/src/system/filesize.c +++ b/modules/src/system/filesize.c @@ -8,13 +8,11 @@ #include #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; } diff --git a/modules/src/system/modtime.c b/modules/src/system/modtime.c index 129c3bce3..9fe9455a6 100644 --- a/modules/src/system/modtime.c +++ b/modules/src/system/modtime.c @@ -8,13 +8,11 @@ #include #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; } diff --git a/modules/src/system/remove.c b/modules/src/system/remove.c index 435d67b05..82b6fd4c6 100644 --- a/modules/src/system/remove.c +++ b/modules/src/system/remove.c @@ -4,11 +4,10 @@ */ /* $Id$ */ +#include #include "system.h" -int -sys_remove(path) - char *path; +int sys_remove(char* path) { - return unlink(path) == 0; + return remove(path) == 0; } diff --git a/modules/src/system/stop.c b/modules/src/system/stop.c index 05986b162..5e94367d4 100644 --- a/modules/src/system/stop.c +++ b/modules/src/system/stop.c @@ -7,15 +7,13 @@ #include #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(); diff --git a/modules/src/system/system.h b/modules/src/system/system.h index f596defe4..6d394d0da 100644 --- a/modules/src/system/system.h +++ b/modules/src/system/system.h @@ -6,6 +6,10 @@ #ifndef __SYSTEM_INCLUDED__ #define __SYSTEM_INCLUDED__ +#include +#include +#include + 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 index 000000000..d02a91b0f --- /dev/null +++ b/modules/src/system/tmpdir.c @@ -0,0 +1,27 @@ +/* Copyright (c) 2019. See the file "Copyright" in + * the root directory for more information. + * + * Created on: 2019-02-09 + * + */ +#include + +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 "."; +} + +