Casper.Dik at Sun.COM wrote:
>> Hi!
>>
>> ----
>>
>> Is there a way to detect whether a fd refers to "/dev/null" based on the
>> return values of |stat()| ?
> 
> Sure, stat("/dev/null") and compare the fields:
> 
>       st_mode (the type of the file, must be a char device)
> 
>       st_rdev (the major, minor of the device), note: "st_*r*dev", not
>               st_dev.

Remember that /dev/null is a symlink though.  So Ideally do an open(2) [ 
and *don't* specify O_NOFOLLOW] then fstat it.

Or given Roland asked about an existing fd just fstat.
This is the type of info you will get:

portellen:pts/2$ stat /dev/null
   File: `/dev/null' -> `../devices/pseudo/mm at 0:null'
   Size: 27             Blocks: 1          IO Block: 512    symbolic link
Device: 5400000h/88080384d      Inode: 1522315013  Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-09-17 16:41:22.256354770 +0100
Modify: 2009-07-27 10:53:27.691048106 +0100
Change: 2009-07-27 10:53:27.691081044 +0100

portellen:pts/2$ stat -L /dev/null
   File: `/dev/null'
   Size: 0              Blocks: 0          IO Block: 8192   character 
special file
Device: 53c0000h/87818240d      Inode: 6815752     Links: 1     Device type: d,2
Access: (0666/crw-rw-rw-)  Uid: (    0/    root)   Gid: (    3/     sys)
Access: 2009-09-09 10:37:22.000000000 +0100
Modify: 2009-09-17 16:00:01.000000000 +0100
Change: 2009-09-03 13:18:40.000000000 +0100

That doesn't tell it is /dev/null but as Casper said it tells you the 
device type.


-- 
Darren J Moffat

Reply via email to