fcntl.h doesn't contain all status flag constants

1.3k views Asked by At

I have been going through header files and I'm not able to find any file with status flag definitions (like O_RDONLY).

Thanks, John

1

There are 1 answers

3
Mikel On BEST ANSWER

Summary

If you're on Linux, it should be in /usr/include/bits/fcntl.h.

#define O_RDONLY 00

You can find it using rgrep, or ctags and Vim, or cpp, or cwhere.


rgrep

The simplest way is to use rgrep, a.k.a. grep -R.

$ rgrep O_WRONLY .
./X11/Xw32defs.h:#  define O_WRONLY    _O_WRONLY
./linux/fs.h: * to O_WRONLY and O_RDWR via the strange trick in __dentry_open()
./linux/smbno.h:#define SMB_O_WRONLY    0x0001
./asm-generic/fcntl.h:#define O_WRONLY  00000001
./security/_pam_macros.h:    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_NOFOLLOW|O_APPEND)) != -1) {
./security/_pam_macros.h:    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_APPEND)) != -1) {
./security/_pam_macros.h:    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_NOFOLLOW|O_APPEND)) != -1) {
./security/_pam_macros.h:    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_APPEND)) != -1) {
./bits/fcntl.h:#define O_WRONLY      01

ctags

Or, you could run ctags -R in /usr/include and then run vim -t O_WRONLY.

Or, a bit better, but more typing:

find . /usr/include -name "*.h" | 
ctags -f - -L - |
grep "^O_WRONLY   " |
cut -d "    " -f 1,2,3 |
sed -e 's# /\^#    #;s#\$/\;"$##'

cpp

The best way I have found is using cpp.

Assuming you have a source file called YOUR_SOURCE_FILE with the necessary #includes, try running:

cpp -dD YOUR_SOURCE_FILE | less

Then search for your #define, e.g. /O_WRONLY, then scroll up to find the first file name above it. In this case:

# 27 "/usr/include/bits/fcntl.h" 2 3 4

means that O_WRONLY is being picked up from /usr/include/bits/fcntl.h if you include the three header files mentioned in man fcntl.


cwhere

I have a script called cwhere to automate running cpp:

$ cwhere O_WRONLY sys/types.h sys/stat.h fcntl.h
/usr/include/bits/fcntl.h: #define O_WRONLY 01

Download cwhere here

If desc is installed, you can just type the name of the function that uses that #define, e.g.

$ cwhere O_WRONLY 'fcntl()'
/usr/include/bits/fcntl.h: #define O_WRONLY 01

Download desc here