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" |
1750
|
58 #include "oct-obj.h" |
1355
|
59 #include "pager.h" |
|
60 #include "procstream.h" |
|
61 #include "sysdep.h" |
1750
|
62 #include "toplev.h" |
1449
|
63 #include "unwind-prot.h" |
523
|
64 #include "utils.h" |
1742
|
65 #include "variables.h" |
523
|
66 |
5640
|
67 // TRUE means we ask for confirmation before recursively removing a |
|
68 // directory tree. |
|
69 static bool Vconfirm_recursive_rmdir = true; |
|
70 |
5775
|
71 // FIXME -- changing the plotter directory should be handled |
2926
|
72 // by registering a function for octave_env::chdir to call so that |
|
73 // this function can be eliminated. |
523
|
74 |
1328
|
75 static int |
3523
|
76 octave_change_to_directory (const std::string& newdir) |
1328
|
77 { |
2926
|
78 int cd_ok = octave_env::chdir (newdir); |
1328
|
79 |
|
80 if (cd_ok) |
5775
|
81 // FIXME -- this should be handled as a list of functions |
5102
|
82 // to call so users can add their own chdir handlers. |
|
83 /* do_external_plotter_cd (newdir) */; |
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 { |
3523
|
124 std::string dirname = file_ops::tilde_expand (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 { |
4233
|
226 octave_value retval; |
523
|
227 |
3523
|
228 std::string directory = octave_env::getcwd (); |
523
|
229 |
2926
|
230 if (directory.empty ()) |
|
231 warning ("pwd: can't find working directory!"); |
523
|
232 else |
|
233 { |
1592
|
234 if (nargout == 0) |
2095
|
235 octave_stdout << directory << "\n"; |
1592
|
236 else |
|
237 retval = directory; |
523
|
238 } |
1592
|
239 |
523
|
240 return retval; |
|
241 } |
|
242 |
1957
|
243 DEFUN (readdir, args, , |
3301
|
244 "-*- texinfo -*-\n\ |
|
245 @deftypefn {Built-in Function} {[@var{files}, @var{err}, @var{msg}] =} readdir (@var{dir})\n\ |
4691
|
246 Return names of the files in the directory @var{dir} as a cell array of\n\ |
|
247 strings. If an error occurs, return an empty cell array in @var{files}.\n\ |
1389
|
248 \n\ |
3301
|
249 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
250 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
251 system-dependent error message.\n\ |
5597
|
252 @seealso{dir, glob}\n\ |
3301
|
253 @end deftypefn") |
1389
|
254 { |
2086
|
255 octave_value_list retval; |
1389
|
256 |
3523
|
257 retval(2) = std::string (); |
2669
|
258 retval(1) = -1.0; |
4691
|
259 retval(0) = Cell (); |
2669
|
260 |
1401
|
261 if (args.length () == 1) |
1389
|
262 { |
3523
|
263 std::string dirname = args(0).string_value (); |
1389
|
264 |
1401
|
265 if (error_state) |
1781
|
266 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
267 else |
|
268 { |
2926
|
269 dir_entry dir (file_ops::tilde_expand (dirname)); |
1389
|
270 |
1401
|
271 if (dir) |
1389
|
272 { |
1781
|
273 string_vector dirlist = dir.read (); |
4691
|
274 retval(0) = Cell (dirlist.qsort ()); |
2669
|
275 retval(1) = 0.0; |
1401
|
276 } |
|
277 else |
|
278 { |
2669
|
279 retval(2) = dir.error (); |
1401
|
280 } |
1389
|
281 } |
|
282 } |
|
283 else |
|
284 print_usage ("readdir"); |
|
285 |
1401
|
286 return retval; |
|
287 } |
|
288 |
5775
|
289 // FIXME -- should maybe also allow second arg to specify |
5476
|
290 // mode? OTOH, that might cause trouble with compatibility later... |
1401
|
291 |
5637
|
292 DEFCMD (mkdir, args, , |
3301
|
293 "-*- texinfo -*-\n\ |
5476
|
294 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} mkdir (@var{dir})\n\ |
3301
|
295 Create a directory named @var{dir}.\n\ |
1401
|
296 \n\ |
5476
|
297 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
|
298 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ |
|
299 system-dependent error message, and @var{msgid} contains a unique\n\ |
|
300 message identifier.\n\ |
5597
|
301 @seealso{rmdir}\n\ |
3301
|
302 @end deftypefn") |
1401
|
303 { |
2086
|
304 octave_value_list retval; |
1401
|
305 |
5476
|
306 retval(2) = std::string (); |
3523
|
307 retval(1) = std::string (); |
5476
|
308 retval(0) = false; |
1401
|
309 |
|
310 if (args.length () == 1) |
|
311 { |
3523
|
312 std::string dirname = args(0).string_value (); |
1401
|
313 |
|
314 if (error_state) |
1402
|
315 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
316 else |
1401
|
317 { |
3523
|
318 std::string msg; |
2669
|
319 |
2926
|
320 int status = file_ops::mkdir (file_ops::tilde_expand (dirname), |
|
321 0777, msg); |
1489
|
322 |
2669
|
323 if (status < 0) |
5476
|
324 { |
|
325 retval(2) = "mkdir"; |
|
326 retval(1) = msg; |
|
327 } |
|
328 else |
|
329 retval(0) = true; |
1401
|
330 } |
|
331 } |
|
332 else |
|
333 print_usage ("mkdir"); |
|
334 |
|
335 return retval; |
|
336 } |
|
337 |
5637
|
338 DEFCMD (rmdir, args, , |
3301
|
339 "-*- texinfo -*-\n\ |
5476
|
340 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir})\n\ |
|
341 @deftypefnx {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir}, @code{\"s\"})\n\ |
3301
|
342 Remove the directory named @var{dir}.\n\ |
1401
|
343 \n\ |
5476
|
344 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
|
345 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ |
|
346 system-dependent error message, and @var{msgid} contains a unique\n\ |
|
347 message identifier.\n\ |
|
348 \n\ |
|
349 If the optional second parameter is suplied, recursively remove all\n\ |
|
350 subdirectories as well.\n\ |
5640
|
351 @seealso{mkdir, confirm_recursive_rmdir}\n\ |
3301
|
352 @end deftypefn") |
1401
|
353 { |
2086
|
354 octave_value_list retval; |
1401
|
355 |
5476
|
356 retval(2) = std::string (); |
3523
|
357 retval(1) = std::string (); |
5476
|
358 retval(0) = false; |
1401
|
359 |
5476
|
360 int nargin = args.length (); |
|
361 |
|
362 if (nargin == 1 || nargin == 2) |
1401
|
363 { |
3523
|
364 std::string dirname = args(0).string_value (); |
1401
|
365 |
|
366 if (error_state) |
1402
|
367 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
368 else |
1401
|
369 { |
5640
|
370 std::string fulldir = file_ops::tilde_expand (dirname); |
|
371 int status = -1; |
3523
|
372 std::string msg; |
2669
|
373 |
5639
|
374 if (nargin == 2) |
|
375 { |
|
376 if (args(1).string_value () == "s") |
5640
|
377 { |
|
378 bool doit = true; |
|
379 |
|
380 if (interactive && Vconfirm_recursive_rmdir) |
|
381 { |
|
382 std::string prompt |
|
383 = "remove entire contents of " + fulldir + "? "; |
|
384 |
|
385 doit = octave_yes_or_no (prompt); |
|
386 } |
|
387 |
|
388 if (doit) |
|
389 status = file_ops::recursive_rmdir (fulldir, msg); |
|
390 } |
5639
|
391 else |
|
392 error ("rmdir: expecting second argument to be \"s\""); |
|
393 } |
|
394 else |
5640
|
395 status = file_ops::rmdir (fulldir, msg); |
2669
|
396 |
|
397 if (status < 0) |
5476
|
398 { |
|
399 retval(2) = "rmdir"; |
|
400 retval(1) = msg; |
|
401 } |
|
402 else |
|
403 retval(0) = true; |
1401
|
404 } |
|
405 } |
1389
|
406 else |
1401
|
407 print_usage ("rmdir"); |
|
408 |
|
409 return retval; |
|
410 } |
|
411 |
3710
|
412 DEFUN (link, args, , |
|
413 "-*- texinfo -*-\n\ |
|
414 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} link (@var{old}, @var{new})\n\ |
|
415 Create a new link (also known as a hard link) to an existing file.\n\ |
|
416 \n\ |
|
417 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
418 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
419 system-dependent error message.\n\ |
5597
|
420 @seealso{symlink}\n\ |
3710
|
421 @end deftypefn") |
|
422 { |
|
423 octave_value_list retval; |
|
424 |
|
425 retval(1) = std::string (); |
|
426 retval(0) = -1.0; |
|
427 |
|
428 if (args.length () == 2) |
|
429 { |
|
430 std::string from = args(0).string_value (); |
|
431 |
|
432 if (error_state) |
|
433 gripe_wrong_type_arg ("link", args(0)); |
|
434 else |
|
435 { |
|
436 std::string to = args(1).string_value (); |
|
437 |
|
438 if (error_state) |
|
439 gripe_wrong_type_arg ("link", args(1)); |
|
440 else |
|
441 { |
|
442 std::string msg; |
|
443 |
|
444 int status = file_ops::link (from, to, msg); |
|
445 |
4233
|
446 retval(0) = status; |
3710
|
447 |
|
448 if (status < 0) |
|
449 retval(1) = msg; |
|
450 } |
|
451 } |
|
452 } |
|
453 else |
|
454 print_usage ("link"); |
|
455 |
|
456 return retval; |
|
457 } |
|
458 |
|
459 DEFUN (symlink, args, , |
|
460 "-*- texinfo -*-\n\ |
|
461 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} symlink (@var{old}, @var{new})\n\ |
|
462 Create a symbolic link @var{new} which contains the string @var{old}.\n\ |
|
463 \n\ |
|
464 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
465 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
466 system-dependent error message.\n\ |
5597
|
467 @seealso{link, readlink}\n\ |
3710
|
468 @end deftypefn") |
|
469 { |
|
470 octave_value_list retval; |
|
471 |
|
472 retval(1) = std::string (); |
|
473 retval(0) = -1.0; |
|
474 |
|
475 if (args.length () == 2) |
|
476 { |
|
477 std::string from = args(0).string_value (); |
|
478 |
|
479 if (error_state) |
|
480 gripe_wrong_type_arg ("symlink", args(0)); |
|
481 else |
|
482 { |
|
483 std::string to = args(1).string_value (); |
|
484 |
|
485 if (error_state) |
|
486 gripe_wrong_type_arg ("symlink", args(1)); |
|
487 else |
|
488 { |
|
489 std::string msg; |
|
490 |
|
491 int status = file_ops::symlink (from, to, msg); |
|
492 |
4233
|
493 retval(0) = status; |
3710
|
494 |
|
495 if (status < 0) |
|
496 retval(1) = msg; |
|
497 } |
|
498 } |
|
499 } |
|
500 else |
|
501 print_usage ("symlink"); |
|
502 |
|
503 return retval; |
|
504 } |
|
505 |
|
506 DEFUN (readlink, args, , |
|
507 "-*- texinfo -*-\n\ |
4169
|
508 @deftypefn {Built-in Function} {[@var{result}, @var{err}, @var{msg}] =} readlink (@var{symlink})\n\ |
3710
|
509 Read the value of the symbolic link @var{symlink}.\n\ |
|
510 \n\ |
|
511 If successful, @var{result} contains the contents of the symbolic link\n\ |
|
512 @var{symlink}, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
513 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
514 system-dependent error message.\n\ |
5597
|
515 @seealso{link, symlink}\n\ |
3710
|
516 @end deftypefn") |
|
517 { |
|
518 octave_value_list retval; |
|
519 |
|
520 retval(2) = std::string (); |
|
521 retval(1) = -1.0; |
|
522 retval(0) = std::string (); |
|
523 |
|
524 if (args.length () == 1) |
|
525 { |
|
526 std::string symlink = args(0).string_value (); |
|
527 |
|
528 if (error_state) |
|
529 gripe_wrong_type_arg ("readlink", args(0)); |
|
530 else |
|
531 { |
|
532 std::string result; |
|
533 std::string msg; |
|
534 |
|
535 int status = file_ops::readlink (symlink, result, msg); |
|
536 |
|
537 retval(0) = result; |
|
538 |
4233
|
539 retval(1) = status; |
3710
|
540 |
|
541 if (status < 0) |
|
542 retval(2) = msg; |
|
543 } |
|
544 } |
|
545 else |
|
546 print_usage ("readlink"); |
|
547 |
|
548 return retval; |
|
549 } |
|
550 |
1957
|
551 DEFUN (rename, args, , |
3301
|
552 "-*- texinfo -*-\n\ |
|
553 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})\n\ |
|
554 Change the name of file @var{old} to @var{new}.\n\ |
1401
|
555 \n\ |
3301
|
556 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
557 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
558 system-dependent error message.\n\ |
5597
|
559 @seealso{ls, dir}\n\ |
3301
|
560 @end deftypefn") |
1401
|
561 { |
2086
|
562 octave_value_list retval; |
1401
|
563 |
3523
|
564 retval(1) = std::string (); |
2669
|
565 retval(0) = -1.0; |
1401
|
566 |
|
567 if (args.length () == 2) |
|
568 { |
3523
|
569 std::string from = args(0).string_value (); |
1728
|
570 |
1401
|
571 if (error_state) |
1402
|
572 gripe_wrong_type_arg ("rename", args(0)); |
|
573 else |
1401
|
574 { |
3523
|
575 std::string to = args(1).string_value (); |
1728
|
576 |
1402
|
577 if (error_state) |
|
578 gripe_wrong_type_arg ("rename", args(1)); |
2669
|
579 else |
1402
|
580 { |
3523
|
581 std::string msg; |
2669
|
582 |
2926
|
583 int status = file_ops::rename (from, to, msg); |
2669
|
584 |
4233
|
585 retval(0) = status; |
2669
|
586 |
|
587 if (status < 0) |
|
588 retval(1) = msg; |
1402
|
589 } |
1401
|
590 } |
|
591 } |
|
592 else |
|
593 print_usage ("rename"); |
|
594 |
1389
|
595 return retval; |
|
596 } |
|
597 |
2495
|
598 DEFUN (glob, args, , |
3301
|
599 "-*- texinfo -*-\n\ |
|
600 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\ |
5444
|
601 Given an array of strings (as a char array or a cell array) in\n\ |
|
602 @var{pattern}, return a cell array of file names that match any of\n\ |
|
603 them, or an empty cell array if no patterns match. Tilde expansion\n\ |
|
604 is performed on each of the patterns before looking for matching file\n\ |
|
605 names. For example,\n\ |
2495
|
606 \n\ |
3301
|
607 @example\n\ |
|
608 @group\n\ |
|
609 glob (\"/vm*\")\n\ |
|
610 @result{} \"/vmlinuz\"\n\ |
|
611 @end group\n\ |
|
612 @end example\n\ |
5597
|
613 @seealso{dir, ls, stat, readdir}\n\ |
|
614 @end deftypefn") |
2495
|
615 { |
|
616 octave_value retval; |
|
617 |
|
618 if (args.length () == 1) |
|
619 { |
|
620 string_vector pat = args(0).all_strings (); |
|
621 |
|
622 if (error_state) |
|
623 gripe_wrong_type_arg ("glob", args(0)); |
|
624 else |
|
625 { |
2926
|
626 glob_match pattern (file_ops::tilde_expand (pat)); |
2495
|
627 |
4691
|
628 retval = Cell (pattern.glob ()); |
2495
|
629 } |
|
630 } |
|
631 else |
|
632 print_usage ("glob"); |
|
633 |
|
634 return retval; |
|
635 } |
|
636 |
2496
|
637 DEFUN (fnmatch, args, , |
3301
|
638 "-*- texinfo -*-\n\ |
|
639 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\ |
|
640 Return 1 or zero for each element of @var{string} that matches any of\n\ |
|
641 the elements of the string array @var{pattern}, using the rules of\n\ |
|
642 filename pattern matching. For example,\n\ |
2496
|
643 \n\ |
3301
|
644 @example\n\ |
|
645 @group\n\ |
|
646 fnmatch (\"a*b\", [\"ab\"; \"axyzb\"; \"xyzab\"])\n\ |
|
647 @result{} [ 1; 1; 0 ]\n\ |
|
648 @end group\n\ |
|
649 @end example\n\ |
|
650 @end deftypefn") |
2496
|
651 { |
|
652 octave_value retval; |
|
653 |
|
654 if (args.length () == 2) |
|
655 { |
|
656 string_vector pat = args(0).all_strings (); |
|
657 string_vector str = args(1).all_strings (); |
|
658 |
|
659 if (error_state) |
|
660 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
661 else |
|
662 { |
2926
|
663 glob_match pattern (file_ops::tilde_expand (pat)); |
2496
|
664 |
|
665 Array<bool> tmp = pattern.match (str); |
|
666 |
5275
|
667 octave_idx_type n = tmp.length (); |
2496
|
668 |
|
669 ColumnVector result (n); |
|
670 |
5275
|
671 for (octave_idx_type i = 0; i < n; i++) |
2496
|
672 result(i) = tmp(i); |
|
673 |
3418
|
674 retval = result; |
2496
|
675 } |
|
676 } |
|
677 else |
|
678 print_usage ("fnmatch"); |
|
679 |
|
680 return retval; |
|
681 } |
|
682 |
5777
|
683 DEFUN (filesep, args, , |
|
684 "-*- texinfo -*-\n\ |
|
685 @detypefn {Built-in Function} {} filesep ()\n\ |
|
686 Return the system-dependent character used to separate directory names.\n\ |
|
687 @seealso{pathsep, dir, ls}\n\ |
|
688 @end deftypefn") |
|
689 { |
|
690 octave_value retval; |
|
691 |
|
692 if (args.length () == 0) |
|
693 retval = file_ops::dir_sep_str; |
|
694 else |
|
695 print_usage ("filesep"); |
|
696 |
|
697 return retval; |
|
698 } |
|
699 |
|
700 DEFUN (pathsep, args, , |
|
701 "-*- texinfo -*-\n\ |
|
702 @deftypefn {Built-in Function} {} pathsep ()\n\ |
|
703 Return the system-dependent character used to separate directories in\n\ |
|
704 a path.\n\ |
|
705 @seealso{filesep, dir, ls}\n\ |
|
706 @end deftypefn") |
|
707 { |
|
708 octave_value retval; |
|
709 |
|
710 if (args.length () == 0) |
|
711 retval = dir_path::path_sep_str; |
|
712 else |
|
713 print_usage ("pathsep"); |
|
714 |
|
715 return retval; |
|
716 } |
|
717 |
5794
|
718 DEFUN (confirm_recursive_rmdir, args, nargout, |
|
719 "-*- texinfo -*-\n\ |
|
720 @deftypefn {Built-in Function} {@var{val} =} confirm_recursive_rmdir ()\n\ |
|
721 @deftypefnx {Built-in Function} {@var{old_val} =} confirm_recursive_rmdir (@var{new_val})\n\ |
|
722 Query or set the internal variable that controls whether Octave\n\ |
|
723 will ask for confirmation before recursively removing a directory tree.\n\ |
|
724 @end deftypefn") |
5640
|
725 { |
5794
|
726 return SET_INTERNAL_VARIABLE (confirm_recursive_rmdir); |
4264
|
727 } |
|
728 |
523
|
729 /* |
|
730 ;;; Local Variables: *** |
|
731 ;;; mode: C++ *** |
|
732 ;;; End: *** |
|
733 */ |