@c Copyright (C) 1993, 1994, 1995 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::            
* Interacting with the OS::     
* System Information::          
* Other Functions::             
@end menu

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

@findex clock

The function @code{clock} returns a vector containing the current year,
month (1-12), day (1-31), hour (0-23), minute (0-59) and second (0-60).
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.

@findex date

To get the date as a character string in the form DD-MMM-YY, use
the command @code{date}.  For example,

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

@findex tic
@findex toc
@findex etime

Octave also has functions for computing time intervals and CPU time
used.  The functions @code{tic} and @code{toc} can be used to 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}.

The function @code{etime} provides another way to get elapsed wall-clock
time by returning 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.

@findex cputime

The function @code{cputime} allows you to obtain information about the
amount of CPU time your Octave session is using.  For example,

@example
[total, user, system] = cputime ();
@end example

@noindent
returns 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.

@findex is_leap_year

Finally, Octave's function @code{is_leap_year} returns 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

@noindent
Contrary to what many people who post misinformation to Usenet
apparently believe, Octave knows that the year 2000 will be a leap year.

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

@findex shell_cmd
@findex system

You can execute any shell command using the function
@code{system (@var{cmd}, @var{flag})}.  The second argument is optional.
If it is present, the output of the command is returned by
@code{system} as a string.  If it is not supplied, any output from
the command is printed, with the standard output filtered through the
pager.  For example,

@c XXX FIXME XXX -- perhaps there should be another possible value for
@c the second argument, to indicate that the command is to be run
@c interactively, with the standard input and output connected to
@c /dev/tty.

@example
users = system ("finger", 1)
@end example

@noindent
places the output of the command @code{finger} in the variable
@code{users}.

If you want to execute a shell command and have it behave as if it were
typed directly from the shell prompt, you may need to specify extra
arguments for the command.  For example, to get @code{bash} to behave as
an interactive shell, you can type

@example
system ("bash -i >/dev/tty");
@end example

The first argument, @samp{-i}, tells @code{bash} to behave as an
interactive shell, and the redirection of the standard output stream
prevents any output produced by @code{bash} from being sent back to
Octave, where it would be buffered until Octave displays another prompt.

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

The name @code{shell_cmd} exists for compatibility with earlier versions
of Octave.

@findex getenv

You can find the values of environment variables using the function
@code{getenv}.  For example,

@example
getenv ("PATH")
@end example

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

@findex clc
@findex home

The functions @code{clc}, and @code{home} clear your
terminal screen and move the cursor to the upper left corner.

@findex cd

You can change the current working directory using the @code{cd}
command.  Tilde expansion is performed on the path.  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.

@findex chdir

The name @code{chdir} is an alias for @code{cd}.

@findex pwd

The command @code{pwd} prints the current working directory.

@findex dir
@findex ls

The functions @code{dir} and @code{ls} 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.

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

@findex computer

If possible, @code{computer} prints 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

@findex isieee

The function @code{isieee} returns 1 if your computer claims to conform
to the IEEE standard for floating point calculations.

@findex version
The function @code{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}.

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

@findex pause

The function @code{pause} allows you to suspend the execution of a
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
fprintf (stderr, "wait please...\n"), pause (5), clc
@end example
