From: John Hodge Date: Sat, 8 Mar 2014 06:02:53 +0000 (+0800) Subject: Usermode/libposix - Header wrapping, mktemp X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=e5ee5df5d32c18b7679637056a7301f59ebbfefe;p=tpg%2Facess2.git Usermode/libposix - Header wrapping, mktemp --- diff --git a/Usermode/Libraries/libposix.so_src/Makefile b/Usermode/Libraries/libposix.so_src/Makefile index ca566cdc..30c4cd2e 100644 --- a/Usermode/Libraries/libposix.so_src/Makefile +++ b/Usermode/Libraries/libposix.so_src/Makefile @@ -11,7 +11,7 @@ LDFLAGS += -soname libposix.so -Map map.txt -lc OBJ = main.o unistd.o dirent.o stat.o utmpx.o termios.o OBJ += pwd.o syslog.o sys_time.o sys_ioctl.o sys_resource.o OBJ += fcntl.o clocks.o sys_wait.o unistd_crypt.o -OBJ += grp.o pty.o +OBJ += grp.o pty.o mktemp.o DEPFILES := $(OBJ:%.o=%.d) BIN = libposix.so diff --git a/Usermode/Libraries/libposix.so_src/include_exp/dirent.h b/Usermode/Libraries/libposix.so_src/include_exp/dirent.h index 747d7ea5..8eec42ab 100644 --- a/Usermode/Libraries/libposix.so_src/include_exp/dirent.h +++ b/Usermode/Libraries/libposix.so_src/include_exp/dirent.h @@ -10,6 +10,10 @@ #include "sys/stat.h" +#ifdef __cplusplus +extern "C" { +#endif + #define NAME_MAX 255 struct dirent @@ -28,5 +32,9 @@ extern void rewinddir(DIR *); extern void seekdir(DIR *, long int); extern long int telldir(DIR *); +#ifdef __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libposix.so_src/include_exp/libposix_stdlib.h b/Usermode/Libraries/libposix.so_src/include_exp/libposix_stdlib.h new file mode 100644 index 00000000..c5f8c8ef --- /dev/null +++ b/Usermode/Libraries/libposix.so_src/include_exp/libposix_stdlib.h @@ -0,0 +1,17 @@ +/* + * Acess2 POSIX Emulation + * - By John Hodge (thePowersGang) + * + * stdlib.h (libposix version) + * - Misc functions + * + * NOTE: Included by libc's stdlib.h + */ +#ifndef _LIBPOSIX__STDLIB_H_ +#define _LIBPOSIX__STDLIB_H_ + +extern int mkstemp(char *_template); + + +#endif + diff --git a/Usermode/Libraries/libposix.so_src/include_exp/sys/mman.h b/Usermode/Libraries/libposix.so_src/include_exp/sys/mman.h new file mode 100644 index 00000000..7fbb9a5a --- /dev/null +++ b/Usermode/Libraries/libposix.so_src/include_exp/sys/mman.h @@ -0,0 +1,16 @@ +/* + * Acess2 POSIX Emulation Layer + * - By John Hodge + * + * sys/mman.h + * - Memory Management (mmap and friends) + */ +#ifndef _LIBPOSIX__SYS_MMAN_H_ +#define _LIBPOSIX__SYS_MMAN_H_ + +extern void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); +extern int munmap(void *addr, size_t length); + + +#endif + diff --git a/Usermode/Libraries/libposix.so_src/include_exp/sys/stat.h b/Usermode/Libraries/libposix.so_src/include_exp/sys/stat.h index 0340a9fc..faac6c1b 100644 --- a/Usermode/Libraries/libposix.so_src/include_exp/sys/stat.h +++ b/Usermode/Libraries/libposix.so_src/include_exp/sys/stat.h @@ -11,6 +11,10 @@ #include #include "sys/types.h" // off_t +#ifdef __cplusplus +extern "C" { +#endif + typedef void *dev_t; /* TODO: How to identify a device with Acess */ typedef uint64_t ino_t; typedef unsigned int blksize_t; @@ -27,6 +31,9 @@ typedef uint32_t mode_t; #define S_IFSOCK 0140000 /* socket */ #define S_IFIFO 0010000 /* fifo */ +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) +#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) struct stat { @@ -50,4 +57,8 @@ extern int lstat(const char *path, struct stat *buf); extern int fstat(int fd, struct stat *buf); extern int mkdir(const char *pathname, mode_t mode); +#ifdef __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libposix.so_src/include_exp/sys/time.h b/Usermode/Libraries/libposix.so_src/include_exp/sys/time.h index 79e40138..cef859bb 100644 --- a/Usermode/Libraries/libposix.so_src/include_exp/sys/time.h +++ b/Usermode/Libraries/libposix.so_src/include_exp/sys/time.h @@ -10,6 +10,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + typedef unsigned long suseconds_t; struct timeval @@ -55,5 +59,9 @@ extern int gettimeofday(struct timeval *tv, struct timezone *tz); // select extern int utimes(const char *, const struct timeval [2]); +#ifdef __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libposix.so_src/include_exp/unistd.h b/Usermode/Libraries/libposix.so_src/include_exp/unistd.h index f24011ae..6519b3b7 100644 --- a/Usermode/Libraries/libposix.so_src/include_exp/unistd.h +++ b/Usermode/Libraries/libposix.so_src/include_exp/unistd.h @@ -10,6 +10,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + //! \brief flags for open(2) #define O_WRONLY 0x01 #define O_RDONLY 0x02 @@ -65,6 +69,10 @@ extern int chown(const char *path, uid_t owner, gid_t group); #define S_IXOTH 00001 extern int chmod(const char *path, mode_t mode); +extern int unlink(const char *pathname); + +extern int access(const char *pathname, int mode); + extern pid_t setsid(void); extern uid_t getuid(void); @@ -79,6 +87,7 @@ typedef uint32_t useconds_t; extern unsigned int sleep(unsigned int seconds); extern int usleep(useconds_t usec); +extern unsigned int alarm(unsigned int seconds); // - crypt.c extern char *crypt(const char *key, const char *salt); @@ -97,5 +106,9 @@ extern int rmdir(const char *pathname); #define PASS_MAX 63 extern char *getpass(const char *prompt); +#if __cplusplus +} +#endif + #endif diff --git a/Usermode/Libraries/libposix.so_src/include_exp/utime.h b/Usermode/Libraries/libposix.so_src/include_exp/utime.h new file mode 100644 index 00000000..dcc43c77 --- /dev/null +++ b/Usermode/Libraries/libposix.so_src/include_exp/utime.h @@ -0,0 +1,29 @@ +/* + * Acess2 POSIX Emulation Layer + * - By John Hodge + * + * utime.h + * - + */ +#ifndef _LIBPOSIX__UTIME_H_ +#define _LIBPOSIX__UTIME_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include // time_t + +struct utimbuf { + time_t actime; // access time + time_t modtime; // modifcation time +}; + +extern int utime(const char *filename, const struct utimbuf *times); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/Usermode/Libraries/libposix.so_src/mktemp.c b/Usermode/Libraries/libposix.so_src/mktemp.c new file mode 100644 index 00000000..863b9256 --- /dev/null +++ b/Usermode/Libraries/libposix.so_src/mktemp.c @@ -0,0 +1,49 @@ +/* + * Acess2 POSIX Emulation Layer + * - By John Hodge + * + * mkstemp.c + * - mkstemp/mktemp + */ +#include // mktemp +#include // mkstemp +#include // str* +#include + +// === CODE === +int mkstemp(char *template) +{ + size_t tpl_len = strlen(template); + if( tpl_len < 6 ) { + errno = EINVAL; + return -1; + } + if( strcmp(template+tpl_len-6, "XXXXXX") != 0 ) { + errno = EINVAL; + return -1; + } + + for( int i = 0; i < 1000000; i ++ ) + { + sprintf(template+tpl_len-6, "%06d", i); + int fd = open(template, O_EXCL|O_CREAT, 0600); + if(fd == -1) continue ; + + return fd; + } + + errno = EEXIST; + template[0] = '\0'; + return -1; +} + +char *mktemp(char *template) +{ + int fd = mkstemp(template); + if( fd == -1 ) + return NULL; + close(fd); + return template; +} + + diff --git a/Usermode/Libraries/libposix.so_src/unistd.c b/Usermode/Libraries/libposix.so_src/unistd.c index f5b330da..64cb70f5 100644 --- a/Usermode/Libraries/libposix.so_src/unistd.c +++ b/Usermode/Libraries/libposix.so_src/unistd.c @@ -183,6 +183,17 @@ int usleep(useconds_t usec) return 0; } +unsigned int alarm(unsigned int seconds) +{ + static int64_t alarm_time; + if( seconds > 0 ) + { + alarm_time = _SysTimestamp() + seconds * 1000; + // TODO: Schedule SIGALRM + } + return (alarm_time - _SysTimestamp()) / 1000; +} + int kill(pid_t pid, int signal) { // TODO: Need special handling?