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 |
5637
|
294 DEFCMD (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 |
5637
|
340 DEFCMD (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 |
5639
|
376 if (nargin == 2) |
|
377 { |
|
378 if (args(1).string_value () == "s") |
|
379 status = file_ops::recursive_rmdir (fulldir, msg); |
|
380 else |
|
381 error ("rmdir: expecting second argument to be \"s\""); |
|
382 } |
|
383 else |
|
384 status = file_ops::rmdir (fulldir, msg) |
2669
|
385 |
|
386 if (status < 0) |
5476
|
387 { |
|
388 retval(2) = "rmdir"; |
|
389 retval(1) = msg; |
|
390 } |
|
391 else |
|
392 retval(0) = true; |
1401
|
393 } |
|
394 } |
1389
|
395 else |
1401
|
396 print_usage ("rmdir"); |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
3710
|
401 DEFUN (link, args, , |
|
402 "-*- texinfo -*-\n\ |
|
403 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} link (@var{old}, @var{new})\n\ |
|
404 Create a new link (also known as a hard link) to an existing file.\n\ |
|
405 \n\ |
|
406 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
407 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
408 system-dependent error message.\n\ |
5597
|
409 @seealso{symlink}\n\ |
3710
|
410 @end deftypefn") |
|
411 { |
|
412 octave_value_list retval; |
|
413 |
|
414 retval(1) = std::string (); |
|
415 retval(0) = -1.0; |
|
416 |
|
417 if (args.length () == 2) |
|
418 { |
|
419 std::string from = args(0).string_value (); |
|
420 |
|
421 if (error_state) |
|
422 gripe_wrong_type_arg ("link", args(0)); |
|
423 else |
|
424 { |
|
425 std::string to = args(1).string_value (); |
|
426 |
|
427 if (error_state) |
|
428 gripe_wrong_type_arg ("link", args(1)); |
|
429 else |
|
430 { |
|
431 std::string msg; |
|
432 |
|
433 int status = file_ops::link (from, to, msg); |
|
434 |
4233
|
435 retval(0) = status; |
3710
|
436 |
|
437 if (status < 0) |
|
438 retval(1) = msg; |
|
439 } |
|
440 } |
|
441 } |
|
442 else |
|
443 print_usage ("link"); |
|
444 |
|
445 return retval; |
|
446 } |
|
447 |
|
448 DEFUN (symlink, args, , |
|
449 "-*- texinfo -*-\n\ |
|
450 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} symlink (@var{old}, @var{new})\n\ |
|
451 Create a symbolic link @var{new} which contains the string @var{old}.\n\ |
|
452 \n\ |
|
453 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
454 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
455 system-dependent error message.\n\ |
5597
|
456 @seealso{link, readlink}\n\ |
3710
|
457 @end deftypefn") |
|
458 { |
|
459 octave_value_list retval; |
|
460 |
|
461 retval(1) = std::string (); |
|
462 retval(0) = -1.0; |
|
463 |
|
464 if (args.length () == 2) |
|
465 { |
|
466 std::string from = args(0).string_value (); |
|
467 |
|
468 if (error_state) |
|
469 gripe_wrong_type_arg ("symlink", args(0)); |
|
470 else |
|
471 { |
|
472 std::string to = args(1).string_value (); |
|
473 |
|
474 if (error_state) |
|
475 gripe_wrong_type_arg ("symlink", args(1)); |
|
476 else |
|
477 { |
|
478 std::string msg; |
|
479 |
|
480 int status = file_ops::symlink (from, to, msg); |
|
481 |
4233
|
482 retval(0) = status; |
3710
|
483 |
|
484 if (status < 0) |
|
485 retval(1) = msg; |
|
486 } |
|
487 } |
|
488 } |
|
489 else |
|
490 print_usage ("symlink"); |
|
491 |
|
492 return retval; |
|
493 } |
|
494 |
|
495 DEFUN (readlink, args, , |
|
496 "-*- texinfo -*-\n\ |
4169
|
497 @deftypefn {Built-in Function} {[@var{result}, @var{err}, @var{msg}] =} readlink (@var{symlink})\n\ |
3710
|
498 Read the value of the symbolic link @var{symlink}.\n\ |
|
499 \n\ |
|
500 If successful, @var{result} contains the contents of the symbolic link\n\ |
|
501 @var{symlink}, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
502 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
503 system-dependent error message.\n\ |
5597
|
504 @seealso{link, symlink}\n\ |
3710
|
505 @end deftypefn") |
|
506 { |
|
507 octave_value_list retval; |
|
508 |
|
509 retval(2) = std::string (); |
|
510 retval(1) = -1.0; |
|
511 retval(0) = std::string (); |
|
512 |
|
513 if (args.length () == 1) |
|
514 { |
|
515 std::string symlink = args(0).string_value (); |
|
516 |
|
517 if (error_state) |
|
518 gripe_wrong_type_arg ("readlink", args(0)); |
|
519 else |
|
520 { |
|
521 std::string result; |
|
522 std::string msg; |
|
523 |
|
524 int status = file_ops::readlink (symlink, result, msg); |
|
525 |
|
526 retval(0) = result; |
|
527 |
4233
|
528 retval(1) = status; |
3710
|
529 |
|
530 if (status < 0) |
|
531 retval(2) = msg; |
|
532 } |
|
533 } |
|
534 else |
|
535 print_usage ("readlink"); |
|
536 |
|
537 return retval; |
|
538 } |
|
539 |
1957
|
540 DEFUN (rename, args, , |
3301
|
541 "-*- texinfo -*-\n\ |
|
542 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})\n\ |
|
543 Change the name of file @var{old} to @var{new}.\n\ |
1401
|
544 \n\ |
3301
|
545 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
546 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
547 system-dependent error message.\n\ |
5597
|
548 @seealso{ls, dir}\n\ |
3301
|
549 @end deftypefn") |
1401
|
550 { |
2086
|
551 octave_value_list retval; |
1401
|
552 |
3523
|
553 retval(1) = std::string (); |
2669
|
554 retval(0) = -1.0; |
1401
|
555 |
|
556 if (args.length () == 2) |
|
557 { |
3523
|
558 std::string from = args(0).string_value (); |
1728
|
559 |
1401
|
560 if (error_state) |
1402
|
561 gripe_wrong_type_arg ("rename", args(0)); |
|
562 else |
1401
|
563 { |
3523
|
564 std::string to = args(1).string_value (); |
1728
|
565 |
1402
|
566 if (error_state) |
|
567 gripe_wrong_type_arg ("rename", args(1)); |
2669
|
568 else |
1402
|
569 { |
3523
|
570 std::string msg; |
2669
|
571 |
2926
|
572 int status = file_ops::rename (from, to, msg); |
2669
|
573 |
4233
|
574 retval(0) = status; |
2669
|
575 |
|
576 if (status < 0) |
|
577 retval(1) = msg; |
1402
|
578 } |
1401
|
579 } |
|
580 } |
|
581 else |
|
582 print_usage ("rename"); |
|
583 |
1389
|
584 return retval; |
|
585 } |
|
586 |
2495
|
587 DEFUN (glob, args, , |
3301
|
588 "-*- texinfo -*-\n\ |
|
589 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\ |
5444
|
590 Given an array of strings (as a char array or a cell array) in\n\ |
|
591 @var{pattern}, return a cell array of file names that match any of\n\ |
|
592 them, or an empty cell array if no patterns match. Tilde expansion\n\ |
|
593 is performed on each of the patterns before looking for matching file\n\ |
|
594 names. For example,\n\ |
2495
|
595 \n\ |
3301
|
596 @example\n\ |
|
597 @group\n\ |
|
598 glob (\"/vm*\")\n\ |
|
599 @result{} \"/vmlinuz\"\n\ |
|
600 @end group\n\ |
|
601 @end example\n\ |
5597
|
602 @seealso{dir, ls, stat, readdir}\n\ |
|
603 @end deftypefn") |
2495
|
604 { |
|
605 octave_value retval; |
|
606 |
|
607 if (args.length () == 1) |
|
608 { |
|
609 string_vector pat = args(0).all_strings (); |
|
610 |
|
611 if (error_state) |
|
612 gripe_wrong_type_arg ("glob", args(0)); |
|
613 else |
|
614 { |
2926
|
615 glob_match pattern (file_ops::tilde_expand (pat)); |
2495
|
616 |
4691
|
617 retval = Cell (pattern.glob ()); |
2495
|
618 } |
|
619 } |
|
620 else |
|
621 print_usage ("glob"); |
|
622 |
|
623 return retval; |
|
624 } |
|
625 |
2496
|
626 DEFUN (fnmatch, args, , |
3301
|
627 "-*- texinfo -*-\n\ |
|
628 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\ |
|
629 Return 1 or zero for each element of @var{string} that matches any of\n\ |
|
630 the elements of the string array @var{pattern}, using the rules of\n\ |
|
631 filename pattern matching. For example,\n\ |
2496
|
632 \n\ |
3301
|
633 @example\n\ |
|
634 @group\n\ |
|
635 fnmatch (\"a*b\", [\"ab\"; \"axyzb\"; \"xyzab\"])\n\ |
|
636 @result{} [ 1; 1; 0 ]\n\ |
|
637 @end group\n\ |
|
638 @end example\n\ |
|
639 @end deftypefn") |
2496
|
640 { |
|
641 octave_value retval; |
|
642 |
|
643 if (args.length () == 2) |
|
644 { |
|
645 string_vector pat = args(0).all_strings (); |
|
646 string_vector str = args(1).all_strings (); |
|
647 |
|
648 if (error_state) |
|
649 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
650 else |
|
651 { |
2926
|
652 glob_match pattern (file_ops::tilde_expand (pat)); |
2496
|
653 |
|
654 Array<bool> tmp = pattern.match (str); |
|
655 |
5275
|
656 octave_idx_type n = tmp.length (); |
2496
|
657 |
|
658 ColumnVector result (n); |
|
659 |
5275
|
660 for (octave_idx_type i = 0; i < n; i++) |
2496
|
661 result(i) = tmp(i); |
|
662 |
3418
|
663 retval = result; |
2496
|
664 } |
|
665 } |
|
666 else |
|
667 print_usage ("fnmatch"); |
|
668 |
|
669 return retval; |
|
670 } |
|
671 |
4264
|
672 void |
|
673 symbols_of_dirfns (void) |
|
674 { |
|
675 DEFCONST (filesep, file_ops::dir_sep_str, |
|
676 "-*- texinfo -*-\n\ |
|
677 @defvr {Built-in Variable} filesep\n\ |
|
678 The character used to separate directory names. The value\n\ |
|
679 of this variable is system dependent.\n\ |
5597
|
680 @seealso{dir, ls}\n\ |
4264
|
681 @end defvr"); |
|
682 |
|
683 } |
|
684 |
523
|
685 /* |
|
686 ;;; Local Variables: *** |
|
687 ;;; mode: C++ *** |
|
688 ;;; End: *** |
|
689 */ |