LILO defines these flags as O_NOACCESS as follows:
#ifdef O_ACCMODE
# define O_NOACCESS O_ACCMODE
#else
/* open a file for "no access" */
# define O_NOACCESS 3
#endif
..as in this example, you may find these flags more widely known as O_NOACCESS even though they are not defined in the standard fcntl.h headers.
Below is a very simple example of the use of O_WRONLY | O_RDWR:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd;
struct winsize ws;
if ((fd = open("/dev/tty", O_WRONLY | O_RDWR)) < 0) {
perror("open /dev/tty failed");
exit(EXIT_FAILURE);
}
if (ioctl(fd, TIOCGWINSZ, &ws) == 0)
printf("%d x %d\n", ws.ws_row, ws.ws_col);
close(fd);
exit(EXIT_SUCCESS);
}
It is a little arcane and not portable but also an interesting feature to know about.
Good blog post, but i am somewhat confused.
ReplyDeleteIs this just talking about opening a file descriptor in read only or read/write mode? If so, are people really not using this on a regular basis?
I'm opening using O_WRONLY | O_RDWR (which is flag 1 | 2, in other words flags == 3). The semantics here are peculiar, O_WRONLY would normally mean just Write Only, but we are also or'ing in O_RDWR (Read and Write) - which one would think are not compatible with O_WRONLY.
DeleteAhh, i see! That makes much more sense.. thanks. :)
ReplyDelete-- Daviey