wasi-libc/libc-bottom-half/sources/__wasilibc_tell.c
Dan Gohman cf366c06d1 Optimize lseek in the tell case.
`lseek(x, 0, SEEK_CUR)` has no effect other than to return the current
file offset. The patch here uses a macro with `__builtin_constant_p` to
recognize this case and rewrite it to a library call that uses `fd_tell`
rather than `fd_seek`, so that programs that don't need actual seeking
don't end up importing `fd_seek`.

This is also the first usage of `__wasi_fd_tell` in WASI libc, so this
adds it to undefined-symbols.txt.
2019-04-30 16:19:05 -07:00

15 lines
426 B
C

#include <wasi/core.h>
#include <errno.h>
off_t __wasilibc_tell(int fildes) {
__wasi_filesize_t offset;
__wasi_errno_t error = __wasi_fd_tell(fildes, &offset);
if (error != 0) {
// lseek returns ESPIPE on when called on a pipe, socket, or fifo,
// which on WASI would translate into ENOTCAPABLE.
errno = error == ENOTCAPABLE ? ESPIPE : error;
return -1;
}
return offset;
}