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

I am playing around with a possible improvement to the File::Slurp module that I would eventually submit to the author, but right now, I am running into a strange problem.

This is the piece of code that I am adding to the module:
sub read_long_dir{ my($d)=$_; my(@r); my $stats; my@r1=read_dir($d); foreach my $file(@r1){ my @stats=stat("$d/$file"); my($mode,$uid,$gid,$size,$atime)=@stats[2,4,5,7,8]; $uid=getpwuid($uid); $gid=getgrgid($gid); $atime=scalar localtime($atime); $mode=sprintf"%04o",$mode & 07777; $stats="$mode $uid $gid $size $atime $file"; push @r,$stats; } return @r; }
When I run the test program that I have as root, it runs fine, however, if I run the test as myself, it coredumps. Do the getgrgid/getpwuid functions need to be run as root? Any help would be appreciated.

TStanley
--------
"Suppose you were an idiot... And suppose you were a
member of Congress... But I repeat myself." -- Mark Twain

Replies are listed 'Best First'.
Re: Module addition
by Beatnik (Parson) on Jan 04, 2002 at 04:13 UTC
    It could be the lack of sleep but sub parameters are in @_ which makes it rather hard to address them thru $_, as you do in your code.
    This has very little to do with the core dumps. If some function requires you to have root access, I doubt it will core dump on you. Are you using a stable release? ancient release?

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Module addition
by frag (Hermit) on Jan 04, 2002 at 04:20 UTC
    No, they shouldn't require root. Unless /etc/group is set to be only readable by root.

    -- Frag.
    --
    "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

Re: Module addition
by TStanley (Canon) on Jan 04, 2002 at 17:14 UTC
    I checked the version on my system against the one out on CPAN, and they are the same. And the permissions for both /etc/passwd and /etc/group are 444 (read permission for everyone)

    UPDATE: After inserting some print statements into the module, I determined that the "Bus error (coredump)" is occuring when I get the user id with the getpwuid function. Any suggestions??

    FINAL UPDATE: After going over the section of the Camel concerning the getpw* commmands, I noticed this little snippet:
    "On systems that support shadow passwords, you will have to be the super user to retrieve the actual password"
    I also tried using the User::pwent module, but with no success. So in the end, I had to settle for the following snippet to get the user name:
    my $cmd=`grep $uid /etc/passwd`; my ($name,$rest)=split /:/,$cmd;


    TStanley
    --------
    "Suppose you were an idiot... And suppose you were a
    member of Congress... But I repeat myself." -- Mark Twain