From 70e31a9454b3161fe5c71d7f8a4c48ead85584b8 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 10 Aug 2024 13:00:13 +0200 Subject: [PATCH] Dup stdout rather than opening /dev/stdout. The former is more portable, even on Linux if for example sudo/doas is used. --- tests/stdio/dprintf.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/stdio/dprintf.c b/tests/stdio/dprintf.c index 76601281..b61d5cfa 100644 --- a/tests/stdio/dprintf.c +++ b/tests/stdio/dprintf.c @@ -1,15 +1,14 @@ #include <stdio.h> -#include <fcntl.h> +#include <unistd.h> #include "test_helpers.h" -int main(void) -{ - int fd = open("/dev/stdout", O_WRONLY, 0222); - ERROR_IF(open, fd, < 0); - +int main(void) { const char *msg = "Hello, %s"; + int fd = dup(STDOUT_FILENO); + ERROR_IF(dup, fd, == -1); + int result = dprintf(fd, msg, "world"); ERROR_IF(dprintf, result, != sizeof("Hello, world") - 1); UNEXP_IF(dprintf, result, < 0); @@ -21,4 +20,4 @@ int main(void) close(fd); return 0; -} \ No newline at end of file +} -- GitLab