palamedes has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i have to write a clone of perl's stat using external programs. Basically i use /usr/bin/stat and massage a little the output.

But i had a problem with the 'rdev' return value ('rdev' is the sixth element of the returned list). I just don't know what is his meaning and therefore i couldn't get a way to get it with a external program.

The stat documentation in perlfunc states the following about 'rdev':

'6 rdev the device identifier (special files only)'
I was unable to found nay other reference about this return value.

Any info or pointer about the elusive 'rdev' would be greatly welcome. Thanks!

Replies are listed 'Best First'.
Re: Meaning of stat's 'rdev' value.
by gellyfish (Monsignor) on Sep 06, 2006 at 09:29 UTC

    For a regular file st_rdev is 0, for a device file (e.g. /dev/tty) it holds the major and minor device numbers (assuming a POSIX like system) which can derived by masking and shifting appropriately. You can see how this is done in the source of File::Stat::Bits. The following is derived from that module:

    my $tty_rdev = (stat('/dev/dsp0'))[6]; print major($tty_rdev), " ", minor($tty_rdev),"\n"; sub MAJOR_MASK () { 03777400 } sub MAJOR_SHIFT () { 0000010 } sub MINOR_MASK () { 037774000377 } sub MINOR_SHIFT () { 0000000 } sub major { my $dev = shift; return defined MAJOR_MASK ? ($dev & MAJOR_MASK) >> MAJOR_SHIFT : u +ndef; } sub minor { my $dev = shift; return defined MINOR_MASK ? ($dev & MINOR_MASK) >> MINOR_SHIFT : u +ndef; }
    If you want to recreate the st_rdev from the output of the stat command you want to get the values for the major and minor device type and combine them using the dev_join from File::Stat::Bits.

    I'm not clear however why you want to use the external command rather than the builtin stat.

    /J\

      Thanks, you have saved my day.

      We are doings this in order to let a user stat files with super-user privilegies using sudo. This operation is done inside a perl framework, so for convenience we wanted the output format where the same that perl's stat.

      Cheers

      This is off topic sorta, but what's the point of

      sub MAJOR_MASK () { 03777400 } sub MAJOR_SHIFT () { 0000010 } sub MINOR_MASK () { 037774000377 } sub MINOR_SHIFT () { 0000000 }
      ?

      Those subs only return numbers, why not just put them into constants or even variables?

      meh.
        I can't speak for gellyfish. Once upon a time that was a popular way to do constants in Perl, though. The pragmas haven't always been around, and not changing an upper-cased variable just by convention often doesn't cut it. By making it returned from a sub, you can make it really constant without pragmas.


        Christopher E. Stith

        In File::Stat::Bits these (and some other values) are defined in a separate .ph file that is generated at install time. As mr_mischief points out it is probably preferable to have these as constants so that they aren't changed from underneath you, but using constant in a separate file is going to add another layer of complexity, when all it does really is create a subroutine just like the above - the extra stuff is just to put the subroutines in the right package and make sure you don't overwrite some important symbols.

        /J\