Previous Up Next

21.3  File System

A number of built-in predicates is provided for dealing with files and directories. Here we consider only the file as a whole, for opening files and accessing their contents refer to chapter 11.

21.3.1  Current Directory

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.

21.3.2  Looking at Directories

To look at the contents of a directory, read_directory/4 is available. It takes a directory pathname and a file name 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.

21.3.3  Checking Files

For checking the existence of files, exists/1 or the more powerful existing_file/4 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); not all entries are meaningful under Windows), and accessibility information. It fails when the specified file does not exist. Refer to the reference manual or help/1 for details.

21.3.4  Renaming and Removing Files

For these basic operations with files, rename/2 and delete/1 are provided.

21.3.5  File names

The file names used by ECLiPSe is in the Unix format, including on Window platforms, with the addition that the disk such as C: is indicated by //C/, so a Windows file name such as "C:\my\path\name.ecl" should be writen as "//C/my/path/name.pl". The utility predicate os_file_name/2 provides for the conversion between the format used in ECLiPSe and the Operating Systems’ format.

The utility pathname/4 is provided to ease the handling of file names. It takes a full pathname and cuts it into the directory pathname, the file name proper and a suffix ( the part beginning with the last dot in the string). It also expands symbolic pathnames, starting with ~, ~user or $var.

[eclipse 1]: Name = "~octopus/prolog/file.pl",
        pathname(Name, Path, File, Suffix).

Path = "/usr/octopus/prolog/"
File = "file.pl"
Name = "~octopus/prolog/file.pl"
Suffix = ".pl"
yes.

Previous Up Next