Article View: de.comp.lang.perl
Article #14Re: C open function in perl (lockfile)
From: Tom Christiansen
Date: Thu, 02 Nov 1995 00:00
Date: Thu, 02 Nov 1995 00:00
95 lines
3378 bytes
3378 bytes
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
pvr@korfu.igd.fhg.de (Reiners Dirk) writes:
:Can I emulate/call the normal C open function in/from perl.
:I want to translate the following code to perl:
:
:st=open("foo",O_CREAT|O_EXCL,0777)
:
:for writing a lockfile function (some machines here don't support flock :-(
:
:I have already a solution like
:
:syscall(&SYS_open, "foo", O_CREAT|O_EXCL, 0777);
:
:but there seems to be no such function in sys/syscall.ph.
:I don't know if this is normal.
:(e.g.:
:IRIX 5.3 no SYS_open in syscall.ph
:SunOS 4.1.3 no syscall.ph at all
:)
:
:And I want the solution to work on both platform :-)
:Does anyone have an idea ?
I agree that this is not the best situation, but I *KNOW* that
SunOS has a <syscall.h>, and wonder whether Irix does. Check
with igrep:
$ igrep open /usr/include/syscall.h
Meanwhile, we're thinking about a better solution. Seems stoopid
to have to write a dot-xs module to do this. It would be trivial
though.
Meanwhile, here's igrep...
--tom
#!/usr/local/bin/perl
# igrep - tchrist@perl.com
die "usage: $0 [-I path] pattern file ...\n" unless @ARGV > 1;
@ipath = ( '.', '/usr/include', '/sys/h');
while ($ARGV[0] =~ /^-I(.*)/) {
shift;
push(@ipath, $1 || shift);
}
$pattern = shift;
eval { /$pattern/ }; # test her
die "$0: $@" if $@ =~ s/ at \(eval\).*//;
for (@ARGV) { &process($_); }
exit (!$ok);
sub process {
local($fname) = @_;
local(*FH);
return if $seen{$fname};
unless ($fname =~ m#^/# ? open(FH, $fname) : &iopen(FH, $fname)) {
warn "can't open $fname: $! called from "
. join(" from ", reverse @in) . "\n";
$seen{$fname}++; # sigh
return;
}
push(@in, $fname);
unless($seen{$fname}++) {
while (<FH>) {
#print "$fname: $.: $_" if /$pattern/o;
print join(" from ", reverse @in), ": $.: $_"
if /$pattern/o;
$ok++;
next unless /^\s*#\s*include\s*(["<])([^">]+)\1/;
&process($2);
}
}
pop @in;
close(FH);
}
sub iopen {
local($fh, $path) = @_;
local($mypath) = $in[$#in]; # cpp extra inc path
$mypath = '' unless $mypath =~ s#/[^/]*$##;
for $dir ($mypath, @ipath) {
next if $dir eq '';
next unless open ($fh, "$dir/$path");
$_[1] = "$dir/$path"; # clobber caller ref for better msgs
return 1;
}
return 0;
}
--
Tom Christiansen Perl Consultant, Gamer, Hiker tchrist@mox.perl.com
Yield to Temptation ... it may not pass your way again.
--Lazarus Long, "Time Enough for Love"
Technical Details
Message-ID:
<47asja$728@csnews.cs.colorado.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4786us$a0f@korfu.igd.fhg.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1043e7,5ddc5a1c50ff66cd X-Google-Attributes: gid1043e7,public X-Google-Thread: 1133f4,5ddc5a1c50ff66cd X-Google-Attributes: gid1133f4,public X-Google-Thread: fa0ae,5ddc5a1c50ff66cd X-Google-Attributes: gidfa0ae,public From: Tom Christiansen <tchrist@mox.perl.com> Subject: Re: C open function in perl (lockfile) Date: 1995/11/02 Message-ID: <47asja$728@csnews.cs.colorado.edu> X-Deja-AN: 118463936 references: <4786us$a0f@korfu.igd.fhg.de> organization: Perl Consulting and Training reply-to: tchrist@mox.perl.com (Tom Christiansen) newsgroups: de.comp.lang.perl,fj.lang.perl,comp.lang.perl.misc originator: tchrist@mox