view doc/interpreter/system.texi @ 2465:7ee42ff6536a

[project @ 1996-11-03 08:54:14 by jwe]
author jwe
date Sun, 03 Nov 1996 08:54:22 +0000
parents 5edc0b8b0543
children f201716926bb
line wrap: on
line source

@c Copyright (C) 1996 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node System Utilities, Command History Functions, String Functions, Top
@chapter System Utilities

This chapter describes the functions that are available to allow you to
get information about what is happening outside of Octave, while it is
still running, and use this information in your program.  For example,
you can get information about environment variables, the current time,
and even start other programs from the Octave prompt.

@menu
* Timing Utilities::            
* Filesystem Utilities::        
* Interacting with the OS::     
* Password Database Functions::  
* System Information::          
* Other Functions::             
@end menu

@node Timing Utilities, Filesystem Utilities, System Utilities, System Utilities
@section Timing Utilities

@deftypefn {Loadable Function} {} time ()
Return the current time as the number of seconds since the epoch.  The
epoch is referenced to 00:00:00 CUT (Coordinated Universal Time) 1 Jan
1970.
@end deftypefn

Several of Octave's time functions a data structure for time that
includes the following elements:

@table @code
@item usec
Microseconds after the second (0-999999).

@item sec
Seconds after the minute (0-61).  This number can be 61 to account
for leap seconds.

@item min
Minutes after the hour (0-59).

@item hour
Hours since midnight (0-23).

@item mday
Day of the month (1-31).

@item mon
Months since January (0-11).

@item year
Years since 1900.

@item wday
Days since Sunday (0-6).

@item yday
Days since January 1 (0-365).

@item isdst
Daylight Savings Time flag.

@item zone
Time zone.
@end table

@deftypefn {Loadable Function} {} mktime (@var{time_struct})
Convert a time structure to the number of seconds since the epoch.
@end deftypefn

@deftypefn {Loadable Function} {} localtime (@var{t})
Given a value returned from time (or any nonnegative integer),
return a time structure corresponding to the local time zone.
@end deftypefn

@deftypefn {Loadable Function} {} gmtime (@var{t})
Given a value returned from time (or any nonnegative integer),
return a time structure corresponding to CUT.
@end deftypefn

@deftypefn {Function File} {} asctime (@var{time_struct})
Convert a time structure to a string using the following five-field
format: Thu Mar 28 08:40:14 1996.  The function @code{ctime (time)} is
equivalent to @code{asctime (localtime (time))}.
@end deftypefn

@deftypefn {Loadable Function} {} strftime (@var{time_struct})
Format a time structure in a flexible way using @samp{%} substitutions
similar to those in @code{printf}.  Except where noted, substituted
fields have a fixed size; numeric fields are padded if necessary.
Padding is with zeros by default; for fields that display a single
number, padding can be changed or inhibited by following the @samp{%}
with one of the modifiers described below.  Unknown field specifiers are
copied as normal characters.  All other characters are copied to the
output without change.

Octave's @code{strftime} function supports a superset of the ANSI C
field specifiers.

@noindent
Literal character fields:

@table @code
@item %
% character.

@item n
Newline character.

@item t
Tab character.
@end table

@noindent
Numeric modifiers (a nonstandard extension):

@table @code
@item -
Do not pad the field.

@item _
Pad the field with spaces.
@end table

@noindent
Time fields:

@table @code
@item %H
Hour (00-23).

@item %I
Hour (01-12).

@item %k
Hour (0-23).

@item %l
Hour (1-12).

@item %M
Minute (00-59).

@item %p
Locale's AM or PM.

@item %r
Time, 12-hour (hh:mm:ss [AP]M).

@item %R
Time, 24-hour (hh:mm).

@item %s
Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).

@item %S
Second (00-61).

@item %T
Time, 24-hour (hh:mm:ss).

@item %X
Locale's time representation (%H:%M:%S).

@item %Z
Time zone (EDT), or nothing if no time zone is determinable.
@end table

@noindent
Date fields:

@table @code
@item %a
Locale's abbreviated weekday name (Sun-Sat).

@item %A
Locale's full weekday name, variable length (Sunday-Saturday).

@item %b
Locale's abbreviated month name (Jan-Dec).

@item %B
Locale's full month name, variable length (January-December).

@item %c
Locale's date and time (Sat Nov 04 12:02:33 EST 1989).

@item %C
Century (00-99).

@item %d
Day of month (01-31).

@item %e
Day of month ( 1-31).

@item %D
Date (mm/dd/yy).

@item %h
Same as %b.

@item %j
Day of year (001-366).

@item %m
Month (01-12).

@item %U
Week number of year with Sunday as first day of week (00-53).

@item %w
Day of week (0-6).

@item %W
Week number of year with Monday as first day of week (00-53).

@item %x
Locale's date representation (mm/dd/yy).

@item %y
Last two digits of year (00-99).

@item %Y
Year (1970-).
@end table
@end deftypefn

@deftypefn {Function File} {} clock ()
Return a vector containing the current year, month (1-12), day (1-31),
hour (0-23), minute (0-59) and second (0-61).  For example,

@example
octave:13> clock
ans =

  1993     8    20     4    56     1
@end example

The function clock is more accurate on systems that have the
@code{gettimeofday} function.
@end deftypefn

@deftypefn {Function File} {} date ()
Returns the date as a character string in the form DD-MMM-YY.  For
example,

@example
octave:13> date
ans = 20-Aug-93
@end example
@end deftypefn

@deftypefn {Function File} {} tic ()
@deftypefnx {Function File} {} toc ()
These functions set and check a wall-clock timer.  For example,

@example
tic ();
# many computations later...
elapsed_time = toc ();
@end example

@noindent
will set the variable @code{elapsed_time} to the number of seconds since
the most recent call to the function @code{tic}.
@end deftypefn

@deftypefn {Function File} {} etime (@var{t1}, @var{t2})
Return the difference (in seconds) between two time values returned from
@code{clock}.  For example:

@example
t0 = clock ();
# many computations later...
elapsed_time = etime (clock (), t0);
@end example

@noindent
will set the variable @code{elapsed_time} to the number of seconds since
the variable @code{t0} was set.
@end deftypefn

@deftypefn {Built-in Function} {[@var{total}, @var{user}, @var{system}] =} cputime ();
Return the CPU time used by your Octave session.  The first output is
the total time spent executing your process and is equal to the sum of
second and third outputs, which are the number of CPU seconds spent
executing in user mode and the number of CPU seconds spent executing in
system mode, respectively.  If your system does not have a way to report
CPU time usage, @code{cputime} returns 0 for each of its output values.
@end deftypefn

@deftypefn {Function File} {} is_leap_year (@var{year})
Return 1 if the given year is a leap year and 0 otherwise.  If no
arguments are provided, @code{is_leap_year} will use the current year.
For example,

@example
octave:13> is_leap_year (2000)
ans = 1
@end example
@end deftypefn

@node Filesystem Utilities, Interacting with the OS, Timing Utilities, System Utilities
@section Filesystem Utilities

Octave includes the following functions for renaming and deleting files,
creating, deleting, and reading directories, and for getting information
about the status of files.

@deftypefn {Built-in Function} {} rename (@var{from}, @var{to})
Rename a file.
@end deftypefn

@deftypefn {Built-in Function} {} unlink (@var{file})
Delete a file.
@end deftypefn

@deftypefn {Built-in Function} {} readdir (@var{dir})
Returns names of files in the directory @var{dir} as an array of
strings.
@end deftypefn

@deftypefn {Built-in Function} {} mkdir (@var{dir})
Create a directory
@end deftypefn

@deftypefn {Built-in Function} {} rmdir (@var{dir})
Remove a directory.
@end deftypefn

@c XXX FIXME XXX -- this needs to be explained, but I don't feel up to
@c it just now...

@deftypefn {Built-in Function} {} umask (@var{mask})
Set permission mask for file creation.
@end deftypefn

@deftypefn {Built-in Function} {} stat (@var{file})
Get information about a file.  If @var{file} is a symbolic link,
@code{stat} returns information about the file that the symbolic link
references.
@end deftypefn

@deftypefn {Built-in Function} {} lstat (@var{file})
Get information about a symbolic link.  If @var{file} is not a symbolic
link, @code{lstat} is equivalent to @code{stat}.
@end deftypefn

@node Interacting with the OS, Password Database Functions, Filesystem Utilities, System Utilities
@section Interacting with the OS

@deftypefn {Built-in Function} {} fork ()
Create a copy of the current process.
@end deftypefn

@deftypefn {Built-in Function} {} exec (@var{file}, @var{args})
Replace current process with a new process.
@end deftypefn

@deftypefn {Built-in Function} {fid =} dup2 (@var{old}, @var{new})
Duplicate a file descriptor.
@end deftypefn

@deftypefn {Built-in Function} {[@var{file_ids}, @var{status}] =} pipe ()
Create an interprocess channel.
@end deftypefn

@deftypefn {Built-in Function} {} fcntl (@var{fid}, @var{request}, @var{argument})
Control open file descriptors.

@vtable @code
@item F_DUPFD
@item F_GETFD
@item F_GETFL
@item F_SETFD
@item F_SETFL
@item O_APPEND
@item O_CREAT
@item O_EXCL
@item O_NONBLOCK
@item O_RDONLY
@item O_RDWR
@item O_TRUNC
@item O_WRONLY
@end vtable
@end deftypefn

@deftypefn {Built-in Function} {} getpgrp ()
Return the process group id of the current process.
@end deftypefn

@deftypefn {Built-in Function} {} getpid ()
Return the process id of the current process.
@end deftypefn

@deftypefn {Built-in Function} {} getppid ()
Return the process id of the parent process.
@end deftypefn

@deftypefn {Built-in Function} {} mkfifo       
Create a FIFO special file.
@end deftypefn

@deftypefn {Built-in Function} {} waitpid      
Check the status of or wait for subprocesses.
@end deftypefn

@deftypefn {Built-in Function} {} atexit (@var{fcn})
Register function to be called when Octave exits.
@end deftypefn

@deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})
Execute a shell command specified by @var{string}.  The second argument is optional.
If @var{type} is @code{"async"}, the process is started in the
background and the process id of the child proces is returned
immediately.  Otherwise, the process is started, and Octave waits until
it exits.  If @var{type} argument is omitted, a value of @code{"sync"}
is assumed.

If two input arguments are given (the actual value of
@var{return_output} is irrelevant) and the subprocess is started
synchronously, or if @var{system} is called with one input argument and
one or more output arguments, the output from the command is returned.
Otherwise, if the subprocess is executed synchronously, it's output is
sent to the standard output.  To send the output of a command executed
with @var{system} through the pager, use a command like

@example
disp (system (cmd, 1));
@end example

@noindent
or

@example
printf ("%s\n", system (cmd, 1));
@end example

The @code{system} function can return two values.  The first is any
output from the command that was written to the standard output stream,
and the second is the output status of the command.  For example,

@example
[output, status] = system ("echo foo; exit 2");
@end example

@noindent
will set the variable @code{output} to the string @samp{foo}, and the
variable @code{status} to the integer @samp{2}.
@end deftypefn

@defvr {Built-in Variable} EXEC_PATH
The variable @code{EXEC_PATH} is a colon separated list of directories
to search when executing subprograms.  Its initial value is taken from
the environment variable @code{OCTAVE_EXEC_PATH} (if it exists) or
@code{PATH}, but that value can be overridden by the the command line
argument @code{--exec-path PATH}, or by setting the value of
@code{EXEC_PATH} in a startup script.  If the value of @code{EXEC_PATH}
begins (ends) with a colon, the directories
@code{OCTAVE_HOME/libexec/octave/VERSION/exec/ARCH} and
@code{OCTAVE_HOME/bin} are prepended (appended) to @code{EXEC_PATH} (if
you don't specify a value for @code{EXEC_PATH} explicitly, these special
directories are prepended to your shell path).
@end defvr

@deftypefn {Built-in Function} {} getenv (@var{var})
Returns the value of the environment variable @var{var}.  For example,

@example
getenv ("PATH")
@end example

@noindent
returns a string containing the value of your path.
@end deftypefn

@deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})
Set the value of the environment variable @var{var} to @var{value}.
@end deftypefn

@deftypefn {Built-in Function} {} clc ()
@deftypefnx {Built-in Function} {} home ()
Clear the terminal screen and move the cursor to the upper left corner.
@end deftypefn

@deffn {Command} cd dir
@deffnx {Command} chdir dir
Change the current working directory to @var{dir}.  For example,

@example
cd ~/octave
@end example

@noindent
Changes the current working directory to @file{~/octave}.  If the
directory does not exist, an error message is printed and the working
directory is not changed.
@end deffn

@deftypefn {Built-in Function} {} pwd ()
Returns the current working directory.
@end deftypefn

@defvr {Built-in Variable} PWD
The current working directory.  The value of @code{PWD} is updated each
time the current working directory is changed with the @samp{cd}
command.
@end defvr

@deffn {Command} ls
@deffnx {Command} dir
List directory contents.  For example,

@example
octave:13> ls -l
total 12
-rw-r--r--   1 jwe      users        4488 Aug 19 04:02 foo.m
-rw-r--r--   1 jwe      users        1315 Aug 17 23:14 bar.m
@end example

The @code{dir} and @code{ls} commands are implemented by calling your
system's directory listing command, so the available options may vary
from system to system.
@end deffn

@node Password Database Functions, System Information, Interacting with the OS, System Utilities
@section Password Database Functions

Octave's password database functions return information in a structure
with the following fields.

@table @code
@item name
The user name.

@item passwd
The encrypted password, if available.

@item uid
The numeric user id.

@item gid
The numeric group id.

@item gecos
The GECOS field.

@item dir
The home directory.

@item shell
The initial shell.
@end table

@deftypefn {Loadable Function} {passwd_struct =} getpwent ()
Return an entry from the password database, opening it if necessary.
Once the end of the data has been reached, @code{getpwent} returns 0.
@end deftypefn

@deftypefn {Loadable Function} {passwd_struct =} getpwuid (@var{uid}).
Return the first entry from the password database with the user ID
@var{uid}.  If the user ID does not exist in the database,
@code{getpwuid} returns 0.
@end deftypefn

@deftypefn {Loadable Function} {passwd_struct =} getpwnam (@var{name})
Return the first entry from the password database with the user name
@var{name}.  If the user name does not exist in the database,
@code{getpwname} returns 0.
@end deftypefn

@deftypefn {Loadable Function} {} setpwent ()
Return the internal pointer to the beginning of the password database.
@end deftypefn

@deftypefn {Loadable Function} {} endpwent ()
Close the password database.
@end deftypefn

@node System Information, Other Functions, Password Database Functions, System Utilities
@section System Information

@deftypefn {Built-in Function} {} computer ()
Returns a string of the form @var{cpu}-@var{vendor}-@var{os} that
identifies the kind of computer Octave is running on.  For example,

@example
octave:13> computer
sparc-sun-sunos4.1.2
@end example
@end deftypefn

@deftypefn {Built-in Function} {} isieee ()
Return 1 if your computer claims to conform to the IEEE standard for
floating point calculations.
@end deftypefn

@deftypefn {Built-in Function} {} version ()
Returns Octave's version number as a string.  This is also the value of
the built-in variable @code{OCTAVE_VERSION}.  @xref{Built-in Variables}.
@end deftypefn

@deftypefn {Loadable Function} {} getrusage ()
Return a structure containing a number of statistics about the current
Octave process.  Not all fields are available on all systems.  If it is
not possible to get CPU time statistics, the CPU time slots are set to
zero.  Other missing data are replaced by NaN.  Here is a list of all
the possible fields that can be present in the structure returned by
@code{getrusage}:

@table @code
@item 
@item idrss
Unshared data size.

@item inblock
Number of block input operations.

@item isrss
Unshared stack size.

@item ixrss
Shared memory size.

@item majflt
Number of major page faults.

@item maxrss
Maximum data size.

@item minflt
Number of minor page faults.

@item msgrcv
Number of messages received.

@item msgsnd
Number of messages sent.

@item nivcsw
Number of involuntary context switches.

@item nsignals
Number of signals received.

@item nswap
Number of swaps.

@item nvcsw
Number of voluntary context switches.

@item oublock
Number of block output operations.

@item stime
A structure containing the system CPU time used.  The structure has the
elements @code{sec} (seconds) @code{usec} (microseconds).

@item utime
A structure containing the user CPU time used.  The structure has the
elements @code{sec} (seconds) @code{usec} (microseconds).
@end table
@end deftypefn

@node Other Functions,  , System Information, System Utilities
@section Other Functions

@c XXX FIXME XXX -- need to define tilde expansion.
 
@deftypefn {Built-in Function} {} tilde_expand (@var{string})
Performs tilde expansion on @var{string}.
@end deftypefn

@deftypefn {Built-in Function} {} pause (@var{seconds})
Suspend the execution of the program.  If invoked without any arguments,
Octave waits until you type a character.  With a numeric argument, it
pauses for the given number of seconds.  For example, the following
statement prints a message and then waits 5 seconds before clearing the
screen.

@example
@group
fprintf (stderr, "wait please...\n");
pause (5);
clc;
@end group
@end example
@end deftypefn