perl 5.003_06: lib/File/Basename.pm
[p5sagit/p5-mst-13.2.git] / lib / File / Copy.pm
CommitLineData
f716a1dd 1# File/Copy.pm. Written in 1994 by Aaron Sherman <ajs@ajs.com>. This
2# source code has been placed in the public domain by the author.
3# Please be kind and preserve the documentation.
4#
5
6package File::Copy;
7
8require Exporter;
9use Carp;
10
11@ISA=qw(Exporter);
12@EXPORT=qw(copy);
13@EXPORT_OK=qw(copy cp);
14
15$File::Copy::VERSION = '1.5';
16$File::Copy::Too_Big = 1024 * 1024 * 2;
17
18sub VERSION {
19 # Version of File::Copy
20 return $File::Copy::VERSION;
21}
22
23sub copy {
24 croak("Usage: copy( file1, file2 [, buffersize]) ")
25 unless(@_ == 2 || @_ == 3);
26
9b957b78 27 if (($^O eq 'VMS' or $^O eq 'os2') && ref(\$to) ne 'GLOB' &&
28 !(defined ref $to and (ref($to) eq 'GLOB' ||
29 ref($to) eq 'FileHandle' || ref($to) eq 'VMS::Stdio')))
30 { return File::Copy::syscopy($_[0],$_[1]) }
a5f75d66 31
f716a1dd 32 my $from = shift;
33 my $to = shift;
f716a1dd 34 my $closefrom=0;
35 my $closeto=0;
36 my ($size, $status, $r, $buf);
37 local(*FROM, *TO);
48a5c399 38 local($\) = '';
f716a1dd 39
40 if (ref(\$from) eq 'GLOB') {
41 *FROM = $from;
42 } elsif (defined ref $from and
9b957b78 43 (ref($from) eq 'GLOB' || ref($from) eq 'FileHandle' ||
44 ref($from) eq 'VMS::Stdio')) {
f716a1dd 45 *FROM = *$from;
46 } else {
47 open(FROM,"<$from")||goto(fail_open1);
9b957b78 48 binmode FROM;
f716a1dd 49 $closefrom = 1;
50 }
51
52 if (ref(\$to) eq 'GLOB') {
53 *TO = $to;
54 } elsif (defined ref $to and
9b957b78 55 (ref($to) eq 'GLOB' || ref($to) eq 'FileHandle' ||
56 ref($to) eq 'VMS::Stdio')) {
f716a1dd 57 *TO = *$to;
58 } else {
59 open(TO,">$to")||goto(fail_open2);
9b957b78 60 binmode TO;
f716a1dd 61 $closeto=1;
62 }
63
64 if (@_) {
65 $size = shift(@_) + 0;
66 croak("Bad buffer size for copy: $size\n") unless ($size > 0);
67 } else {
68 $size = -s FROM;
69 $size = 1024 if ($size < 512);
70 $size = $File::Copy::Too_Big if ($size > $File::Copy::Too_Big);
71 }
72
73 $buf = '';
74 while(defined($r = read(FROM,$buf,$size)) && $r > 0) {
75 if (syswrite (TO,$buf,$r) != $r) {
76 goto fail_inner;
77 }
78 }
79 goto fail_inner unless(defined($r));
80 close(TO) || goto fail_open2 if $closeto;
81 close(FROM) || goto fail_open1 if $closefrom;
48a5c399 82 # Use this idiom to avoid uninitialized value warning.
f716a1dd 83 return 1;
84
85 # All of these contortions try to preserve error messages...
86 fail_inner:
87 if ($closeto) {
88 $status = $!;
89 $! = 0;
90 close TO;
91 $! = $status unless $!;
92 }
93 fail_open2:
94 if ($closefrom) {
95 $status = $!;
96 $! = 0;
97 close FROM;
98 $! = $status unless $!;
99 }
100 fail_open1:
f716a1dd 101 return 0;
102}
9b957b78 103
104
f716a1dd 105*cp = \&copy;
9b957b78 106# &syscopy is an XSUB under OS/2
107*syscopy = ($^O eq 'VMS' ? \&rmscopy : \&copy) unless $^O eq 'os2';
f716a1dd 108
1091;
110
111__END__
a5f75d66 112
f716a1dd 113=head1 NAME
114
115File::Copy - Copy files or filehandles
116
a5f75d66 117=head1 SYNOPSIS
f716a1dd 118
119 use File::Copy;
120
121 copy("file1","file2");
122 copy("Copy.pm",\*STDOUT);'
123
124 use POSIX;
125 use File::Copy cp;
126
127 $n=FileHandle->new("/dev/null","r");
128 cp($n,"x");'
129
130=head1 DESCRIPTION
131
9b957b78 132The File::Copy module provides a basic function C<copy> which takes two
f716a1dd 133parameters: a file to copy from and a file to copy to. Either
134argument may be a string, a FileHandle reference or a FileHandle
135glob. Obviously, if the first argument is a filehandle of some
136sort, it will be read from, and if it is a file I<name> it will
137be opened for reading. Likewise, the second argument will be
9b957b78 138written to (and created if need be). Note that passing in
139files as handles instead of names may lead to loss of information
140on some operating systems; it is recommended that you use file
141names whenever possible.
f716a1dd 142
143An optional third parameter can be used to specify the buffer
144size used for copying. This is the number of bytes from the
145first file, that wil be held in memory at any given time, before
146being written to the second file. The default buffer size depends
147upon the file, but will generally be the whole file (up to 2Mb), or
1481k for filehandles that do not reference files (eg. sockets).
149
150You may use the syntax C<use File::Copy "cp"> to get at the
151"cp" alias for this function. The syntax is I<exactly> the same.
152
9b957b78 153File::Copy also provides the C<syscopy> routine, which copies the
154file specified in the first parameter to the file specified in the
155second parameter, preserving OS-specific attributes and file
156structure. For Unix systems, this is equivalent to the simple
157C<copy> routine. For VMS systems, this calls the C<rmscopy>
158routine (see below). For OS/2 systems, this calls the C<syscopy>
159XSUB directly.
160
161=head2 Special behavior under VMS
162
163If the second argument to C<copy> is not a file handle for an
164already opened file, then C<copy> will perform an RMS copy of
165the input file to a new output file, in order to preserve file
166attributes, indexed file structure, I<etc.> The buffer size
167parameter is ignored. If the second argument to C<copy> is a
168Perl handle to an opened file, then data is copied using Perl
169operators, and no effort is made to preserve file attributes
170or record structure.
171
172The RMS copy routine may also be called directly under VMS
173as C<File::Copy::rmscopy> (or C<File::Copy::syscopy>, which
174is just an alias for this routine).
175
176=item rmscopy($from,$to[,$date_flag])
177
178The first and second arguments may be strings, typeglobs, or
179typeglob references; they are used in all cases to obtain the
180I<filespec> of the input and output files, respectively. The
181name and type of the input file are used as defaults for the
182output file, if necessary.
183
184A new version of the output file is always created, which
185inherits the structure and RMS attributes of the input file,
186except for owner and protections (and possibly timestamps;
187see below). All data from the input file is copied to the
188output file; if either of the first two parameters to C<rmscopy>
189is a file handle, its position is unchanged. (Note that this
190means a file handle pointing to the output file will be
191associated with an old version of that file after C<rmscopy>
192returns, not the newly created version.)
193
194The third parameter is an integer flag, which tells C<rmscopy>
1fef88e7 195how to handle timestamps. If it is E<lt> 0, none of the input file's
196timestamps are propagated to the output file. If it is E<gt> 0, then
9b957b78 197it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
198timestamps other than the revision date are propagated; if bit 1
199is set, the revision date is propagated. If the third parameter
200to C<rmscopy> is 0, then it behaves much like the DCL COPY command:
201if the name or type of the output file was explicitly specified,
202then no timestamps are propagated, but if they were taken implicitly
203from the input filespec, then all timestamps other than the
204revision date are propagated. If this parameter is not supplied,
205it defaults to 0.
206
207Like C<copy>, C<rmscopy> returns 1 on success. If an error occurs,
208it sets C<$!>, deletes the output file, and returns 0.
209
f716a1dd 210=head1 RETURN
211
212Returns 1 on success, 0 on failure. $! will be set if an error was
213encountered.
214
215=head1 AUTHOR
216
9b957b78 217File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995.
218The VMS-specific code was added by Charles Bailey
219I<E<lt>bailey@genetics.upenn.eduE<gt>> in March 1996.
f716a1dd 220
221=cut