Search code examples
cunixsystem-callsstat

stat st_mode is always equal to 16877


I want to know if a file is a directory or a regular file with stat :

#define _DEFAULT_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_regular_file(const char *path)
{
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISREG(path_stat.st_mode);
}

I try on Mac and Linux and when I print S_ISREG(path_stat.st_mode) is always equal to 1 and path_stat.st_mode is always equal to 16877.


Solution

  • 16877 is octal 40755, which denotes a directory (octal 40000) with permissions 755 (user has full rights, everyone else has read and traversal rights). As suggested, the stat and chmod manual pages are useful.

    Just for example, here is a screenshot with my directory-editor showing octal modes (an option) rather than the usual symbol ones:

    ded showing octal permissions