When I run ls -l /dev
on OS X I get an output with the following format (but many more files):
crw-rw-rw- 1 root wheel 4, 126 Jun 11 20:28 ttywe
crw-rw-rw- 1 root wheel 4, 127 Jun 11 20:28 ttywf
As far as I can tell, it is specific to that folder and I can't find anywhere what that 4,
means.
I am rewriting ls
in C, so I would like to know what it is, and, if possible, how to retrieve that value in C.
From the POSIX specification for
ls
:In this particular case, you almost certainly have an implementation printing the major and minor device numbers -- the values which would be passed to
mknod
to create a file pointing to the same device. However, no implementation is required to provide this information, and your implementation ofls
can conform with the standard without it.If you want to know how to implement this in C, a good place to start is
man 2 stat
.st_rdev
is the field you care about in thestruct stat
which this call fills out. On Linux, you can extract the major and minor numbers by calling the macrosMAJOR(stat_result.st_rdev)
andMINOR(stat_result.st_rdev)
(assuming you toldstat
to write to a structure namedstat_result
).