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.
10 use UNIVERSAL qw(isa);
14 @EXPORT_OK=qw(copy cp);
16 $File::Copy::VERSION = '1.5';
17 $File::Copy::Too_Big = 1024 * 1024 * 2;
20 # Version of File::Copy
21 return $File::Copy::VERSION;
25 croak("Usage: copy( file1, file2 [, buffersize]) ")
26 unless(@_ == 2 || @_ == 3);
28 if (defined &File::Copy::syscopy &&
29 \&File::Copy::syscopy != \&File::Copy::copy &&
30 ref(\$_[1]) ne 'GLOB' &&
31 !(defined ref $_[1] and isa($_[1], 'GLOB')))
32 { return File::Copy::syscopy($_[0],$_[1]) }
38 my ($size, $status, $r, $buf);
42 if (ref(\$from) eq 'GLOB') {
44 } elsif (defined ref $from and
45 (ref($from) eq 'GLOB' || ref($from) eq 'FileHandle' ||
46 ref($from) eq 'VMS::Stdio')) {
49 open(FROM,"<$from")||goto(fail_open1);
54 if (ref(\$to) eq 'GLOB') {
56 } elsif (defined ref $to and
57 (ref($to) eq 'GLOB' || ref($to) eq 'FileHandle' ||
58 ref($to) eq 'VMS::Stdio')) {
61 open(TO,">$to")||goto(fail_open2);
67 $size = shift(@_) + 0;
68 croak("Bad buffer size for copy: $size\n") unless ($size > 0);
71 $size = 1024 if ($size < 512);
72 $size = $File::Copy::Too_Big if ($size > $File::Copy::Too_Big);
76 while(defined($r = read(FROM,$buf,$size)) && $r > 0) {
77 if (syswrite (TO,$buf,$r) != $r) {
81 goto fail_inner unless(defined($r));
82 close(TO) || goto fail_open2 if $closeto;
83 close(FROM) || goto fail_open1 if $closefrom;
84 # Use this idiom to avoid uninitialized value warning.
87 # All of these contortions try to preserve error messages...
93 $! = $status unless $!;
100 $! = $status unless $!;
108 # &syscopy is an XSUB under OS/2
109 *syscopy = ($^O eq 'VMS' ? \&rmscopy : \©) unless $^O eq 'os2';
117 File::Copy - Copy files or filehandles
123 copy("file1","file2");
124 copy("Copy.pm",\*STDOUT);'
129 $n=FileHandle->new("/dev/null","r");
134 The File::Copy module provides a basic function C<copy> which takes two
135 parameters: a file to copy from and a file to copy to. Either
136 argument may be a string, a FileHandle reference or a FileHandle
137 glob. Obviously, if the first argument is a filehandle of some
138 sort, it will be read from, and if it is a file I<name> it will
139 be opened for reading. Likewise, the second argument will be
140 written to (and created if need be). Note that passing in
141 files as handles instead of names may lead to loss of information
142 on some operating systems; it is recommended that you use file
143 names whenever possible.
145 An optional third parameter can be used to specify the buffer
146 size used for copying. This is the number of bytes from the
147 first file, that wil be held in memory at any given time, before
148 being written to the second file. The default buffer size depends
149 upon the file, but will generally be the whole file (up to 2Mb), or
150 1k for filehandles that do not reference files (eg. sockets).
152 You may use the syntax C<use File::Copy "cp"> to get at the
153 "cp" alias for this function. The syntax is I<exactly> the same.
155 File::Copy also provides the C<syscopy> routine, which copies the
156 file specified in the first parameter to the file specified in the
157 second parameter, preserving OS-specific attributes and file
158 structure. For Unix systems, this is equivalent to the simple
159 C<copy> routine. For VMS systems, this calls the C<rmscopy>
160 routine (see below). For OS/2 systems, this calls the C<syscopy>
163 =head2 Special behavior if C<syscopy> is defined (VMS and OS/2)
165 If the second argument to C<copy> is not a file handle for an
166 already opened file, then C<copy> will perform an "system copy" of
167 the input file to a new output file, in order to preserve file
168 attributes, indexed file structure, I<etc.> The buffer size
169 parameter is ignored. If the second argument to C<copy> is a
170 Perl handle to an opened file, then data is copied using Perl
171 operators, and no effort is made to preserve file attributes
174 The system copy routine may also be called directly under VMS and OS/2
175 as C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
176 is just an alias for this routine).
180 =item rmscopy($from,$to[,$date_flag])
182 The first and second arguments may be strings, typeglobs, or
183 typeglob references; they are used in all cases to obtain the
184 I<filespec> of the input and output files, respectively. The
185 name and type of the input file are used as defaults for the
186 output file, if necessary.
188 A new version of the output file is always created, which
189 inherits the structure and RMS attributes of the input file,
190 except for owner and protections (and possibly timestamps;
191 see below). All data from the input file is copied to the
192 output file; if either of the first two parameters to C<rmscopy>
193 is a file handle, its position is unchanged. (Note that this
194 means a file handle pointing to the output file will be
195 associated with an old version of that file after C<rmscopy>
196 returns, not the newly created version.)
198 The third parameter is an integer flag, which tells C<rmscopy>
199 how to handle timestamps. If it is E<lt> 0, none of the input file's
200 timestamps are propagated to the output file. If it is E<gt> 0, then
201 it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
202 timestamps other than the revision date are propagated; if bit 1
203 is set, the revision date is propagated. If the third parameter
204 to C<rmscopy> is 0, then it behaves much like the DCL COPY command:
205 if the name or type of the output file was explicitly specified,
206 then no timestamps are propagated, but if they were taken implicitly
207 from the input filespec, then all timestamps other than the
208 revision date are propagated. If this parameter is not supplied,
211 Like C<copy>, C<rmscopy> returns 1 on success. If an error occurs,
212 it sets C<$!>, deletes the output file, and returns 0.
218 Returns 1 on success, 0 on failure. $! will be set if an error was
223 File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995.
224 The VMS-specific code was added by Charles Bailey
225 I<E<lt>bailey@genetics.upenn.eduE<gt>> in March 1996.