Skip to content
Snippets Groups Projects
Verified Commit 70e31a94 authored by Jacob Lorentzon's avatar Jacob Lorentzon
Browse files

Dup stdout rather than opening /dev/stdout.

The former is more portable, even on Linux if for example sudo/doas is used.
parent 8dc3cca1
No related branches found
No related tags found
No related merge requests found
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <unistd.h>
#include "test_helpers.h" #include "test_helpers.h"
int main(void) int main(void) {
{
int fd = open("/dev/stdout", O_WRONLY, 0222);
ERROR_IF(open, fd, < 0);
const char *msg = "Hello, %s"; const char *msg = "Hello, %s";
int fd = dup(STDOUT_FILENO);
ERROR_IF(dup, fd, == -1);
int result = dprintf(fd, msg, "world"); int result = dprintf(fd, msg, "world");
ERROR_IF(dprintf, result, != sizeof("Hello, world") - 1); ERROR_IF(dprintf, result, != sizeof("Hello, world") - 1);
UNEXP_IF(dprintf, result, < 0); UNEXP_IF(dprintf, result, < 0);
...@@ -21,4 +20,4 @@ int main(void) ...@@ -21,4 +20,4 @@ int main(void)
close(fd); close(fd);
return 0; return 0;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment