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.
This commit is contained in:
carl 2019-02-19 00:36:48 +08:00
parent 7317ae3291
commit b4df26e79d
9 changed files with 85 additions and 25 deletions

View file

@ -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)

View file

@ -6,7 +6,7 @@ clibrary {
--"./lock.c", --"./lock.c",
"./modtime.c", "./open.c", "./read.c", "./remove.c", "./modtime.c", "./open.c", "./read.c", "./remove.c",
"./rename.c", "./seek.c", "./stop.c", "./system.c", "./rename.c", "./seek.c", "./stop.c", "./system.c",
--"./unlock.c", --"./unlock.c”,”./tmpdir.c”,
"./write.c", "./write.c",
}, },
hdrs = { "./system.h" }, hdrs = { "./system.h" },

View file

@ -4,12 +4,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <sys/stat.h>
#include "system.h" #include "system.h"
int int sys_chmode(char* path,int mode)
sys_chmode(path, mode)
char *path;
int mode;
{ {
return chmod(path, mode) == 0; return chmod(path, (mode_t)mode) == 0;
} }

View file

@ -8,13 +8,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "system.h" #include "system.h"
long off_t sys_filesize(char* path)
sys_filesize(path)
char *path;
{ {
struct stat st_buf; struct stat st_buf;
if (stat(path, &st_buf) != 0) if (stat(path, &st_buf) != 0)
return -1L; return -1L;
return (long) st_buf.st_size; return st_buf.st_size;
} }

View file

@ -8,13 +8,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "system.h" #include "system.h"
long time_t sys_modtime(char* path)
sys_modtime(path)
char *path;
{ {
struct stat st_buf; struct stat st_buf;
if (stat(path, &st_buf) != 0) if (stat(path, &st_buf) != 0)
return -1L; return -1L;
return (long) st_buf.st_mtime; return st_buf.st_mtime;
} }

View file

@ -4,11 +4,10 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <stdio.h>
#include "system.h" #include "system.h"
int int sys_remove(char* path)
sys_remove(path)
char *path;
{ {
return unlink(path) == 0; return remove(path) == 0;
} }

View file

@ -7,15 +7,13 @@
#include <stdlib.h> #include <stdlib.h>
#include "system.h" #include "system.h"
void void sys_stop(int how)
sys_stop(how)
int how;
{ {
switch(how) { switch(how) {
case S_END: case S_END:
exit(0); exit(EXIT_SUCCESS);
case S_EXIT: case S_EXIT:
exit(1); exit(EXIT_FAILURE);
case S_ABORT: case S_ABORT:
default: default:
abort(); abort();

View file

@ -6,6 +6,10 @@
#ifndef __SYSTEM_INCLUDED__ #ifndef __SYSTEM_INCLUDED__
#define __SYSTEM_INCLUDED__ #define __SYSTEM_INCLUDED__
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
struct _sys_fildes { struct _sys_fildes {
int o_fd; /* UNIX filedescriptor */ int o_fd; /* UNIX filedescriptor */
int o_flags; /* flags for open; 0 if not used */ 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_access(char *, int);
int sys_remove(char *); int sys_remove(char *);
int sys_rename(char *, char *); int sys_rename(char *, char *);
long sys_filesize(char *); off_t sys_filesize(char *);
int sys_chmode(char *, int); int sys_chmode(char *, int);
/* Return the temporary directory location */
char* sys_gettmpdir(void);
#if 0 #if 0
int sys_lock(char *); int sys_lock(char *);
int sys_unlock(char *); int sys_unlock(char *);
#endif #endif
char *sys_break(int); char *sys_break(int);
void sys_stop(int); void sys_stop(int);
long sys_modtime(char *); time_t sys_modtime(char *);
/* standard file decsriptors */ /* standard file decsriptors */
#define STDIN &_sys_ftab[0] #define STDIN &_sys_ftab[0]

View file

@ -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 ".";
}