Implement dprintf and vdprintf

This commit is contained in:
Agoston Szepessy
2024-07-22 22:24:37 +02:00
parent 798d17c5b3
commit bb1e8fe8d7
5 changed files with 42 additions and 0 deletions
+15
View File
@@ -1185,6 +1185,21 @@ pub unsafe extern "C" fn fprintf(
vfprintf(file, format, __valist.as_va_list())
}
#[no_mangle]
pub unsafe extern "C" fn vdprintf(fd: c_int, format: *const c_char, ap: va_list) -> c_int {
let mut f = File::new(fd);
// We don't want to close the file on drop; we're merely
// borrowing the file descriptor here
f.reference = true;
printf::printf(f, format, ap)
}
#[no_mangle]
pub unsafe extern "C" fn dprintf(fd: c_int, format: *const c_char, mut __valist: ...) -> c_int {
vdprintf(fd, format, __valist.as_va_list())
}
#[no_mangle]
pub unsafe extern "C" fn vprintf(format: *const c_char, ap: va_list) -> c_int {
vfprintf(&mut *stdout, format, ap)
+1
View File
@@ -33,6 +33,7 @@ EXPECT_NAMES=\
signal \
stdio/all \
stdio/buffer \
stdio/dprintf \
stdio/fgets \
stdio/fputs \
stdio/fread \
@@ -0,0 +1,2 @@
Hello, world
a
+24
View File
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <fcntl.h>
#include "test_helpers.h"
int main(void)
{
int fd = open("/dev/stdout", O_WRONLY, 0222);
ERROR_IF(open, fd, < 0);
const char *msg = "Hello, %s";
int result = dprintf(fd, msg, "world");
ERROR_IF(dprintf, result, != sizeof("Hello, world") - 1);
UNEXP_IF(dprintf, result, < 0);
result = dprintf(fd, "\na\n");
UNEXP_IF(dprintf, result, < 0);
ERROR_IF(dprintf, result, != sizeof("\na\n") - 1);
close(fd);
return 0;
}