Unix: Where does "ls -l" get comma-separated values in size field for devices?

388 views Asked by At

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.

1

There are 1 answers

3
Charles Duffy On BEST ANSWER

From the POSIX specification for ls:

If the file is a character special or block special file, the size of the file may be replaced with implementation-defined information associated with the device in question.

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 of ls 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 the struct stat which this call fills out. On Linux, you can extract the major and minor numbers by calling the macros MAJOR(stat_result.st_rdev) and MINOR(stat_result.st_rdev) (assuming you told stat to write to a structure named stat_result).