=head1 NAME
POSIX - Perl interface to IEEE Std 1003.1
=head1 SYNOPSIS
use POSIX;
use POSIX qw(setsid);
use POSIX qw(:errno_h :fcntl_h);
printf "EINTR is %d\n", EINTR;
$sess_id = POSIX::setsid();
$fd = POSIX::open($path, O_CREAT|O_EXCL|O_WRONLY, 0644);
# note: that's a filedescriptor, *NOT* a filehandle
=head1 DESCRIPTION
The POSIX module permits you to access all (or nearly all) the standard
POSIX 1003.1 identifiers. Many of these identifiers have been given Perl-ish
interfaces.
I with the exception of any POSIX
functions with the same name as a built-in Perl function, such as
C, C, C, C, etc.., which will be exported
only if you ask for them explicitly. This is an unfortunate backwards
compatibility feature. You can stop the exporting by saying C and then use the fully qualified names (ie. C).
This document gives a condensed list of the features available in the POSIX
module. Consult your operating system's manpages for general information on
most features. Consult L for functions which are noted as being
identical to Perl's builtin functions.
The first section describes POSIX functions from the 1003.1 specification.
The second section describes some classes for signal objects, TTY objects,
and other miscellaneous objects. The remaining sections list various
constants and macros in an organization which roughly follows IEEE Std
1003.1b-1993.
=head1 NOTE
The POSIX module is probably the most complex Perl module supplied with
the standard distribution. It incorporates autoloading, namespace games,
and dynamic loading of code that's in Perl, C, or both. It's a great
source of wisdom.
=head1 CAVEATS
A few functions are not implemented because they are C specific. If you
attempt to call these, they will print a message telling you that they
aren't implemented, and suggest using the Perl equivalent should one
exist. For example, trying to access the setjmp() call will elicit the
message "setjmp() is C-specific: use eval {} instead".
Furthermore, some evil vendors will claim 1003.1 compliance, but in fact
are not so: they will not pass the PCTS (POSIX Compliance Test Suites).
For example, one vendor may not define EDEADLK, or the semantics of the
errno values set by open(2) might not be quite right. Perl does not
attempt to verify POSIX compliance. That means you can currently
successfully say "use POSIX", and then later in your program you find
that your vendor has been lax and there's no usable ICANON macro after
all. This could be construed to be a bug.
=head1 FUNCTIONS
=over 8
=item _exit
This is identical to the C function C<_exit()>. It exits the program
immediately which means among other things buffered I/O is B flushed.
Note that when using threads and in Linux this is B a good way to
exit a thread because in Linux processes and threads are kind of the
same thing (Note: while this is the situation in early 2003 there are
projects under way to have threads with more POSIXly semantics in Linux).
If you want not to return from a thread, detach the thread.
=item abort
This is identical to the C function C. It terminates the
process with a C signal unless caught by a signal handler or
if the handler does not return normally (it e.g. does a C).
=item abs
This is identical to Perl's builtin C function, returning
the absolute value of its numerical argument.
=item access
Determines the accessibility of a file.
if( POSIX::access( "/", &POSIX::R_OK ) ){
print "have read permission\n";
}
Returns C on failure. Note: do not use C for
security purposes. Between the C call and the operation
you are preparing for the permissions might change: a classic
I.
=item acos
This is identical to the C function C, returning
the arcus cosine of its numerical argument. See also L.
=item alarm
This is identical to Perl's builtin C function,
either for arming or disarming the C timer.
=item asctime
This is identical to the C function C. It returns
a string of the form
"Fri Jun 2 18:22:13 2000\n\0"
and it is called thusly
$asctime = asctime($sec, $min, $hour, $mday, $mon, $year,
$wday, $yday, $isdst);
The C<$mon> is zero-based: January equals C<0>. The C<$year> is
1900-based: 2001 equals C<101>. C<$wday> and C<$yday> default to zero
(and are usually ignored anyway), and C<$isdst> defaults to -1.
=item asin
This is identical to the C function C, returning
the arcus sine of its numerical argument. See also L.
=item assert
Unimplemented, but you can use L and the L module
to achieve similar things.
=item atan
This is identical to the C function C, returning the
arcus tangent of its numerical argument. See also L.
=item atan2
This is identical to Perl's builtin C function, returning
the arcus tangent defined by its two numerical arguments, the I
coordinate and the I coordinate. See also L.
=item atexit
atexit() is C-specific: use C instead, see L.
=item atof
atof() is C-specific. Perl converts strings to numbers transparently.
If you need to force a scalar to a number, add a zero to it.
=item atoi
atoi() is C-specific. Perl converts strings to numbers transparently.
If you need to force a scalar to a number, add a zero to it.
If you need to have just the integer part, see L.
=item atol
atol() is C-specific. Perl converts strings to numbers transparently.
If you need to force a scalar to a number, add a zero to it.
If you need to have just the integer part, see L.
=item bsearch
bsearch() not supplied. For doing binary search on wordlists,
see L.
=item calloc
calloc() is C-specific. Perl does memory management transparently.
=item ceil
This is identical to the C function C, returning the smallest
integer value greater than or equal to the given numerical argument.
=item chdir
This is identical to Perl's builtin C function, allowing
one to change the working (default) directory, see L.
=item chmod
This is identical to Perl's builtin C function, allowing
one to change file and directory permissions, see L.
=item chown
This is identical to Perl's builtin C function, allowing one
to change file and directory owners and groups, see L.
=item clearerr
Use the method C instead, to reset the error
state (if any) and EOF state (if any) of the given stream.
=item clock
This is identical to the C function C, returning the
amount of spent processor time in microseconds.
=item close
Close the file. This uses file descriptors such as those obtained by calling
C.
$fd = POSIX::open( "foo", &POSIX::O_RDONLY );
POSIX::close( $fd );
Returns C on failure.
See also L.
=item closedir
This is identical to Perl's builtin C function for closing
a directory handle, see L.
=item cos
This is identical to Perl's builtin C function, for returning
the cosine of its numerical argument, see L.
See also L.
=item cosh
This is identical to the C function C, for returning
the hyperbolic cosine of its numeric argument. See also L.
=item creat
Create a new file. This returns a file descriptor like the ones returned by
C. Use C to close the file.
$fd = POSIX::creat( "foo", 0611 );
POSIX::close( $fd );
See also L and its C flag.
=item ctermid
Generates the path name for the controlling terminal.
$path = POSIX::ctermid();
=item ctime
This is identical to the C function C and equivalent
to C, see L and L.
=item cuserid
Get the login name of the owner of the current process.
$name = POSIX::cuserid();
=item difftime
This is identical to the C function C, for returning
the time difference (in seconds) between two times (as returned
by C), see L .
=item div
div() is C-specific, use L on the usual C> division and
the modulus C<%>.
=item dup
This is similar to the C function C, for duplicating a file
descriptor.
This uses file descriptors such as those obtained by calling
C.
Returns C on failure.
=item dup2
This is similar to the C function C, for duplicating a file
descriptor to an another known file descriptor.
This uses file descriptors such as those obtained by calling
C.
Returns C on failure.
=item errno
Returns the value of errno.
$errno = POSIX::errno();
This identical to the numerical values of the C<$!>, see L.
=item execl
execl() is C-specific, see L.
=item execle
execle() is C-specific, see L.
=item execlp
execlp() is C-specific, see L.
=item execv
execv() is C-specific, see L.
=item execve
execve() is C-specific, see L.
=item execvp
execvp() is C-specific, see L.
=item exit
This is identical to Perl's builtin C function for exiting the
program, see L.
=item exp
This is identical to Perl's builtin C function for
returning the exponent (I-based) of the numerical argument,
see L.
=item fabs
This is identical to Perl's builtin C function for returning
the absolute value of the numerical argument, see L.
=item fclose
Use method C instead, or see L.
=item fcntl
This is identical to Perl's builtin C function,
see L.
=item fdopen
Use method C instead, or see L.
=item feof
Use method C instead, or see L.
=item ferror
Use method C instead.
=item fflush
Use method C instead.
See also L.
=item fgetc
Use method C instead, or see L.
=item fgetpos
Use method C instead, or see L.
=item fgets
Use method C instead. Similar to EE, also known
as L.
=item fileno
Use method C instead, or see L.
=item floor
This is identical to the C function C, returning the largest
integer value less than or equal to the numerical argument.
=item fmod
This is identical to the C function C.
$r = fmod($x, $y);
It returns the remainder C<$r = $x - $n*$y>, where C<$n = trunc($x/$y)>.
The C<$r> has the same sign as C<$x> and magnitude (absolute value)
less than the magnitude of C<$y>.
=item fopen
Use method C instead, or see L.
=item fork
This is identical to Perl's builtin C function
for duplicating the current process, see L
and L if you are in Windows.
=item fpathconf
Retrieves the value of a configurable limit on a file or directory. This
uses file descriptors such as those obtained by calling C.
The following will determine the maximum length of the longest allowable
pathname on the filesystem which holds C.
$fd = POSIX::open( "/var/foo", &POSIX::O_RDONLY );
$path_max = POSIX::fpathconf( $fd, &POSIX::_PC_PATH_MAX );
Returns C on failure.
=item fprintf
fprintf() is C-specific, see L instead.
=item fputc
fputc() is C-specific, see L instead.
=item fputs
fputs() is C-specific, see L instead.
=item fread
fread() is C-specific, see L instead.
=item free
free() is C-specific. Perl does memory management transparently.
=item freopen
freopen() is C-specific, see L instead.
=item frexp
Return the mantissa and exponent of a floating-point number.
($mantissa, $exponent) = POSIX::frexp( 1.234e56 );
=item fscanf
fscanf() is C-specific, use EE and regular expressions instead.
=item fseek
Use method C instead, or see L.
=item fsetpos
Use method C instead, or seek L.
=item fstat
Get file status. This uses file descriptors such as those obtained by
calling C. The data returned is identical to the data from
Perl's builtin C function.
$fd = POSIX::open( "foo", &POSIX::O_RDONLY );
@stats = POSIX::fstat( $fd );
=item fsync
Use method C instead.
=item ftell
Use method C instead, or see L.
=item fwrite
fwrite() is C-specific, see L instead.
=item getc
This is identical to Perl's builtin C function,
see L.
=item getchar
Returns one character from STDIN. Identical to Perl's C,
see L.
=item getcwd
Returns the name of the current working directory.
See also L.
=item getegid
Returns the effective group identifier. Similar to Perl' s builtin
variable C<$(>, see L.
=item getenv
Returns the value of the specified environment variable.
The same information is available through the C<%ENV> array.
=item geteuid
Returns the effective user identifier. Identical to Perl's builtin C<$E>
variable, see L.
=item getgid
Returns the user's real group identifier. Similar to Perl's builtin
variable C<$)>, see L.
=item getgrgid
This is identical to Perl's builtin C function for
returning group entries by group identifiers, see
L.
=item getgrnam
This is identical to Perl's builtin C function for
returning group entries by group names, see L.
=item getgroups
Returns the ids of the user's supplementary groups. Similar to Perl's
builtin variable C<$)>, see L.
=item getlogin
This is identical to Perl's builtin C function for
returning the user name associated with the current session, see
L.
=item getpgrp
This is identical to Perl's builtin C function for
returning the process group identifier of the current process, see
L.
=item getpid
Returns the process identifier. Identical to Perl's builtin
variable C<$$>, see L.
=item getppid
This is identical to Perl's builtin C function for
returning the process identifier of the parent process of the current
process , see L.
=item getpwnam
This is identical to Perl's builtin C function for
returning user entries by user names, see L.
=item getpwuid
This is identical to Perl's builtin C function for
returning user entries by user identifiers, see L.
=item gets
Returns one line from C, similar to EE, also known
as the C function, see L.
B: if you have C programs that still use C, be very
afraid. The C function is a source of endless grief because
it has no buffer overrun checks. It should B be used. The
C function should be preferred instead.
=item getuid
Returns the user's identifier. Identical to Perl's builtin C<$E> variable,
see L.
=item gmtime
This is identical to Perl's builtin C function for
converting seconds since the epoch to a date in Greenwich Mean Time,
see L.
=item isalnum
This is identical to the C function, except that it can apply to a
single character or to a whole string. Note that locale settings may
affect what characters are considered C. Does not work on
Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:alnum:]]/> construct instead, or possibly
the C\w/> construct.
=item isalpha
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:alpha:]]/> construct instead.
=item isatty
Returns a boolean indicating whether the specified filehandle is connected
to a tty. Similar to the C<-t> operator, see L.
=item iscntrl
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:cntrl:]]/> construct instead.
=item isdigit
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C (unlikely, but
still possible). Does not work on Unicode characters code point 256
or higher. Consider using regular expressions and the C[[:digit:]]/>
construct instead, or the C\d/> construct.
=item isgraph
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:graph:]]/> construct instead.
=item islower
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:lower:]]/> construct instead. Do B use
C[a-z]/>.
=item isprint
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:print:]]/> construct instead.
=item ispunct
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:punct:]]/> construct instead.
=item isspace
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:space:]]/> construct instead, or the C\s/>
construct. (Note that C\s/> and C[[:space:]]/> are slightly
different in that C[[:space:]]/> can normally match a vertical tab,
while C\s/> does not.)
=item isupper
This is identical to the C function, except that it can apply to
a single character or to a whole string. Note that locale settings
may affect what characters are considered C. Does not work
on Unicode characters code point 256 or higher. Consider using regular
expressions and the C[[:upper:]]/> construct instead. Do B use
C[A-Z]/>.
=item isxdigit
This is identical to the C function, except that it can apply to a single
character or to a whole string. Note that locale settings may affect what
characters are considered C (unlikely, but still possible).
Does not work on Unicode characters code point 256 or higher.
Consider using regular expressions and the C[[:xdigit:]]/>
construct instead, or simply C[0-9a-f]/i>.
=item kill
This is identical to Perl's builtin C function for sending
signals to processes (often to terminate them), see L.
=item labs
(For returning absolute values of long integers.)
labs() is C-specific, see L instead.
=item ldexp
This is identical to the C function C
for multiplying floating point numbers with powers of two.
$x_quadrupled = POSIX::ldexp($x, 2);
=item ldiv
(For computing dividends of long integers.)
ldiv() is C-specific, use C> and C instead.
=item link
This is identical to Perl's builtin C function
for creating hard links into files, see L.
=item localeconv
Get numeric formatting information. Returns a reference to a hash
containing the current locale formatting values.
Here is how to query the database for the B (Deutsch or German) locale.
$loc = POSIX::setlocale( &POSIX::LC_ALL, "de" );
print "Locale = $loc\n";
$lconv = POSIX::localeconv();
print "decimal_point = ", $lconv->{decimal_point}, "\n";
print "thousands_sep = ", $lconv->{thousands_sep}, "\n";
print "grouping = ", $lconv->{grouping}, "\n";
print "int_curr_symbol = ", $lconv->{int_curr_symbol}, "\n";
print "currency_symbol = ", $lconv->{currency_symbol}, "\n";
print "mon_decimal_point = ", $lconv->{mon_decimal_point}, "\n";
print "mon_thousands_sep = ", $lconv->{mon_thousands_sep}, "\n";
print "mon_grouping = ", $lconv->{mon_grouping}, "\n";
print "positive_sign = ", $lconv->{positive_sign}, "\n";
print "negative_sign = ", $lconv->{negative_sign}, "\n";
print "int_frac_digits = ", $lconv->{int_frac_digits}, "\n";
print "frac_digits = ", $lconv->{frac_digits}, "\n";
print "p_cs_precedes = ", $lconv->{p_cs_precedes}, "\n";
print "p_sep_by_space = ", $lconv->{p_sep_by_space}, "\n";
print "n_cs_precedes = ", $lconv->{n_cs_precedes}, "\n";
print "n_sep_by_space = ", $lconv->{n_sep_by_space}, "\n";
print "p_sign_posn = ", $lconv->{p_sign_posn}, "\n";
print "n_sign_posn = ", $lconv->{n_sign_posn}, "\n";
=item localtime
This is identical to Perl's builtin C function for
converting seconds since the epoch to a date see L.
=item log
This is identical to Perl's builtin C function,
returning the natural (I-based) logarithm of the numerical argument,
see L.
=item log10
This is identical to the C function C,
returning the 10-base logarithm of the numerical argument.
You can also use
sub log10 { log($_[0]) / log(10) }
or
sub log10 { log($_[0]) / 2.30258509299405 }
or
sub log10 { log($_[0]) * 0.434294481903252 }
=item longjmp
longjmp() is C-specific: use L instead.
=item lseek
Move the file's read/write position. This uses file descriptors such as
those obtained by calling C.
$fd = POSIX::open( "foo", &POSIX::O_RDONLY );
$off_t = POSIX::lseek( $fd, 0, &POSIX::SEEK_SET );
Returns C on failure.
=item malloc
malloc() is C-specific. Perl does memory management transparently.
=item mblen
This is identical to the C function C.
Perl does not have any support for the wide and multibyte
characters of the C standards, so this might be a rather
useless function.
=item mbstowcs
This is identical to the C function C.
Perl does not have any support for the wide and multibyte
characters of the C standards, so this might be a rather
useless function.
=item mbtowc
This is identical to the C function C.
Perl does not have any support for the wide and multibyte
characters of the C standards, so this might be a rather
useless function.
=item memchr
memchr() is C-specific, see L instead.
=item memcmp
memcmp() is C-specific, use C instead, see L.
=item memcpy
memcpy() is C-specific, use C<=>, see L, or see L.
=item memmove
memmove() is C-specific, use C<=>, see L, or see L.
=item memset
memset() is C-specific, use C instead, see L.
=item mkdir
This is identical to Perl's builtin C function
for creating directories, see L.
=item mkfifo
This is similar to the C function C for creating
FIFO special files.
if (mkfifo($path, $mode)) { ....
Returns C on failure. The C<$mode> is similar to the
mode of C, see L, though for C
you B specify the C<$mode>.
=item mktime
Convert date/time info to a calendar time.
Synopsis:
mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = -1)
The month (C), weekday (C), and yearday (C) begin at zero.
I.e. January is 0, not 1; Sunday is 0, not 1; January 1st is 0, not 1. The
year (C) is given in years since 1900. I.e. The year 1995 is 95; the
year 2001 is 101. Consult your system's C manpage for details
about these and the other arguments.
Calendar time for December 12, 1995, at 10:30 am.
$time_t = POSIX::mktime( 0, 30, 10, 12, 11, 95 );
print "Date = ", POSIX::ctime($time_t);
Returns C on failure.
=item modf
Return the integral and fractional parts of a floating-point number.
($fractional, $integral) = POSIX::modf( 3.14 );
=item nice
This is similar to the C function C, for changing
the scheduling preference of the current process. Positive
arguments mean more polite process, negative values more
needy process. Normal user processes can only be more polite.
Returns C on failure.
=item offsetof
offsetof() is C-specific, you probably want to see L instead.
=item open
Open a file for reading for writing. This returns file descriptors, not
Perl filehandles. Use C to close the file.
Open a file read-only with mode 0666.
$fd = POSIX::open( "foo" );
Open a file for read and write.
$fd = POSIX::open( "foo", &POSIX::O_RDWR );
Open a file for write, with truncation.
$fd = POSIX::open( "foo", &POSIX::O_WRONLY | &POSIX::O_TRUNC );
Create a new file with mode 0640. Set up the file for writing.
$fd = POSIX::open( "foo", &POSIX::O_CREAT | &POSIX::O_WRONLY, 0640 );
Returns C on failure.
See also L.
=item opendir
Open a directory for reading.
$dir = POSIX::opendir( "/var" );
@files = POSIX::readdir( $dir );
POSIX::closedir( $dir );
Returns C on failure.
=item pathconf
Retrieves the value of a configurable limit on a file or directory.
The following will determine the maximum length of the longest allowable
pathname on the filesystem which holds C.
$path_max = POSIX::pathconf( "/var", &POSIX::_PC_PATH_MAX );
Returns C on failure.
=item pause
This is similar to the C function C, which suspends
the execution of the current process until a signal is received.
Returns C on failure.
=item perror
This is identical to the C function C, which outputs to the
standard error stream the specified message followed by ": " and the
current error string. Use the C function and the C<$!>
variable instead, see L and L.
=item pipe
Create an interprocess channel. This returns file descriptors like those
returned by C.
my ($read, $write) = POSIX::pipe();
POSIX::write( $write, "hello", 5 );
POSIX::read( $read, $buf, 5 );
See also L.
=item pow
Computes C<$x> raised to the power C<$exponent>.
$ret = POSIX::pow( $x, $exponent );
You can also use the C<**> operator, see L.
=item printf
Formats and prints the specified arguments to STDOUT.
See also L.
=item putc
putc() is C-specific, see L instead.
=item putchar
putchar() is C-specific, see L instead.
=item puts
puts() is C-specific, see L instead.
=item qsort
qsort() is C-specific, see L instead.
=item raise
Sends the specified signal to the current process.
See also L and the C<$$> in L.
=item rand
C is non-portable, see L instead.
=item read
Read from a file. This uses file descriptors such as those obtained by
calling C. If the buffer C<$buf> is not large enough for the
read then Perl will extend it to make room for the request.
$fd = POSIX::open( "foo", &POSIX::O_RDONLY );
$bytes = POSIX::read( $fd, $buf, 3 );
Returns C