I know this is many years later, but for posterity you did it wrong:
@alk was right, the st_mode field contains more information, for example, file type, file permissions, etc.
To extract the file type, you perform bitwise in the st_mode field and the file type mask S_IFMT. Then check the result for everything you want. This is exactly what the macros mentioned by @Ernest Friedman-Hill do . Swicth is better suited for comprehensive checking ie
for a simple case:
if ((file_info.st_mode & S_IFMT)==S_IFDIR) puts("|| directory");
for a full check:
struct stat st; ... switch (st.st_mode & S_IFMT) { case S_IFREG: puts("|| regular file"); break; case S_IFDIR: puts("|| directory"); break; case S_IFCHR: puts("|| character device"); break; case S_IFBLK: puts("|| block device"); break; case S_IFLNK: puts("|| symbolic link"); break; case S_IFIFO: puts("|| pipe"); break; case S_IFSOCK: puts("|| socket"); break; default: puts("|| unknown"); }
source share