in reply to Meaning of stat's 'rdev' value.

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\

Replies are listed 'Best First'.
Re^2: Meaning of stat's 'rdev' value.
by palamedes (Novice) on Sep 06, 2006 at 14:51 UTC
    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

Re^2: Meaning of stat's 'rdev' value.
by stonecolddevin (Parson) on Sep 06, 2006 at 15:09 UTC

    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\