Search code examples
clinuxposixheader-filessystem-calls

Why are the mkdir() and rmdir() POSIX system calls in different header files?


mkdir() is in <sys/stat.h> whereas rmdir() is in <unistd.h>. Wouldn't it make more sense to have them in the same header file?


Solution

  • The reason is, that with mkdir(2) you specify the permissions in the second argument:

      int mkdir(const char *pathname, mode_t mode);
    

    These mode-flags and the type mode_t are defined in sys/stat.h (at least indirectly by including bits/stat.h and bits/types.h on my system), so it seems appropriate to define mkdir() there, too. Otherwise, including unistd.h would lead to an error, since the type mode_t is unknown there.

    In contrast, rmdir(2) doesn't take any arguments besides the filename, so it can remain in unistd.h, since there are no other dependencies.