Previous section To contents Next section

9.8 Other Stdio functions

The Stdio module also contains a collection of high level IO functions to make it easy to write short and readable Pike programs. Most of these functions are implemented using Stdio.File and Stdio.FILE.

FUNCTION
Stdio.append_path - append paths in a secure way

SYNTAX
string append_path(string absolute, string ... relative);

DESCRIPTION
Append relative paths to an absolute path and remove any "//", "../" or "/." to produce a straightforward absolute path as a result. "../" is ignorded in the relative paths if it makes the created path begin with something else than the absolute path (or so far created path).

EXAMPLE
> Stdio.append_path("/foo/bar/","..");
Result: /foo/bar/
> Stdio.append_path("/foo/bar/","../apa.c");
Result: /foo/bar/apa.c
> Stdio.append_path("/foo/bar","./sune.c");
Result: /foo/bar/sune.c
> Stdio.append_path("/foo/bar","bertil/../../sune.c");
Result: /foo/bar/sune.c
> Stdio.append_path("/foo/bar","klas","bertil/../../sune.c");
Result: /foo/bar/klas/sune.c

SEE ALSO
combine_path

FUNCTION
Stdio.file_size - return the size of a file in bytes

SYNTAX
int file_size(string file);

DESCRIPTION
Give the size of a file. Size -1 indicates that the file either does not exist, or that it is not readable by you. Size -2 indicates that it is a directory.

SEE ALSO
Stdio.write_file and Stdio.read_bytes

FUNCTION
Stdio.exist - check if a path exists

SYNTAX
int exist(string path);

DESCRIPTION
Returns true if the given path exists (is a directory or file), otherwise false.

SEE ALSO
Stdio.is_dir, Stdio.is_file and Stdio.is_link

FUNCTION
Stdio.is_dir - check if a path is a directory

SYNTAX
int is_dir(string path);

DESCRIPTION
Returns true if the given path is a directory, otherwise false.

SEE ALSO
Stdio.exist, Stdio.is_file and Stdio.is_link

FUNCTION
Stdio.is_file - check if a path is a file

SYNTAX
int is_file(string path);

DESCRIPTION
Returns true if the given path is a file, otherwise false.

SEE ALSO
Stdio.exist, Stdio.is_dir and Stdio.is_link

FUNCTION
Stdio.is_link - check if a path is a symbolic link

SYNTAX
int is_link(string path);

DESCRIPTION
Returns true if the given path is a symbolic link, otherwise false.

SEE ALSO
Stdio.exist, Stdio.is_dir and Stdio.is_file

FUNCTION
Stdio.mkdirhier - make a directory hierarchy

SYNTAX
int mkdirhier(string pathname, void|int mode);

DESCRIPTION
Creates zero or more directories to ensure that the given pathname is a directory. Returns zero if it fails and nonzero if it is successful. If a mode is given, it's used for the new directories after being &'ed with the current umask (on OS'es that supports this).

SEE ALSO
mkdir

FUNCTION
Stdio.perror - print error

SYNTAX
void perror(string s);

DESCRIPTION
This function prints a message to stderr along with a description of what went wrong if available. It uses the system errno to find out what went wrong, so it is only applicable to IO errors.

SEE ALSO
Stdio.werror

FUNCTION
Stdio.read_bytes - read a number of bytes into a string from a file

SYNTAX
string read_bytes(string file,int start,int len);
string read_bytes(string file,int start);
string read_bytes(string file);

DESCRIPTION
Read len number of bytes from file file staring at byte start and return it as a string. If len is omitted, the rest of the file will be returned. If start is also omitted, the entire file will be returned.

SEE ALSO
Stdio.write_file

FUNCTION
Stdio.read_file - read a number of lines into a string from file

SYNTAX
string read_file(string file, int start, int len);
string read_file(string file);

DESCRIPTION
Read len lines from the file file after skipping start lines and return those lines as a string. If start and len are omitted the whole file is read.

SEE ALSO
Stdio.read_bytes and Stdio.write_file

FUNCTION
Stdio.readline - read a line from stdin

SYNTAX
string readline(string prompt);

DESCRIPTION
This function is gone. Use Stdio.Readline()->read instead.

SEE ALSO
Stdio.File

FUNCTION
Stdio.recursive_rm - remove a file or a directory tree recursively

SYNTAX
int recursive_rm(string path);

DESCRIPTION
Remove a file or directory a directory tree, return 0 if it fails. Nonzero otherwise.

SEE ALSO
rm

FUNCTION
Stdio.sendfile - send contents from one file to another

SYNTAX
object sendfile(array(string) headers,
object from, int offset, int len,
array(string) trailers,
object to);

or
object sendfile(array(string) headers,
object from, int offset, int len,
array(string) trailers,
object to,
function(int, mixed ...:void) callback,
mixed ... args);

DESCRIPTION
Sends headers followed by len bytes starting at offset from the file from followed by trailers to the file to. When completed callback is called with the total number of bytes sent as the first argument, followed by args.

Any of headers, from and trailers may be left out by setting them to 0.

Setting offset to -1 means send from the current position in from.

Setting len to -1 means send until from's end of file is reached.

NOTE
The sending is performed asynchronously, and may complete before the function returns.

For callback to be called, the backend must be active (ie main() must have returned -1).

In some cases, the backend must also be active for any sending to be performed at all.

SEE ALSO
Stdio.File->set_nonblocking

FUNCTION
Stdio.werror - write to stderr

SYNTAX
void werror(string s);

DESCRIPTION
Writes a message to stderr. Stderr is normally the console, even if the process output has been redirected to a file or pipe.

FUNCTION
Stdio.write_file - append a string to a file

SYNTAX
int write_file(string file, string str)

DESCRIPTION
Append the string str onto the file file. Returns number of bytes written.

SEE ALSO
Stdio.read_bytes

Previous section To contents Next section