Node:Getting File Status Flags, Previous:Operating Modes, Up:File Status Flags
The fcntl function can fetch or change file status flags.
| int F_GETFL | Macro |
This macro is used as the command argument to fcntl, to
read the file status flags for the open file with descriptor
filedes.
The normal return value from In case of an error,
|
| int F_SETFL | Macro |
This macro is used as the command argument to fcntl, to set
the file status flags for the open file corresponding to the
filedes argument. This command requires a third int
argument to specify the new flags, so the call looks like this:
fcntl (filedes, F_SETFL, new-flags) You can't change the access mode for the file in this way; that is, whether the file descriptor was opened for reading or writing. The normal return value from |
If you want to modify the file status flags, you should get the current
flags with F_GETFL and modify the value. Don't assume that the
flags listed here are the only ones that are implemented; your program
may be run years from now and more flags may exist then. For example,
here is a function to set or clear the flag O_NONBLOCK without
altering any other flags:
/* Set theO_NONBLOCKflag of desc if value is nonzero, or clear the flag if value is 0. Return 0 on success, or -1 on error witherrnoset. */ int set_nonblock_flag (int desc, int value) { int oldflags = fcntl (desc, F_GETFL, 0); /* If reading the flags failed, return error indication now. */ if (oldflags == -1) return -1; /* Set just the flag we want to set. */ if (value != 0) oldflags |= O_NONBLOCK; else oldflags &= ~O_NONBLOCK; /* Store modified flag word in the descriptor. */ return fcntl (desc, F_SETFL, oldflags); }