Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,9 @@ char *fgets(char *str, int n, FILE *stream)

int fputc(int c, FILE *stream)
{
char buf[1];
buf[0] = c;
__syscall(__syscall_write, stream, buf, 1);
return 0;
if (__syscall(__syscall_write, stream, &c, 1) < 0)
return -1;
return c;
}

/* Non-portable: Assume page size is 4KiB */
Expand Down
29 changes: 29 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1674,4 +1674,33 @@ int main() {
}
EOF

# test the return value when calling fputc().
#
# Since the FILE data type is defined as an int in
# the built-in C library, and most of the functions
# such as fputc(), fgetc(), fclose() and fgets() directly
# treat the "stream" parameter (of type FILE *) as a file
# descriptor for performing input/output operations, the
# following test cases define "stdout" as 1, which is the
# file descriptor for the standard output.
try_output 0 "awritten = a" << EOF
#define stdout 1
int main()
{
int c = fputc('a', stdout);
printf("written = %c", c);
return 0;
}
EOF

try_output 1 "" << EOF
#define stdout 1
int main()
{
__syscall(__syscall_close, 1);
int c = fputc('a', stdout);
return c == -1;
}
EOF

echo OK
2 changes: 1 addition & 1 deletion tests/snapshots/fib-arm.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/fib-riscv.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/hello-arm.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/hello-riscv.json

Large diffs are not rendered by default.