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.

Timing Utilities
================

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

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

usec
     Microseconds after the second (0-999999).

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

min
     Minutes after the hour (0-59).

hour
     Hours since midnight (0-23).

mday
     Day of the month (1-31).

mon
     Months since January (0-11).

year
     Years since 1900.

wday
     Days since Sunday (0-6).

yday
     Days since January 1 (0-365).

isdst
     Daylight Savings Time flag.

zone
     Time zone.

 -- Loadable Function: mktime (TIME_STRUCT)
     Convert a time structure to the number of seconds since the epoch.

 -- Loadable Function: localtime (T)
     Given a value returned from time (or any nonnegative integer), return a time structure corresponding to the local time zone.

 -- Loadable Function: gmtime (T)
     Given a value returned from time (or any nonnegative integer), return a time structure corresponding to CUT.

 -- Function File: asctime (TIME_STRUCT)
     Convert a time structure to a string using the following five-field format: Thu Mar 28 08:40:14 1996.  The function ctime (time) is equivalent to asctime (localtime (time)).

 -- Loadable Function: strftime (TIME_STRUCT)
     Format a time structure in a flexible way using % substitutions similar to those in 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 % 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 strftime function supports a superset of the ANSI C field specifiers.

     Literal character fields:

     %
          % character.

     n
          Newline character.

     t
          Tab character.

     Numeric modifiers (a nonstandard extension):

     -
          Do not pad the field.

     _
          Pad the field with spaces.

     Time fields:

     %H
          Hour (00-23).

     %I
          Hour (01-12).

     %k
          Hour (0-23).

     %l
          Hour (1-12).

     %M
          Minute (00-59).

     %p
          Locale's AM or PM.

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

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

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

     %S
          Second (00-61).

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

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

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

     Date fields:

     %a
          Locale's abbreviated weekday name (Sun-Sat).

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

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

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

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

     %C
          Century (00-99).

     %d
          Day of month (01-31).

     %e
          Day of month ( 1-31).

     %D
          Date (mm/dd/yy).

     %h
          Same as %b.

     %j
          Day of year (001-366).

     %m
          Month (01-12).

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

     %w
          Day of week (0-6).

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

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

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

     %Y
          Year (1970-).

 -- 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,

          octave:13> clock
          ans =

            1993     8    20     4    56     1

     The function clock is more accurate on systems that have the gettimeofday function.

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

          octave:13> date
          ans = 20-Aug-93

 -- Function File: tic ()
 -- Function File: toc ()
     These functions set and check a wall-clock timer.  For example,

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

     will set the variable elapsed_time to the number of seconds since the most recent call to the function tic.

 -- Function File: etime (T1, T2)
     Return the difference (in seconds) between two time values returned from clock.  For example:

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

     will set the variable elapsed_time to the number of seconds since the variable t0 was set.

 -- Built-in Function: [TOTAL, USER, 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, cputime returns 0 for each of its output values.

 -- Function File: is_leap_year (YEAR)
     Return 1 if the given year is a leap year and 0 otherwise.  If no arguments are provided, is_leap_year will use the current year. For example,

          octave:13> is_leap_year (2000)
          ans = 1

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.

 -- Built-in Function: rename (FROM, TO)
     Rename a file.

 -- Built-in Function: unlink (FILE)
     Delete a file.

 -- Built-in Function: readdir (DIR)
     Returns names of files in the directory DIR as an array of strings.

 -- Built-in Function: mkdir (DIR)
     Create a directory

 -- Built-in Function: rmdir (DIR)
     Remove a directory.

 -- Built-in Function: umask (MASK)
     Set permission mask for file creation.

 -- Built-in Function: stat (FILE)
     Get information about a file.  If FILE is a symbolic link, stat returns information about the file that the symbolic link references.

 -- Built-in Function: lstat (FILE)
     Get information about a symbolic link.  If FILE is not a symbolic link, lstat is equivalent to stat.

 -- Built-in Function: glob (PATTERN)
     Given an array of strings in PATTERN, return the list of file names that any of them, or an empty string if no patterns match.  Tilde expansion is performed on each of the patterns before looking for matching file names.

 -- Built-in Function: fnmatch (PATTERN, STRING)
     Return 1 or zero for each element of STRING that matches any of the elements of the string array PATTERN, using the rules of filename pattern matching.

Interacting with the OS
=======================

 -- Built-in Function: fork ()
     Create a copy of the current process.

 -- Built-in Function: exec (FILE, ARGS)
     Replace current process with a new process.

 -- Built-in Function: fid = dup2 (OLD, NEW)
     Duplicate a file descriptor.

 -- Built-in Function: [FILE_IDS, STATUS] = pipe ()
     Create an interprocess channel.

 -- Built-in Function: fcntl (FID, REQUEST, ARGUMENT)
     Control open file descriptors.

     F_DUPFD

     F_GETFD

     F_GETFL

     F_SETFD

     F_SETFL

     O_APPEND

     O_CREAT

     O_EXCL

     O_NONBLOCK

     O_RDONLY

     O_RDWR

     O_TRUNC

     O_WRONLY

 -- Built-in Function: getpgrp ()
     Return the process group id of the current process.

 -- Built-in Function: getpid ()
     Return the process id of the current process.

 -- Built-in Function: getppid ()
     Return the process id of the parent process.

 -- Built-in Function: geteuid ()
     Return the effective user id of the current process.

 -- Built-in Function: getuid ()
     Return the real user id of the current process.

 -- Built-in Function: getegid ()
     Return the effective group id of the current process.

 -- Built-in Function: getgid ()
     Return the real group id of the current process.

 -- Built-in Function: mkfifo
     Create a FIFO special file.

 -- Built-in Function: waitpid
     Check the status of or wait for subprocesses.

 -- Built-in Function: atexit (FCN)
     Register function to be called when Octave exits.

 -- Built-in Function: system (STRING, RETURN_OUTPUT, TYPE)
     Execute a shell command specified by STRING.  The second argument is optional. If TYPE is "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 TYPE argument is omitted, a value of "sync" is assumed.

     If two input arguments are given (the actual value of RETURN_OUTPUT is irrelevant) and the subprocess is started synchronously, or if 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 SYSTEM through the pager, use a command like

          disp (system (cmd, 1));

     or

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

     The 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,

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

     will set the variable output to the string foo, and the variable status to the integer 2.

 -- Built-in Variable: EXEC_PATH
     The variable EXEC_PATH is a colon separated list of directories to search when executing subprograms.  Its initial value is taken from the environment variable OCTAVE_EXEC_PATH (if it exists) or PATH, but that value can be overridden by the the command line argument --exec-path PATH, or by setting the value of EXEC_PATH in a startup script.  If the value of EXEC_PATH begins (ends) with a colon, the directories

          OCTAVE_HOME/libexec/octave/site/exec/ARCH
          OCTAVE_HOME/libexec/octave/VERSION/exec/ARCH
          OCTAVE_HOME/bin

     are prepended (appended) to EXEC_PATH, where OCTAVE_HOME is the top-level directory where all of Octave is installed (/usr/local by default).  If you don't specify a value for EXEC_PATH explicitly, these special directories are prepended to your shell path.

 -- Built-in Function: getenv (VAR)
     Returns the value of the environment variable VAR.  For example,

          getenv ("PATH")

     returns a string containing the value of your path.

 -- Built-in Function: putenv (VAR, VALUE)
     Set the value of the environment variable VAR to VALUE.

 -- Built-in Function: clc ()
 -- Built-in Function: home ()
     Clear the terminal screen and move the cursor to the upper left corner.

 -- Command: cd dir
 -- Command: chdir dir
     Change the current working directory to DIR.  For example,

          cd ~/octave

     Changes the current working directory to ~/octave.  If the directory does not exist, an error message is printed and the working directory is not changed.

 -- Built-in Function: pwd ()
     Returns the current working directory.

 -- Built-in Variable: PWD
     The current working directory.  The value of PWD is updated each time the current working directory is changed with the cd command.

 -- Command: ls
 -- Command: dir
     List directory contents.  For 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

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

Password Database Functions
===========================

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

name
     The user name.

passwd
     The encrypted password, if available.

uid
     The numeric user id.

gid
     The numeric group id.

gecos
     The GECOS field.

dir
     The home directory.

shell
     The initial shell.

 -- 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, getpwent returns 0.

 -- Loadable Function: passwd_struct = getpwuid (UID).
     Return the first entry from the password database with the user ID UID.  If the user ID does not exist in the database, getpwuid returns 0.

 -- Loadable Function: passwd_struct = getpwnam (NAME)
     Return the first entry from the password database with the user name NAME.  If the user name does not exist in the database, getpwname returns 0.

 -- Loadable Function: setpwent ()
     Return the internal pointer to the beginning of the password database.

 -- Loadable Function: endpwent ()
     Close the password database.

Group Database Functions
========================

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

name
     The user name.

passwd
     The encrypted password, if available.

gid
     The numeric group id.

mem
     The members of the group.

 -- Loadable Function: group_struct = getgrent ()
     Return an entry from the group database, opening it if necessary. Once the end of the data has been reached, getgrent returns 0.

 -- Loadable Function: group_struct = getgrgid (GID).
     Return the first entry from the group database with the group ID GID.  If the group ID does not exist in the database, getgrgid returns 0.

 -- Loadable Function: group_struct = getgrnam (NAME)
     Return the first entry from the group database with the group name NAME.  If the group name does not exist in the database, getgrname returns 0.

 -- Loadable Function: setgrent ()
     Return the internal pointer to the beginning of the group database.

 -- Loadable Function: endgrent ()
     Close the group database.

System Information
==================

 -- Built-in Function: computer ()
     Returns a string of the form CPU-VENDOR-OS that identifies the kind of computer Octave is running on.  For example,

          octave:13> computer
          sparc-sun-sunos4.1.2

 -- Built-in Function: isieee ()
     Return 1 if your computer claims to conform to the IEEE standard for floating point calculations.

 -- Built-in Function: version ()
     Returns Octave's version number as a string.  This is also the value of the built-in variable OCTAVE_VERSION.  See Built-in Variables.

 -- 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 getrusage:

     idrss
          Unshared data size.

     inblock
          Number of block input operations.

     isrss
          Unshared stack size.

     ixrss
          Shared memory size.

     majflt
          Number of major page faults.

     maxrss
          Maximum data size.

     minflt
          Number of minor page faults.

     msgrcv
          Number of messages received.

     msgsnd
          Number of messages sent.

     nivcsw
          Number of involuntary context switches.

     nsignals
          Number of signals received.

     nswap
          Number of swaps.

     nvcsw
          Number of voluntary context switches.

     oublock
          Number of block output operations.

     stime
          A structure containing the system CPU time used.  The structure has the elements sec (seconds) usec (microseconds).

     utime
          A structure containing the user CPU time used.  The structure has the elements sec (seconds) usec (microseconds).

Other Functions
===============

 -- Built-in Function: tilde_expand (STRING)
     Performs tilde expansion on STRING.

 -- Built-in Function: pause (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.

          fprintf (stderr, "wait please...\n");
          pause (5);
          clc;