Next: Creating Communicating Processes
Up: Operating System Interface
Previous: Environment Access
  Index
Subsections
A number of built-in predicates is provided for dealing with UNIX files and
directories.
Here we consider only the file as a whole, for opening files and accessing
their contents refer to chapter 12.
The current working directory is an important notion in UNIX.
It can be read and changed within the ECLiPSe system by using getcwd/1 and
cd/1 respectively.
The current working directory is accessible as a global flag as well.
Reading and writing this flag is equivalent to the use of getcwd/1 and
cd/1:
[eclipse 1]: getcwd(Where).
Where = "/usr/name/prolog"
yes.
[eclipse 2]: cd(..).
yes.
[eclipse 3]: get_flag(cwd, Where)
Where = "/usr/name"
yes.
All ECLiPSe built-ins that take file names as arguments accept absolute
pathnames as well as relative pathnames starting at the current directory.
To look at the contents of a directory, read_directory/4 is available.
It takes a directory pathname and a filename pattern and returns a list
of subdirectories and a list of files matching the pattern.
The following metacharacters are recognised in the pattern:
* matches an arbitrary sequence of characters,
? matches any single character, [] matches one of the
characters inside the brackets unless the first one is a ^
in which case it matches any character but those inside the brackets.
[eclipse 1]: read_directory("/usr/john", "*", Dirlist, Filelist).
Dirlist = ["subdir1", "subdir2"]
Filelist = ["one.c", "two.c", "three.pl", "four.pl"]
yes.
For checking the existence of files, exists/1 is used.
For accessing any file properties there is get_file_info/3.
It can return file permissions, type, owner, size, inode, number of
links as well as creation, access and modification times
(as defined by the UNIX system call stat(2)), and accessibility
information.
It fails when the specified file does not exist.
Refer to the BIP book or help/1 for details.
For these basic operations with files, rename/2 and delete/1
are provided.
The utilities pathname/3, pathname/2 and suffix/2
are provided to ease the handling of filenames.
pathname/3 takes a full pathname and cuts it into the directory
pathname and the filename proper.
It also expands symbolic pathnames, starting with ~
, ~
user
or $var.
suffix/2 unifies its second argument with the suffix of the filename,
i.e. the part beginning with the last dot in the string.
[eclipse 1]: Name = "~octopus/prolog/file.pl",
pathname(Name, Path, File),
suffix(Name, Suffix).
Path = "/usr/octopus/prolog/"
File = "file.pl"
Name = "~octopus/prolog/file.pl"
Suffix = ".pl"
yes.
Next: Creating Communicating Processes
Up: Operating System Interface
Previous: Environment Access
  Index
1999-08-06