Previous Up Next

20.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 10.

20.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"
[eclipse 2]: cd(..).

[eclipse 3]: get_flag(cwd, Where)

Where = "/usr/name"
All ECLiPSe built-ins that take file names as arguments accept absolute pathnames as well as relative pathnames starting at the current directory.

20.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 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", "", ""]

20.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.

20.3.4  Renaming and Removing Files

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

20.3.5  Filenames

The filenames 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 filename such as "C:\my\path\name.ecl" should be writen as "//C/my/path/". The utility os_file_name/2 provides for the interconversion between the format used in ECLiPSe and the Operating Systems' format.

The utility pathname/4 is provided to ease the handling of filenames. It takes a full pathname and cuts it into the directory pathname, the filename 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/",
        pathname(Name, Path, File, Suffix).

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

Previous Up Next