libposix: add dup, dup2, getrusage, fstat and lstat

This commit is contained in:
Giacomo Tesio 2017-09-03 17:51:44 +02:00
parent 7f3297dada
commit c5aaa72bbb
3 changed files with 88 additions and 1 deletions

View File

@ -82,11 +82,21 @@ struct __attribute__((__packed__)) dirent
typedef unsigned long clock_t;
typedef enum PosixRUsages
{
PosixRUsageSelf = 0,
PosixRUsageChildren = 1,
PosixRUsageUnknown = -1
} PosixRUsages;
#define __POSIX_EXIT_PREFIX "posix error "
#define __POSIX_EXIT_SIGNAL_PREFIX "terminated by posix signal "
#define __POSIX_SIGNAL_PREFIX "posix: "
extern int POSIX_access(int *errnop, const char *path, int amode);
extern int POSIX_dup(int *errnop, int fildes);
extern int POSIX_dup2(int *errnop, int fildes, int fildes2);
extern void POSIX_exit(int code) __attribute__((noreturn));
extern int POSIX_chmod(int *errnop, const char *path, int mode);
extern int POSIX_fchmodat(int *errnop, int fd, const char *path, long mode, int flag);
@ -99,7 +109,7 @@ extern int POSIX_mkdir(int *errnop, const char *path, int mode);
extern int POSIX_close(int *errnop, int file);
extern int POSIX_execve(int *errnop, const char *name, char * const*argv, char * const*env);
extern int POSIX_fork(int *errnop);
extern int POSIX_fstat(int *errnop, int file, void *stat);
extern int POSIX_getrusage(int *errnop, PosixRUsages who, void *r_usage);
extern char* POSIX_getcwd(int *errnop, char *buf, int size);
extern char* POSIX_getlogin(int *errnop);
extern int POSIX_getlogin_r(int *errnop, char *name, int namesize);
@ -115,6 +125,8 @@ extern long POSIX_pread(int *errnop, int fd, char *buf, size_t len, long offset)
extern long POSIX_pwrite(int *errnop, int fd, const char *buf, size_t len, long offset);
extern long POSIX_read(int *errnop, int fd, char *buf, size_t len);
extern int POSIX_stat(int *errnop, const char *file, void *stat);
extern int POSIX_fstat(int *errnop, int file, void *stat);
extern int POSIX_lstat(int *errnop, const char *file, void *stat);
extern clock_t POSIX_times(int *errnop, void *tms);
extern int POSIX_unlink(int *errnop, const char *name);
extern int POSIX_wait(int *errnop, int *status);

View File

@ -153,6 +153,38 @@ AccessDone:
return -1;
}
int
POSIX_dup2(int *errnop, int fildes, int fildes2)
{
int newfd;
if(fildes < 0 || fildes2 < 0){
*errnop = __libposix_get_errno(PosixEBADF);
return -1;
}
newfd = dup(fildes, fildes2);
if(newfd < 0){
*errnop = __libposix_get_errno(PosixEINTR);
return -1;
}
return newfd;
}
int
POSIX_dup(int *errnop, int fildes)
{
int newfd;
if(fildes < 0){
*errnop = __libposix_get_errno(PosixEBADF);
return -1;
}
newfd = dup(fildes, -1);
if(newfd < 0){
*errnop = __libposix_get_errno(PosixEBADF);
return -1;
}
return newfd;
}
int
POSIX_pipe(int *errnop, int fildes[2])
{
@ -427,6 +459,12 @@ POSIX_stat(int *errnop, const char *file, void *pstat)
return -1;
}
int
POSIX_lstat(int *errnop, const char *file, void *pstat)
{
return POSIX_stat(errnop, file, pstat);
}
int
POSIX_chmod(int *errnop, const char *path, int mode)
{

View File

@ -28,6 +28,17 @@ ChildList **__libposix_child_list;
static PosixExitStatusTranslator __libposix_exit_status_translator;
static int __libposix_wnohang;
struct timeval {
unsigned int tv_sec;
unsigned int tv_usec;
};
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
struct timeval ru_etime; /* real elapsed time */
};
#define __POSIX_SIGNAL_PREFIX_LEN (sizeof(__POSIX_SIGNAL_PREFIX)-1)
static int
@ -138,6 +149,32 @@ POSIX_exit(int code)
exits(buf);
}
int
POSIX_getrusage(int *errnop, PosixRUsages who, void *r_usagep)
{
int32_t t[4];
struct rusage *r_usage = r_usagep;
times(&t[0]);
switch(who){
case PosixRUsageSelf:
r_usage->ru_utime.tv_sec = t[0]/1000;
r_usage->ru_utime.tv_sec = (t[0]%1000)*1000;
r_usage->ru_stime.tv_sec = t[1]/1000;
r_usage->ru_stime.tv_sec = (t[1]%1000)*1000;
return 0;
case PosixRUsageChildren:
r_usage->ru_utime.tv_sec = t[2]/1000;
r_usage->ru_utime.tv_sec = (t[2]%1000)*1000;
r_usage->ru_stime.tv_sec = t[3]/1000;
r_usage->ru_stime.tv_sec = (t[3]%1000)*1000;
return 0;
default:
*errnop = __libposix_get_errno(PosixEINVAL);
return -1;
}
}
int
POSIX_execve(int *errnop, const char *name, char * const*argv, char * const*env)
{