5 our(@ISA, %EXPORT_TAGS, @EXPORT_OK, @EXPORT, $AUTOLOAD, %SIGRT) = ();
13 use Fcntl qw(FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK F_RDLCK F_SETFD
14 F_SETFL F_SETLK F_SETLKW F_UNLCK F_WRLCK O_ACCMODE O_APPEND
15 O_CREAT O_EXCL O_NOCTTY O_NONBLOCK O_RDONLY O_RDWR O_TRUNC
18 # Grandfather old foo_h form to new :foo_h form
22 load_imports() unless $loaded++;
24 my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_;
25 local $Exporter::ExportLevel = 1;
26 Exporter::import($this,@list);
29 sub croak { require Carp; goto &Carp::croak }
30 # declare usage to assist AutoLoad
33 XSLoader::load 'POSIX', $VERSION;
35 my %NON_CONSTS = (map {($_,1)}
36 qw(S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISREG WEXITSTATUS
37 WIFEXITED WIFSIGNALED WIFSTOPPED WSTOPSIG WTERMSIG));
41 no warnings 'uninitialized';
42 if ($AUTOLOAD =~ /::(_?[a-z])/) {
44 $AutoLoader::AUTOLOAD = $AUTOLOAD;
45 goto &AutoLoader::AUTOLOAD
48 my $constname = $AUTOLOAD;
49 $constname =~ s/.*:://;
50 if ($NON_CONSTS{$constname}) {
51 my ($val, $error) = &int_macro_int($constname, $_[0]);
52 croak $error if $error;
53 *$AUTOLOAD = sub { &int_macro_int($constname, $_[0]) };
55 my ($error, $val) = constant($constname);
56 croak $error if $error;
57 *$AUTOLOAD = sub { $val };
63 package POSIX::SigAction;
65 use AutoLoader 'AUTOLOAD';
69 use AutoLoader 'AUTOLOAD';
73 use vars qw($SIGACTION_FLAGS $_SIGRTMIN $_SIGRTMAX $_sigrtn @ISA);
74 @POSIX::SigRt::ISA = qw(Tie::StdHash);
78 tie %POSIX::SIGRT, 'POSIX::SigRt';
89 croak "Usage: POSIX::$mess";
94 croak "Use method $mess instead";
100 croak "Unimplemented: POSIX::$mess";
104 usage "assert(expr)" if @_ != 1;
106 croak "Assertion failed";
111 usage "tolower(string)" if @_ != 1;
116 usage "toupper(string)" if @_ != 1;
121 usage "closedir(dirhandle)" if @_ != 1;
122 CORE::closedir($_[0]);
126 usage "opendir(directory)" if @_ != 1;
128 CORE::opendir($dirhandle, $_[0])
134 usage "readdir(dirhandle)" if @_ != 1;
135 CORE::readdir($_[0]);
139 usage "rewinddir(dirhandle)" if @_ != 1;
140 CORE::rewinddir($_[0]);
144 usage "errno()" if @_ != 0;
149 usage "creat(filename, mode)" if @_ != 2;
150 &open($_[0], &O_WRONLY | &O_CREAT | &O_TRUNC, $_[1]);
154 usage "fcntl(filehandle, cmd, arg)" if @_ != 3;
155 CORE::fcntl($_[0], $_[1], $_[2]);
159 usage "getgrgid(gid)" if @_ != 1;
160 CORE::getgrgid($_[0]);
164 usage "getgrnam(name)" if @_ != 1;
165 CORE::getgrnam($_[0]);
169 usage "atan2(x,y)" if @_ != 2;
170 CORE::atan2($_[0], $_[1]);
174 usage "cos(x)" if @_ != 1;
179 usage "exp(x)" if @_ != 1;
184 usage "fabs(x)" if @_ != 1;
189 usage "log(x)" if @_ != 1;
194 usage "pow(x,exponent)" if @_ != 2;
199 usage "sin(x)" if @_ != 1;
204 usage "sqrt(x)" if @_ != 1;
209 usage "getpwnam(name)" if @_ != 1;
210 CORE::getpwnam($_[0]);
214 usage "getpwuid(uid)" if @_ != 1;
215 CORE::getpwuid($_[0]);
219 unimpl "longjmp() is C-specific: use die instead";
223 unimpl "setjmp() is C-specific: use eval {} instead";
227 unimpl "siglongjmp() is C-specific: use die instead";
231 unimpl "sigsetjmp() is C-specific: use eval {} instead";
235 usage "kill(pid, sig)" if @_ != 2;
236 CORE::kill $_[1], $_[0];
240 usage "raise(sig)" if @_ != 1;
241 CORE::kill $_[0], $$; # Is this good enough?
245 unimpl "offsetof() is C-specific, stopped";
249 redef "IO::Handle::clearerr()";
253 redef "IO::Handle::close()";
257 redef "IO::Handle::new_from_fd()";
261 redef "IO::Handle::eof()";
265 redef "IO::Handle::getc()";
269 redef "IO::Handle::gets()";
273 redef "IO::Handle::fileno()";
277 redef "IO::File::open()";
281 unimpl "fprintf() is C-specific--use printf instead";
285 unimpl "fputc() is C-specific--use print instead";
289 unimpl "fputs() is C-specific--use print instead";
293 unimpl "fread() is C-specific--use read instead";
297 unimpl "freopen() is C-specific--use open instead";
301 unimpl "fscanf() is C-specific--use <> and regular expressions instead";
305 redef "IO::Seekable::seek()";
309 redef "IO::Handle::sync()";
313 redef "IO::Handle::error()";
317 redef "IO::Handle::flush()";
321 redef "IO::Seekable::getpos()";
325 redef "IO::Seekable::setpos()";
329 redef "IO::Seekable::tell()";
333 unimpl "fwrite() is C-specific--use print instead";
337 usage "getc(handle)" if @_ != 1;
342 usage "getchar()" if @_ != 0;
347 usage "gets()" if @_ != 0;
352 print STDERR "@_: " if @_;
353 print STDERR $!,"\n";
357 usage "printf(pattern, args...)" if @_ < 1;
358 CORE::printf STDOUT @_;
362 unimpl "putc() is C-specific--use print instead";
366 unimpl "putchar() is C-specific--use print instead";
370 unimpl "puts() is C-specific--use print instead";
374 usage "remove(filename)" if @_ != 1;
375 (-d $_[0]) ? CORE::rmdir($_[0]) : CORE::unlink($_[0]);
379 usage "rename(oldfilename, newfilename)" if @_ != 2;
380 CORE::rename($_[0], $_[1]);
384 usage "rewind(filehandle)" if @_ != 1;
385 CORE::seek($_[0],0,0);
389 unimpl "scanf() is C-specific--use <> and regular expressions instead";
393 usage "sprintf(pattern,args)" if @_ == 0;
394 CORE::sprintf(shift,@_);
398 unimpl "sscanf() is C-specific--use regular expressions instead";
402 redef "IO::File::new_tmpfile()";
406 redef "IO::Handle::ungetc()";
410 unimpl "vfprintf() is C-specific";
414 unimpl "vprintf() is C-specific";
418 unimpl "vsprintf() is C-specific";
422 usage "abs(x)" if @_ != 1;
427 unimpl "atexit() is C-specific: use END {} instead";
431 unimpl "atof() is C-specific, stopped";
435 unimpl "atoi() is C-specific, stopped";
439 unimpl "atol() is C-specific, stopped";
443 unimpl "bsearch() not supplied";
447 unimpl "calloc() is C-specific, stopped";
451 unimpl "div() is C-specific, use /, % and int instead";
455 usage "exit(status)" if @_ != 1;
460 unimpl "free() is C-specific, stopped";
464 usage "getenv(name)" if @_ != 1;
469 unimpl "labs() is C-specific, use abs instead";
473 unimpl "ldiv() is C-specific, use /, % and int instead";
477 unimpl "malloc() is C-specific, stopped";
481 unimpl "qsort() is C-specific, use sort instead";
485 unimpl "rand() is non-portable, use Perl's rand instead";
489 unimpl "realloc() is C-specific, stopped";
497 usage "system(command)" if @_ != 1;
502 unimpl "memchr() is C-specific, use index() instead";
506 unimpl "memcmp() is C-specific, use eq instead";
510 unimpl "memcpy() is C-specific, use = instead";
514 unimpl "memmove() is C-specific, use = instead";
518 unimpl "memset() is C-specific, use x instead";
522 unimpl "strcat() is C-specific, use .= instead";
526 unimpl "strchr() is C-specific, use index() instead";
530 unimpl "strcmp() is C-specific, use eq instead";
534 unimpl "strcpy() is C-specific, use = instead";
538 unimpl "strcspn() is C-specific, use regular expressions instead";
542 usage "strerror(errno)" if @_ != 1;
548 unimpl "strlen() is C-specific, use length instead";
552 unimpl "strncat() is C-specific, use .= instead";
556 unimpl "strncmp() is C-specific, use eq instead";
560 unimpl "strncpy() is C-specific, use = instead";
564 unimpl "strpbrk() is C-specific, stopped";
568 unimpl "strrchr() is C-specific, use rindex() instead";
572 unimpl "strspn() is C-specific, stopped";
576 usage "strstr(big, little)" if @_ != 2;
577 CORE::index($_[0], $_[1]);
581 unimpl "strtok() is C-specific, stopped";
585 usage "chmod(mode, filename)" if @_ != 2;
586 CORE::chmod($_[0], $_[1]);
590 usage "fstat(fd)" if @_ != 1;
592 CORE::open(TMP, "<&$_[0]"); # Gross.
593 my @l = CORE::stat(TMP);
599 usage "mkdir(directoryname, mode)" if @_ != 2;
600 CORE::mkdir($_[0], $_[1]);
604 usage "stat(filename)" if @_ != 1;
609 usage "umask(mask)" if @_ != 1;
614 usage "wait()" if @_ != 0;
619 usage "waitpid(pid, options)" if @_ != 2;
620 CORE::waitpid($_[0], $_[1]);
624 usage "gmtime(time)" if @_ != 1;
629 usage "localtime(time)" if @_ != 1;
630 CORE::localtime($_[0]);
634 usage "time()" if @_ != 0;
639 usage "alarm(seconds)" if @_ != 1;
644 usage "chdir(directory)" if @_ != 1;
649 usage "chown(uid, gid, filename)" if @_ != 3;
650 CORE::chown($_[0], $_[1], $_[2]);
654 unimpl "execl() is C-specific, stopped";
658 unimpl "execle() is C-specific, stopped";
662 unimpl "execlp() is C-specific, stopped";
666 unimpl "execv() is C-specific, stopped";
670 unimpl "execve() is C-specific, stopped";
674 unimpl "execvp() is C-specific, stopped";
678 usage "fork()" if @_ != 0;
683 usage "getegid()" if @_ != 0;
688 usage "geteuid()" if @_ != 0;
693 usage "getgid()" if @_ != 0;
698 usage "getgroups()" if @_ != 0;
700 grep(!$seen{$_}++, split(' ', $) ));
704 usage "getlogin()" if @_ != 0;
709 usage "getpgrp()" if @_ != 0;
714 usage "getpid()" if @_ != 0;
719 usage "getppid()" if @_ != 0;
724 usage "getuid()" if @_ != 0;
729 usage "isatty(filehandle)" if @_ != 1;
734 usage "link(oldfilename, newfilename)" if @_ != 2;
735 CORE::link($_[0], $_[1]);
739 usage "rmdir(directoryname)" if @_ != 1;
744 redef "IO::Handle::setbuf()";
748 redef "IO::Handle::setvbuf()";
752 usage "sleep(seconds)" if @_ != 1;
753 $_[0] - CORE::sleep($_[0]);
757 usage "unlink(filename)" if @_ != 1;
762 usage "utime(filename, atime, mtime)" if @_ != 3;
763 CORE::utime($_[1], $_[2], $_[0]);
769 assert_h => [qw(assert NDEBUG)],
771 ctype_h => [qw(isalnum isalpha iscntrl isdigit isgraph islower
772 isprint ispunct isspace isupper isxdigit tolower toupper)],
776 errno_h => [qw(E2BIG EACCES EADDRINUSE EADDRNOTAVAIL EAFNOSUPPORT
777 EAGAIN EALREADY EBADF EBUSY ECHILD ECONNABORTED
778 ECONNREFUSED ECONNRESET EDEADLK EDESTADDRREQ EDOM EDQUOT
779 EEXIST EFAULT EFBIG EHOSTDOWN EHOSTUNREACH EINPROGRESS
780 EINTR EINVAL EIO EISCONN EISDIR ELOOP EMFILE EMLINK
781 EMSGSIZE ENAMETOOLONG ENETDOWN ENETRESET ENETUNREACH
782 ENFILE ENOBUFS ENODEV ENOENT ENOEXEC ENOLCK ENOMEM
783 ENOPROTOOPT ENOSPC ENOSYS ENOTBLK ENOTCONN ENOTDIR
784 ENOTEMPTY ENOTSOCK ENOTTY ENXIO EOPNOTSUPP EPERM
785 EPFNOSUPPORT EPIPE EPROCLIM EPROTONOSUPPORT EPROTOTYPE
786 ERANGE EREMOTE ERESTART EROFS ESHUTDOWN ESOCKTNOSUPPORT
787 ESPIPE ESRCH ESTALE ETIMEDOUT ETOOMANYREFS ETXTBSY
788 EUSERS EWOULDBLOCK EXDEV errno)],
790 fcntl_h => [qw(FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK F_RDLCK
791 F_SETFD F_SETFL F_SETLK F_SETLKW F_UNLCK F_WRLCK
792 O_ACCMODE O_APPEND O_CREAT O_EXCL O_NOCTTY O_NONBLOCK
793 O_RDONLY O_RDWR O_TRUNC O_WRONLY
795 SEEK_CUR SEEK_END SEEK_SET
796 S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
797 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG S_ISUID
798 S_IWGRP S_IWOTH S_IWUSR)],
800 float_h => [qw(DBL_DIG DBL_EPSILON DBL_MANT_DIG
801 DBL_MAX DBL_MAX_10_EXP DBL_MAX_EXP
802 DBL_MIN DBL_MIN_10_EXP DBL_MIN_EXP
803 FLT_DIG FLT_EPSILON FLT_MANT_DIG
804 FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP
805 FLT_MIN FLT_MIN_10_EXP FLT_MIN_EXP
807 LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG
808 LDBL_MAX LDBL_MAX_10_EXP LDBL_MAX_EXP
809 LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP)],
813 limits_h => [qw( ARG_MAX CHAR_BIT CHAR_MAX CHAR_MIN CHILD_MAX
814 INT_MAX INT_MIN LINK_MAX LONG_MAX LONG_MIN MAX_CANON
815 MAX_INPUT MB_LEN_MAX NAME_MAX NGROUPS_MAX OPEN_MAX
816 PATH_MAX PIPE_BUF SCHAR_MAX SCHAR_MIN SHRT_MAX SHRT_MIN
817 SSIZE_MAX STREAM_MAX TZNAME_MAX UCHAR_MAX UINT_MAX
818 ULONG_MAX USHRT_MAX _POSIX_ARG_MAX _POSIX_CHILD_MAX
819 _POSIX_LINK_MAX _POSIX_MAX_CANON _POSIX_MAX_INPUT
820 _POSIX_NAME_MAX _POSIX_NGROUPS_MAX _POSIX_OPEN_MAX
821 _POSIX_PATH_MAX _POSIX_PIPE_BUF _POSIX_SSIZE_MAX
822 _POSIX_STREAM_MAX _POSIX_TZNAME_MAX)],
824 locale_h => [qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES
825 LC_MONETARY LC_NUMERIC LC_TIME NULL
826 localeconv setlocale)],
828 math_h => [qw(HUGE_VAL acos asin atan ceil cosh fabs floor fmod
829 frexp ldexp log10 modf pow sinh tan tanh)],
833 setjmp_h => [qw(longjmp setjmp siglongjmp sigsetjmp)],
835 signal_h => [qw(SA_NOCLDSTOP SA_NOCLDWAIT SA_NODEFER SA_ONSTACK
836 SA_RESETHAND SA_RESTART SA_SIGINFO SIGABRT SIGALRM
837 SIGCHLD SIGCONT SIGFPE SIGHUP SIGILL SIGINT SIGKILL
838 SIGPIPE %SIGRT SIGRTMIN SIGRTMAX SIGQUIT SIGSEGV SIGSTOP
839 SIGTERM SIGTSTP SIGTTIN SIGTTOU SIGUSR1 SIGUSR2
840 SIG_BLOCK SIG_DFL SIG_ERR SIG_IGN SIG_SETMASK SIG_UNBLOCK
841 raise sigaction signal sigpending sigprocmask sigsuspend)],
845 stddef_h => [qw(NULL offsetof)],
847 stdio_h => [qw(BUFSIZ EOF FILENAME_MAX L_ctermid L_cuserid
848 L_tmpname NULL SEEK_CUR SEEK_END SEEK_SET
849 STREAM_MAX TMP_MAX stderr stdin stdout
850 clearerr fclose fdopen feof ferror fflush fgetc fgetpos
851 fgets fopen fprintf fputc fputs fread freopen
852 fscanf fseek fsetpos ftell fwrite getchar gets
853 perror putc putchar puts remove rewind
854 scanf setbuf setvbuf sscanf tmpfile tmpnam
855 ungetc vfprintf vprintf vsprintf)],
857 stdlib_h => [qw(EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX NULL RAND_MAX
858 abort atexit atof atoi atol bsearch calloc div
859 free getenv labs ldiv malloc mblen mbstowcs mbtowc
860 qsort realloc strtod strtol strtoul wcstombs wctomb)],
862 string_h => [qw(NULL memchr memcmp memcpy memmove memset strcat
863 strchr strcmp strcoll strcpy strcspn strerror strlen
864 strncat strncmp strncpy strpbrk strrchr strspn strstr
867 sys_stat_h => [qw(S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
868 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG
869 S_ISUID S_IWGRP S_IWOTH S_IWUSR S_IXGRP S_IXOTH S_IXUSR
876 sys_utsname_h => [qw(uname)],
878 sys_wait_h => [qw(WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED
879 WNOHANG WSTOPSIG WTERMSIG WUNTRACED)],
881 termios_h => [qw( B0 B110 B1200 B134 B150 B1800 B19200 B200 B2400
882 B300 B38400 B4800 B50 B600 B75 B9600 BRKINT CLOCAL
883 CREAD CS5 CS6 CS7 CS8 CSIZE CSTOPB ECHO ECHOE ECHOK
884 ECHONL HUPCL ICANON ICRNL IEXTEN IGNBRK IGNCR IGNPAR
885 INLCR INPCK ISIG ISTRIP IXOFF IXON NCCS NOFLSH OPOST
886 PARENB PARMRK PARODD TCIFLUSH TCIOFF TCIOFLUSH TCION
887 TCOFLUSH TCOOFF TCOON TCSADRAIN TCSAFLUSH TCSANOW
888 TOSTOP VEOF VEOL VERASE VINTR VKILL VMIN VQUIT VSTART
890 cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain
891 tcflow tcflush tcgetattr tcsendbreak tcsetattr )],
893 time_h => [qw(CLK_TCK CLOCKS_PER_SEC NULL asctime clock ctime
894 difftime mktime strftime tzset tzname)],
896 unistd_h => [qw(F_OK NULL R_OK SEEK_CUR SEEK_END SEEK_SET
897 STDERR_FILENO STDIN_FILENO STDOUT_FILENO W_OK X_OK
898 _PC_CHOWN_RESTRICTED _PC_LINK_MAX _PC_MAX_CANON
899 _PC_MAX_INPUT _PC_NAME_MAX _PC_NO_TRUNC _PC_PATH_MAX
900 _PC_PIPE_BUF _PC_VDISABLE _POSIX_CHOWN_RESTRICTED
901 _POSIX_JOB_CONTROL _POSIX_NO_TRUNC _POSIX_SAVED_IDS
902 _POSIX_VDISABLE _POSIX_VERSION _SC_ARG_MAX
903 _SC_CHILD_MAX _SC_CLK_TCK _SC_JOB_CONTROL
904 _SC_NGROUPS_MAX _SC_OPEN_MAX _SC_PAGESIZE _SC_SAVED_IDS
905 _SC_STREAM_MAX _SC_TZNAME_MAX _SC_VERSION
906 _exit access ctermid cuserid
907 dup2 dup execl execle execlp execv execve execvp
908 fpathconf fsync getcwd getegid geteuid getgid getgroups
909 getpid getuid isatty lseek pathconf pause setgid setpgid
910 setsid setuid sysconf tcgetpgrp tcsetpgrp ttyname)],
916 # Exporter::export_tags();
918 # De-duplicate the export list:
920 @export{map {@$_} values %EXPORT_TAGS} = ();
921 # Doing the de-dup with a temporary hash has the advantage that the SVs in
922 # @EXPORT are actually shared hash key sacalars, which will save some memory.
923 push @EXPORT, keys %export;
988 package POSIX::SigAction;
990 sub new { bless {HANDLER => $_[1], MASK => $_[2], FLAGS => $_[3] || 0, SAFE => 0}, $_[0] }
991 sub handler { $_[0]->{HANDLER} = $_[1] if @_ > 1; $_[0]->{HANDLER} };
992 sub mask { $_[0]->{MASK} = $_[1] if @_ > 1; $_[0]->{MASK} };
993 sub flags { $_[0]->{FLAGS} = $_[1] if @_ > 1; $_[0]->{FLAGS} };
994 sub safe { $_[0]->{SAFE} = $_[1] if @_ > 1; $_[0]->{SAFE} };
996 package POSIX::SigRt;
1000 $_SIGRTMIN = &POSIX::SIGRTMIN;
1001 $_SIGRTMAX = &POSIX::SIGRTMAX;
1002 $_sigrtn = $_SIGRTMAX - $_SIGRTMIN;
1006 &_init unless defined $_sigrtn;
1007 die "POSIX::SigRt not available" unless defined $_sigrtn && $_sigrtn > 0;
1013 # Allow (SIGRT)?MIN( + n)?, a common idiom when doing these things in C.
1014 $rtsig = $_SIGRTMIN + ($1 || 0)
1015 if $rtsig =~ /^(?:(?:SIG)?RT)?MIN(\s*\+\s*(\d+))?$/;
1020 my $rtsig = _getsig($_[1]);
1021 my $ok = $rtsig >= $_SIGRTMIN && $rtsig <= $_SIGRTMAX;
1026 my ($rtsig, $ok) = &_exist;
1027 die "No POSIX::SigRt signal $_[1] (valid range SIGRTMIN..SIGRTMAX, or $_SIGRTMIN..$_SIGRTMAX)"
1033 my ($rtsig, $handler, $flags) = @_;
1034 my $sigset = POSIX::SigSet->new($rtsig);
1035 my $sigact = POSIX::SigAction->new($handler,
1038 POSIX::sigaction($rtsig, $sigact);
1041 sub EXISTS { &_exist }
1042 sub FETCH { my $rtsig = &_check;
1043 my $oa = POSIX::SigAction->new();
1044 POSIX::sigaction($rtsig, undef, $oa);
1045 return $oa->{HANDLER} }
1046 sub STORE { my $rtsig = &_check; new($rtsig, $_[2], $SIGACTION_FLAGS) }
1047 sub DELETE { delete $SIG{ &_check } }
1048 sub CLEAR { &_exist; delete @SIG{ &POSIX::SIGRTMIN .. &POSIX::SIGRTMAX } }
1049 sub SCALAR { &_croak; $_sigrtn + 1 }