annotate scripts/miscellaneous/copyfile.m @ 30241:ba4aebad10d1

Return logical status variable from file functions for Matlab compatibility. * NEWS: Announce change. * dirfns.cc (Frmdir): Update documentation. Change return status to true/false rather than 1.0/0.0. * copyfile.m: Update documentation. Change internal variable "sts" to logical variable. Change any assignments to status to true/false. * movefile.m: Update documentation. Change internal variable "sts" to logical variable. Change any assignments to status to true/false. * mkdir.m: Update documentation. * mkdir.m (mkdir_recur): Change internal variable "status" to logical.
author Rik <rik@octave.org>
date Wed, 13 Oct 2021 10:36:20 -0700
parents 7854d5752dd2
children 796f54d4ddbf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
3 ## Copyright (C) 2005-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
7 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
14 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
19 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6679
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
25
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
25040
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
27 ## @deftypefn {} {} copyfile @var{f1} @var{f2}
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
28 ## @deftypefnx {} {} copyfile @var{f1} @var{f2} f
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
29 ## @deftypefnx {} {} copyfile (@var{f1}, @var{f2})
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
30 ## @deftypefnx {} {} copyfile (@var{f1}, @var{f2}, 'f')
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
31 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@dots{})
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
32 ## Copy the source file(s) or directory @var{f1} to the destination @var{f2}.
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
33 ##
25040
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
34 ## The name @var{f1} may contain globbing patterns, or may be a cell array of
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
35 ## strings. If @var{f1} expands to multiple filenames, @var{f2} must be a
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
36 ## directory.
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ##
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
38 ## When the force flag @qcode{'f'} is given any existing files will be
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
39 ## overwritten without prompting.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
40 ##
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
41 ## If successful, @var{status} is logical 1, and @var{msg}, @var{msgid} are
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
42 ## empty character strings (""). Otherwise, @var{status} is logical 0,
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
43 ## @var{msg} contains a system-dependent error message, and @var{msgid}
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
44 ## contains a unique message identifier. Note that the status code is exactly
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
45 ## opposite that of the @code{system} command.
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17394
diff changeset
46 ## @seealso{movefile, rename, unlink, delete, glob}
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
47 ## @end deftypefn
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
48
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
49 function [status, msg, msgid] = copyfile (f1, f2, force)
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
50
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28103
diff changeset
51 if (nargin < 2)
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
52 print_usage ();
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
53 endif
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
54
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
55 max_cmd_line = 1024;
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
56 sts = true;
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
57 msg = "";
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
58 msgid = "";
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
59
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
60 ## FIXME: Maybe use the same method as in ls to allow users control
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
61 ## over the command that is executed.
6210
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
62
11300
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
63 if (ispc () && ! isunix ()
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
64 && isempty (file_in_path (getenv ("PATH"), "cp.exe")))
6233
a299c8a6d96e [project @ 2007-01-09 04:31:18 by jwe]
jwe
parents: 6210
diff changeset
65 ## Windows.
6210
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
66 cmd = "cmd /C xcopy /E";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
67 cmd_force_flag = "/Y";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
68 else
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
69 cmd = "cp -r";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
70 cmd_force_flag = "-f";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
71 endif
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6152
diff changeset
72
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
73 ## Input type check.
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
74 if (ischar (f1))
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 f1 = cellstr (f1);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
76 elseif (! iscellstr (f1))
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 error ("copyfile: F1 must be a string or a cell array of strings");
19262
57147e909042 copyfile.m: Fix failing BIST test.
Rik <rik@octave.org>
parents: 19260
diff changeset
78 endif
57147e909042 copyfile.m: Fix failing BIST test.
Rik <rik@octave.org>
parents: 19260
diff changeset
79 if (! ischar (f2))
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
80 error ("copyfile: F2 must be a string");
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
81 endif
6233
a299c8a6d96e [project @ 2007-01-09 04:31:18 by jwe]
jwe
parents: 6210
diff changeset
82
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
83 if (nargin == 3 && strcmp (force, "f"))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
84 cmd = [cmd " " cmd_force_flag];
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
85 endif
6069
67b1a61a85ce [project @ 2006-10-21 14:33:53 by jwe]
jwe
parents: 6047
diff changeset
86
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
87 ## If f1 has more than 1 element then f2 must be a directory
25781
e04c56bbbace isdir.m: Make m-file a legacy function (bug #54489)
Rik <rik@octave.org>
parents: 25054
diff changeset
88 isdir = isfolder (f2);
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
89 if (numel (f1) > 1 && ! isdir)
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
90 if (nargout == 0)
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
91 error ("copyfile: when copying multiple files, F2 must be a directory");
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
92 else
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
93 status = false;
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
94 msg = "when copying multiple files, F2 must be a directory";
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
95 msgid = "copyfile";
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
96 return;
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
97 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
98 endif
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
99
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 19697
diff changeset
100 ## Protect the filename(s).
29093
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
101 if (ispc ())
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
102 f1 = __wglob__ (f1);
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
103 else
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
104 f1 = glob (f1);
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
105 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
106 if (isempty (f1))
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
107 if (nargout == 0)
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
108 error ("copyfile: no files to move");
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
109 else
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
110 status = false;
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
111 msg = "no files to move";
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
112 msgid = "copyfile";
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
113 return;
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
114 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
115 endif
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
116 p1 = sprintf ('"%s" ', f1{:});
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
117 p2 = tilde_expand (f2);
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
118
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
119 if (isdir && length (p1) > max_cmd_line)
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
120 l2 = length (p2) + length (cmd) + 6;
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
121 while (! isempty (f1))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
122 p1 = sprintf ('"%s" ', f1{1});
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
123 f1(1) = [];
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
124 while (! isempty (f1)
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
125 && (length (p1) + length (f1{1}) + l2 < max_cmd_line))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
126 p1 = sprintf ('%s"%s" ', p1, f1{1});
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
127 f1(1) = [];
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
128 endwhile
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
129
11300
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
130 if (ispc () && ! isunix ()
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
131 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
132 p1 = strrep (p1, '\', '/');
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
133 p2 = strrep (p2, '\', '/');
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
134 endif
6398
9bd49ed48ee6 [project @ 2007-03-08 19:40:01 by jwe]
jwe
parents: 6233
diff changeset
135
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
136 ## Copy the files.
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
137 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
138 if (err != 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
139 sts = false;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9245
diff changeset
140 msgid = "copyfile";
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
141 break;
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6645
diff changeset
142 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
143 endwhile
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
144 else
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
145 if (ispc () && ! isunix ()
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
146 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
147 p1 = strrep (p1, '\', '/');
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
148 p2 = strrep (p2, '\', '/');
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
149 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
150
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
151 ## Copy the files.
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
152 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
153 if (err != 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
154 sts = false;
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
155 msgid = "copyfile";
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
156 endif
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
157 endif
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
158
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
159 if (nargout == 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
160 if (! sts)
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
161 error ("copyfile: operation failed: %s", msg);
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
162 endif
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
163 else
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
164 status = sts;
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
165 endif
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
166
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
167 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
168
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
169
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
170 %!test
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
171 %! unwind_protect
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
172 %! f1 = tempname ();
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
173 %! tmp_var = pi;
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
174 %! save (f1, "tmp_var");
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
175 %! f2 = tempname ();
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
176 %! assert (copyfile (f1, f2));
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19262
diff changeset
177 %! assert (exist (f2, "file"));
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
178 %! fid = fopen (f1, "rb");
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
179 %! assert (fid >= 0);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
180 %! orig_data = fread (fid);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
181 %! fclose (fid);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
182 %! fid = fopen (f2, "rb");
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
183 %! assert (fid >= 0);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
184 %! new_data = fread (fid);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
185 %! fclose (fid);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
186 %! if (orig_data != new_data)
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
187 %! error ("copied file not equal to original file!");
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
188 %! endif
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
189 %! unwind_protect_cleanup
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
190 %! delete (f1);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
191 %! delete (f2);
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
192 %! end_unwind_protect
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
193
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
194 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28892
diff changeset
195 %!error <Invalid call> copyfile ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28892
diff changeset
196 %!error <Invalid call> copyfile (1)
19260
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
197 %!error <F1 must be a string> copyfile (1, "foobar")
08259ddc15a9 copyfile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
198 %!error <F2 must be a string> copyfile ("foobar", 1)
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19262
diff changeset
199 %!error <F2 must be a directory> copyfile ({"a", "b"}, "%_NOT_A_DIR_%")
28103
1cb3c33f97dc copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
200 %!error <no files to move> copyfile ("%_NOT_A_FILENAME1_%", "%_NOT_A_FILENAME2_%")