How do commands like ls or stat distinguish the file type, whether the object is a file or directory?
For example, I created these two objects, considering the fact that a directory is also a file... with some special rules, I want to know how in the output of the command stat
these are labelled as "directory" and "regular empty file".
$ mkdir testdir;touch testfile
$ stat testdir | head -2;stat testfile | head -2
File: `testdir'
Size: 4096 Blocks: 8 IO Block: 4096 directory
File: `testfile'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Later, I did a strace while executing stat of the directory testdir and the file testfile respectively. In the trace output I noticed these
lstat("testdir/", {st_mode=S_IFDIR|0775, st_size=4096, ...}) = 0
and
lstat("testfile", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
Someone please tell me how st_mode
gets these values S_IFDIR
and S_IFREG
.
I may sound confused; I am indeed.