523
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
523
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
523
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
523
|
26 #endif |
|
27 |
1341
|
28 #include <cerrno> |
|
29 #include <cstdio> |
|
30 #include <cstddef> |
|
31 #include <cstdlib> |
|
32 #include <cstring> |
|
33 |
5765
|
34 #include <sstream> |
1728
|
35 #include <string> |
|
36 |
1832
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
1832
|
39 #include <sys/types.h> |
2442
|
40 #endif |
1832
|
41 #include <unistd.h> |
|
42 #endif |
|
43 |
2926
|
44 #include "file-ops.h" |
|
45 #include "file-stat.h" |
|
46 #include "glob-match.h" |
|
47 #include "oct-env.h" |
5777
|
48 #include "pathsearch.h" |
1755
|
49 #include "str-vec.h" |
|
50 |
5102
|
51 #include "Cell.h" |
1355
|
52 #include "defun.h" |
1781
|
53 #include "dir-ops.h" |
1355
|
54 #include "dirfns.h" |
|
55 #include "error.h" |
1402
|
56 #include "gripes.h" |
5640
|
57 #include "input.h" |
5832
|
58 #include "load-path.h" |
1750
|
59 #include "oct-obj.h" |
1355
|
60 #include "pager.h" |
|
61 #include "procstream.h" |
|
62 #include "sysdep.h" |
1750
|
63 #include "toplev.h" |
1449
|
64 #include "unwind-prot.h" |
523
|
65 #include "utils.h" |
1742
|
66 #include "variables.h" |
523
|
67 |
5640
|
68 // TRUE means we ask for confirmation before recursively removing a |
|
69 // directory tree. |
|
70 static bool Vconfirm_recursive_rmdir = true; |
|
71 |
1328
|
72 static int |
3523
|
73 octave_change_to_directory (const std::string& newdir) |
1328
|
74 { |
5979
|
75 int cd_ok = octave_env::chdir (file_ops::tilde_expand (newdir)); |
1328
|
76 |
|
77 if (cd_ok) |
5832
|
78 { |
|
79 // FIXME -- should this be handled as a list of functions |
|
80 // to call so users can add their own chdir handlers? |
|
81 |
|
82 load_path::update (); |
|
83 } |
1328
|
84 else |
3531
|
85 { |
|
86 using namespace std; |
|
87 |
|
88 error ("%s: %s", newdir.c_str (), strerror (errno)); |
|
89 } |
1328
|
90 |
|
91 return cd_ok; |
|
92 } |
|
93 |
4208
|
94 DEFCMD (cd, args, , |
3301
|
95 "-*- texinfo -*-\n\ |
|
96 @deffn {Command} cd dir\n\ |
|
97 @deffnx {Command} chdir dir\n\ |
|
98 Change the current working directory to @var{dir}. If @var{dir} is\n\ |
|
99 omitted, the current directory is changed to the users home\n\ |
|
100 directory. For example,\n\ |
523
|
101 \n\ |
3301
|
102 @example\n\ |
|
103 cd ~/octave\n\ |
|
104 @end example\n\ |
|
105 \n\ |
|
106 @noindent\n\ |
|
107 Changes the current working directory to @file{~/octave}. If the\n\ |
|
108 directory does not exist, an error message is printed and the working\n\ |
|
109 directory is not changed.\n\ |
5597
|
110 @seealso{mkdir, rmdir, dir}\n\ |
3301
|
111 @end deffn") |
523
|
112 { |
2086
|
113 octave_value_list retval; |
523
|
114 |
1755
|
115 int argc = args.length () + 1; |
|
116 |
1965
|
117 string_vector argv = args.make_argv ("cd"); |
1755
|
118 |
|
119 if (error_state) |
|
120 return retval; |
523
|
121 |
|
122 if (argc > 1) |
|
123 { |
5872
|
124 std::string dirname = argv[1]; |
523
|
125 |
1750
|
126 if (dirname.length () > 0 |
1755
|
127 && ! octave_change_to_directory (dirname)) |
523
|
128 { |
|
129 return retval; |
|
130 } |
|
131 } |
|
132 else |
|
133 { |
3523
|
134 std::string home_dir = octave_env::get_home_directory (); |
2926
|
135 |
|
136 if (home_dir.empty () || ! octave_change_to_directory (home_dir)) |
|
137 return retval; |
523
|
138 } |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
611
|
143 DEFALIAS (chdir, cd); |
|
144 |
661
|
145 // Get a directory listing. |
|
146 |
1965
|
147 static void |
|
148 cleanup_iprocstream (void *p) |
|
149 { |
2800
|
150 delete static_cast <iprocstream *> (p); |
1965
|
151 } |
|
152 |
4691
|
153 DEFCMD (ls, args, nargout, |
3301
|
154 "-*- texinfo -*-\n\ |
|
155 @deffn {Command} ls options\n\ |
|
156 List directory contents. For example,\n\ |
523
|
157 \n\ |
3301
|
158 @example\n\ |
|
159 ls -l\n\ |
|
160 @print{} total 12\n\ |
|
161 @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m\n\ |
|
162 @print{} -rw-r--r-- 1 jwe users 1315 Aug 17 23:14 bar.m\n\ |
|
163 @end example\n\ |
|
164 \n\ |
|
165 The @code{dir} and @code{ls} commands are implemented by calling your\n\ |
|
166 system's directory listing command, so the available options may vary\n\ |
|
167 from system to system.\n\ |
5597
|
168 @seealso{dir, stat, readdir, glob, filesep}\n\ |
3301
|
169 @end deffn") |
523
|
170 { |
4691
|
171 octave_value retval; |
523
|
172 |
1755
|
173 int argc = args.length () + 1; |
|
174 |
1965
|
175 string_vector argv = args.make_argv ("ls"); |
1755
|
176 |
|
177 if (error_state) |
|
178 return retval; |
523
|
179 |
5765
|
180 std::ostringstream ls_buf; |
523
|
181 |
|
182 ls_buf << "ls -C "; |
|
183 for (int i = 1; i < argc; i++) |
2926
|
184 ls_buf << file_ops::tilde_expand (argv[i]) << " "; |
523
|
185 |
5765
|
186 iprocstream *cmd = new iprocstream (ls_buf.str ()); |
1469
|
187 |
2985
|
188 unwind_protect::add (cleanup_iprocstream, cmd); |
523
|
189 |
1449
|
190 if (cmd && *cmd) |
|
191 { |
3147
|
192 char ch; |
|
193 |
5765
|
194 std::ostringstream output_buf; |
4691
|
195 |
4489
|
196 for (;;) |
|
197 { |
|
198 if (cmd->get (ch)) |
5145
|
199 output_buf << ch; |
4489
|
200 else |
5144
|
201 break; |
4489
|
202 } |
4691
|
203 |
5765
|
204 std::string output = output_buf.str (); |
5145
|
205 |
4691
|
206 if (nargout > 0) |
5145
|
207 retval = output; |
|
208 else |
|
209 octave_stdout << output; |
1449
|
210 } |
|
211 else |
|
212 error ("couldn't start process for ls!"); |
523
|
213 |
2985
|
214 unwind_protect::run (); |
523
|
215 |
|
216 return retval; |
|
217 } |
|
218 |
1957
|
219 DEFUN (pwd, , nargout, |
3301
|
220 "-*- texinfo -*-\n\ |
|
221 @deftypefn {Built-in Function} {} pwd ()\n\ |
|
222 Return the current working directory.\n\ |
5597
|
223 @seealso{dir, ls}\n\ |
3301
|
224 @end deftypefn") |
523
|
225 { |
5979
|
226 return octave_value (octave_env::getcwd ()); |
523
|
227 } |
|
228 |
1957
|
229 DEFUN (readdir, args, , |
3301
|
230 "-*- texinfo -*-\n\ |
|
231 @deftypefn {Built-in Function} {[@var{files}, @var{err}, @var{msg}] =} readdir (@var{dir})\n\ |
4691
|
232 Return names of the files in the directory @var{dir} as a cell array of\n\ |
|
233 strings. If an error occurs, return an empty cell array in @var{files}.\n\ |
1389
|
234 \n\ |
3301
|
235 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
236 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
237 system-dependent error message.\n\ |
5597
|
238 @seealso{dir, glob}\n\ |
3301
|
239 @end deftypefn") |
1389
|
240 { |
2086
|
241 octave_value_list retval; |
1389
|
242 |
3523
|
243 retval(2) = std::string (); |
2669
|
244 retval(1) = -1.0; |
4691
|
245 retval(0) = Cell (); |
2669
|
246 |
1401
|
247 if (args.length () == 1) |
1389
|
248 { |
3523
|
249 std::string dirname = args(0).string_value (); |
1389
|
250 |
1401
|
251 if (error_state) |
1781
|
252 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
253 else |
|
254 { |
5872
|
255 dir_entry dir (dirname); |
1389
|
256 |
1401
|
257 if (dir) |
1389
|
258 { |
1781
|
259 string_vector dirlist = dir.read (); |
4691
|
260 retval(0) = Cell (dirlist.qsort ()); |
2669
|
261 retval(1) = 0.0; |
1401
|
262 } |
|
263 else |
|
264 { |
2669
|
265 retval(2) = dir.error (); |
1401
|
266 } |
1389
|
267 } |
|
268 } |
|
269 else |
5823
|
270 print_usage (); |
1389
|
271 |
1401
|
272 return retval; |
|
273 } |
|
274 |
5775
|
275 // FIXME -- should maybe also allow second arg to specify |
5476
|
276 // mode? OTOH, that might cause trouble with compatibility later... |
1401
|
277 |
5637
|
278 DEFCMD (mkdir, args, , |
3301
|
279 "-*- texinfo -*-\n\ |
5476
|
280 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} mkdir (@var{dir})\n\ |
3301
|
281 Create a directory named @var{dir}.\n\ |
1401
|
282 \n\ |
5476
|
283 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
|
284 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ |
|
285 system-dependent error message, and @var{msgid} contains a unique\n\ |
|
286 message identifier.\n\ |
5597
|
287 @seealso{rmdir}\n\ |
3301
|
288 @end deftypefn") |
1401
|
289 { |
2086
|
290 octave_value_list retval; |
1401
|
291 |
5476
|
292 retval(2) = std::string (); |
3523
|
293 retval(1) = std::string (); |
5476
|
294 retval(0) = false; |
1401
|
295 |
|
296 if (args.length () == 1) |
|
297 { |
3523
|
298 std::string dirname = args(0).string_value (); |
1401
|
299 |
|
300 if (error_state) |
1402
|
301 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
302 else |
1401
|
303 { |
3523
|
304 std::string msg; |
2669
|
305 |
2926
|
306 int status = file_ops::mkdir (file_ops::tilde_expand (dirname), |
|
307 0777, msg); |
1489
|
308 |
2669
|
309 if (status < 0) |
5476
|
310 { |
|
311 retval(2) = "mkdir"; |
|
312 retval(1) = msg; |
|
313 } |
|
314 else |
|
315 retval(0) = true; |
1401
|
316 } |
|
317 } |
|
318 else |
5823
|
319 print_usage (); |
1401
|
320 |
|
321 return retval; |
|
322 } |
|
323 |
5637
|
324 DEFCMD (rmdir, args, , |
3301
|
325 "-*- texinfo -*-\n\ |
5476
|
326 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir})\n\ |
|
327 @deftypefnx {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir}, @code{\"s\"})\n\ |
3301
|
328 Remove the directory named @var{dir}.\n\ |
1401
|
329 \n\ |
5476
|
330 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
|
331 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ |
|
332 system-dependent error message, and @var{msgid} contains a unique\n\ |
|
333 message identifier.\n\ |
|
334 \n\ |
|
335 If the optional second parameter is suplied, recursively remove all\n\ |
|
336 subdirectories as well.\n\ |
5640
|
337 @seealso{mkdir, confirm_recursive_rmdir}\n\ |
3301
|
338 @end deftypefn") |
1401
|
339 { |
2086
|
340 octave_value_list retval; |
1401
|
341 |
5476
|
342 retval(2) = std::string (); |
3523
|
343 retval(1) = std::string (); |
5476
|
344 retval(0) = false; |
1401
|
345 |
5476
|
346 int nargin = args.length (); |
|
347 |
|
348 if (nargin == 1 || nargin == 2) |
1401
|
349 { |
3523
|
350 std::string dirname = args(0).string_value (); |
1401
|
351 |
|
352 if (error_state) |
1402
|
353 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
354 else |
1401
|
355 { |
5640
|
356 std::string fulldir = file_ops::tilde_expand (dirname); |
|
357 int status = -1; |
3523
|
358 std::string msg; |
2669
|
359 |
5639
|
360 if (nargin == 2) |
|
361 { |
|
362 if (args(1).string_value () == "s") |
5640
|
363 { |
|
364 bool doit = true; |
|
365 |
|
366 if (interactive && Vconfirm_recursive_rmdir) |
|
367 { |
|
368 std::string prompt |
|
369 = "remove entire contents of " + fulldir + "? "; |
|
370 |
|
371 doit = octave_yes_or_no (prompt); |
|
372 } |
|
373 |
|
374 if (doit) |
|
375 status = file_ops::recursive_rmdir (fulldir, msg); |
|
376 } |
5639
|
377 else |
|
378 error ("rmdir: expecting second argument to be \"s\""); |
|
379 } |
|
380 else |
5640
|
381 status = file_ops::rmdir (fulldir, msg); |
2669
|
382 |
|
383 if (status < 0) |
5476
|
384 { |
|
385 retval(2) = "rmdir"; |
|
386 retval(1) = msg; |
|
387 } |
|
388 else |
|
389 retval(0) = true; |
1401
|
390 } |
|
391 } |
1389
|
392 else |
5823
|
393 print_usage (); |
1401
|
394 |
|
395 return retval; |
|
396 } |
|
397 |
3710
|
398 DEFUN (link, args, , |
|
399 "-*- texinfo -*-\n\ |
|
400 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} link (@var{old}, @var{new})\n\ |
|
401 Create a new link (also known as a hard link) to an existing file.\n\ |
|
402 \n\ |
|
403 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
404 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
405 system-dependent error message.\n\ |
5597
|
406 @seealso{symlink}\n\ |
3710
|
407 @end deftypefn") |
|
408 { |
|
409 octave_value_list retval; |
|
410 |
|
411 retval(1) = std::string (); |
|
412 retval(0) = -1.0; |
|
413 |
|
414 if (args.length () == 2) |
|
415 { |
|
416 std::string from = args(0).string_value (); |
|
417 |
|
418 if (error_state) |
|
419 gripe_wrong_type_arg ("link", args(0)); |
|
420 else |
|
421 { |
|
422 std::string to = args(1).string_value (); |
|
423 |
|
424 if (error_state) |
|
425 gripe_wrong_type_arg ("link", args(1)); |
|
426 else |
|
427 { |
|
428 std::string msg; |
|
429 |
|
430 int status = file_ops::link (from, to, msg); |
|
431 |
4233
|
432 retval(0) = status; |
3710
|
433 |
|
434 if (status < 0) |
|
435 retval(1) = msg; |
|
436 } |
|
437 } |
|
438 } |
|
439 else |
5823
|
440 print_usage (); |
3710
|
441 |
|
442 return retval; |
|
443 } |
|
444 |
|
445 DEFUN (symlink, args, , |
|
446 "-*- texinfo -*-\n\ |
|
447 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} symlink (@var{old}, @var{new})\n\ |
|
448 Create a symbolic link @var{new} which contains the string @var{old}.\n\ |
|
449 \n\ |
|
450 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
451 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
452 system-dependent error message.\n\ |
5597
|
453 @seealso{link, readlink}\n\ |
3710
|
454 @end deftypefn") |
|
455 { |
|
456 octave_value_list retval; |
|
457 |
|
458 retval(1) = std::string (); |
|
459 retval(0) = -1.0; |
|
460 |
|
461 if (args.length () == 2) |
|
462 { |
|
463 std::string from = args(0).string_value (); |
|
464 |
|
465 if (error_state) |
|
466 gripe_wrong_type_arg ("symlink", args(0)); |
|
467 else |
|
468 { |
|
469 std::string to = args(1).string_value (); |
|
470 |
|
471 if (error_state) |
|
472 gripe_wrong_type_arg ("symlink", args(1)); |
|
473 else |
|
474 { |
|
475 std::string msg; |
|
476 |
|
477 int status = file_ops::symlink (from, to, msg); |
|
478 |
4233
|
479 retval(0) = status; |
3710
|
480 |
|
481 if (status < 0) |
|
482 retval(1) = msg; |
|
483 } |
|
484 } |
|
485 } |
|
486 else |
5823
|
487 print_usage (); |
3710
|
488 |
|
489 return retval; |
|
490 } |
|
491 |
|
492 DEFUN (readlink, args, , |
|
493 "-*- texinfo -*-\n\ |
4169
|
494 @deftypefn {Built-in Function} {[@var{result}, @var{err}, @var{msg}] =} readlink (@var{symlink})\n\ |
3710
|
495 Read the value of the symbolic link @var{symlink}.\n\ |
|
496 \n\ |
|
497 If successful, @var{result} contains the contents of the symbolic link\n\ |
|
498 @var{symlink}, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
499 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
500 system-dependent error message.\n\ |
5597
|
501 @seealso{link, symlink}\n\ |
3710
|
502 @end deftypefn") |
|
503 { |
|
504 octave_value_list retval; |
|
505 |
|
506 retval(2) = std::string (); |
|
507 retval(1) = -1.0; |
|
508 retval(0) = std::string (); |
|
509 |
|
510 if (args.length () == 1) |
|
511 { |
|
512 std::string symlink = args(0).string_value (); |
|
513 |
|
514 if (error_state) |
|
515 gripe_wrong_type_arg ("readlink", args(0)); |
|
516 else |
|
517 { |
|
518 std::string result; |
|
519 std::string msg; |
|
520 |
|
521 int status = file_ops::readlink (symlink, result, msg); |
|
522 |
|
523 retval(0) = result; |
|
524 |
4233
|
525 retval(1) = status; |
3710
|
526 |
|
527 if (status < 0) |
|
528 retval(2) = msg; |
|
529 } |
|
530 } |
|
531 else |
5823
|
532 print_usage (); |
3710
|
533 |
|
534 return retval; |
|
535 } |
|
536 |
1957
|
537 DEFUN (rename, args, , |
3301
|
538 "-*- texinfo -*-\n\ |
|
539 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})\n\ |
|
540 Change the name of file @var{old} to @var{new}.\n\ |
1401
|
541 \n\ |
3301
|
542 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
543 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
544 system-dependent error message.\n\ |
5597
|
545 @seealso{ls, dir}\n\ |
3301
|
546 @end deftypefn") |
1401
|
547 { |
2086
|
548 octave_value_list retval; |
1401
|
549 |
3523
|
550 retval(1) = std::string (); |
2669
|
551 retval(0) = -1.0; |
1401
|
552 |
|
553 if (args.length () == 2) |
|
554 { |
3523
|
555 std::string from = args(0).string_value (); |
1728
|
556 |
1401
|
557 if (error_state) |
1402
|
558 gripe_wrong_type_arg ("rename", args(0)); |
|
559 else |
1401
|
560 { |
3523
|
561 std::string to = args(1).string_value (); |
1728
|
562 |
1402
|
563 if (error_state) |
|
564 gripe_wrong_type_arg ("rename", args(1)); |
2669
|
565 else |
1402
|
566 { |
3523
|
567 std::string msg; |
2669
|
568 |
2926
|
569 int status = file_ops::rename (from, to, msg); |
2669
|
570 |
4233
|
571 retval(0) = status; |
2669
|
572 |
|
573 if (status < 0) |
|
574 retval(1) = msg; |
1402
|
575 } |
1401
|
576 } |
|
577 } |
|
578 else |
5823
|
579 print_usage (); |
1401
|
580 |
1389
|
581 return retval; |
|
582 } |
|
583 |
2495
|
584 DEFUN (glob, args, , |
3301
|
585 "-*- texinfo -*-\n\ |
|
586 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\ |
5444
|
587 Given an array of strings (as a char array or a cell array) in\n\ |
|
588 @var{pattern}, return a cell array of file names that match any of\n\ |
|
589 them, or an empty cell array if no patterns match. Tilde expansion\n\ |
|
590 is performed on each of the patterns before looking for matching file\n\ |
|
591 names. For example,\n\ |
2495
|
592 \n\ |
3301
|
593 @example\n\ |
|
594 @group\n\ |
|
595 glob (\"/vm*\")\n\ |
|
596 @result{} \"/vmlinuz\"\n\ |
|
597 @end group\n\ |
|
598 @end example\n\ |
5597
|
599 @seealso{dir, ls, stat, readdir}\n\ |
|
600 @end deftypefn") |
2495
|
601 { |
|
602 octave_value retval; |
|
603 |
|
604 if (args.length () == 1) |
|
605 { |
|
606 string_vector pat = args(0).all_strings (); |
|
607 |
|
608 if (error_state) |
|
609 gripe_wrong_type_arg ("glob", args(0)); |
|
610 else |
|
611 { |
2926
|
612 glob_match pattern (file_ops::tilde_expand (pat)); |
2495
|
613 |
4691
|
614 retval = Cell (pattern.glob ()); |
2495
|
615 } |
|
616 } |
|
617 else |
5823
|
618 print_usage (); |
2495
|
619 |
|
620 return retval; |
|
621 } |
|
622 |
2496
|
623 DEFUN (fnmatch, args, , |
3301
|
624 "-*- texinfo -*-\n\ |
|
625 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\ |
|
626 Return 1 or zero for each element of @var{string} that matches any of\n\ |
|
627 the elements of the string array @var{pattern}, using the rules of\n\ |
|
628 filename pattern matching. For example,\n\ |
2496
|
629 \n\ |
3301
|
630 @example\n\ |
|
631 @group\n\ |
|
632 fnmatch (\"a*b\", [\"ab\"; \"axyzb\"; \"xyzab\"])\n\ |
|
633 @result{} [ 1; 1; 0 ]\n\ |
|
634 @end group\n\ |
|
635 @end example\n\ |
|
636 @end deftypefn") |
2496
|
637 { |
|
638 octave_value retval; |
|
639 |
|
640 if (args.length () == 2) |
|
641 { |
|
642 string_vector pat = args(0).all_strings (); |
|
643 string_vector str = args(1).all_strings (); |
|
644 |
|
645 if (error_state) |
|
646 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
647 else |
|
648 { |
2926
|
649 glob_match pattern (file_ops::tilde_expand (pat)); |
2496
|
650 |
|
651 Array<bool> tmp = pattern.match (str); |
|
652 |
5275
|
653 octave_idx_type n = tmp.length (); |
2496
|
654 |
|
655 ColumnVector result (n); |
|
656 |
5275
|
657 for (octave_idx_type i = 0; i < n; i++) |
2496
|
658 result(i) = tmp(i); |
|
659 |
3418
|
660 retval = result; |
2496
|
661 } |
|
662 } |
|
663 else |
5823
|
664 print_usage (); |
2496
|
665 |
|
666 return retval; |
|
667 } |
|
668 |
5777
|
669 DEFUN (filesep, args, , |
5832
|
670 "-*- texinfo -*-\n\ |
|
671 @deftypefn {Built-in Function} {} filesep ()\n\ |
5777
|
672 Return the system-dependent character used to separate directory names.\n\ |
|
673 @seealso{pathsep, dir, ls}\n\ |
|
674 @end deftypefn") |
|
675 { |
|
676 octave_value retval; |
|
677 |
|
678 if (args.length () == 0) |
|
679 retval = file_ops::dir_sep_str; |
|
680 else |
5823
|
681 print_usage (); |
5777
|
682 |
|
683 return retval; |
|
684 } |
|
685 |
|
686 DEFUN (pathsep, args, , |
|
687 "-*- texinfo -*-\n\ |
|
688 @deftypefn {Built-in Function} {} pathsep ()\n\ |
|
689 Return the system-dependent character used to separate directories in\n\ |
|
690 a path.\n\ |
|
691 @seealso{filesep, dir, ls}\n\ |
|
692 @end deftypefn") |
|
693 { |
|
694 octave_value retval; |
|
695 |
|
696 if (args.length () == 0) |
|
697 retval = dir_path::path_sep_str; |
|
698 else |
5823
|
699 print_usage (); |
5777
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
5794
|
704 DEFUN (confirm_recursive_rmdir, args, nargout, |
|
705 "-*- texinfo -*-\n\ |
|
706 @deftypefn {Built-in Function} {@var{val} =} confirm_recursive_rmdir ()\n\ |
|
707 @deftypefnx {Built-in Function} {@var{old_val} =} confirm_recursive_rmdir (@var{new_val})\n\ |
|
708 Query or set the internal variable that controls whether Octave\n\ |
|
709 will ask for confirmation before recursively removing a directory tree.\n\ |
|
710 @end deftypefn") |
5640
|
711 { |
5794
|
712 return SET_INTERNAL_VARIABLE (confirm_recursive_rmdir); |
4264
|
713 } |
|
714 |
523
|
715 /* |
|
716 ;;; Local Variables: *** |
|
717 ;;; mode: C++ *** |
|
718 ;;; End: *** |
|
719 */ |