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