Mercurial > octave
annotate scripts/miscellaneous/movefile.m @ 25054:6652d3823428 stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Mar 2018 09:19:05 -0400 |
parents | 328518881291 |
children | e04c56bbbace |
rev | line source |
---|---|
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25040
diff
changeset
|
1 ## Copyright (C) 2005-2018 John W. Eaton |
6047 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
6047 | 6 ## 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
|
7 ## 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
|
8 ## (at your option) any later version. |
6047 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
6047 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## 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
|
17 ## <https://www.gnu.org/licenses/>. |
6047 | 18 |
19 ## -*- texinfo -*- | |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
20 ## @deftypefn {} {} movefile @var{f1} |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
21 ## @deftypefnx {} {} movefile @var{f1} @var{f2} |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
22 ## @deftypefnx {} {} movefile @var{f1} @var{f2} f |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
23 ## @deftypefnx {} {} movefile (@var{f1}) |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
24 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
25 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2}, 'f') |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
26 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@dots{}) |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
27 ## Move the source file or directory @var{f1} to the destination @var{f2}. |
6047 | 28 ## |
25040
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
29 ## 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
|
30 ## 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
|
31 ## directory. |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
32 ## |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
33 ## If no destination @var{f2} is specified then the destination is the present |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
34 ## working directory. If @var{f2} is a filename then @var{f1} is renamed to |
328518881291
Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
35 ## @var{f2}. |
19261 | 36 ## |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
37 ## When the force flag @qcode{'f'} is given any existing files will be |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
38 ## overwritten without prompting. |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
39 ## |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
40 ## If successful, @var{status} is 1, and @var{msg}, @var{msgid} are empty |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
41 ## character strings (""). Otherwise, @var{status} is 0, @var{msg} contains a |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
42 ## system-dependent error message, and @var{msgid} contains a unique message |
17514
5b916efea542
doc: spellcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
43 ## identifier. Note that the status code is exactly opposite that of the |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
44 ## @code{system} command. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17394
diff
changeset
|
45 ## @seealso{rename, copyfile, unlink, delete, glob} |
6047 | 46 ## @end deftypefn |
47 | |
48 function [status, msg, msgid] = movefile (f1, f2, force) | |
49 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
50 if (nargin < 1 || nargin > 3) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
51 print_usage (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
52 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
53 |
6679 | 54 max_cmd_line = 1024; |
6047 | 55 status = true; |
56 msg = ""; | |
57 msgid = ""; | |
58 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
59 ## FIXME: maybe use the same method as in ls to allow users control |
19261 | 60 ## over the command that is executed. |
6210 | 61 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
62 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
63 && isempty (file_in_path (getenv ("PATH"), "mv.exe"))) |
6233 | 64 ## Windows. |
6210 | 65 cmd = "cmd /C move"; |
66 cmd_force_flag = "/Y"; | |
67 else | |
68 cmd = "mv"; | |
69 cmd_force_flag = "-f"; | |
70 endif | |
71 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
72 ## Input type check. |
19261 | 73 if (ischar (f1)) |
74 f1 = cellstr (f1); | |
75 elseif (! iscellstr (f1)) | |
76 error ("copyfile: F1 must be a string or a cell array of strings"); | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
77 endif |
6233 | 78 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
79 if (nargin == 1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
80 f2 = pwd (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
81 elseif (! ischar (f2)) |
19261 | 82 error ("movefile: F2 must be a string"); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
83 endif |
6069 | 84 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
85 if (nargin == 3 && strcmp (force, "f")) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
86 cmd = [cmd " " cmd_force_flag]; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
87 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
89 ## If f1 has more than 1 element f2 must be a directory |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
90 isdir = (exist (f2, "dir") != 0); |
19261 | 91 if (numel (f1) > 1 && ! isdir) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
92 error ("movefile: when moving multiple files, F2 must be a directory"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
93 endif |
6069 | 94 |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
95 ## Protect the filename(s). |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
96 f1 = glob (f1); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
97 if (isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
98 error ("movefile: no files to move"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
99 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
100 p1 = sprintf ('"%s" ', f1{:}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
101 p2 = tilde_expand (f2); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
102 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
103 if (isdir && length (p1) > max_cmd_line) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
104 l2 = length (p2) + length (cmd) + 6; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
105 while (! isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
106 p1 = sprintf ('"%s" ', f1{1}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
107 f1(1) = []; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
108 while (! isempty (f1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
109 && (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
|
110 p1 = sprintf ('%s"%s" ', p1, f1{1}); |
10549 | 111 f1(1) = []; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
112 endwhile |
6679 | 113 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
114 if (ispc () && ! isunix () |
12497
1536ed546219
Fix bug #32443 preventing 'pkg install' on Windows platforms.
Rik <octave@nomad.inbox5.com>
parents:
12211
diff
changeset
|
115 && ! 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
|
116 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
117 p2 = strrep (p2, '\', '/'); |
6679 | 118 endif |
119 | |
120 ## Move the file(s). | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
121 [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
|
122 if (err != 0) |
10549 | 123 status = false; |
124 msgid = "movefile"; | |
6679 | 125 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
126 endwhile |
6047 | 127 else |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
128 if (ispc () && ! isunix () |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
129 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
130 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
131 p2 = strrep (p2, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
132 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
133 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
134 ## Move the file(s). |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
135 [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
|
136 if (err != 0) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
137 status = false; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
138 msgid = "movefile"; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
139 endif |
6047 | 140 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
141 |
6047 | 142 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
143 |
19261 | 144 |
145 %!test | |
146 %! unwind_protect | |
147 %! f1 = tempname; | |
148 %! tmp_var = pi; | |
149 %! save (f1, "tmp_var"); | |
150 %! fid = fopen (f1, "rb"); | |
151 %! assert (fid >= 0); | |
152 %! orig_data = fread (fid); | |
153 %! fclose (fid); | |
154 %! f2 = tempname; | |
155 %! assert (movefile (f1, f2)); | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
156 %! assert (! exist (f1, "file")); |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
157 %! assert (exist (f2, "file")); |
19261 | 158 %! fid = fopen (f2, "rb"); |
159 %! assert (fid >= 0); | |
160 %! new_data = fread (fid); | |
161 %! fclose (fid); | |
162 %! if (orig_data != new_data) | |
163 %! error ("moved file not equal to original file!"); | |
164 %! endif | |
165 %! unwind_protect_cleanup | |
166 %! delete (f2); | |
167 %! end_unwind_protect | |
168 | |
169 ## Test input validation | |
170 %!error movefile () | |
171 %!error movefile (1,2,3,4) | |
172 %!error <F1 must be a string> movefile (1, "foobar") | |
173 %!error <F2 must be a string> movefile ("foobar", 1) | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19261
diff
changeset
|
174 %!error <F2 must be a directory> movefile ({"a", "b"}, "%_NOT_A_DIR_%") |