18 |
# Written by Akim Demaille <akim@freefriends.org>. |
# Written by Akim Demaille <akim@freefriends.org>. |
19 |
|
|
20 |
############################################################### |
############################################################### |
21 |
# The main copy of this file is in Autoconf's CVS repository. # |
# The main copy of this file is in Automake's CVS repository. # |
22 |
# Updates should be sent to autoconf-patches@gnu.org. # |
# Updates should be sent to automake-patches@gnu.org. # |
23 |
############################################################### |
############################################################### |
24 |
|
|
25 |
package Autom4te::XFile; |
package Autom4te::XFile; |
33 |
use Autom4te::XFile; |
use Autom4te::XFile; |
34 |
|
|
35 |
$fh = new Autom4te::XFile; |
$fh = new Autom4te::XFile; |
36 |
$fh->open("< file")) |
$fh->open ("< file"); |
37 |
# No need to check $FH: we died if open failed. |
# No need to check $FH: we died if open failed. |
38 |
print <$fh>; |
print <$fh>; |
39 |
$fh->close; |
$fh->close; |
50 |
print <$fh>; |
print <$fh>; |
51 |
undef $fh; # automatically closes the file and checks for errors. |
undef $fh; # automatically closes the file and checks for errors. |
52 |
|
|
53 |
$fh = new Autom4te::XFile "file", O_WRONLY|O_APPEND; |
$fh = new Autom4te::XFile "file", O_WRONLY | O_APPEND; |
54 |
# No need to check $FH: we died if new failed. |
# No need to check $FH: we died if new failed. |
55 |
print $fh "corge\n"; |
print $fh "corge\n"; |
56 |
|
|
57 |
$pos = $fh->getpos; |
$pos = $fh->getpos; |
58 |
$fh->setpos($pos); |
$fh->setpos ($pos); |
59 |
|
|
60 |
undef $fh; # automatically closes the file and checks for errors. |
undef $fh; # automatically closes the file and checks for errors. |
61 |
|
|
89 |
use Carp; |
use Carp; |
90 |
use IO::File; |
use IO::File; |
91 |
use File::Basename; |
use File::Basename; |
92 |
|
use Autom4te::ChannelDefs; |
93 |
|
use Autom4te::FileUtils; |
94 |
|
|
95 |
require Exporter; |
require Exporter; |
96 |
require DynaLoader; |
require DynaLoader; |
119 |
sub new |
sub new |
120 |
{ |
{ |
121 |
my $type = shift; |
my $type = shift; |
122 |
my $class = ref($type) || $type || "Autom4te::XFile"; |
my $class = ref $type || $type || "Autom4te::XFile"; |
123 |
my $fh = $class->SUPER::new (); |
my $fh = $class->SUPER::new (); |
124 |
if (@_) |
if (@_) |
125 |
{ |
{ |
134 |
|
|
135 |
sub open |
sub open |
136 |
{ |
{ |
137 |
my ($fh) = shift; |
my $fh = shift; |
138 |
my ($file) = @_; |
my ($file) = @_; |
139 |
|
|
140 |
# WARNING: Gross hack: $FH is a typeglob: use its hash slot to store |
# WARNING: Gross hack: $FH is a typeglob: use its hash slot to store |
145 |
|
|
146 |
if (!$fh->SUPER::open (@_)) |
if (!$fh->SUPER::open (@_)) |
147 |
{ |
{ |
148 |
croak "$me: cannot open $file: $!\n"; |
fatal "cannot open $file: $!"; |
149 |
} |
} |
150 |
|
|
151 |
# In case we're running under MSWindows, don't write with CRLF. |
# In case we're running under MSWindows, don't write with CRLF. |
161 |
|
|
162 |
sub close |
sub close |
163 |
{ |
{ |
164 |
my ($fh) = shift; |
my $fh = shift; |
165 |
if (!$fh->SUPER::close (@_)) |
if (!$fh->SUPER::close (@_)) |
166 |
{ |
{ |
167 |
my $file = $fh->name; |
my $file = $fh->name; |
168 |
croak "$me: cannot close $file: $!\n"; |
Autom4te::FileUtils::handle_exec_errors $file |
169 |
|
unless $!; |
170 |
|
fatal "cannot close $file: $!"; |
171 |
} |
} |
172 |
} |
} |
173 |
|
|
204 |
|
|
205 |
sub name |
sub name |
206 |
{ |
{ |
207 |
my ($fh) = shift; |
my $fh = shift; |
208 |
return ${*$fh}{'autom4te_xfile_file'}; |
return ${*$fh}{'autom4te_xfile_file'}; |
209 |
} |
} |
210 |
|
|
219 |
if (!flock ($fh, $mode)) |
if (!flock ($fh, $mode)) |
220 |
{ |
{ |
221 |
my $file = $fh->name; |
my $file = $fh->name; |
222 |
croak "$me: cannot lock $file with mode $mode: $!\n"; |
fatal "cannot lock $file with mode $mode: $!"; |
223 |
} |
} |
224 |
} |
} |
225 |
|
|
229 |
|
|
230 |
sub seek |
sub seek |
231 |
{ |
{ |
232 |
my ($fh) = shift; |
my $fh = shift; |
233 |
# Cannot use @_ here. |
# Cannot use @_ here. |
234 |
if (!seek ($fh, $_[0], $_[1])) |
if (!seek ($fh, $_[0], $_[1])) |
235 |
{ |
{ |
236 |
my $file = $fh->name; |
my $file = $fh->name; |
237 |
croak "$me: cannot rewind $file with @_: $!\n"; |
fatal "$me: cannot rewind $file with @_: $!"; |
238 |
} |
} |
239 |
} |
} |
240 |
|
|
248 |
if (!truncate ($fh, $len)) |
if (!truncate ($fh, $len)) |
249 |
{ |
{ |
250 |
my $file = $fh->name; |
my $file = $fh->name; |
251 |
croak "$me: cannot truncate $file at $len: $!\n"; |
fatal "cannot truncate $file at $len: $!"; |
252 |
} |
} |
253 |
} |
} |
254 |
|
|