Reputation: 6350
What is open()
? What does this do?
if (open("/dev/null", O_RDWR) < 0) {
die("error=open: %m");
}
Upvotes: 1
Views: 198
Reputation: 13484
open("filename", O_RDWR)
is equivalent to fopen("filename", "w")
/dev/null - This is used to truncate any unwanted streams. Consider that you are running a command in terminal, which will print both stdout
and stderr
in the terminal. If you want to truncate errors, we can run the command like cmd 2>/dev/null
. If you want to see only the compiler warnings during compilation, we can run like make 1> /dev/null
.
Upvotes: 1
Reputation: 27572
it opens a device known as /dev/null that discards anything written to it. Basically a waste basket.
So the code is opening that device in read/write mode. If the open fails it calls a function die
which prints the error literal you see along with the system error message returned from strerror(errno) and exits the program. The literal you see passed to die
is probably just a format string for printf
.
From man 3 printf:
m (Glibc extension.) Print output of strerror(errno). No argument is required.
There doesn't seem to be a glibc function called die()
; it is presumably a function defined by the author of the code snippet, and it presumably exits the function after reporting the error. However, we can only make educated guesses.
Upvotes: 2
Reputation: 1913
You should look at man 2 open
in a terminal window, or Google for it.
The open()
function is used to open a file and assign a file descriptor. If open()
fails, it returns a negative value.
Upvotes: 1