Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A friend of mine gave me the below code today and tells me it is exhibiting some strange behaviour. He is using sysopen to modify a passwd file to edit it in place. His hope is that should a user need to read it, he isn't going to clobber it while they're reading it. I'm not sure how much traffic is involved here, but he is concerned about it, so I didn't ask any questions.
#!/usr/local/bin/perl use strict; use warnings; use Fcntl qw(:DEFAULT :flock); # sysopen(PASSWD, "./passwd", O_RDWR) open (PASSWD, "+>>./passwd") or die "can't open passwd file ($!)"; flock(PASSWD, LOCK_EX); # or die "can't get lock on passwd file ($!)"; my @temp; foreach my $readLine (PASSWD) { chomp $readline; my ( $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell ) = split /:/, $readLine; if ($name eq "www") { $shell =~ s[/bin/bash][/dev/null]; } my $line = join ':', ($name,$passwd,$uid,$gid,$quota,$comment,$gcos, +$dir,$shell); push @temp, $line; } truncate (PASSWD, 0); foreach my $user (@temp) { print PASSWD "$user\n"; } close (PASSWD);
He says that what it eventually does is obliterate the file and replace it with the text
PASSWD::::::::
This strikes me as rather bizarre. Now, this is on linux on x86, and perl 5.5. I suggested he use File::Slurp (/me bows down and worships File::Slurp). However, this script needs to be deployed to > 4000 machines, and installing modules is simply not an option (I have entertained the possibility of just taking the code from the module and putting it in the script, that seems reasonable).

I'm concerned because I can't think of any reason why it would be doing this. Any ideas, Monks?

el dep mas fina

--
Laziness, Impatience, Hubris, and Generosity.


In reply to Questions with sysopen (code) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found