Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/copyfile.m @ 20296:23fb65b45d8c
do not call custom editor at startup and when debugging (bug #44701)
* file-editor.cc (call_custom_editor): return with true but without opening
a file;
(empty_script): do not open an empty script in the cutom editor
at startup
author | Torsten <ttl@justmail.de> |
---|---|
date | Fri, 17 Apr 2015 19:55:24 +0200 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
1 ## Copyright (C) 2005-2015 John W. Eaton |
6047 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## 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 | |
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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6047 | 18 |
19 ## -*- texinfo -*- | |
12211
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}) |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, 'f') |
19452 | 22 ## Copy the source files or directories @var{f1} to the destination @var{f2}. |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
23 ## |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
24 ## The name @var{f1} may contain globbing patterns. If @var{f1} expands to |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
25 ## multiple file names, @var{f2} must be a directory. |
19452 | 26 ## |
27 ## 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
|
28 ## overwritten without prompting. |
6047 | 29 ## |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
30 ## 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
|
31 ## 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
|
32 ## 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
|
33 ## 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
|
34 ## @code{system} command. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17394
diff
changeset
|
35 ## @seealso{movefile, rename, unlink, delete, glob} |
6047 | 36 ## @end deftypefn |
37 | |
38 function [status, msg, msgid] = copyfile (f1, f2, force) | |
39 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
40 if (nargin < 2 || nargin > 3) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
41 print_usage (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
42 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
43 |
6679 | 44 max_cmd_line = 1024; |
6047 | 45 status = true; |
46 msg = ""; | |
47 msgid = ""; | |
48 | |
19452 | 49 ## FIXME: Maybe use the same method as in ls to allow users control |
50 ## over the command that is executed. | |
6210 | 51 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
52 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
53 && isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
6233 | 54 ## Windows. |
6210 | 55 cmd = "cmd /C xcopy /E"; |
56 cmd_force_flag = "/Y"; | |
57 else | |
58 cmd = "cp -r"; | |
59 cmd_force_flag = "-f"; | |
60 endif | |
61 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
62 ## Input type check. |
19452 | 63 if (ischar (f1)) |
64 f1 = cellstr (f1); | |
65 elseif (! iscellstr (f1)) | |
66 error ("copyfile: F1 must be a string or a cell array of strings"); | |
19454 | 67 endif |
68 if (! ischar (f2)) | |
19452 | 69 error ("copyfile: F2 must be a string"); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
70 endif |
6233 | 71 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
72 if (nargin == 3 && strcmp (force, "f")) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
73 cmd = [cmd " " cmd_force_flag]; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
74 endif |
6069 | 75 |
19452 | 76 ## If f1 has more than 1 element then f2 must be a directory |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
77 isdir = (exist (f2, "dir") != 0); |
19452 | 78 if (numel (f1) > 1 && ! isdir) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
79 error ("copyfile: when copying multiple files, F2 must be a directory"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
80 endif |
6679 | 81 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
82 ## Protect the file name(s). |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
83 f1 = glob (f1); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
84 if (isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
85 error ("copyfile: no files to move"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
86 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
87 p1 = sprintf ('"%s" ', f1{:}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
88 p2 = tilde_expand (f2); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
89 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
90 if (isdir && length (p1) > max_cmd_line) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
91 l2 = length (p2) + length (cmd) + 6; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
92 while (! isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
93 p1 = sprintf ('"%s" ', f1{1}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
94 f1(1) = []; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
95 while (! isempty (f1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
96 && (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
|
97 p1 = sprintf ('%s"%s" ', p1, f1{1}); |
10549 | 98 f1(1) = []; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
99 endwhile |
6679 | 100 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
101 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
102 && ! 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
|
103 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
104 p2 = strrep (p2, '\', '/'); |
6679 | 105 endif |
6398 | 106 |
6679 | 107 ## Copy the files. |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
108 [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
|
109 if (err != 0) |
10549 | 110 status = false; |
111 msgid = "copyfile"; | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
112 break; |
6679 | 113 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
114 endwhile |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
115 else |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
116 if (ispc () && ! isunix () |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
117 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
118 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
119 p2 = strrep (p2, '\', '/'); |
6047 | 120 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
121 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
122 ## Copy the files. |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
123 [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
|
124 if (err != 0) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
125 status = false; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
126 msgid = "copyfile"; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
127 endif |
6047 | 128 endif |
129 | |
130 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
131 |
19452 | 132 |
133 %!test | |
134 %! unwind_protect | |
135 %! f1 = tempname; | |
136 %! tmp_var = pi; | |
137 %! save (f1, "tmp_var"); | |
138 %! f2 = tempname; | |
139 %! assert (copyfile (f1, f2)); | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19454
diff
changeset
|
140 %! assert (exist (f2, "file")); |
19452 | 141 %! fid = fopen (f1, "rb"); |
142 %! assert (fid >= 0); | |
143 %! orig_data = fread (fid); | |
144 %! fclose (fid); | |
145 %! fid = fopen (f2, "rb"); | |
146 %! assert (fid >= 0); | |
147 %! new_data = fread (fid); | |
148 %! fclose (fid); | |
149 %! if (orig_data != new_data) | |
150 %! error ("copied file not equal to original file!"); | |
151 %! endif | |
152 %! unwind_protect_cleanup | |
153 %! delete (f1); | |
154 %! delete (f2); | |
155 %! end_unwind_protect | |
156 | |
157 ## Test input validation | |
158 %!error copyfile () | |
159 %!error copyfile (1) | |
160 %!error copyfile (1,2,3,4) | |
161 %!error <F1 must be a string> copyfile (1, "foobar") | |
162 %!error <F2 must be a string> copyfile ("foobar", 1) | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19454
diff
changeset
|
163 %!error <F2 must be a directory> copyfile ({"a", "b"}, "%_NOT_A_DIR_%") |
19452 | 164 |