Skip to content

Commit 7cdc028

Browse files
committed
Set errno
Fix #148
1 parent f566ddc commit 7cdc028

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+67
-172
lines changed

libsrc/_wasm/unistd/clock_gettime.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#include "time.h"
2+
#include "errno.h"
23
#include "stddef.h" // NULL
34
#include "stdint.h"
45
#include "../wasi.h"
56

67
int clock_gettime(clockid_t clk_id, struct timespec *tp) {
7-
if (tp == NULL)
8+
if (tp == NULL) {
9+
errno = EINVAL;
810
return -1;
11+
}
912

1013
uint64_t time;
1114
clock_time_get(clk_id, 1, &time);

libsrc/_wasm/unistd/fstat.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "sys/stat.h"
2+
#include "errno.h"
23
#include "../wasi.h"
34

45
extern void _set_stat(Filestat *fs, struct stat *st);
@@ -10,5 +11,6 @@ int fstat(int fd, struct stat *st) {
1011
_set_stat(&fs, st);
1112
return 0;
1213
}
13-
return 1;
14+
errno = ENOENT;
15+
return -1;
1416
}

libsrc/_wasm/unistd/lseek.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "unistd.h"
2+
#include "errno.h"
23
#include "../wasi.h"
34

45
off_t lseek(int fd, off_t offset, int whence) {
56
size_t size;
67
int result = fd_seek(fd, offset, whence, &size);
7-
return result == 0 ? size : -1;
8+
if (result == 0)
9+
return size;
10+
errno = EIO; // TODO
11+
return -1;
812
}

libsrc/_wasm/unistd/mkdir.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "sys/stat.h"
2+
#include "errno.h"
23
#include "string.h" // memset, strncmp
34
#include "../wasi.h"
45

@@ -34,5 +35,6 @@ int mkdir(const char *fn, mode_t mode) {
3435
if (result == 0)
3536
return 0;
3637
}
38+
errno = EPERM; // TODO
3739
return -1;
3840
}

libsrc/_wasm/unistd/open.c

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "unistd.h"
2+
#include "errno.h"
23
#include "fcntl.h"
34
#include "string.h"
45
#include "../wasi.h"
@@ -21,6 +22,7 @@ int open(const char *fn, int flag, ...) {
2122
fs_rights_base = FD_DATASYNC | FD_READ | FD_SEEK | FD_FDSTAT_SET_FLAGS | FD_SYNC | FD_TELL | FD_WRITE | FD_ADVISE | FD_ALLOCATE | PATH_CREATE_DIRECTORY | PATH_CREATE_FILE | PATH_LINK_SOURCE | PATH_LINK_TARGET | PATH_OPEN | FD_READDIR | PATH_READLINK | PATH_RENAME_SOURCE | PATH_RENAME_TARGET | PATH_FILESTAT_GET | FD_FILESTAT_GET | FD_FILESTAT_SET_SIZE | FD_FILESTAT_SET_TIMES | PATH_SYMLINK | PATH_REMOVE_DIRECTORY | PATH_UNLINK_FILE | POLL_FD_READWRITE;
2223
break;
2324
default:
25+
errno = EINVAL;
2426
return -1;
2527
}
2628
uint64_t fs_rights_inheriting = fs_rights_base;
@@ -62,5 +64,6 @@ int open(const char *fn, int flag, ...) {
6264
if (result == 0)
6365
return opened;
6466
}
67+
errno = ENOENT; // (flag & O_CREAT) ? EEXIST, or something.
6568
return -1;
6669
}

libsrc/_wasm/unistd/read.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "unistd.h"
2+
#include "errno.h"
23
#include "../wasi.h"
34

45
ssize_t read(int fd, void *buf, size_t size) {
@@ -7,5 +8,8 @@ ssize_t read(int fd, void *buf, size_t size) {
78
iov.n = size;
89
size_t readed;
910
int result = fd_read(fd, &iov, 1, &readed);
10-
return result == 0 ? readed : -1;
11+
if (result == 0)
12+
return readed;
13+
errno = EIO; // TODO
14+
return -1;
1115
}

libsrc/_wasm/unistd/rmdir.c

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ int rmdir(const char *fn) {
3636
if (result == 0)
3737
return 0;
3838
}
39+
errno = ENOENT;
3940
return -1;
4041
}

libsrc/_wasm/unistd/stat.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "sys/stat.h"
2+
#include "errno.h"
23
#include "string.h" // memset, strncmp
34
#include "../wasi.h"
45

@@ -39,5 +40,6 @@ int stat(const char *fn, struct stat *st) {
3940
return 0;
4041
}
4142
}
43+
errno = ENOENT;
4244
return -1;
4345
}

libsrc/_wasm/unistd/unlink.c

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ int unlink(const char *fn) {
3636
if (result == 0)
3737
return 0;
3838
}
39+
errno = ENOENT; // TODO
3940
return -1;
4041
}

libsrc/_wasm/unistd/write.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "unistd.h"
2+
#include "errno.h"
23
#include "../wasi.h"
34

45
ssize_t write(int fd, const void *str, size_t len) {
@@ -7,5 +8,8 @@ ssize_t write(int fd, const void *str, size_t len) {
78
iov.n = len;
89
size_t written;
910
int result = fd_write(fd, &iov, 1, &written);
10-
return result == 0 ? written : -1;
11+
if (result == 0)
12+
return written;
13+
errno = EIO; // TODO
14+
return -1;
1115
}

libsrc/stdlib/exit.c

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ OnExitChain *__on_exit_chain;
1111
#else
1212
#include "../unistd/_syscall.h"
1313

14-
#if defined(__GNUC__)
15-
#pragma GCC diagnostic ignored "-Wunused-parameter"
16-
#endif
1714
_Noreturn static void proc_exit(int code);
1815
static void proc_exit(int code) {
1916
#ifdef __NR_exit_group

libsrc/unistd/_syscall.h

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "errno.h"
4+
35
#if defined(__x86_64__)
46

57
#define SYSCALL(no) _SYSCALL2(no)
@@ -117,3 +119,9 @@
117119
#else
118120
#error unknown
119121
#endif
122+
123+
#define SET_ERRNO(res) do { if ((res) < 0) { errno = -(res); res = -1; } } while (0)
124+
125+
#if defined(__GNUC__)
126+
#pragma GCC diagnostic ignored "-Wunused-parameter"
127+
#endif

libsrc/unistd/brk.c

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include "unistd.h"
22
#include "stdio.h" // EOF
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__linux__)
95
#include "_syscall.h"
106

libsrc/unistd/chdir.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
int chdir(const char *path) {
95
int ret;
106
SYSCALL_RET(__NR_chdir, ret);
7+
SET_ERRNO(ret);
118
return ret;
129
}

libsrc/unistd/chmod.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#include "sys/stat.h" // mode_t
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_chmod)
95
int chmod(const char *pathname, mode_t mode) {
106
int ret;
117
SYSCALL_RET(__NR_chmod, ret);
8+
SET_ERRNO(ret);
129
return ret;
1310
}
1411

libsrc/unistd/clock_gettime.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#include "time.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_clock_gettime)
95
int clock_gettime(clockid_t clk_id, struct timespec *tp) {
106
int ret;
117
SYSCALL_RET(__NR_clock_gettime, ret);
8+
SET_ERRNO(ret);
129
return ret;
1310
}
1411
#endif

libsrc/unistd/clone3.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_clone3)
95
#include "stdint.h"
106

117
long clone3(struct clone_args *cl_args, size_t size) {
128
long ret;
139
SYSCALL_RET(__NR_clone3, ret);
10+
SET_ERRNO(ret);
1411
return ret;
1512
}
1613
#endif

libsrc/unistd/close.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
int close(int fd) {
95
int ret;
106
SYSCALL_RET(__NR_close, ret);
7+
SET_ERRNO(ret);
118
return ret;
129
}

libsrc/unistd/dup.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
int dup(int fd) {
95
int ret;
106
SYSCALL_RET(__NR_dup, ret);
7+
SET_ERRNO(ret);
118
return ret;
129
}

libsrc/unistd/execve.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
int execve(const char *path, char *const args[], char *const envp[]) {
95
int ret;
106
SYSCALL_RET(__NR_execve, ret);
7+
SET_ERRNO(ret);
118
return ret;
129
}

libsrc/unistd/fchmodat.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#include "sys/stat.h" // mode_t
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_fchmodat)
95
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
106
int ret;
117
#if defined(__x86_64__)
128
SYSCALL_ARGCOUNT(4);
139
#endif
1410
SYSCALL_RET(__NR_fchmodat, ret);
11+
SET_ERRNO(ret);
1512
return ret;
1613
}
1714
#endif

libsrc/unistd/fstat.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#include "sys/stat.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_fstat)
95
int fstat(int fd, struct stat *buf) {
106
int ret;
117
SYSCALL_RET(__NR_fstat, ret);
8+
SET_ERRNO(ret);
129
return ret;
1310
}
1411
#endif

libsrc/unistd/fstatat.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#include "sys/stat.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_fstatat)
95
int fstatat(int fd, const char *pathname, struct stat *buf, int flag) {
106
int ret;
117
#if defined(__x86_64__)
128
SYSCALL_ARGCOUNT(4);
139
#endif
1410
SYSCALL_RET(__NR_fstatat, ret);
11+
SET_ERRNO(ret);
1512
return ret;
1613
}
1714

@@ -22,6 +19,7 @@ int fstatat(int fd, const char *pathname, struct stat *buf, int flag) {
2219
SYSCALL_ARGCOUNT(4);
2320
#endif
2421
SYSCALL_RET(__NR_newfstatat, ret);
22+
SET_ERRNO(ret);
2523
return ret;
2624
}
2725
#endif

libsrc/unistd/getcwd.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#include "errno.h"
33
#include "stdlib.h" // malloc
44

5-
#if defined(__GNUC__)
6-
#pragma GCC diagnostic ignored "-Wunused-parameter"
7-
#endif
8-
95
#if defined(__APPLE__)
106
extern int _getcwd(char *, size_t);
117

@@ -15,6 +11,7 @@ extern int _getcwd(char *, size_t);
1511
static int _getcwd(char *buffer, size_t size) {
1612
int ret;
1713
SYSCALL_RET(__NR_getcwd, ret);
14+
SET_ERRNO(ret);
1815
return ret;
1916
}
2017
#endif

libsrc/unistd/ioctl.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
#if defined(__NR_ioctl)
95
int ioctl(int fd, int request, ...) {
106
int ret;
117
SYSCALL_RET(__NR_ioctl, ret);
8+
SET_ERRNO(ret);
129
return ret;
1310
}
1411
#endif

libsrc/unistd/kill.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "unistd.h"
22
#include "_syscall.h"
33

4-
#if defined(__GNUC__)
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#endif
7-
84
int kill(pid_t pid, int sig) {
95
int ret;
106
SYSCALL_RET(__NR_kill, ret);
7+
SET_ERRNO(ret);
118
return ret;
129
}

0 commit comments

Comments
 (0)