Skip to content

Commit dd01611

Browse files
committed
Add sprintf, snprintf
1 parent e040007 commit dd01611

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

docs/tcc-wasm.wasm

5.69 KB
Binary file not shown.

tcc-wasm.wasm

5.69 KB
Binary file not shown.

zig/tcc-wasm.zig

+18-7
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,31 @@ export fn vsnprintf(str: [*:0]u8, size: size_t, format: [*:0]const u8, ...) c_in
104104
str[strlen(format)] = 0;
105105
}
106106
debug("TODO: vsnprintf: return str={s}", .{str});
107-
return @intCast(strlen(format));
107+
return @intCast(strlen(format)); // TODO: Should be str?
108108
}
109109

110110
export fn fprintf(stream: *FILE, format: [*:0]const u8, ...) c_int {
111111
debug("fprintf: stream={*}, format={s}", .{ stream, format });
112112
return @intCast(strlen(format));
113113
}
114114

115+
export fn sprintf(str: [*:0]u8, format: [*:0]const u8, ...) c_int {
116+
debug("TODO: sprintf: format={s}", .{format});
117+
_ = memcpy(str, format, strlen(format));
118+
str[strlen(format)] = 0;
119+
debug("TODO: sprintf: return str={s}", .{str});
120+
return @intCast(strlen(str));
121+
}
122+
123+
export fn snprintf(str: [*:0]u8, size: size_t, format: [*:0]const u8, ...) c_int {
124+
debug("TODO: snprintf: size={}, format={s}", .{ size, format });
125+
// TODO: Catch overflow
126+
_ = memcpy(str, format, strlen(format));
127+
str[strlen(format)] = 0;
128+
debug("TODO: snprintf: return str={s}", .{str});
129+
return @intCast(strlen(str));
130+
}
131+
115132
const size_t = c_ulong; // TODO: Should be usize like strlen()?
116133
const FILE = opaque {};
117134

@@ -386,12 +403,6 @@ pub export fn remove(_: c_int) c_int {
386403
pub export fn sem_post(_: c_int) c_int {
387404
@panic("TODO: sem_post");
388405
}
389-
pub export fn snprintf(_: c_int) c_int {
390-
@panic("TODO: snprintf");
391-
}
392-
pub export fn sprintf(_: c_int) c_int {
393-
@panic("TODO: sprintf");
394-
}
395406
pub export fn strcat(_: c_int) c_int {
396407
@panic("TODO: strcat");
397408
}

0 commit comments

Comments
 (0)