Skip to content

Commit

Permalink
Add fdopen, fwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
lupyuen committed Jan 27, 2024
1 parent 401bae9 commit 7380fe1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Binary file modified docs/tcc-wasm.wasm
Binary file not shown.
Binary file modified tcc-wasm.wasm
Binary file not shown.
18 changes: 12 additions & 6 deletions zig/tcc-wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export fn open(path: [*:0]const u8, oflag: c_uint, ...) c_int {
return ret;
}

export fn fdopen(fd0: c_int, mode: [*:0]const u8) *FILE {
debug("fdopen: fd={}, mode={s}, return FILE={}", .{ fd0, mode, fd });
const ret = fd;
fd += 1;
return @ptrFromInt(@as(usize, @intCast(ret)));
}

export fn read(fd0: c_int, buf: [*:0]u8, nbyte: size_t) isize {
debug("read: fd={}, nbyte={}", .{ fd0, nbyte });
// Return the contents once only
Expand All @@ -72,6 +79,11 @@ export fn read(fd0: c_int, buf: [*:0]u8, nbyte: size_t) isize {
return @intCast(strlen(s));
}

export fn fwrite(ptr: [*:0]u8, size: usize, nmemb: usize, stream: *FILE) usize {
debug("fwrite: ptr={s}, size={}, nmemb={}, stream={*}", .{ ptr, size, nmemb, stream });
return nmemb;
}

export fn close(fd0: c_int) c_int {
debug("close: fd={}", .{fd0});
return 0;
Expand Down Expand Up @@ -371,9 +383,6 @@ pub export fn exit(_: c_int) c_int {
pub export fn fclose(_: c_int) c_int {
@panic("TODO: fclose");
}
pub export fn fdopen(_: c_int) c_int {
@panic("TODO: fdopen");
}
pub export fn fopen(_: c_int) c_int {
@panic("TODO: fopen");
}
Expand All @@ -392,9 +401,6 @@ pub export fn fseek(_: c_int) c_int {
pub export fn ftell(_: c_int) c_int {
@panic("TODO: ftell");
}
pub export fn fwrite(_: c_int) c_int {
@panic("TODO: fwrite");
}
pub export fn getcwd(_: c_int) c_int {
@panic("TODO: getcwd");
}
Expand Down

0 comments on commit 7380fe1

Please sign in to comment.