~gabriel1984sibiu/octave/octave

« back to all changes in this revision

Viewing changes to scripts/miscellaneous/copyfile.m

  • Committer: Grevutiu Gabriel
  • Date: 2014-01-02 13:05:54 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20140102130554-3r7ivdjln1ni6kcg
New version (3.8.0) from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2005-2013 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 destination @var{f2}.
 
23
##
 
24
## The name @var{f1} may contain globbing patterns.  If @var{f1} expands to
 
25
## multiple file names, @var{f2} must be a directory.
 
26
## when the force flag @qcode{'f'} is given any existing files will be
 
27
## overwritten without prompting.
 
28
##
 
29
## If successful, @var{status} is 1, and @var{msg}, @var{msgid} are empty
 
30
## character strings ("").  Otherwise, @var{status} is 0, @var{msg} contains a
 
31
## system-dependent error message, and @var{msgid} contains a unique message
 
32
## identifier.  Note that the status code is exactly opposite that of the
 
33
## @code{system} command.
 
34
## @seealso{movefile, rename, unlink, delete, glob}
 
35
## @end deftypefn
 
36
 
 
37
function [status, msg, msgid] = copyfile (f1, f2, force)
 
38
 
 
39
  if (nargin < 2 || nargin > 3)
 
40
    print_usage ();
 
41
  endif
 
42
 
 
43
  max_cmd_line = 1024;
 
44
  status = true;
 
45
  msg = "";
 
46
  msgid = "";
 
47
 
 
48
  ## FIXME: maybe use the same method as in ls to allow users control
 
49
  ## over the command that is executed.
 
50
 
 
51
  if (ispc () && ! isunix ()
 
52
      && isempty (file_in_path (getenv ("PATH"), "cp.exe")))
 
53
    ## Windows.
 
54
    cmd = "cmd /C xcopy /E";
 
55
    cmd_force_flag = "/Y";
 
56
  else
 
57
    cmd = "cp -r";
 
58
    cmd_force_flag = "-f";
 
59
  endif
 
60
 
 
61
  ## Input type check.
 
62
  if (! (ischar (f1) || iscellstr (f1)))
 
63
    error ("copyfile: F1 must be a character string or a cell array of character strings");
 
64
  endif
 
65
 
 
66
  if (! ischar (f2))
 
67
    error ("copyfile: F2 must be a character string");
 
68
  endif
 
69
 
 
70
  if (nargin == 3 && strcmp (force, "f"))
 
71
    cmd = [cmd " " cmd_force_flag];
 
72
  endif
 
73
 
 
74
  ## If f1 isn't a cellstr convert it to one.
 
75
  if (ischar (f1))
 
76
    f1 = cellstr (f1);
 
77
  endif
 
78
 
 
79
  ## If f1 has more than 1 element f2 must be a directory
 
80
  isdir = (exist (f2, "dir") != 0);
 
81
  if (length (f1) > 1 && ! isdir)
 
82
    error ("copyfile: when copying multiple files, F2 must be a directory");
 
83
  endif
 
84
 
 
85
  ## Protect the file name(s).
 
86
  f1 = glob (f1);
 
87
  if (isempty (f1))
 
88
    error ("copyfile: no files to move");
 
89
  endif
 
90
  p1 = sprintf ('"%s" ', f1{:});
 
91
  p2 = tilde_expand (f2);
 
92
 
 
93
  if (isdir && length (p1) > max_cmd_line)
 
94
    l2 = length (p2) + length (cmd) + 6;
 
95
    while (! isempty (f1))
 
96
      p1 = sprintf ('"%s" ', f1{1});
 
97
      f1(1) = [];
 
98
      while (! isempty (f1)
 
99
             && (length (p1) + length (f1{1}) + l2 < max_cmd_line))
 
100
        p1 = sprintf ('%s"%s" ', p1, f1{1});
 
101
        f1(1) = [];
 
102
      endwhile
 
103
 
 
104
      if (ispc () && ! isunix ()
 
105
          && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
 
106
        p1 = strrep (p1, '\', '/');
 
107
        p2 = strrep (p2, '\', '/');
 
108
      endif
 
109
 
 
110
      ## Copy the files.
 
111
      [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));
 
112
      if (err != 0)
 
113
        status = false;
 
114
        msgid = "copyfile";
 
115
        break;
 
116
      endif
 
117
    endwhile
 
118
  else
 
119
    if (ispc () && ! isunix ()
 
120
        && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
 
121
      p1 = strrep (p1, '\', '/');
 
122
      p2 = strrep (p2, '\', '/');
 
123
    endif
 
124
 
 
125
    ## Copy the files.
 
126
    [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));
 
127
    if (err != 0)
 
128
      status = false;
 
129
      msgid = "copyfile";
 
130
    endif
 
131
  endif
 
132
 
 
133
endfunction
 
134