Skip to content

Commit 5de8956

Browse files
committed
Fix WASIX build
WASIX is missing a couple common functions like fchdir() and ctermid(). Add some configure-time checks for those. Link: wasix-org/wasix-libc#105
1 parent 8babc4c commit 5de8956

6 files changed

Lines changed: 37 additions & 4 deletions

File tree

build/has/ctermid.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright © Tavian Barnes <tavianator@tavianator.com>
2+
// SPDX-License-Identifier: 0BSD
3+
4+
#include <stdio.h>
5+
6+
int main(void) {
7+
char path[L_ctermid];
8+
return !ctermid(path);
9+
}

build/has/fchdir.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright © Tavian Barnes <tavianator@tavianator.com>
2+
// SPDX-License-Identifier: 0BSD
3+
4+
#include <unistd.h>
5+
6+
int main(void) {
7+
return fchdir(0);
8+
}

build/header.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ HEADERS := \
2222
gen/has/acl-trivial.h \
2323
gen/has/builtin-riscv-pause.h \
2424
gen/has/confstr.h \
25+
gen/has/ctermid.h \
2526
gen/has/dprintf.h \
2627
gen/has/extattr-get-file.h \
2728
gen/has/extattr-get-link.h \
2829
gen/has/extattr-list-file.h \
2930
gen/has/extattr-list-link.h \
31+
gen/has/fchdir.h \
3032
gen/has/fdclosedir.h \
3133
gen/has/getdents.h \
3234
gen/has/getdents64-syscall.h \

src/bfstd.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,15 @@ pid_t xwaitpid(pid_t pid, int *status, int flags) {
645645
}
646646

647647
int open_cterm(int flags) {
648+
#if BFS_HAS_CTERMID
648649
char path[L_ctermid];
649650
if (ctermid(path) == NULL || strlen(path) == 0) {
650651
errno = ENOTTY;
651652
return -1;
652653
}
654+
#else
655+
const char *path = "/dev/tty";
656+
#endif
653657

654658
return open(path, flags);
655659
}
@@ -1019,7 +1023,7 @@ size_t xstrwidth(const char *str) {
10191023
mbstate_t mb = {0};
10201024
while (i < len) {
10211025
wint_t wc = xmbrtowc(str, &i, len, &mb);
1022-
if (wc == WEOF) {
1026+
if (wc == (wint_t)WEOF) {
10231027
// Assume a single-width '?'
10241028
++ret;
10251029
continue;
@@ -1103,7 +1107,7 @@ static size_t printable_len(const char *str, size_t len, enum wesc_flags flags)
11031107
mbstate_t mb = {0};
11041108
for (size_t j = i; i < len; i = j) {
11051109
wint_t wc = xmbrtowc(str, &j, len, &mb);
1106-
if (wc == WEOF) {
1110+
if (wc == (wint_t)WEOF) {
11071111
break;
11081112
}
11091113
if (!wesc_iswprint(wc, flags)) {
@@ -1153,7 +1157,7 @@ static char *dollar_quote(char *dest, char *end, const char *str, size_t len, en
11531157
bool safe = false;
11541158

11551159
wint_t wc = xmbrtowc(str, &i, len, &mb);
1156-
if (wc != WEOF) {
1160+
if (wc != (wint_t)WEOF) {
11571161
safe = wesc_iswprint(wc, flags);
11581162
}
11591163

src/eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ static void eval_status(struct bfs_eval *state, struct bfs_bar *bar, size_t coun
11871187
for (size_t i = lhslen; lhslen < pathlen; lhslen = i) {
11881188
wint_t wc = xmbrtowc(status, &i, pathlen, &mb);
11891189
int cwidth;
1190-
if (wc == WEOF) {
1190+
if (wc == (wint_t)WEOF) {
11911191
// Invalid byte sequence, assume a single-width '?'
11921192
cwidth = 1;
11931193
} else {

src/xspawn.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ int bfs_spawn_adddup2(struct bfs_spawn *ctx, int oldfd, int newfd) {
238238
#define BFS_POSIX_SPAWN_AFTER_FCHDIR !__NetBSD__
239239

240240
int bfs_spawn_addfchdir(struct bfs_spawn *ctx, int fd) {
241+
#if BFS_HAS_FCHDIR
241242
struct bfs_spawn_action *action = bfs_spawn_action(BFS_SPAWN_FCHDIR);
242243
if (!action) {
243244
return -1;
@@ -275,6 +276,10 @@ int bfs_spawn_addfchdir(struct bfs_spawn *ctx, int fd) {
275276
action->in_fd = fd;
276277
SLIST_APPEND(ctx, action);
277278
return 0;
279+
#else // !BFS_HAS_FCHDIR
280+
errno = ENOTSUP;
281+
return -1;
282+
#endif
278283
}
279284

280285
int bfs_spawn_setrlimit(struct bfs_spawn *ctx, int resource, const struct rlimit *rl) {
@@ -627,10 +632,15 @@ static void bfs_spawn_exec(struct bfs_resolver *res, const struct bfs_spawn *ctx
627632
}
628633
break;
629634
case BFS_SPAWN_FCHDIR:
635+
#if BFS_HAS_FCHDIR
630636
if (fchdir(action->in_fd) != 0) {
631637
goto fail;
632638
}
633639
break;
640+
#else
641+
errno = ENOTSUP;
642+
goto fail;
643+
#endif
634644
case BFS_SPAWN_SETRLIMIT:
635645
if (setrlimit(action->resource, &action->rlimit) != 0) {
636646
goto fail;

0 commit comments

Comments
 (0)