4 # Ramrodded by Dean Roehrich.
6 # Submissions for function descriptions are needed. Don't write a tutorial,
7 # and don't repeat things that can be found in the system's manpages,
8 # just give a quick 2-3 line note and a one-line example.
10 # Check the latest version of the Perl5 Module List for Dean's current
11 # email address (listed as DMR).
13 my $VERS = 951129; # yymmdd
18 open( XS, "<POSIX.xs" ) || die "Unable to open POSIX.xs";
19 open( PM, "<POSIX.pm" ) || die "Unable to open POSIX.pm";
21 open( STDOUT, ">POSIX.pod" ) || die "Unable to open POSIX.pod";
26 POSIX - Perl interface to IEEE Std 1003.1
32 use POSIX qw(:errno_h :fcntl_h);
34 printf "EINTR is %d\n", EINTR;
36 $sess_id = POSIX::setsid();
38 $fd = POSIX::open($path, O_CREAT|O_EXCL|O_WRONLY, 0644);
39 # note: that's a filedescriptor, *NOT* a filehandle
43 The POSIX module permits you to access all (or nearly all) the standard
44 POSIX 1003.1 identifiers. Many of these identifiers have been given Perl-ish
45 interfaces. Things which are C<#defines> in C, like EINTR or O_NDELAY, are
46 automatically exported into your namespace. All functions are only exported
47 if you ask for them explicitly. Most likely people will prefer to use the
48 fully-qualified function names.
50 This document gives a condensed list of the features available in the POSIX
51 module. Consult your operating system's manpages for general information on
52 most features. Consult L<perlfunc> for functions which are noted as being
53 identical to Perl's builtin functions.
55 The first section describes POSIX functions from the 1003.1 specification.
56 The second section describes some classes for signal objects, TTY objects,
57 and other miscellaneous objects. The remaining sections list various
58 constants and macros in an organization which roughly follows IEEE Std
63 The POSIX module is probably the most complex Perl module supplied with
64 the standard distribution. It incorporates autoloading, namespace games,
65 and dynamic loading of code that's in Perl, C, or both. It's a great
70 A few functions are not implemented because they are C specific. If you
71 attempt to call these, they will print a message telling you that they
72 aren't implemented, and suggest using the Perl equivalent should one
73 exist. For example, trying to access the setjmp() call will elicit the
74 message "setjmp() is C-specific: use eval {} instead".
76 Furthermore, some evil vendors will claim 1003.1 compliance, but in fact
77 are not so: they will not pass the PCTS (POSIX Compliance Test Suites).
78 For example, one vendor may not define EDEADLK, or the semantics of the
79 errno values set by open(2) might not be quite right. Perl does not
80 attempt to verify POSIX compliance. That means you can currently
81 successfully say "use POSIX", and then later in your program you find
82 that your vendor has been lax and there's no usable ICANON macro after
83 all. This could be construed to be a bug.
93 my $posixpack = Package->new( 'POSIX' );
94 my $descriptions = Description->new;
96 get_constants( 'XS', $constants, $macros );
97 get_functions( 'XS', $packages, $posixpack );
98 get_PMfunctions( 'PM', $packages, $posixpack, $descriptions );
101 # It is possible that the matches of setup_*() may depend on
102 # the matches of an earlier setup_*(). If you change the order,
103 # be careful that you're getting only what you want, and no more.
105 my $termios_flags = setup_termios( $constants );
106 my $wait_stuff = setup_wait( $constants, $macros );
107 my $stat = setup_file_char( $constants, $macros );
108 my $port = setup_pat( $constants, '^_POSIX' );
109 my $sc = setup_pat( $constants, '^_SC_' );
110 my $pc = setup_pat( $constants, '^_PC_' );
111 my $fcntl = setup_pat( $constants, '^([FO]_|FD_)' );
112 my $sigs = setup_pat( $constants, '^(SIG|SA_)' );
113 my $float = setup_pat( $constants, '^(L?DBL_|FLT_)' );
114 my $locale = setup_pat( $constants, '^LC_' );
115 my $stdio = setup_pat( $constants, '(^BUFSIZ$)|(^L_)|(^_IO...$)|(^EOF$)|(^FILENAME_MAX$)|(^TMP_MAX$)' );
116 my $stdlib = setup_pat( $constants, '(^EXIT_)|(^MB_CUR_MAX$)|(^RAND_MAX$)' );
117 my $limits = setup_pat( $constants, '(_MAX$)|(_MIN$)|(_BIT$)|(^MAX_)|(_BUF$)' );
118 my $math = setup_pat( $constants, '^HUGE_VAL$' );
119 my $time = setup_pat( $constants, '^CL' );
120 my $unistd = setup_pat( $constants, '(_FILENO$)|(^SEEK_...$)|(^._OK$)' );
121 my $errno = setup_pat( $constants, '^E' );
123 print_posix( $posixpack, $descriptions );
124 print_classes( $packages, $constants, $termios_flags, $descriptions );
125 print_misc( 'Pathname Constants', $pc );
126 print_misc( 'POSIX Constants', $port );
127 print_misc( 'System Configuration', $sc );
128 print_misc( 'Errno', $errno );
129 print_misc( 'Fcntl', $fcntl );
130 print_misc( 'Float', $float );
131 print_misc( 'Limits', $limits );
132 print_misc( 'Locale', $locale );
133 print_misc( 'Math', $math );
134 print_misc( 'Signal', $sigs );
135 print_misc( 'Stat', $stat );
136 print_misc( 'Stdlib', $stdlib );
137 print_misc( 'Stdio', $stdio );
138 print_misc( 'Time', $time );
139 print_misc( 'Unistd', $unistd );
140 print_misc( 'Wait', $wait_stuff );
144 dregs( $macros, $constants );
152 my $constants = shift;
154 foreach (keys %$macros){
155 warn "Unknown macro $_ in the POSIX.xs module.\n";
157 foreach (keys %$constants){
158 warn "Unknown constant $_ in the POSIX.xs module.\n";
165 my $constants = shift;
174 if( /return\s+([^;]+)/ ){
179 $v =~ s,\(.*?\)\s*(\w),$1,;
181 if( $v =~ s/(\(.*?\))// ){
185 $constants->{$v} = 1;
192 Close the file. This uses file descriptors such as those obtained by calling
195 $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
201 my $packages = shift;
202 my $posixpack = shift;
207 my( $curfuncs, $curpack );
211 if( /^MODULE.*?PACKAGE\s*=\s*([^\s]+)/ ){
214 if( /PREFIX\s*=\s*([^\n]+)/ ){
217 #print "package($pack) prefix($prefix)\n";
218 if( $pack eq 'POSIX' ){
219 $curpack = $posixpack;
222 $curpack = Package->new( $pack );
223 push @$packages, $curpack;
225 $curfuncs = $curpack->curfuncs;
230 # find function header
231 if( /^[^\s]/ && ! /^#/ ){
232 $ret = /^SysRet/ ? 2 : 1;
234 next if( $pack eq 'POSIX' and $x =~ /^constant/ );
235 $x =~ /^(.*?)\s*\((.*?)\)/;
236 ($x,$y) = ($1, $2); # func,sig
238 $curfuncs->{$x} = $ret;
241 # leave function header
249 sub get_PMfunctions {
252 my $packages = shift;
253 my $posixpack = shift;
256 my( $curfuncs, $curpack );
261 # find the second package statement.
263 if( /^package\s+(.*?);/ ){
265 last if $pack ne 'POSIX';
269 # Check if this package is already
272 foreach (@$packages){
273 if( $_->name eq $pack ){
278 # maybe start a new package.
279 if( $curpack eq '' ){
280 $curpack = Package->new( $pack );
281 push @$packages, $curpack;
283 $curfuncs = $curpack->curfuncs;
285 # now fetch functions
287 if( /^package\s+(.*?);/ ){
289 if( $pack eq 'POSIX' ){
290 $curpack = $posixpack;
293 # Check if this package is already
296 foreach (@$packages){
297 if( $_->name() eq $pack ){
302 # maybe start a new package.
303 if( $curpack eq '' ){
304 $curpack = Package->new( $pack );
305 push @$packages, $curpack;
308 $curfuncs = $curpack->curfuncs;
311 if( /^sub\s+(.*?)\s/ ){
315 if( $pack eq 'POSIX::SigAction' and
317 $curfuncs->{$sub} = 1;
319 elsif( $pack eq 'POSIX' and $sub eq 'perror' ){
320 $curfuncs->{$sub} = 1;
325 if( /usage.*?\((.*?)\)/ ){
327 $curfuncs->{$sub} = 1;
330 if( /^\s+unimpl\s+"(.*?)"/ ){
333 $desc->append( $pack, $sub, $y );
334 $curfuncs->{$sub} = 1;
337 if( /^\s+redef\s+"(.*?)"/ ){
339 $y = "Use method C<$x> instead";
340 $desc->append( $pack, $sub, $y );
341 $curfuncs->{$sub} = 1;
347 Retrieves the value of a configurable limit on a file or directory. This
348 uses file descriptors such as those obtained by calling C<POSIX::open>.
350 The following will determine the maximum length of the longest allowable
351 pathname on the filesystem which holds C</tmp/foo>.
353 $fd = POSIX::open( "/tmp/foo", &POSIX::O_RDONLY );
354 $path_max = POSIX::fpathconf( $fd, &POSIX::_PC_PATH_MAX );
355 Return the mantissa and exponent of a floating-point number.
357 ($mantissa, $exponent) = POSIX::frexp( 3.14 );
358 Get file status. This uses file descriptors such as those obtained by
359 calling C<POSIX::open>. The data returned is identical to the data from
360 Perl's builtin C<stat> function.
362 $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
363 @stats = POSIX::fstat( $fd );
369 print "=head1 FUNCTIONS\n\n";
371 dumpfuncs( $pack, $desc );
376 my $packages = shift;
377 my $constants = shift;
383 print "=head1 CLASSES\n\n";
384 @pkgs = sort { $main::a->name() cmp $main::b->name() } @$packages;
387 print "=head2 ", $pack->name(), "\n\n";
390 dumpfuncs( $pack, $desc );
392 if( $pack->name() =~ /termios/i ){
393 dumpflags( $termios );
400 my $constants = shift;
404 'c_iflag field' => [qw( BRKINT ICRNL IGNBRK IGNCR IGNPAR
405 INLCR INPCK ISTRIP IXOFF IXON
407 'c_oflag field' => [qw( OPOST )],
408 'c_cflag field' => [qw( CLOCAL CREAD CSIZE CS5 CS6 CS7 CS8
409 CSTOPB HUPCL PARENB PARODD )],
410 'c_lflag field' => [qw( ECHO ECHOE ECHOK ECHONL ICANON
411 IEXTEN ISIG NOFLSH TOSTOP )],
412 'c_cc field' => [qw( VEOF VEOL VERASE VINTR VKILL VQUIT
413 VSUSP VSTART VSTOP VMIN VTIME NCCS )],
415 'Terminal interface' => [],
417 # look for baud rates in constants, add to termios
418 foreach (keys %$constants){
420 push @{$obj->{'Baud rate'}}, $_;
423 # look for TC* in constants, add to termios
424 foreach (keys %$constants){
426 push @{$obj->{'Terminal interface'}}, $_;
430 foreach (keys %$obj){
431 trim_hash( 'Constant', $obj->{$_}, $constants );
440 my $curfuncs = $pack->curfuncs;
441 my $pname = $pack->name;
443 my @funcs = sort keys %$curfuncs;
445 if( exists $curfuncs->{'new'} ){ # do new first
446 @funcs = grep( $_ ne 'new', @funcs );
447 unshift @funcs, 'new';
450 $func = shift @funcs;
451 if( $func eq 'DESTROY' ){
452 next; # don't do DESTROY
454 print "=item $func\n\n";
455 if( $desc->print( $pname, $func, $curfuncs->{$func} ) ){
456 # if it was printed, note that
457 delete $curfuncs->{$func};
466 foreach $field (sort keys %$flags){
467 print "=item $field values\n\n";
468 print join( ' ', @{$flags->{$field}} ), "\n\n";
473 my $constants = shift;
478 'Macros' => [qw( WIFEXITED WEXITSTATUS WIFSIGNALED
479 WTERMSIG WIFSTOPPED WSTOPSIG )],
480 'Constants' => [qw( WNOHANG WUNTRACED )],
482 trim_hash( 'Constant', $obj->{Constants}, $constants );
483 trim_hash( 'Macro', $obj->{Macros}, $macros );
487 sub setup_file_char {
488 my $constants = shift;
496 # find S_* constants and add to object.
497 foreach (sort keys %$constants){
499 push @{$obj->{'Constants'}}, $_;
502 # find S_* macros and add to object.
503 foreach (sort keys %$macros){
505 push @{$obj->{'Macros'}}, $_;
509 trim_hash( 'Constant', $obj->{Constants}, $constants );
510 trim_hash( 'Macro', $obj->{Macros}, $macros );
516 my $constants = shift;
520 $obj = { 'Constants' => [] };
521 foreach (sort keys %$constants){
523 push @{$obj->{'Constants'}}, $_;
526 trim_hash( 'Constant', $obj->{Constants}, $constants );
530 Get numeric formatting information. Returns a reference to a hash
531 containing the current locale formatting values.
533 The database for the B<de> (Deutsch or German) locale.
535 $loc = POSIX::setlocale( &POSIX::LC_ALL, "de" );
536 print "Locale = $loc\n";
537 $lconv = POSIX::localeconv();
538 print "decimal_point = ", $lconv->{decimal_point}, "\n";
539 print "thousands_sep = ", $lconv->{thousands_sep}, "\n";
540 print "grouping = ", $lconv->{grouping}, "\n";
541 print "int_curr_symbol = ", $lconv->{int_curr_symbol}, "\n";
542 print "currency_symbol = ", $lconv->{currency_symbol}, "\n";
543 print "mon_decimal_point = ", $lconv->{mon_decimal_point}, "\n";
544 print "mon_thousands_sep = ", $lconv->{mon_thousands_sep}, "\n";
545 print "mon_grouping = ", $lconv->{mon_grouping}, "\n";
546 print "positive_sign = ", $lconv->{positive_sign}, "\n";
547 print "negative_sign = ", $lconv->{negative_sign}, "\n";
548 print "int_frac_digits = ", $lconv->{int_frac_digits}, "\n";
549 print "frac_digits = ", $lconv->{frac_digits}, "\n";
550 print "p_cs_precedes = ", $lconv->{p_cs_precedes}, "\n";
551 print "p_sep_by_space = ", $lconv->{p_sep_by_space}, "\n";
552 print "n_cs_precedes = ", $lconv->{n_cs_precedes}, "\n";
553 print "n_sep_by_space = ", $lconv->{n_sep_by_space}, "\n";
554 print "p_sign_posn = ", $lconv->{p_sign_posn}, "\n";
555 print "n_sign_posn = ", $lconv->{n_sign_posn}, "\n";
556 Move the read/write file pointer. This uses file descriptors such as
557 those obtained by calling C<POSIX::open>.
559 $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
560 $off_t = POSIX::lseek( $fd, 0, &POSIX::SEEK_SET );
565 print "=head1 CREATION\n\n";
566 print "This document generated by $0 version $vers.\n\n";
574 print "=head1 ", uc($hdr), "\n\n";
576 foreach $item (sort keys %$obj){
577 print "=item $item\n\n";
578 print join( ' ', @{$obj->{$item}}), "\n\n";
589 if( exists $hv->{$_} ){
593 warn "$name $_ is not in the POSIX.xs module";
598 { package Package; ## Package package
602 my $pack = shift || die;
603 my $self = [ $pack, {} ];
616 my $pack = $self->name;
617 foreach (keys %{$self->curfuncs}){
618 if( $_ eq 'DESTROY' ){
619 next; # don't expect much on DESTROY
621 warn "Function ". $pack . "::$_ did not have a description.\n";
625 { package Description; ## Function description
647 if( /^=([^\+]+)\+\+/ ){
651 elsif( /^=([^\+]+)\+C/ ){
655 elsif( /^=([^\+]+)\+(\w+)/ ){
665 $self->{$pack."::$sub"} = '';
666 $c = \($self->{$pack."::$sub"});
668 $$c .= "This is identical to the C function C<$sub()>.\n";
671 $$c .= "This is identical to Perl's builtin C<$as()> function.\n";
680 foreach (keys %$self){
681 warn "Function $_ is not in the POSIX.xs module.\n";
688 my $str = shift || die;
690 if( exists $self->{$pack."::$sub"} ){
691 $self->{$pack."::$sub"} .= "\n$str.\n";
694 $self->{$pack."::$sub"} = "$str.\n";
701 my $rtype = shift || die;
704 if( exists $self->{$pack."::$sub"} ){
706 $self->{$pack."::$sub"} =~ s/identical/similar/;
708 print $self->{$pack."::$sub"}, "\n";
709 delete $self->{$pack."::$sub"};
711 print "Returns C<undef> on failure.\n\n";
719 Create an interprocess channel. This returns file descriptors like those
720 returned by C<POSIX::open>.
722 ($fd0, $fd1) = POSIX::pipe();
723 POSIX::write( $fd0, "hello", 5 );
724 POSIX::read( $fd1, $buf, 5 );
725 Read from a file. This uses file descriptors such as those obtained by
726 calling C<POSIX::open>. If the buffer C<$buf> is not large enough for the
727 read then Perl will extend it to make room for the request.
729 $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
730 $bytes = POSIX::read( $fd, $buf, 3 );
731 This is similar to the C function C<setpgid()>.
732 Detailed signal management. This uses C<POSIX::SigAction> objects for the
733 C<action> and C<oldaction> arguments. Consult your system's C<sigaction>
738 sigaction(sig, action, oldaction = 0)
739 Install a signal mask and suspend process until signal arrives. This uses
740 C<POSIX::SigSet> objects for the C<signal_mask> argument. Consult your
741 system's C<sigsuspend> manpage for details.
745 sigsuspend(signal_mask)
746 This is identical to Perl's builtin C<sprintf()> function.
747 Convert date and time information to string. Returns the string.
751 strftime(fmt, sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
753 The month (C<mon>), weekday (C<wday>), and yearday (C<yday>) begin at zero.
754 I.e. January is 0, not 1; Sunday is 0, not 1; January 1st is 0, not 1. The
755 year (C<year>) is given in years since 1900. I.e. The year 1995 is 95; the
756 year 2001 is 101. Consult your system's C<strftime()> manpage for details
757 about these and the other arguments.
759 The string for Tuesday, December 12, 1995.
761 $str = POSIX::strftime( "%A, %B %d, %Y", 0, 0, 0, 12, 11, 95, 2 );
763 String transformation. Returns the transformed string.
765 $dst = POSIX::strxfrm( $src );
766 Get name of current operating system.
768 ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
769 Returns the current file position, in bytes.
772 Get terminal control attributes.
774 Obtain the attributes for stdin.
778 Obtain the attributes for stdout.
780 $termios->getattr( 1 )
781 Set terminal control attributes.
783 Set attributes immediately for stdout.
785 $termios->setattr( 1, &POSIX::TCSANOW );
791 Create a new SigSet object. This object will be destroyed automatically
792 when it is no longer needed. Arguments may be supplied to initialize the
797 $sigset = POSIX::SigSet->new;
799 Create a set with SIGUSR1.
801 $sigset = POSIX::SigSet->new( &POSIX::SIGUSR1 );
803 Add a signal to a SigSet object.
805 $sigset->addset( &POSIX::SIGUSR2 );
807 Remove a signal from the SigSet object.
809 $sigset->delset( &POSIX::SIGUSR2 );
811 Initialize the SigSet object to be empty.
815 Initialize the SigSet object to include all signals.
819 Tests the SigSet object to see if it contains a specific signal.
821 if( $sigset->ismember( &POSIX::SIGUSR1 ) ){
822 print "contains SIGUSR1\n";
827 Create a new Termios object. This object will be destroyed automatically
828 when it is no longer needed.
830 $termios = POSIX::Termios->new;
832 Retrieve the c_iflag field of a termios object.
834 $c_iflag = $termios->getiflag;
836 Retrieve the c_oflag field of a termios object.
838 $c_oflag = $termios->getoflag;
840 Retrieve the c_cflag field of a termios object.
842 $c_cflag = $termios->getcflag;
844 Retrieve the c_lflag field of a termios object.
846 $c_lflag = $termios->getlflag;
848 Retrieve a value from the c_cc field of a termios object. The c_cc field is
849 an array so an index must be specified.
851 $c_cc[1] = $termios->getcc(1);
853 Retrieve the output baud rate.
855 $ospeed = $termios->getospeed;
857 Retrieve the input baud rate.
859 $ispeed = $termios->getispeed;
861 Set the c_iflag field of a termios object.
863 $termios->setiflag( &POSIX::BRKINT );
865 Set the c_oflag field of a termios object.
867 $termios->setoflag( &POSIX::OPOST );
869 Set the c_cflag field of a termios object.
871 $termios->setcflag( &POSIX::CLOCAL );
873 Set the c_lflag field of a termios object.
875 $termios->setlflag( &POSIX::ECHO );
877 Set a value in the c_cc field of a termios object. The c_cc field is an
878 array so an index must be specified.
880 $termios->setcc( 1, &POSIX::VEOF );
882 Set the output baud rate.
884 $termios->setospeed( &POSIX::B9600 );
886 Set the input baud rate.
888 $termios->setispeed( &POSIX::B9600 );
986 Returns a boolean indicating whether the specified filehandle is connected
989 Sets the real user id for this process.
991 Sets the real group id for this process.
993 Returns the process's id.
995 Returns the user's id.
997 Returns the effective group id.
999 Returns the effective user id.
1001 Returns the user's real group id.
1003 Returns the ids of the user's supplementary groups.
1005 Returns the name of the current working directory.
1007 Returns the error string for the specified errno.
1009 Returns the value of the specified enironment variable.
1011 Returns one character from STDIN.
1013 Sends the specified signal to the current process.
1015 Returns one line from STDIN.
1017 Prints the specified arguments to STDOUT.
1019 Seeks to the beginning of the file.
1022 Returns a name for a temporary file.
1024 $tmpfile = POSIX::tmpnam();
1026 Get the character login name of the user.
1028 $name = POSIX::cuserid();
1030 Generates the path name for controlling terminal.
1032 $path = POSIX::ctermid();
1034 The times() function returns elapsed realtime since some point in the past
1035 (such as system startup), user and system times for this process, and user
1036 and system times used by child processes. All times are returned in clock
1039 ($realtime, $user, $system, $cuser, $csystem) = POSIX::times();
1041 Note: Perl's builtin C<times()> function returns four values, measured in
1044 Computes $x raised to the power $exponent.
1046 $ret = POSIX::pow( $x, $exponent );
1048 Returns the value of errno.
1050 $errno = POSIX::errno();
1052 Retrieves values of system configurable variables.
1054 The following will get the machine's clock speed.
1056 $clock_ticks = POSIX::sysconf( &POSIX::_SC_CLK_TCK );
1058 Retrieves the value of a configurable limit on a file or directory.
1060 The following will determine the maximum length of the longest allowable
1061 pathname on the filesystem which holds C</tmp>.
1063 $path_max = POSIX::pathconf( "/tmp", &POSIX::_PC_PATH_MAX );
1065 Determines the accessibility of a file.
1067 if( POSIX::access( "/", &POSIX::R_OK ) ){
1068 print "have read permission\n";
1071 Modifies and queries program's locale.
1073 The following will set the traditional UNIX system locale behavior.
1075 This document generated by ./mkposixman.PL version 19951212.
1133 Creates a new SigAction object. This object will be destroyed automatically
1134 when it is no longer needed.