Module UnixExtra.Extra (.ml)


module Extra: sig .. end
Extra definitions.

type filename = string 
A filename is a string.
type foldername = string 
A foldername is a string.
type content = string 
A content is a string.
val current_umask : int
The current value of umask.
val touch : ?perm:Unix.file_perm -> filename -> unit
Create a file if necessary with the given permissions (by default equal to 0o640).

Copying


module Copylib: sig .. end
Support for copying files.
val file_copy : ?perm:Unix.file_perm ->
filename -> filename -> unit
Copy a file into another. Optional permissions (by default 0o640) concern of course the target.
val file_append : ?perm:Unix.file_perm ->
filename -> filename -> unit
Append a file into another. Optional permissions (by default 0o640) concern of course the target.

Saving strings


val put : ?perm:Unix.file_perm ->
filename -> content -> unit
Write or rewrite the file with the given content. If the file does not exists, it is created with the given permission (set by default to 0o640).
val rewrite : ?perm:Unix.file_perm ->
filename -> content -> unit
Alias for put.
val append : ?perm:Unix.file_perm ->
filename -> content -> unit
Similar to the function put described above, but the content is appended instead of rewrited. If the file doesn't exists, it is created with the given permissions (set by default to 0o640).

Loading strings


val cat : filename -> string
Return the whole content (caution!) of the file as a string. Use only for small files. Great for making pipelines. For instance, the following pipeline catches the first line of /etc/fstab containing the substring "hda1":
# "/etc/fstab" => ( cat || String.to_list || Str.grep ".*hda1.*" || hd ) 


Temporary files


module Templib: sig .. end
Support for this section.
val temp_dir : ?perm:Unix.file_perm ->
?parent:string -> ?prefix:string -> ?suffix:string -> unit -> string
Create a temporary directory in a parent directory. By default: The function returns the name of the created directory.

Example:

# temp_dir ();;
  : string = "/tmp/819618234"

# temp_dir ~suffix:".txt" ();;
  : string = "/tmp/625724514.txt"

val temp_file : ?perm:Unix.file_perm ->
?parent:string ->
?prefix:string ->
?suffix:string -> ?content:content -> unit -> string
Create a temporary file in a parent directory. The optional parameters have the same meaning of the function temp_dir. In addition, the parameter content may be set in order to put it in the created file. By default the content is the empty string. The function returns the name of the created directory.

Kind


val file_kind_of_char : char -> Unix.file_kind option
Heuristic that tries to convert a char into a value of the type:

Unix.file_kind = S_REG | S_DIR | S_CHR | S_BLK | S_LNK | S_FIFO | S_SOCK

The input character must belong to the set 'f';'d';'c';'b';'h';'p';'s', following the convention of the standard unix test command. Otherwise, the result is None.


Directories


val iter_dir : (string -> 'a) -> string -> unit
iter_dir f dirname iterate the function f on each entry of the directory dirname. -- From Xavier Leroy and Didier Remy's OS course, Chapter 2.

Find


module Findlib: sig .. end
Support for finding in a directory hierarchy.
val find : ?follow:bool ->
?maxdepth:int -> ?kind:char -> ?name:string -> string -> string list
Find something in an input directory. This function is an interface for the tool Findlib.find.

The default assignements are:

The set of characters corresponding to the kind of file are the same of the standard test unix command (i.e. 'f';'d';'c';'b';'h';'p';'s'); see the function UnixExtra.Extra.file_kind_of_char for more details.

Warning: use this function with caution: the good version of this function will be a version returning a sequence (stream) instead of a list.

Examples:

# find "/etc/ssh/" ;;
  : string list = ["/etc/ssh/"; "/etc/ssh/ssh_config"; "/etc/ssh/sshd_config";
 "/etc/ssh/ssh_host_key"; "/etc/ssh/ssh_host_dsa_key.pub";
 "/etc/ssh/ssh_host_rsa_key.pub"; "/etc/ssh/moduli";
 "/etc/ssh/ssh_host_key.pub"; "/etc/ssh/ssh_host_dsa_key";
 "/etc/ssh/ssh_host_rsa_key"]

# find ~kind:'d' "/etc/ssh/" ;;
  : string list = ["/etc/ssh/"]

# find ~name:"moduli" "/etc/ssh/" ;;
  : string list = ["/etc/ssh/moduli"]


Password


module Passwdlib: sig .. end
Support for input passwords.
val read_passwd : string -> string
Prompt for a password. The terminal is set for hiding the characters read from keyboard.

Running


type command = string 
A command is a string.
val run : ?trace:bool ->
?input:content ->
command -> string * Unix.process_status
Returns the pair (output, exit-code) of the given system command. A string can be specified as input for the command. The flag trace (by default set to false) permits to obtain some informations about the running on stderr.

Examples:

# run "ls /etc/*tab";;
  : string * Unix.process_status =
("/etc/crontab\n/etc/fstab\n/etc/inittab\n/etc/mtab\n/etc/quotatab\n", Unix.WEXITED 0)

# run ~input:"hello" "cat";;
  : string * Unix.process_status = ("hello", Unix.WEXITED 0)

# run ~input:"HELLO" "head -n 1 /etc/passwd /dev/stdin | cut -c-15";;
  : string * Unix.process_status =
("==> /etc/passwd\nat:x:25:25:Batc\n\n==> /dev/stdin \nHELLO\n", Unix.WEXITED 0)

val shell : ?trace:bool ->
?input:content -> command -> string
As run, but ignoring the exit-code. This function is simply a shortcut for the composition of run with fst. Examples:

# shell "date";;
  : string = "ven avr 13 18:34:02 CEST 2007\n"

# String.Text.to_matrix (shell "wc -l /etc/*tab");;
  : string list list =
[["8"; "/etc/crontab"]; ["20"; "/etc/fstab"]; ["98"; "/etc/inittab"];
 ["11"; "/etc/mtab"]; ["127"; "/etc/naccttab"]; ["9"; "/etc/quotatab"];
 ["273"; "total"]]