3 our(@ISA, %EXPORT_TAGS, @EXPORT_OK, $AUTOLOAD, %SIGRT) = ();
11 use Fcntl qw(FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK F_RDLCK F_SETFD
12 F_SETFL F_SETLK F_SETLKW F_UNLCK F_WRLCK O_ACCMODE O_APPEND
13 O_CREAT O_EXCL O_NOCTTY O_NONBLOCK O_RDONLY O_RDWR O_TRUNC
16 # Grandfather old foo_h form to new :foo_h form
20 load_imports() unless $loaded++;
22 my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_;
23 local $Exporter::ExportLevel = 1;
24 Exporter::import($this,@list);
27 sub croak { require Carp; goto &Carp::croak }
28 # declare usage to assist AutoLoad
31 XSLoader::load 'POSIX', $VERSION;
33 my %NON_CONSTS = (map {($_,1)}
34 qw(S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISREG WEXITSTATUS
35 WIFEXITED WIFSIGNALED WIFSTOPPED WSTOPSIG WTERMSIG));
38 if ($AUTOLOAD =~ /::(_?[a-z])/) {
40 $AutoLoader::AUTOLOAD = $AUTOLOAD;
41 goto &AutoLoader::AUTOLOAD
44 my $constname = $AUTOLOAD;
45 $constname =~ s/.*:://;
46 if ($NON_CONSTS{$constname}) {
47 my ($val, $error) = &int_macro_int($constname, $_[0]);
48 croak $error if $error;
49 *$AUTOLOAD = sub { &int_macro_int($constname, $_[0]) };
51 my ($error, $val) = constant($constname);
52 croak $error if $error;
53 *$AUTOLOAD = sub { $val };
59 package POSIX::SigAction;
61 use AutoLoader 'AUTOLOAD';
68 use base qw(Tie::StdHash);
70 use vars qw($SIGACTION_FLAGS);
74 my ($SIGRTMIN, $SIGRTMAX, $sigrtn);
77 $SIGRTMIN = &POSIX::SIGRTMIN;
78 $SIGRTMAX = &POSIX::SIGRTMAX;
79 $sigrtn = $SIGRTMAX - $SIGRTMIN;
83 &_init unless defined $sigrtn;
84 die "POSIX::SigRt not available" unless defined $sigrtn && $sigrtn > 0;
90 # Allow (SIGRT)?MIN( + n)?, a common idiom when doing these things in C.
91 $rtsig = $SIGRTMIN + ($1 || 0)
92 if $rtsig =~ /^(?:(?:SIG)?RT)?MIN(\s*\+\s*(\d+))?$/;
97 my $rtsig = _getsig($_[1]);
98 my $ok = $rtsig >= $SIGRTMIN && $rtsig <= $SIGRTMAX;
103 my ($rtsig, $ok) = &_exist;
104 die "No POSIX::SigRt signal $_[1] (valid range SIGRTMIN..SIGRTMAX, or $SIGRTMIN..$SIGRTMAX)"
110 my ($rtsig, $handler, $flags) = @_;
111 my $sigset = POSIX::SigSet->new($rtsig);
112 my $sigact = POSIX::SigAction->new($handler,
115 POSIX::sigaction($rtsig, $sigact);
118 sub EXISTS { &_exist }
119 sub FETCH { my $rtsig = &_check;
120 my $oa = POSIX::SigAction->new();
121 POSIX::sigaction($rtsig, undef, $oa);
122 return $oa->{HANDLER} }
123 sub STORE { my $rtsig = &_check; new($rtsig, $_[2], $SIGACTION_FLAGS) }
124 sub DELETE { delete $SIG{ &_check } }
125 sub CLEAR { &_exist; delete @SIG{ &POSIX::SIGRTMIN .. &POSIX::SIGRTMAX } }
126 sub SCALAR { &_croak; $sigrtn + 1 }
128 tie %POSIX::SIGRT, 'POSIX::SigRt';
137 croak "Usage: POSIX::$mess";
142 croak "Use method $mess instead";
148 croak "Unimplemented: POSIX::$mess";
152 usage "assert(expr)" if @_ != 1;
154 croak "Assertion failed";
159 usage "tolower(string)" if @_ != 1;
164 usage "toupper(string)" if @_ != 1;
169 usage "closedir(dirhandle)" if @_ != 1;
170 CORE::closedir($_[0]);
174 usage "opendir(directory)" if @_ != 1;
176 CORE::opendir($dirhandle, $_[0])
182 usage "readdir(dirhandle)" if @_ != 1;
183 CORE::readdir($_[0]);
187 usage "rewinddir(dirhandle)" if @_ != 1;
188 CORE::rewinddir($_[0]);
192 usage "errno()" if @_ != 0;
197 usage "creat(filename, mode)" if @_ != 2;
198 &open($_[0], &O_WRONLY | &O_CREAT | &O_TRUNC, $_[1]);
202 usage "fcntl(filehandle, cmd, arg)" if @_ != 3;
203 CORE::fcntl($_[0], $_[1], $_[2]);
207 usage "getgrgid(gid)" if @_ != 1;
208 CORE::getgrgid($_[0]);
212 usage "getgrnam(name)" if @_ != 1;
213 CORE::getgrnam($_[0]);
217 usage "atan2(x,y)" if @_ != 2;
218 CORE::atan2($_[0], $_[1]);
222 usage "cos(x)" if @_ != 1;
227 usage "exp(x)" if @_ != 1;
232 usage "fabs(x)" if @_ != 1;
237 usage "log(x)" if @_ != 1;
242 usage "pow(x,exponent)" if @_ != 2;
247 usage "sin(x)" if @_ != 1;
252 usage "sqrt(x)" if @_ != 1;
257 usage "getpwnam(name)" if @_ != 1;
258 CORE::getpwnam($_[0]);
262 usage "getpwuid(uid)" if @_ != 1;
263 CORE::getpwuid($_[0]);
267 unimpl "longjmp() is C-specific: use die instead";
271 unimpl "setjmp() is C-specific: use eval {} instead";
275 unimpl "siglongjmp() is C-specific: use die instead";
279 unimpl "sigsetjmp() is C-specific: use eval {} instead";
283 usage "kill(pid, sig)" if @_ != 2;
284 CORE::kill $_[1], $_[0];
288 usage "raise(sig)" if @_ != 1;
289 CORE::kill $_[0], $$; # Is this good enough?
293 unimpl "offsetof() is C-specific, stopped";
297 redef "IO::Handle::clearerr()";
301 redef "IO::Handle::close()";
305 redef "IO::Handle::new_from_fd()";
309 redef "IO::Handle::eof()";
313 redef "IO::Handle::getc()";
317 redef "IO::Handle::gets()";
321 redef "IO::Handle::fileno()";
325 redef "IO::File::open()";
329 unimpl "fprintf() is C-specific--use printf instead";
333 unimpl "fputc() is C-specific--use print instead";
337 unimpl "fputs() is C-specific--use print instead";
341 unimpl "fread() is C-specific--use read instead";
345 unimpl "freopen() is C-specific--use open instead";
349 unimpl "fscanf() is C-specific--use <> and regular expressions instead";
353 redef "IO::Seekable::seek()";
357 redef "IO::Handle::sync()";
361 redef "IO::Handle::error()";
365 redef "IO::Handle::flush()";
369 redef "IO::Seekable::getpos()";
373 redef "IO::Seekable::setpos()";
377 redef "IO::Seekable::tell()";
381 unimpl "fwrite() is C-specific--use print instead";
385 usage "getc(handle)" if @_ != 1;
390 usage "getchar()" if @_ != 0;
395 usage "gets()" if @_ != 0;
400 print STDERR "@_: " if @_;
401 print STDERR $!,"\n";
405 usage "printf(pattern, args...)" if @_ < 1;
406 CORE::printf STDOUT @_;
410 unimpl "putc() is C-specific--use print instead";
414 unimpl "putchar() is C-specific--use print instead";
418 unimpl "puts() is C-specific--use print instead";
422 usage "remove(filename)" if @_ != 1;
423 (-d $_[0]) ? CORE::rmdir($_[0]) : CORE::unlink($_[0]);
427 usage "rename(oldfilename, newfilename)" if @_ != 2;
428 CORE::rename($_[0], $_[1]);
432 usage "rewind(filehandle)" if @_ != 1;
433 CORE::seek($_[0],0,0);
437 unimpl "scanf() is C-specific--use <> and regular expressions instead";
441 usage "sprintf(pattern,args)" if @_ == 0;
442 CORE::sprintf(shift,@_);
446 unimpl "sscanf() is C-specific--use regular expressions instead";
450 redef "IO::File::new_tmpfile()";
454 redef "IO::Handle::ungetc()";
458 unimpl "vfprintf() is C-specific";
462 unimpl "vprintf() is C-specific";
466 unimpl "vsprintf() is C-specific";
470 usage "abs(x)" if @_ != 1;
475 unimpl "atexit() is C-specific: use END {} instead";
479 unimpl "atof() is C-specific, stopped";
483 unimpl "atoi() is C-specific, stopped";
487 unimpl "atol() is C-specific, stopped";
491 unimpl "bsearch() not supplied";
495 unimpl "calloc() is C-specific, stopped";
499 unimpl "div() is C-specific, use /, % and int instead";
503 usage "exit(status)" if @_ != 1;
508 unimpl "free() is C-specific, stopped";
512 usage "getenv(name)" if @_ != 1;
517 unimpl "labs() is C-specific, use abs instead";
521 unimpl "ldiv() is C-specific, use /, % and int instead";
525 unimpl "malloc() is C-specific, stopped";
529 unimpl "qsort() is C-specific, use sort instead";
533 unimpl "rand() is non-portable, use Perl's rand instead";
537 unimpl "realloc() is C-specific, stopped";
545 usage "system(command)" if @_ != 1;
550 unimpl "memchr() is C-specific, use index() instead";
554 unimpl "memcmp() is C-specific, use eq instead";
558 unimpl "memcpy() is C-specific, use = instead";
562 unimpl "memmove() is C-specific, use = instead";
566 unimpl "memset() is C-specific, use x instead";
570 unimpl "strcat() is C-specific, use .= instead";
574 unimpl "strchr() is C-specific, use index() instead";
578 unimpl "strcmp() is C-specific, use eq instead";
582 unimpl "strcpy() is C-specific, use = instead";
586 unimpl "strcspn() is C-specific, use regular expressions instead";
590 usage "strerror(errno)" if @_ != 1;
596 unimpl "strlen() is C-specific, use length instead";
600 unimpl "strncat() is C-specific, use .= instead";
604 unimpl "strncmp() is C-specific, use eq instead";
608 unimpl "strncpy() is C-specific, use = instead";
612 unimpl "strpbrk() is C-specific, stopped";
616 unimpl "strrchr() is C-specific, use rindex() instead";
620 unimpl "strspn() is C-specific, stopped";
624 usage "strstr(big, little)" if @_ != 2;
625 CORE::index($_[0], $_[1]);
629 unimpl "strtok() is C-specific, stopped";
633 usage "chmod(mode, filename)" if @_ != 2;
634 CORE::chmod($_[0], $_[1]);
638 usage "fstat(fd)" if @_ != 1;
640 CORE::open(TMP, "<&$_[0]"); # Gross.
641 my @l = CORE::stat(TMP);
647 usage "mkdir(directoryname, mode)" if @_ != 2;
648 CORE::mkdir($_[0], $_[1]);
652 usage "stat(filename)" if @_ != 1;
657 usage "umask(mask)" if @_ != 1;
662 usage "wait()" if @_ != 0;
667 usage "waitpid(pid, options)" if @_ != 2;
668 CORE::waitpid($_[0], $_[1]);
672 usage "gmtime(time)" if @_ != 1;
677 usage "localtime(time)" if @_ != 1;
678 CORE::localtime($_[0]);
682 usage "time()" if @_ != 0;
687 usage "alarm(seconds)" if @_ != 1;
692 usage "chdir(directory)" if @_ != 1;
697 usage "chown(uid, gid, filename)" if @_ != 3;
698 CORE::chown($_[0], $_[1], $_[2]);
702 unimpl "execl() is C-specific, stopped";
706 unimpl "execle() is C-specific, stopped";
710 unimpl "execlp() is C-specific, stopped";
714 unimpl "execv() is C-specific, stopped";
718 unimpl "execve() is C-specific, stopped";
722 unimpl "execvp() is C-specific, stopped";
726 usage "fork()" if @_ != 0;
731 usage "getegid()" if @_ != 0;
736 usage "geteuid()" if @_ != 0;
741 usage "getgid()" if @_ != 0;
746 usage "getgroups()" if @_ != 0;
748 grep(!$seen{$_}++, split(' ', $) ));
752 usage "getlogin()" if @_ != 0;
757 usage "getpgrp()" if @_ != 0;
762 usage "getpid()" if @_ != 0;
767 usage "getppid()" if @_ != 0;
772 usage "getuid()" if @_ != 0;
777 usage "isatty(filehandle)" if @_ != 1;
782 usage "link(oldfilename, newfilename)" if @_ != 2;
783 CORE::link($_[0], $_[1]);
787 usage "rmdir(directoryname)" if @_ != 1;
792 redef "IO::Handle::setbuf()";
796 redef "IO::Handle::setvbuf()";
800 usage "sleep(seconds)" if @_ != 1;
801 $_[0] - CORE::sleep($_[0]);
805 usage "unlink(filename)" if @_ != 1;
810 usage "utime(filename, atime, mtime)" if @_ != 3;
811 CORE::utime($_[1], $_[2], $_[0]);
817 assert_h => [qw(assert NDEBUG)],
819 ctype_h => [qw(isalnum isalpha iscntrl isdigit isgraph islower
820 isprint ispunct isspace isupper isxdigit tolower toupper)],
824 errno_h => [qw(E2BIG EACCES EADDRINUSE EADDRNOTAVAIL EAFNOSUPPORT
825 EAGAIN EALREADY EBADF EBUSY ECHILD ECONNABORTED
826 ECONNREFUSED ECONNRESET EDEADLK EDESTADDRREQ EDOM EDQUOT
827 EEXIST EFAULT EFBIG EHOSTDOWN EHOSTUNREACH EINPROGRESS
828 EINTR EINVAL EIO EISCONN EISDIR ELOOP EMFILE EMLINK
829 EMSGSIZE ENAMETOOLONG ENETDOWN ENETRESET ENETUNREACH
830 ENFILE ENOBUFS ENODEV ENOENT ENOEXEC ENOLCK ENOMEM
831 ENOPROTOOPT ENOSPC ENOSYS ENOTBLK ENOTCONN ENOTDIR
832 ENOTEMPTY ENOTSOCK ENOTTY ENXIO EOPNOTSUPP EPERM
833 EPFNOSUPPORT EPIPE EPROCLIM EPROTONOSUPPORT EPROTOTYPE
834 ERANGE EREMOTE ERESTART EROFS ESHUTDOWN ESOCKTNOSUPPORT
835 ESPIPE ESRCH ESTALE ETIMEDOUT ETOOMANYREFS ETXTBSY
836 EUSERS EWOULDBLOCK EXDEV errno)],
838 fcntl_h => [qw(FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK F_RDLCK
839 F_SETFD F_SETFL F_SETLK F_SETLKW F_UNLCK F_WRLCK
840 O_ACCMODE O_APPEND O_CREAT O_EXCL O_NOCTTY O_NONBLOCK
841 O_RDONLY O_RDWR O_TRUNC O_WRONLY
843 SEEK_CUR SEEK_END SEEK_SET
844 S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
845 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG S_ISUID
846 S_IWGRP S_IWOTH S_IWUSR)],
848 float_h => [qw(DBL_DIG DBL_EPSILON DBL_MANT_DIG
849 DBL_MAX DBL_MAX_10_EXP DBL_MAX_EXP
850 DBL_MIN DBL_MIN_10_EXP DBL_MIN_EXP
851 FLT_DIG FLT_EPSILON FLT_MANT_DIG
852 FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP
853 FLT_MIN FLT_MIN_10_EXP FLT_MIN_EXP
855 LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG
856 LDBL_MAX LDBL_MAX_10_EXP LDBL_MAX_EXP
857 LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP)],
861 limits_h => [qw( ARG_MAX CHAR_BIT CHAR_MAX CHAR_MIN CHILD_MAX
862 INT_MAX INT_MIN LINK_MAX LONG_MAX LONG_MIN MAX_CANON
863 MAX_INPUT MB_LEN_MAX NAME_MAX NGROUPS_MAX OPEN_MAX
864 PATH_MAX PIPE_BUF SCHAR_MAX SCHAR_MIN SHRT_MAX SHRT_MIN
865 SSIZE_MAX STREAM_MAX TZNAME_MAX UCHAR_MAX UINT_MAX
866 ULONG_MAX USHRT_MAX _POSIX_ARG_MAX _POSIX_CHILD_MAX
867 _POSIX_LINK_MAX _POSIX_MAX_CANON _POSIX_MAX_INPUT
868 _POSIX_NAME_MAX _POSIX_NGROUPS_MAX _POSIX_OPEN_MAX
869 _POSIX_PATH_MAX _POSIX_PIPE_BUF _POSIX_SSIZE_MAX
870 _POSIX_STREAM_MAX _POSIX_TZNAME_MAX)],
872 locale_h => [qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES
873 LC_MONETARY LC_NUMERIC LC_TIME NULL
874 localeconv setlocale)],
876 math_h => [qw(HUGE_VAL acos asin atan ceil cosh fabs floor fmod
877 frexp ldexp log10 modf pow sinh tan tanh)],
881 setjmp_h => [qw(longjmp setjmp siglongjmp sigsetjmp)],
883 signal_h => [qw(SA_NOCLDSTOP SA_NOCLDWAIT SA_NODEFER SA_ONSTACK
884 SA_RESETHAND SA_RESTART SA_SIGINFO SIGABRT SIGALRM
885 SIGCHLD SIGCONT SIGFPE SIGHUP SIGILL SIGINT SIGKILL
886 SIGPIPE %SIGRT SIGRTMIN SIGRTMAX SIGQUIT SIGSEGV SIGSTOP
887 SIGTERM SIGTSTP SIGTTIN SIGTTOU SIGUSR1 SIGUSR2
888 SIG_BLOCK SIG_DFL SIG_ERR SIG_IGN SIG_SETMASK SIG_UNBLOCK
889 raise sigaction signal sigpending sigprocmask sigsuspend)],
893 stddef_h => [qw(NULL offsetof)],
895 stdio_h => [qw(BUFSIZ EOF FILENAME_MAX L_ctermid L_cuserid
896 L_tmpname NULL SEEK_CUR SEEK_END SEEK_SET
897 STREAM_MAX TMP_MAX stderr stdin stdout
898 clearerr fclose fdopen feof ferror fflush fgetc fgetpos
899 fgets fopen fprintf fputc fputs fread freopen
900 fscanf fseek fsetpos ftell fwrite getchar gets
901 perror putc putchar puts remove rewind
902 scanf setbuf setvbuf sscanf tmpfile tmpnam
903 ungetc vfprintf vprintf vsprintf)],
905 stdlib_h => [qw(EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX NULL RAND_MAX
906 abort atexit atof atoi atol bsearch calloc div
907 free getenv labs ldiv malloc mblen mbstowcs mbtowc
908 qsort realloc strtod strtol strtoul wcstombs wctomb)],
910 string_h => [qw(NULL memchr memcmp memcpy memmove memset strcat
911 strchr strcmp strcoll strcpy strcspn strerror strlen
912 strncat strncmp strncpy strpbrk strrchr strspn strstr
915 sys_stat_h => [qw(S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
916 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG
917 S_ISUID S_IWGRP S_IWOTH S_IWUSR S_IXGRP S_IXOTH S_IXUSR
924 sys_utsname_h => [qw(uname)],
926 sys_wait_h => [qw(WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED
927 WNOHANG WSTOPSIG WTERMSIG WUNTRACED)],
929 termios_h => [qw( B0 B110 B1200 B134 B150 B1800 B19200 B200 B2400
930 B300 B38400 B4800 B50 B600 B75 B9600 BRKINT CLOCAL
931 CREAD CS5 CS6 CS7 CS8 CSIZE CSTOPB ECHO ECHOE ECHOK
932 ECHONL HUPCL ICANON ICRNL IEXTEN IGNBRK IGNCR IGNPAR
933 INLCR INPCK ISIG ISTRIP IXOFF IXON NCCS NOFLSH OPOST
934 PARENB PARMRK PARODD TCIFLUSH TCIOFF TCIOFLUSH TCION
935 TCOFLUSH TCOOFF TCOON TCSADRAIN TCSAFLUSH TCSANOW
936 TOSTOP VEOF VEOL VERASE VINTR VKILL VMIN VQUIT VSTART
938 cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain
939 tcflow tcflush tcgetattr tcsendbreak tcsetattr )],
941 time_h => [qw(CLK_TCK CLOCKS_PER_SEC NULL asctime clock ctime
942 difftime mktime strftime tzset tzname)],
944 unistd_h => [qw(F_OK NULL R_OK SEEK_CUR SEEK_END SEEK_SET
945 STDERR_FILENO STDIN_FILENO STDOUT_FILENO W_OK X_OK
946 _PC_CHOWN_RESTRICTED _PC_LINK_MAX _PC_MAX_CANON
947 _PC_MAX_INPUT _PC_NAME_MAX _PC_NO_TRUNC _PC_PATH_MAX
948 _PC_PIPE_BUF _PC_VDISABLE _POSIX_CHOWN_RESTRICTED
949 _POSIX_JOB_CONTROL _POSIX_NO_TRUNC _POSIX_SAVED_IDS
950 _POSIX_VDISABLE _POSIX_VERSION _SC_ARG_MAX
951 _SC_CHILD_MAX _SC_CLK_TCK _SC_JOB_CONTROL
952 _SC_NGROUPS_MAX _SC_OPEN_MAX _SC_PAGESIZE _SC_SAVED_IDS
953 _SC_STREAM_MAX _SC_TZNAME_MAX _SC_VERSION
954 _exit access ctermid cuserid
955 dup2 dup execl execle execlp execv execve execvp
956 fpathconf fsync getcwd getegid geteuid getgid getgroups
957 getpid getuid isatty lseek pathconf pause setgid setpgid
958 setsid setuid sysconf tcgetpgrp tcsetpgrp ttyname)],
964 # Exporter::export_tags();
966 # De-duplicate the export list:
968 @export{map {@$_} values %EXPORT_TAGS} = ();
969 # Doing the de-dup with a temporary hash has the advantage that the SVs in
970 # @EXPORT are actually shared hash key sacalars, which will save some memory.
971 push @EXPORT, keys %export;
1036 package POSIX::SigAction;
1038 sub new { bless {HANDLER => $_[1], MASK => $_[2], FLAGS => $_[3] || 0, SAFE => 0}, $_[0] }
1039 sub handler { $_[0]->{HANDLER} = $_[1] if @_ > 1; $_[0]->{HANDLER} };
1040 sub mask { $_[0]->{MASK} = $_[1] if @_ > 1; $_[0]->{MASK} };
1041 sub flags { $_[0]->{FLAGS} = $_[1] if @_ > 1; $_[0]->{FLAGS} };
1042 sub safe { $_[0]->{SAFE} = $_[1] if @_ > 1; $_[0]->{SAFE} };