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