Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/movefile.m @ 20410:d9f35ceff9e1
Change mkfifo to use an octal argument for MODE (bug #45054).
* NEWS: Announce switch from decimal to octal MODE for mkfifo.
* file-io.cc (convert): Add a FIXME note that this function is repeated in
* syscalls.cc.
* syscalls.cc (convert): New function to convert from one base to another.
* syscalls.cc (Fmkfifo): Change docstring to note that MODE argument is now
octal. Convert MODE from octal to decimal before calling octave_mkfifo().
Add BIST tests.
* __gnuplot_get_var__.m, __gnuplot_ginput__.m: Change instances of MODE
argument in m-files from decimal to octal.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 17 May 2015 10:04:08 -0700 |
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 -*- | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
20 ## @deftypefn {Function File} {} movefile (@var{f1}) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
21 ## @deftypefnx {Function File} {} movefile (@var{f1}, @var{f2}) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
22 ## @deftypefnx {Function File} {} movefile (@var{f1}, @var{f2}, 'f') |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@dots{}) |
19453 | 24 ## Move the source files or directories @var{f1} to the destination @var{f2}. |
6047 | 25 ## |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
26 ## 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
|
27 ## multiple file names, @var{f2} must be a directory. If no destination |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
28 ## @var{f2} is specified then the destination is the present working directory. |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
29 ## If @var{f2} is a file name then @var{f1} is renamed to @var{f2}. |
19453 | 30 ## |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
31 ## 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
|
32 ## overwritten without prompting. |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
33 ## |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
34 ## 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
|
35 ## 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
|
36 ## 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
|
37 ## 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
|
38 ## @code{system} command. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17394
diff
changeset
|
39 ## @seealso{rename, copyfile, unlink, delete, glob} |
6047 | 40 ## @end deftypefn |
41 | |
42 function [status, msg, msgid] = movefile (f1, f2, force) | |
43 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
44 if (nargin < 1 || nargin > 3) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
45 print_usage (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
47 |
6679 | 48 max_cmd_line = 1024; |
6047 | 49 status = true; |
50 msg = ""; | |
51 msgid = ""; | |
52 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
53 ## FIXME: maybe use the same method as in ls to allow users control |
19453 | 54 ## over the command that is executed. |
6210 | 55 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
56 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
57 && isempty (file_in_path (getenv ("PATH"), "mv.exe"))) |
6233 | 58 ## Windows. |
6210 | 59 cmd = "cmd /C move"; |
60 cmd_force_flag = "/Y"; | |
61 else | |
62 cmd = "mv"; | |
63 cmd_force_flag = "-f"; | |
64 endif | |
65 | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
66 ## Input type check. |
19453 | 67 if (ischar (f1)) |
68 f1 = cellstr (f1); | |
69 elseif (! iscellstr (f1)) | |
70 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
|
71 endif |
6233 | 72 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
73 if (nargin == 1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
74 f2 = pwd (); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
75 elseif (! ischar (f2)) |
19453 | 76 error ("movefile: F2 must be a string"); |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
77 endif |
6069 | 78 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
79 if (nargin == 3 && strcmp (force, "f")) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
80 cmd = [cmd " " cmd_force_flag]; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
81 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
82 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
83 ## 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
|
84 isdir = (exist (f2, "dir") != 0); |
19453 | 85 if (numel (f1) > 1 && ! isdir) |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
86 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
|
87 endif |
6069 | 88 |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
89 ## Protect the file name(s). |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
90 f1 = glob (f1); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
91 if (isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
92 error ("movefile: no files to move"); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
93 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
94 p1 = sprintf ('"%s" ', f1{:}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
95 p2 = tilde_expand (f2); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
96 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
97 if (isdir && length (p1) > max_cmd_line) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
98 l2 = length (p2) + length (cmd) + 6; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
99 while (! isempty (f1)) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
100 p1 = sprintf ('"%s" ', f1{1}); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
101 f1(1) = []; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
102 while (! isempty (f1) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
103 && (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
|
104 p1 = sprintf ('%s"%s" ', p1, f1{1}); |
10549 | 105 f1(1) = []; |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
106 endwhile |
6679 | 107 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
108 if (ispc () && ! isunix () |
12497
1536ed546219
Fix bug #32443 preventing 'pkg install' on Windows platforms.
Rik <octave@nomad.inbox5.com>
parents:
12211
diff
changeset
|
109 && ! 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
|
110 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
111 p2 = strrep (p2, '\', '/'); |
6679 | 112 endif |
113 | |
114 ## Move the file(s). | |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
115 [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
|
116 if (err != 0) |
10549 | 117 status = false; |
118 msgid = "movefile"; | |
6679 | 119 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
120 endwhile |
6047 | 121 else |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
122 if (ispc () && ! isunix () |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
123 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
124 p1 = strrep (p1, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
125 p2 = strrep (p2, '\', '/'); |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
126 endif |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
127 |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
128 ## Move the file(s). |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
129 [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
|
130 if (err != 0) |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
131 status = false; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
132 msgid = "movefile"; |
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
133 endif |
6047 | 134 endif |
17394
b6867a09d7cf
Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
135 |
6047 | 136 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
137 |
19453 | 138 |
139 %!test | |
140 %! unwind_protect | |
141 %! f1 = tempname; | |
142 %! tmp_var = pi; | |
143 %! save (f1, "tmp_var"); | |
144 %! fid = fopen (f1, "rb"); | |
145 %! assert (fid >= 0); | |
146 %! orig_data = fread (fid); | |
147 %! fclose (fid); | |
148 %! f2 = tempname; | |
149 %! assert (movefile (f1, f2)); | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19453
diff
changeset
|
150 %! assert (! exist (f1, "file")); |
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19453
diff
changeset
|
151 %! assert (exist (f2, "file")); |
19453 | 152 %! fid = fopen (f2, "rb"); |
153 %! assert (fid >= 0); | |
154 %! new_data = fread (fid); | |
155 %! fclose (fid); | |
156 %! if (orig_data != new_data) | |
157 %! error ("moved file not equal to original file!"); | |
158 %! endif | |
159 %! unwind_protect_cleanup | |
160 %! delete (f2); | |
161 %! end_unwind_protect | |
162 | |
163 ## Test input validation | |
164 %!error movefile () | |
165 %!error movefile (1,2,3,4) | |
166 %!error <F1 must be a string> movefile (1, "foobar") | |
167 %!error <F2 must be a string> movefile ("foobar", 1) | |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19453
diff
changeset
|
168 %!error <F2 must be a directory> movefile ({"a", "b"}, "%_NOT_A_DIR_%") |
19453 | 169 |