Operating system utilities
Author(s): Daniel Cabeza, Manuel Carro.
This module contains predicates for invoking services which are typically provided by the operating system. Note that the predicates which take names of files or directories as arguments in this module expect atoms, not path aliases. I.e., generally these predicates will not call absolute_file_name/2 on names of files or directories taken as arguments.
Usage and interface
- Library usage:
:- use_module(library(system)). - Exports:
- Predicates:
time/1, datime/1, datime/9, getenvstr/2, setenvstr/2, current_env/2, set_env/2, del_env/1, c_errno/1, copy_file/2, copy_file/3, extract_paths/2, get_tmp_dir/1, current_host/1, current_executable/1, umask/2, make_directory/2, make_directory/1, working_directory/2, cd/1, directory_files/2, mktemp/2, mktemp_in_tmp/2, file_exists/1, file_exists/2, file_property/2, file_properties/6, modif_time/2, modif_time0/2, touch/1, fmode/2, chmod/2, chmod/3, set_exec_mode/2, delete_file/1, delete_directory/1, rename_file/2, pause/1, wait/2, kill/2, get_pid/1, get_uid/1, get_gid/1, get_pwnam/1, get_grnam/1, shell/0, shell/1, shell/2, system/1, system/2, using_windows/0, winpath/2, winpath/3, winpath_c/3, cyg2win/3, cyg2win_a/3, no_swapslash/3, replace_characters/4, system_error_report/1, get_home/1, find_executable/2. - Regular Types:
datime_struct/1. - Multifiles:
define_flag/3.
- Predicates:
- Imports:
- System library modules:
lists, pathnames. - Packages:
prelude, nonpure, condcomp, assertions, nortchecks, isomodes, hiord, regtypes, define_flag.
- System library modules:
Documentation on exports
Time is unified with the number of seconds elapsed since January, 1, 1970 (UTC).
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)Time is an integer. - The following properties hold upon exit:
(basic_props:int/1)Time is an integer.
Datime is unified with a term of the form datime(Year,Month,Day,Hour,Minute,Second) which contains the current date and time.
Usage:
- Call and exit should be compatible with:
(system:datime_struct/1)system:datime_struct(Datime) - The following properties should hold upon exit:
(system:datime_struct/1)system:datime_struct(Datime)
Time is as in time/1. WeekDay is the number of days since Sunday, in the range 0 to 6. YearDay is the number of days since January 1, in the range 0 to 365.
(Trust) Usage 1:
If Time is given, the rest of the arguments are unified with the date and time to which the Time argument refers.
- Calls should, and exit will be compatible with:
(basic_props:int/1)Year is an integer.
(basic_props:int/1)Month is an integer.
(basic_props:int/1)Day is an integer.
(basic_props:int/1)Hour is an integer.
(basic_props:int/1)Min is an integer.
(basic_props:int/1)Sec is an integer.
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer. - The following properties should hold at call time:
(basic_props:int/1)Time is an integer. - The following properties hold upon exit:
(basic_props:int/1)Year is an integer.
(basic_props:int/1)Month is an integer.
(basic_props:int/1)Day is an integer.
(basic_props:int/1)Hour is an integer.
(basic_props:int/1)Min is an integer.
(basic_props:int/1)Sec is an integer.
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer.
(Trust) Usage 2:
Bound Time, WeekDay and YearDay as determined by the input arguments.
- Calls should, and exit will be compatible with:
(basic_props:int/1)Time is an integer.
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer. - The following properties should hold at call time:
(basic_props:int/1)Year is an integer.
(basic_props:int/1)Month is an integer.
(basic_props:int/1)Day is an integer.
(basic_props:int/1)Hour is an integer.
(basic_props:int/1)Min is an integer.
(basic_props:int/1)Sec is an integer. - The following properties hold upon exit:
(basic_props:int/1)Time is an integer.
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer.
(Trust) Usage 3:
Bound Time to current time and the rest of the arguments refer to current time.
- Calls should, and exit will be compatible with:
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer. - The following properties should hold at call time:
(term_typing:var/1)Time is a free variable.
(term_typing:var/1)Year is a free variable.
(term_typing:var/1)Month is a free variable.
(term_typing:var/1)Day is a free variable.
(term_typing:var/1)Hour is a free variable.
(term_typing:var/1)Min is a free variable.
(term_typing:var/1)Sec is a free variable. - The following properties hold upon exit:
(basic_props:int/1)Time is an integer.
(basic_props:int/1)Year is an integer.
(basic_props:int/1)Month is an integer.
(basic_props:int/1)Day is an integer.
(basic_props:int/1)Hour is an integer.
(basic_props:int/1)Min is an integer.
(basic_props:int/1)Sec is an integer.
(basic_props:int/1)WeekDay is an integer.
(basic_props:int/1)YearDay is an integer.
datime_struct(datime(Year,Month,Day,Hour,Min,Sec)) :- int(Year), int(Month), int(Day), int(Hour), int(Min), int(Sec).
The environment variable Name has Value. Fails if variable Name is not defined.
Usage:
- Call and exit should be compatible with:
(basic_props:string/1)Value is a string (a list of character codes). - The following properties should hold at call time:
(basic_props:atm/1)Name is an atom. - The following properties should hold upon exit:
(basic_props:string/1)Value is a string (a list of character codes).
The environment variable Name is assigned Value.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Name is an atom.
(basic_props:string/1)Value is a string (a list of character codes).
If Name is an atom, then unifies the environment variable Name with its value. Note that this predicate can be used to enumerate all the environment variables using backtracking.
Usage:
- Call and exit should be compatible with:
(basic_props:atm/1)Name is an atom.
(basic_props:atm/1)Value is an atom. - The following properties should hold upon exit:
(basic_props:atm/1)Name is an atom.
(basic_props:atm/1)Value is an atom.
The environment variable Name is assigned Value.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Name is an atom.
(basic_props:atm/1)Value is an atom.
The environment variable Name is removed.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Name is an atom.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)Arg1 is an integer. - The following properties hold upon exit:
(basic_props:int/1)Arg1 is an integer.
Copies the file Source to Destination.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Source is an atom.
(basic_props:atm/1)Destination is an atom.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Arg1 is an atom.
(basic_props:atm/1)Arg2 is an atom.
(system:copy_options/1)system:copy_options(Arg3)
Split PathList atom into the list of paths Paths. Paths in String are separated by the path list separator character (colons in POSIX-like systems, semicolons in Windows). Empty paths are removed from Paths. Paths is empty if PathList is the empty atom.
(True) Usage:
- Calls should, and exit will be compatible with:
(basic_props:list/2)Paths is a list of atms. - The following properties should hold at call time:
(basic_props:atm/1)PathList is an atom. - The following properties hold upon exit:
(basic_props:list/2)Paths is a list of atms.
Usage:get_tmp_dir(TmpDir)
TmpDir is the (normalized) temporary directory for scratch space. On POSIX systems it is computed from normalizing the path specified in the TMPDIR environment variable. On Windows it is determined by the TMP or TEMP environment variable. If none is defined, this predicate tries to guess it from some usual locations (/tmp, /var/tmp, /usr/tmp on POSIX, c:\temp, c:\tmp, \temp, \tmp on Windows) or use . as last resort.
- The following properties should hold at call time:
(term_typing:var/1)TmpDir is a free variable. - The following properties should hold upon exit:
(basic_props:atm/1)TmpDir is an atom.
Hostname is unified with the fully qualified name of the host.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)Hostname is an atom. - The following properties hold upon exit:
(basic_props:atm/1)Hostname is an atom.
Unifies Path with the path to the current Ciao executable (which may be a standalone binary or bytecode executable)
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)Path is an atom. - The following properties hold upon exit:
(basic_props:atm/1)Path is an atom.
(Trust) Usage 1:umask(OldMask,NewMask)
The process file creation mask was OldMask, and it is changed to NewMask.
- The following properties should hold at call time:
(basic_props:int/1)NewMask is an integer. - The following properties hold upon exit:
(basic_props:int/1)OldMask is an integer.
(Trust) Usage 2:umask(OldMask,NewMask)
Gets the process file creation mask without changing it.
- The following properties should hold at call time:
(term_typing:var/1)OldMask is a free variable.
(term_typing:var/1)NewMask is a free variable.
(term_compare:== /2)The terms OldMask and NewMask are strictly identical. - The following properties hold upon exit:
(basic_props:int/1)OldMask is an integer.
(basic_props:int/1)NewMask is an integer.
Creates the directory DirName with a given Mode. This is, as usual, operated against the current umask value.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)DirName is an atom.
(basic_props:int/1)Mode is an integer.
Equivalent to make_directory(D,0o777).
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)DirName is an atom.
Unifies current working directory with OldDir, and then changes the working directory to NewDir. Calling working_directory(Dir,Dir) simply unifies Dir with the current working directory without changing anything else.
(Trust) Usage 1:
Changes current working directory.
- Calls should, and exit will be compatible with:
(basic_props:atm/1)OldDir is an atom. - The following properties should hold at call time:
(basic_props:atm/1)NewDir is an atom. - The following properties hold upon exit:
(basic_props:atm/1)OldDir is an atom.
(Trust) Usage 2:working_directory(OldDir,NewDir)
Gets current working directory.
- The following properties should hold at call time:
(term_typing:var/1)OldDir is a free variable.
(term_typing:var/1)NewDir is a free variable.
(term_compare:== /2)The terms OldDir and NewDir are strictly identical. - The following properties hold upon exit:
(basic_props:atm/1)OldDir is an atom.
(basic_props:atm/1)NewDir is an atom.
Changes working directory to Path.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Path is an atom.
FileList is the unordered list of entries (files, directories, etc.) in Directory.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:list/2)FileList is a list of atms. - The following properties should hold at call time:
(basic_props:atm/1)Directory is an atom. - The following properties hold upon exit:
(basic_props:list/2)FileList is a list of atms.
Returns a unique Filename based on Template: Template must be a valid file name with six trailing X, which are substituted to create a new file name. Filename is created in read/write mode but closed immediately after creation.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)Filename is an atom. - The following properties should hold at call time:
(basic_props:atm/1)Template is an atom. - The following properties hold upon exit:
(basic_props:atm/1)Filename is an atom.
Succeeds if File (a file or directory) exists (and is accessible).
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
File (a file or directory) exists and it is accessible with Mode, as in the Unix call access(2). Typically, Mode is 4 for read permission, 2 for write permission and 1 for execute permission.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
(basic_props:int/1)Mode is an integer. - The following properties hold upon exit:
(basic_props:atm/1)File is an atom.
(basic_props:int/1)Mode is an integer.
File has the property Property. The possible properties are:
- type(Type)
- Type is one of regular, directory, fifo, socket or unknown.
- linkto(Linkto)
- If File is a symbolic link, Linkto is the file pointed to by the link (and the other properties come from that file, not from the link itself).
- mod_time(ModTime)
- ModTime is the time of last modification (seconds since January, 1, 1970).
- mode(Protection)
- Protection is the protection mode.
- size(Size)
- Size is the size.
If Property is uninstantiated, the predicate will enumerate the properties on backtracking.
Usage:
- Call and exit should be compatible with:
(basic_props:struct/1)Property is a compound term. - The following properties should hold at call time:
(basic_props:atm/1)File is an atom. - The following properties should hold upon exit:
(basic_props:struct/1)Property is a compound term.
The file Path has the following properties:
- File type Type (one of regular, directory, fifo, socket or unknown).
- If Path is a symbolic link, Linkto is the file pointed to. All other properties come from the file pointed, not the link. Linkto is ” if Path is not a symbolic link.
- Time of last modification Time (seconds since January, 1, 1970).
- Protection mode Protection.
- Size in bytes Size.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)Type is an atom.
(basic_props:atm/1)Linkto is an atom.
(basic_props:int/1)Time is an integer.
(basic_props:int/1)Protection is an integer.
(basic_props:int/1)Size is an integer. - The following properties should hold at call time:
(basic_props:atm/1)Path is an atom. - The following properties hold upon exit:
(basic_props:atm/1)Type is an atom.
(basic_props:atm/1)Linkto is an atom.
(basic_props:int/1)Time is an integer.
(basic_props:int/1)Protection is an integer.
(basic_props:int/1)Size is an integer.
The file File was last modified at Time, which is in seconds since January, 1, 1970. Fails if File does not exist.
Usage:
- Call and exit should be compatible with:
(basic_props:int/1)Time is an integer. - The following properties should hold at call time:
(basic_props:atm/1)File is an atom. - The following properties should hold upon exit:
(basic_props:int/1)Time is an integer.
If File exists, Time is its latest modification time, as in modif_time/2. Otherwise, if File does not exist, Time is zero.
Usage:
- Call and exit should be compatible with:
(basic_props:int/1)Time is an integer. - The following properties should hold at call time:
(basic_props:atm/1)File is an atom. - The following properties should hold upon exit:
(basic_props:int/1)Time is an integer.
Change the modification time of File to the current time of day. If the file does not exist, it is created with default permissions.
Note: This operation cannot be fully implemented with modif_time/2. In POSIX systems, that can be done as long as the user has write permissions on the file, even if the owner is different. Change of modification time to arbitrary time values is not allowed in this case.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
The file File has protection mode Mode.
Usage:
- Call and exit should be compatible with:
(basic_props:int/1)Mode is an integer. - The following properties should hold at call time:
(basic_props:atm/1)File is an atom. - The following properties should hold upon exit:
(basic_props:int/1)Mode is an integer.
Change the protection mode of file File to NewMode.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
(basic_props:int/1)NewMode is an integer.
The file File has protection mode OldMode and it is changed to NewMode.
Usage 1:
- Call and exit should be compatible with:
(basic_props:int/1)OldMode is an integer. - The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
(basic_props:int/1)NewMode is an integer. - The following properties should hold upon exit:
(basic_props:int/1)OldMode is an integer.
Usage 2:chmod(File,OldMode,NewMode)
If OldMode is identical to NewMode then it is equivalent to fmode(File,OldMode)
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
(term_typing:var/1)OldMode is a free variable.
(term_typing:var/1)NewMode is a free variable. - The following properties should hold upon exit:
(basic_props:atm/1)File is an atom.
(basic_props:atm/1)OldMode is an atom.
(basic_props:atm/1)NewMode is an atom.
Copies the permissions of SourceName to ExecName adding permissions to execute.
(True) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)SourceName is an atom.
(basic_props:atm/1)ExecName is an atom.
Delete the file File.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
Delete the directory Directory.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File is an atom.
Change the name of File1 to File2.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:atm/1)File1 is an atom.
(basic_props:atm/1)File2 is an atom.
Make this thread sleep for some Seconds.
(Trust) Usage:
- The following properties should hold at call time:
(basic_props:int/1)Seconds is an integer.
(Trust) Usage:wait(Pid,ReturnCode)
wait/2 waits for the process numbered Pid. Fails if the process does not terminate normally or in case of error (see C waitpid() for details). RetCode is the process return code.
- The following properties should hold at call time:
(term_typing:nonvar/1)Pid is currently a term which is not a free variable.
(term_typing:var/1)ReturnCode is a free variable.
(basic_props:int/1)Pid is an integer.
(term_typing:var/1)ReturnCode is a free variable. - The following properties hold upon exit:
(basic_props:int/1)ReturnCode is an integer.
(Trust) Usage:kill(Pid,Signal)
kill/2 sends the signal Signal to the process or process group specified by Pid. See Unix man page for a detailed description of signals.
- The following properties should hold at call time:
(term_typing:nonvar/1)Pid is currently a term which is not a free variable.
(term_typing:nonvar/1)Signal is currently a term which is not a free variable.
(basic_props:int/1)Pid is an integer.
(basic_props:int/1)Signal is an integer.
Unifies Pid with the process identificator of the current process or thread.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)Pid is an integer. - The following properties hold upon exit:
(basic_props:int/1)Pid is an integer.
Unifies Uid with the user id of the current process.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)Uid is an integer. - The following properties hold upon exit:
(basic_props:int/1)Uid is an integer.
Unifies Gid with the group id of the current process.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)Uid is an integer. - The following properties hold upon exit:
(basic_props:int/1)Uid is an integer.
Unifies User with the user of the current process, as specified in the /etc/passwd file.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)User is an atom. - The following properties hold upon exit:
(basic_props:atm/1)User is an atom.
Unifies Group with the group of the current process, as specified in the /etc/group file.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:atm/1)Group is an atom. - The following properties hold upon exit:
(basic_props:atm/1)Group is an atom.
(Trust) Usage:
Executes the OS-specific system shell. When the shell process terminates, control is returned to Prolog. See shell/2 for details.
Command is executed in the OS-specific system shell. It succeeds if the exit code is zero and fails otherwise. See shell/2 for details.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Command is an atom.
Executes Command using the OS-specific system shell and stores the exit code in RetCode.
On POSIX-like systems the system shell is specified by the SHELL environment variable ($SHELL -c "command" for passing user commands). On Windows (native builds, MinGW) it is specified by the COMSPEC environment variable (%COMSPEC% /s /c "command" for passing user commands).
Note that the use of shell/2 predicates is discouraged for portability and security reasons. Please consider process for a more robust way to launch external processes.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)RetCode is an integer. - The following properties should hold at call time:
(basic_props:atm/1)Command is an atom. - The following properties hold upon exit:
(basic_props:int/1)RetCode is an integer.
Like shell/1 but ignores exit code.
Usage:
- The following properties should hold at call time:
(basic_props:atm/1)Command is an atom.
Synonym for shell/2.
(Trust) Usage:
- Calls should, and exit will be compatible with:
(basic_props:int/1)RetCode is an integer. - The following properties should hold at call time:
(basic_props:atm/1)Command is an atom. - The following properties hold upon exit:
(basic_props:int/1)RetCode is an integer.
(True) Usage:
Running in a Windows native environment (not emulating a POSIX system). On Cygwin platforms, this is currently detected by looking at the CIAOSCRIPT environment variable (which is added to the shell profile files during installation).
Usage 1:winpath(A,B)
- The following properties should hold at call time:
(basic_props:atm/1)A is an atom.
(term_typing:var/1)B is a free variable. - The following properties should hold upon exit:
(basic_props:atm/1)A is an atom.
(basic_props:atm/1)B is an atom.
Usage 2:winpath(A,B)
- The following properties should hold at call time:
(term_typing:var/1)A is a free variable.
(basic_props:atm/1)B is an atom. - The following properties should hold upon exit:
(basic_props:atm/1)A is an atom.
(basic_props:atm/1)B is an atom.
Usage 3:winpath(A,B)
- The following properties should hold at call time:
(basic_props:atm/1)A is an atom.
(basic_props:atm/1)B is an atom. - The following properties should hold upon exit:
(basic_props:atm/1)A is an atom.
(basic_props:atm/1)B is an atom.
Option specifies if you want to get a relative or a full path. Posix represent a path as usual in unix, and WinPath is the Windows-Style representation of Posix.
Usage 1:
- The following properties should hold at call time:
(term_typing:var/1)Option is a free variable.
(term_typing:var/1)Posix is a free variable.
(basic_props:atm/1)WinPath is an atom. - The following properties should hold upon exit:
(system:winpath_option/1)system:winpath_option(Option)
(basic_props:atm/1)Posix is an atom.
Usage 2:
- The following properties should hold at call time:
(term_typing:var/1)Option is a free variable.
(basic_props:atm/1)Posix is an atom.
(term_typing:var/1)WinPath is a free variable. - The following properties should hold upon exit:
(system:winpath_option/1)system:winpath_option(Option)
(basic_props:atm/1)WinPath is an atom.
Usage:cyg2win(CygWinPath,WindowsPath,SwapSlash)
Converts a posix path to a Windows-style path. If SwapSlash is swap, slashes are converted in to backslash. If it is noswap, they are preserved.
- The following properties should hold at call time:
(basic_props:string/1)CygWinPath is a string (a list of character codes).
(term_typing:var/1)WindowsPath is a free variable.
(term_typing:atom/1)SwapSlash is currently instantiated to an atom. - The following properties should hold upon exit:
(basic_props:string/1)CygWinPath is a string (a list of character codes).
(basic_props:string/1)WindowsPath is a string (a list of character codes).
(term_typing:atom/1)SwapSlash is currently instantiated to an atom.
Replaces all the occurrences of SearchChar by ReplaceChar and unifies the result with Output
Usage:system_error_report(Report)
Report is the error message from the last system call, like strerror in POSIX.
- The following properties should hold at call time:
(term_typing:var/1)Report is a free variable. - The following properties should hold upon exit:
(basic_props:atm/1)Report is an atom.
Usage:get_home(H)
H is the home directory (HOME environment variable in POSIX systems and APPDATA in Windows)
- The following properties should hold at call time:
(term_typing:var/1)H is a free variable.
Usage:find_executable(Name,Path)
Path is the absolute path of the command Name, reachable from the PATH (environment variable) directories if Name is not an absolute path. The suffix for executable files is optionally added to Path depending on the current operating system.
- The following properties should hold at call time:
(term_typing:nonvar/1)Name is currently a term which is not a free variable.
(term_typing:var/1)Path is a free variable.
Documentation on multifiles
(Trust) Usage:define_flag(Flag,FlagValues,Default)
- The following properties hold upon exit:
(basic_props:atm/1)Flag is an atom.
(basic_props:flag_values/1)Define the valid flag values
Known bugs and planned improvements
- Run-time checks have been reported not to work with this code. That means that either the assertions here, or the code that implements the run-time checks are erroneous.
- We use atoms to represent file names. This pollutes the atom table. We need atom garbage collection and/or native strings.
- In some situations, copy_file don't work when the second argument is a directory, example:
copy_file( 'site/ciaopp_online.html' , ~distdir, yes),