]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/copyfile.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / copyfile.m
1 ## Copyright (C) 2005-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2})
21 ## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, 'f')
22 ## Copy the file @var{f1} to the new name @var{f2}.  The name @var{f1}
23 ## may contain globbing patterns.  If @var{f1} expands to multiple file
24 ## names, @var{f2} must be a directory.  If the force flag 'f' is given then
25 ## existing destination files will be overwritten without prompting.
26 ##
27 ## If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty
28 ## character strings.  Otherwise, @var{status} is 0, @var{msg} contains a
29 ## system-dependent error message, and @var{msgid} contains a unique
30 ## message identifier.
31 ## @seealso{movefile}
32 ## @end deftypefn
33
34 function [status, msg, msgid] = copyfile (f1, f2, force)
35
36   max_cmd_line = 1024;
37   status = true;
38   msg = "";
39   msgid = "";
40
41   ## FIXME -- maybe use the same method as in ls to allow users control
42   ## over the command that is executed.
43
44   if (ispc () && ! isunix ()
45       && isempty (file_in_path (getenv ("PATH"), "cp.exe")))
46     ## Windows.
47     cmd = "cmd /C xcopy /E";
48     cmd_force_flag = "/Y";
49   else
50     cmd = "cp -r";
51     cmd_force_flag = "-f";
52   endif
53
54   if (nargin == 2 || nargin == 3)
55     ## Input type check.
56     if (! (ischar (f1) || iscellstr (f1)))
57       error ("copyfile: first argument must be a character string or a cell array of character strings");
58     endif
59
60     if (! ischar (f2))
61       error ("copyfile: second argument must be a character string");
62     endif
63
64     if (nargin == 3 && strcmp (force, "f"))
65       cmd = cstrcat (cmd, " ", cmd_force_flag);
66     endif
67
68     ## If f1 isn't a cellstr convert it to one.
69     if (ischar (f1))
70       f1 = cellstr (f1);
71     endif
72
73     ## If f1 has more than 1 element f2 must be a directory
74     isdir = (exist (f2, "dir") != 0);
75     if (length(f1) > 1 && ! isdir)
76       error ("copyfile: when copying multiple files, second argument must be a directory");
77     endif
78
79     ## Protect the file name(s).
80     f1 = glob (f1);
81     if (isempty (f1))
82       error ("copyfile: no files to move");
83     endif
84     p1 = sprintf ("\"%s\" ", f1{:});
85     p2 = tilde_expand (f2);
86
87     if (isdir && length(p1) > max_cmd_line)
88       l2 = length(p2) + length (cmd) + 6;
89       while (! isempty(f1))
90         p1 = sprintf ("\"%s\" ", f1{1});
91         f1(1) = [];
92         while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 <
93                                  max_cmd_line))
94           p1 = sprintf ("%s\"%s\" ", p1, f1{1});
95           f1(1) = [];
96         endwhile
97
98         if (ispc () && ! isunix ()
99             && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
100           p1 = strrep (p1, "\\", "/");
101           p2 = strrep (p2, "\\", "/");
102         endif
103
104         ## Copy the files.
105         [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2));
106         if (err < 0)
107           status = false;
108           msgid = "copyfile";
109           break;
110         endif
111       endwhile
112     else
113       if (ispc () && ! isunix ()
114           && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
115         p1 = strrep (p1, "\\", "/");
116         p2 = strrep (p2, "\\", "/");
117       endif
118
119       ## Copy the files.
120       [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2));
121       if (err < 0)
122         status = false;
123         msgid = "copyfile";
124       endif
125     endif
126   else
127     print_usage ();
128   endif
129
130 endfunction