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