1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1994-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <cerrno>
#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <string>
#include "file-ops.h"
#include "file-stat.h"
#include "glob-match.h"
#include "oct-env.h"
#include "oct-glob.h"
#include "pathsearch.h"
#include "str-vec.h"
#include "Cell.h"
#include "defun.h"
#include "dir-ops.h"
#include "error.h"
#include "errwarn.h"
#include "event-manager.h"
#include "input.h"
#include "load-path.h"
#include "octave.h"
#include "ovl.h"
#include "pager.h"
#include "procstream.h"
#include "sysdep.h"
#include "interpreter.h"
#include "unwind-prot.h"
#include "utils.h"
#include "variables.h"
// TRUE means we ask for confirmation before recursively removing a
// directory tree.
static bool Vconfirm_recursive_rmdir = true;
DEFMETHOD (cd, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} cd @var{dir}
@deftypefnx {} {} cd
@deftypefnx {} {@var{old_dir} =} cd
@deftypefnx {} {@var{old_dir} =} cd (@var{dir})
@deftypefnx {} {} chdir @dots{}
Change the current working directory to @var{dir}.
If called with no input or output arguments, the current directory is
changed to the user's home directory (@qcode{"~"}).
For example,
@example
cd ~/octave
@end example
@noindent
changes the current working directory to @file{~/octave}. If the
directory does not exist, an error message is printed and the working
directory is not changed.
@code{chdir} is an alias for @code{cd} and can be used in all of the same
calling formats.
Compatibility Note: When called with no arguments, @sc{matlab} prints the
present working directory rather than changing to the user's home directory.
@seealso{pwd, mkdir, rmdir, dir, ls}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin > 1)
print_usage ();
octave_value_list retval;
if (nargout > 0)
retval = octave_value (octave::sys::env::get_current_directory ());
if (nargin == 1)
{
std::string dirname = args(0).xstring_value ("cd: DIR must be a string");
if (! dirname.empty ())
interp.chdir (dirname);
}
else if (nargout == 0)
{
std::string home_dir = octave::sys::env::get_home_directory ();
if (! home_dir.empty ())
interp.chdir (home_dir);
}
return retval;
}
DEFALIAS (chdir, cd);
DEFUN (pwd, , ,
doc: /* -*- texinfo -*-
@deftypefn {} {} pwd ()
@deftypefnx {} {@var{dir} =} pwd ()
Return the current working directory.
@seealso{cd, dir, ls, mkdir, rmdir}
@end deftypefn */)
{
return ovl (octave::sys::env::get_current_directory ());
}
DEFUN (readdir, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{files} =} readdir (@var{dir})
@deftypefnx {} {[@var{files}, @var{err}, @var{msg}] =} readdir (@var{dir})
Return the names of files in the directory @var{dir} as a cell array of
strings.
If an error occurs, return an empty cell array in @var{files}.
If successful, @var{err} is 0 and @var{msg} is an empty string.
Otherwise, @var{err} is nonzero and @var{msg} contains a system-dependent
error message.
@seealso{ls, dir, glob, what}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
std::string dirname = args(0).xstring_value ("readdir: DIR must be a string");
octave_value_list retval = ovl (Cell (), -1.0, "");
dirname = octave::sys::file_ops::tilde_expand (dirname);
string_vector dirlist;
std::string msg;
if (octave::sys::get_dirlist (dirname, dirlist, msg))
{
retval(0) = Cell (dirlist.sort ());
retval(1) = 0.0;
}
else
retval(2) = msg;
return retval;
}
// FIXME: should maybe also allow second arg to specify mode?
// OTOH, that might cause trouble with compatibility later...
DEFUN (__mkdir__, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} __mkdir__ (@var{parent}, @var{dir})
Internal function called by mkdir.m.
@seealso{mkdir, rmdir, pwd, cd, umask}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ("mkdir");
std::string dirname;
if (nargin == 2)
{
std::string parent = args(0).xstring_value ("mkdir: PARENT must be a string");
std::string dir = args(1).xstring_value ("mkdir: DIR must be a string");
dirname = octave::sys::file_ops::concat (parent, dir);
}
else if (nargin == 1)
dirname = args(0).xstring_value ("mkdir: DIR must be a string");
dirname = octave::sys::file_ops::tilde_expand (dirname);
octave::sys::file_stat fs (dirname);
if (fs && fs.is_dir ())
{
// For Matlab compatibility, return true when directory already exists.
return ovl (true, "directory exists", "mkdir");
}
else
{
std::string msg;
int status = octave::sys::mkdir (dirname, 0777, msg);
if (status < 0)
return ovl (false, msg, "mkdir");
else
return ovl (true, "", "");
}
}
DEFMETHODX ("rmdir", Frmdir, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} rmdir @var{dir}
@deftypefnx {} {} rmdir (@var{dir}, "s")
@deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@dots{})
Remove the directory named @var{dir}.
If the optional second parameter is supplied with value @qcode{"s"},
recursively remove all subdirectories as well.
If successful, @var{status} is 1, and @var{msg}, @var{msgid} are empty
character strings (""). Otherwise, @var{status} is 0, @var{msg} contains a
system-dependent error message, and @var{msgid} contains a unique message
identifier.
@seealso{mkdir, confirm_recursive_rmdir, pwd}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
std::string dirname = args(0).xstring_value ("rmdir: DIR must be a string");
std::string fulldir = octave::sys::file_ops::tilde_expand (dirname);
int status = -1;
std::string msg;
octave::event_manager& evmgr = interp.get_event_manager ();
if (nargin == 2)
{
if (args(1).string_value () != "s")
error (R"(rmdir: second argument must be "s" for recursive removal)");
bool doit = true;
if (interp.interactive ()
&& ! octave::application::forced_interactive ()
&& Vconfirm_recursive_rmdir)
{
octave::input_system& input_sys = interp.get_input_system ();
std::string prompt = "remove entire contents of " + fulldir + "? ";
doit = input_sys.yes_or_no (prompt);
}
if (doit)
{
evmgr.file_remove (fulldir, "");
status = octave::sys::recursive_rmdir (fulldir, msg);
}
}
else
{
evmgr.file_remove (fulldir, "");
status = octave::sys::rmdir (fulldir, msg);
}
evmgr.file_renamed (status >= 0);
if (status < 0)
return ovl (false, msg, "rmdir");
else
return ovl (true, "", "");
}
DEFUNX ("link", Flink, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} link @var{old} @var{new}
@deftypefnx {} {[@var{err}, @var{msg}] =} link (@var{old}, @var{new})
Create a new link (also known as a hard link) to an existing file.
If successful, @var{err} is 0 and @var{msg} is an empty string.
Otherwise, @var{err} is nonzero and @var{msg} contains a system-dependent
error message.
@seealso{symlink, unlink, readlink, lstat}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
std::string from = args(0).xstring_value ("link: OLD must be a string");
std::string to = args(1).xstring_value ("link: NEW must be a string");
from = octave::sys::file_ops::tilde_expand (from);
to = octave::sys::file_ops::tilde_expand (to);
std::string msg;
int status = octave::sys::link (from, to, msg);
if (status < 0)
return ovl (-1.0, msg);
else
return ovl (status, "");
}
DEFUNX ("symlink", Fsymlink, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} symlink @var{old} @var{new}
@deftypefnx {} {[@var{err}, @var{msg}] =} symlink (@var{old}, @var{new})
Create a symbolic link @var{new} which contains the string @var{old}.
If successful, @var{err} is 0 and @var{msg} is an empty string.
Otherwise, @var{err} is nonzero and @var{msg} contains a system-dependent
error message.
@seealso{link, unlink, readlink, lstat}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
std::string from = args(0).xstring_value ("symlink: OLD must be a string");
std::string to = args(1).xstring_value ("symlink: NEW must be a string");
from = octave::sys::file_ops::tilde_expand (from);
to = octave::sys::file_ops::tilde_expand (to);
std::string msg;
int status = octave::sys::symlink (from, to, msg);
if (status < 0)
return ovl (-1.0, msg);
else
return ovl (status, "");
}
DEFUNX ("readlink", Freadlink, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} readlink @var{symlink}
@deftypefnx {} {[@var{result}, @var{err}, @var{msg}] =} readlink (@var{symlink})
Read the value of the symbolic link @var{symlink}.
If successful, @var{result} contains the contents of the symbolic link
@var{symlink}, @var{err} is 0, and @var{msg} is an empty string.
Otherwise, @var{err} is nonzero and @var{msg} contains a system-dependent
error message.
@seealso{lstat, symlink, link, unlink, delete}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
std::string symlink = args(0).xstring_value ("readlink: SYMLINK must be a string");
symlink = octave::sys::file_ops::tilde_expand (symlink);
std::string result, msg;
int status = octave::sys::readlink (symlink, result, msg);
if (status < 0)
return ovl ("", -1.0, msg);
else
return ovl (result, status, "");
}
DEFMETHODX ("rename", Frename, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} rename @var{old} @var{new}
@deftypefnx {} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})
Change the name of file @var{old} to @var{new}.
If successful, @var{err} is 0 and @var{msg} is an empty string.
Otherwise, @var{err} is nonzero and @var{msg} contains a system-dependent
error message.
@seealso{movefile, copyfile, ls, dir}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
std::string from = args(0).xstring_value ("rename: OLD must be a string");
std::string to = args(1).xstring_value ("rename: NEW must be a string");
from = octave::sys::file_ops::tilde_expand (from);
to = octave::sys::file_ops::tilde_expand (to);
std::string msg;
octave::event_manager& evmgr = interp.get_event_manager ();
evmgr.file_remove (from, to);
int status = octave::sys::rename (from, to, msg);
if (status < 0)
{
evmgr.file_renamed (false);
return ovl (-1.0, msg);
}
else
{
evmgr.file_renamed (true);
return ovl (status, "");
}
}
DEFUN (glob, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} glob (@var{pattern})
Given an array of pattern strings (as a char array or a cell array) in
@var{pattern}, return a cell array of filenames that match any of
them, or an empty cell array if no patterns match.
The pattern strings are interpreted as filename globbing patterns (as they
are used by Unix shells).
Within a pattern
@table @code
@item *
matches any string, including the null string,
@item ?
matches any single character, and
@item [@dots{}]
matches any of the enclosed characters.
@end table
Tilde expansion is performed on each of the patterns before looking for
matching filenames. For example:
@example
ls
@result{}
file1 file2 file3 myfile1 myfile1b
glob ("*file1")
@result{}
@{
[1,1] = file1
[2,1] = myfile1
@}
glob ("myfile?")
@result{}
@{
[1,1] = myfile1
@}
glob ("file[12]")
@result{}
@{
[1,1] = file1
[2,1] = file2
@}
@end example
@seealso{ls, dir, readdir, what}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
string_vector pat = args(0).xstring_vector_value ("glob: PATTERN must be a string");
glob_match pattern (octave::sys::file_ops::tilde_expand (pat));
return ovl (Cell (pattern.glob ()));
}
DEFUN (__wglob__, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} __wglob__ (@var{pattern})
Windows-like glob for dir.
Given an array of pattern strings (as a char array or a cell array) in
@var{pattern}, return a cell array of filenames that match any of
them, or an empty cell array if no patterns match.
The pattern strings are interpreted as filename globbing patterns
(roughly as they are used by Windows dir).
Within a pattern
@table @code
@item *
matches any string, including the null string,
@item ?
matches any single character, and
@item *.*
matches any string, even if no . is present.
@end table
Tilde expansion is performed on each of the patterns before looking for
matching filenames. For example:
@example
ls
@result{}
file1 file2 file3 myfile1 myfile1b
glob ("*file1")
@result{}
@{
[1,1] = file1
[2,1] = myfile1
@}
glob ("myfile?")
@result{}
@{
[1,1] = myfile1
@}
glob ("*.*")
@result{}
@{
[1,1] = file1
[2,1] = file2
[3,1] = file3
[4,1] = myfile1
[5,1] = myfile1b
@}
@end example
@seealso{glob, dir}
@end deftypefn */)
{
if (args.length () == 0)
return ovl ();
string_vector pat = args(0).string_vector_value ();
string_vector pattern (octave::sys::file_ops::tilde_expand (pat));
return ovl (Cell (octave::sys::windows_glob (pattern)));
}
/*
%!test
%! tmpdir = tempname;
%! filename = {"file1", "file2", "file3", "myfile1", "myfile1b"};
%! if (mkdir (tmpdir))
%! cwd = pwd;
%! cd (tmpdir);
%! if (strcmp (canonicalize_file_name (pwd), canonicalize_file_name (tmpdir)))
%! a = 0;
%! for n = 1:5
%! save (filename{n}, "a");
%! endfor
%! else
%! rmdir (tmpdir);
%! error ("Couldn't change to temporary dir");
%! endif
%! else
%! error ("Couldn't create temporary directory");
%! endif
%! result1 = glob ("*file1");
%! result2 = glob ("myfile?");
%! result3 = glob ("file[12]");
%! for n = 1:5
%! delete (filename{n});
%! endfor
%! cd (cwd);
%! rmdir (tmpdir);
%! assert (result1, {"file1"; "myfile1"});
%! assert (result2, {"myfile1"});
%! assert (result3, {"file1"; "file2"});
*/
DEFUN (__fnmatch__, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} fnmatch (@var{pattern}, @var{string})
Return true or false for each element of @var{string} that matches any of
the elements of the string array @var{pattern}, using the rules of
filename pattern matching.
For example:
@example
@group
fnmatch ("a*b", @{"ab"; "axyzb"; "xyzab"@})
@result{} [ 1; 1; 0 ]
@end group
@end example
@seealso{glob, regexp}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
string_vector pat = args(0).string_vector_value ();
string_vector str = args(1).string_vector_value ();
glob_match pattern (octave::sys::file_ops::tilde_expand (pat));
return ovl (pattern.match (str));
}
DEFUN (filesep, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} filesep ()
@deftypefnx {} {} filesep ("all")
Return the system-dependent character used to separate directory names.
If @qcode{"all"} is given, the function returns all valid file separators
in the form of a string. The list of file separators is system-dependent.
It is @samp{/} (forward slash) under UNIX or @w{Mac OS X}, @samp{/} and
@samp{\} (forward and backward slashes) under Windows.
@seealso{pathsep}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin > 1)
print_usage ();
octave_value retval;
if (nargin == 0)
retval = octave::sys::file_ops::dir_sep_str ();
else
{
std::string s = args(0).xstring_value ("filesep: argument must be a string");
if (s != "all")
error (R"(filesep: argument must be "all")");
retval = octave::sys::file_ops::dir_sep_chars ();
}
return retval;
}
DEFUN (pathsep, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{val} =} pathsep ()
Query the character used to separate directories in a path.
@seealso{filesep}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin > 0)
print_usage ();
return ovl (octave::directory_path::path_sep_str ());
}
DEFUN (confirm_recursive_rmdir, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{val} =} confirm_recursive_rmdir ()
@deftypefnx {} {@var{old_val} =} confirm_recursive_rmdir (@var{new_val})
@deftypefnx {} {} confirm_recursive_rmdir (@var{new_val}, "local")
Query or set the internal variable that controls whether Octave
will ask for confirmation before recursively removing a directory tree.
When called from inside a function with the @qcode{"local"} option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
@seealso{rmdir}
@end deftypefn */)
{
return SET_INTERNAL_VARIABLE (confirm_recursive_rmdir);
}
|