Dup stdout rather than opening /dev/stdout.

The former is more portable, even on Linux if for example sudo/doas is used.
This commit is contained in:
4lDO2
2024-08-10 13:00:13 +02:00
parent 8dc3cca1e6
commit 70e31a9454
+6 -7
View File
@@ -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;
}
}