tags:

views:

86

answers:

3

Under the /usr/include directory in Linux i entered the command: find -type f -name unistd.h which gave the following output:

./unistd.h ./linux/unistd.h ./asm-generic/unistd.h ./bits/unistd.h ./asm/unistd.h ./sys/unistd.h

my question is, what is the purpose of each unistd.h, since there is only one definiton of that file in the single unix specification ?

Thanks in advance.

+5  A: 

linux/unistd.h actually points to asm/unistd.h, which in turn points to either asm/unistd_32.h or asm/unistd_64.h, which is where system call numbers are defined and presented to user space depending on the system's architecture. These come from the kernel.

bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via:

/* Define some macros helping to catch buffer overflows.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif

In essence, the only POSIX required header is in fact, just unistd.h, the rest are either extensions, or definitions from the kernel.

Tim Post
+3  A: 

It's a common technique in C and C++ - you have a single file with the "standard" name in the "standard" place, in this case ./unistd.h, and then have that file include one or more implementation specific files, depending on preprocessor macros. If you look at almost any "standard" C or C++ header files, you will see it including other files not mentioned in any standard.

anon
A: 

Basically think of /usr/include/unistd.h as a smart symbolic link. It will point to a correct implementation depending on what your operating conditions are.

That said, it makes difficult sometimes to figure out what that correct implementation is.

Alexander Pogrebnyak