5 POSIX - Perl interface to IEEE 1003.1 namespace
14 The POSIX module permits you to access all (or nearly all) the standard
15 POSIX 1003.1 identifiers. Things which are C<#defines> in C, like EINTR
16 or O_NDELAY, are automatically exported into your namespace. All
17 functions are only exported if you ask for them explicitly. Most likely
18 people will prefer to use the fully-qualified function names.
20 To get a list of all the possible identifiers available to you--and
21 their semantics--you should pick up a 1003.1 spec, or look in the
26 printf "EINTR is %d\n", EINTR;
30 $fd = POSIX::open($path, O_CREAT|O_EXCL|O_WRONLY, 0644);
31 # note: that's a filedescriptor, *NOT* a filehandle
35 The POSIX module is probably the most complex Perl module supplied with
36 the standard distribution. It incorporates autoloading, namespace games,
37 and dynamic loading of code that's in Perl, C, or both. It's a great
42 A few functions are not implemented because they are C specific. If you
43 attempt to call these, they will print a message telling you that they
44 aren't implemented, and suggest using the Perl equivalent should one
45 exist. For example, trying to access the setjmp() call will elicit the
46 message "setjmp() is C-specific: use eval {} instead".
48 Furthermore, some evil vendors will claim 1003.1 compliance, but in fact
49 are not so: they will not pass the PCTS (POSIX Compliance Test Suites).
50 For example, one vendor may not define EDEADLK, or the semantics of the
51 errno values set by open(2) might not be quite right. Perl does not
52 attempt to verify POSIX compliance. That means you can currently
53 successfully say "use POSIX", and then later in your program you find
54 that your vendor has been lax and there's no usable ICANON macro after
55 all. This could be construed to be a bug.
64 @ISA = qw(Exporter DynaLoader);
68 assert_h => [qw(assert NDEBUG)],
70 ctype_h => [qw(isalnum isalpha iscntrl isdigit isgraph islower
71 isprint ispunct isspace isupper isxdigit tolower toupper)],
75 errno_h => [qw(E2BIG EACCES EAGAIN EBADF EBUSY ECHILD EDEADLK EDOM
76 EEXIST EFAULT EFBIG EINTR EINVAL EIO EISDIR EMFILE
77 EMLINK ENAMETOOLONG ENFILE ENODEV ENOENT ENOEXEC ENOLCK
78 ENOMEM ENOSPC ENOSYS ENOTDIR ENOTEMPTY ENOTTY ENXIO
79 EPERM EPIPE ERANGE EROFS ESPIPE ESRCH EXDEV errno)],
81 fcntl_h => [qw(FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK F_RDLCK
82 F_SETFD F_SETFL F_SETLK F_SETLKW F_UNLCK F_WRLCK
83 O_ACCMODE O_APPEND O_CREAT O_EXCL O_NOCTTY O_NONBLOCK
84 O_RDONLY O_RDWR O_TRUNC O_WRONLY
86 SEEK_CUR SEEK_END SEEK_SET
87 S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
88 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG S_ISUID
89 S_IWGRP S_IWOTH S_IWUSR)],
91 float_h => [qw(DBL_DIG DBL_EPSILON DBL_MANT_DIG
92 DBL_MAX DBL_MAX_10_EXP DBL_MAX_EXP
93 DBL_MIN DBL_MIN_10_EXP DBL_MIN_EXP
94 FLT_DIG FLT_EPSILON FLT_MANT_DIG
95 FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP
96 FLT_MIN FLT_MIN_10_EXP FLT_MIN_EXP
98 LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG
99 LDBL_MAX LDBL_MAX_10_EXP LDBL_MAX_EXP
100 LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP)],
104 limits_h => [qw( ARG_MAX CHAR_BIT CHAR_MAX CHAR_MIN CHILD_MAX
105 INT_MAX INT_MIN LINK_MAX LONG_MAX LONG_MIN MAX_CANON
106 MAX_INPUT MB_LEN_MAX NAME_MAX NGROUPS_MAX OPEN_MAX
107 PATH_MAX PIPE_BUF SCHAR_MAX SCHAR_MIN SHRT_MAX SHRT_MIN
108 SSIZE_MAX STREAM_MAX TZNAME_MAX UCHAR_MAX UINT_MAX
109 ULONG_MAX USHRT_MAX _POSIX_ARG_MAX _POSIX_CHILD_MAX
110 _POSIX_LINK_MAX _POSIX_MAX_CANON _POSIX_MAX_INPUT
111 _POSIX_NAME_MAX _POSIX_NGROUPS_MAX _POSIX_OPEN_MAX
112 _POSIX_PATH_MAX _POSIX_PIPE_BUF _POSIX_SSIZE_MAX
113 _POSIX_STREADM_MAX _POSIX_TZNAME_MAX)],
115 locale_h => [qw(LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC
116 LC_TIME NULL localeconv setlocale)],
118 math_h => [qw(HUGE_VAL acos asin atan ceil cosh fabs floor fmod
119 frexp ldexp log10 modf pow sinh tan tanh)],
123 setjmp_h => [qw(longjmp setjmp siglongjmp sigsetjmp)],
125 signal_h => [qw(SA_NOCLDSTOP SIGABRT SIGALRM SIGCHLD SIGCONT SIGFPE
126 SIGHUP SIGILL SIGINT SIGKILL SIGPIPE SIGQUIT SIGSEGV
127 SIGSTOP SIGTERM SIGTSTP SIGTTIN SIGTTOU SIGUSR1 SIGUSR2
128 SIG_BLOCK SIG_DFL SIG_ERR SIG_IGN SIG_SETMASK SIG_UNBLOCK
129 raise sigaction signal sigpending sigprocmask
134 stddef_h => [qw(NULL offsetof)],
136 stdio_h => [qw(BUFSIZ EOF FILENAME_MAX L_ctermid L_cuserid
137 L_tmpname NULL SEEK_CUR SEEK_END SEEK_SET STREAM_MAX
138 TMP_MAX stderr stdin stdout _IOFBF _IOLBF _IONBF
139 clearerr fclose fdopen feof ferror fflush fgetc fgetpos
140 fgets fopen fprintf fputc fputs fread freopen
141 fscanf fseek fsetpos ftell fwrite getchar gets
142 perror putc putchar puts remove rewind
143 scanf setbuf setvbuf sscanf tmpfile tmpnam
144 ungetc vfprintf vprintf vsprintf)],
146 stdlib_h => [qw(EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX NULL RAND_MAX
147 abort atexit atof atoi atol bsearch calloc div
148 free getenv labs ldiv malloc mblen mbstowcs mbtowc
149 qsort realloc strtod strtol stroul wcstombs wctomb)],
151 string_h => [qw(NULL memchr memcmp memcpy memmove memset strcat
152 strchr strcmp strcoll strcpy strcspn strerror strlen
153 strncat strncmp strncpy strpbrk strrchr strspn strstr
156 sys_stat_h => [qw(S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU
157 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISREG
158 S_ISUID S_IWGRP S_IWOTH S_IWUSR S_IXGRP S_IXOTH S_IXUSR
161 sys_times_h => [qw()],
163 sys_types_h => [qw()],
165 sys_utsname_h => [qw(uname)],
167 sys_wait_h => [qw(WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED
168 WNOHANG WSTOPSIG WTERMSIG WUNTRACED)],
170 termios_h => [qw( B0 B110 B1200 B134 B150 B1800 B19200 B200 B2400
171 B300 B38400 B4800 B50 B600 B75 B9600 BRKINT CLOCAL
172 CREAD CS5 CS6 CS7 CS8 CSIZE CSTOPB ECHO ECHOE ECHOK
173 ECHONL HUPCL ICANON ICRNL IEXTEN IGNBRK IGNCR IGNPAR
174 INLCR INPCK ISIG ISTRIP IXOFF IXON NCCS NOFLSH OPOST
175 PARENB PARMRK PARODD TCIFLUSH TCIOFF TCIOFLUSH TCION
176 TCOFLUSH TCOOFF TCOON TCSADRAIN TCSAFLUSH TCSANOW
177 TOSTOP VEOF VEOL VERASE VINTR VKILL VMIN VQUIT VSTART
179 cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain
180 tcflow tcflush tcgetattr tcsendbreak tcsetattr )],
182 time_h => [qw(CLK_TCK CLOCKS_PER_SEC NULL asctime clock ctime
183 difftime mktime strftime tzset tzname)],
185 unistd_h => [qw(F_OK NULL R_OK SEEK_CUR SEEK_END SEEK_SET
186 STRERR_FILENO STDIN_FILENO STDOUT_FILENO W_OK X_OK
187 _PC_CHOWN_RESTRICTED _PC_LINK_MAX _PC_MAX_CANON
188 _PC_MAX_INPUT _PC_NAME_MAX _PC_NO_TRUNC _PC_PATH_MAX
189 _PC_PIPE_BUF _PC_VDISABLE _POSIX_CHOWN_RESTRICTED
190 _POSIX_JOB_CONTROL _POSIX_NO_TRUNC _POSIX_SAVED_IDS
191 _POSIX_VDISABLE _POSIX_VERSION _SC_ARG_MAX
192 _SC_CHILD_MAX _SC_CLK_TCK _SC_JOB_CONTROL
193 _SC_NGROUPS_MAX _SC_OPEN_MAX _SC_SAVED_IDS
194 _SC_STREAM_MAX _SC_TZNAME_MAX _SC_VERSION
195 _exit access ctermid cuserid
196 dup2 dup execl execle execlp execv execve execvp
197 fpathconf getcwd getegid geteuid getgid getgroups
198 getpid getuid isatty lseek pathconf pause setgid setpgid
199 setsid setuid sysconf tcgetpgrp tcsetpgrp ttyname)],
205 Exporter::export_tags();
208 closedir opendir readdir rewinddir
211 atan2 cos exp log sin sqrt
214 fileno getc printf rename sprintf
215 abs exit rand srand system
216 chmod mkdir stat umask
219 gmtime localtime time
220 alarm chdir chown close fork getlogin getppid getpgrp link
221 pipe read rmdir sleep unlink write
225 # Grandfather old foo_h form to new :foo_h form
228 my @list = map { m/^\w+_h$/ ? ":$_" : $_ } @_;
229 local $Exporter::ExportLevel = 1;
230 Exporter::import($this,@list);
234 if ($AUTOLOAD =~ /::(_?[a-z])/) {
235 $AutoLoader::AUTOLOAD = $AUTOLOAD;
236 goto &AutoLoader::AUTOLOAD
238 local $constname = $AUTOLOAD;
239 $constname =~ s/.*:://;
240 $val = constant($constname, $_[0]);
242 if ($! =~ /Invalid/) {
243 croak "$constname is not a valid POSIX macro";
246 croak "Your vendor has not defined POSIX macro $constname, used";
249 eval "sub $AUTOLOAD { $val }";
255 @liblist = split ' ', $Config::Config{"POSIX_loadlibs"}
256 if defined $Config::Config{"POSIX_loadlibs"};
257 bootstrap POSIX @liblist;
261 croak "Usage: POSIX::$mess";
266 croak "Use method $mess instead";
272 croak "Unimplemented: POSIX::$mess";
278 *{"POSIX::" . $gensym++};
287 ############################
288 package POSIX::SigAction;
291 bless {HANDLER => $_[1], MASK => $_[2], FLAGS => $_[3]};
294 ############################
298 POSIX::usage "FileHandle->new(filename, posixmode)" if @_ != 3;
299 local($class,$filename,$mode) = @_;
300 local($glob) = &POSIX::gensym;
301 $mode =~ s/a.*/>>/ ||
304 open($glob, "$mode $filename") and
309 POSIX::usage "FileHandle->new_from_fd(fd,mode)" if @_ != 3;
310 local($class,$fd,$mode) = @_;
311 local($glob) = &POSIX::gensym;
312 $mode =~ s/a.*/>>/ ||
315 open($glob, "$mode&=$fd") and
320 POSIX::usage "clearerr(filehandle)" if @_ != 1;
325 POSIX::usage "close(filehandle)" if @_ != 1;
335 POSIX::usage "eof(filehandle)" if @_ != 1;
340 POSIX::usage "getc(filehandle)" if @_ != 1;
345 POSIX::usage "gets(filehandle)" if @_ != 1;
351 POSIX::usage "fileno(filehandle)" if @_ != 1;
356 POSIX::usage "seek(filehandle,pos,whence)" if @_ != 3;
357 seek($_[0], $_[1], $_[2]);
361 POSIX::usage "tell(filehandle)" if @_ != 1;
364 ############################
365 package POSIX; # return to package POSIX so AutoSplit is happy
370 usage "assert(expr)" if @_ != 1;
372 croak "Assertion failed";
377 usage "tolower(string)" if @_ != 1;
382 usage "toupper(string)" if @_ != 1;
387 usage "closedir(dirhandle)" if @_ != 1;
393 usage "opendir(directory)" if @_ != 1;
394 local($dirhandle) = &gensym;
395 opendir($dirhandle, $_[0])
397 : (ungensym($dirhandle), undef);
401 usage "readdir(dirhandle)" if @_ != 1;
406 usage "rewinddir(dirhandle)" if @_ != 1;
411 usage "errno()" if @_ != 0;
416 usage "creat(filename, mode)" if @_ != 2;
417 &open($_[0], &O_WRONLY | &O_CREAT | &O_TRUNC, $_[1]);
421 usage "fcntl(filehandle, cmd, arg)" if @_ != 3;
422 fcntl($_[0], $_[1], $_[2]);
426 usage "getgrgid(gid)" if @_ != 1;
431 usage "getgrnam(name)" if @_ != 1;
436 usage "atan2(x,y)" if @_ != 2;
441 usage "cos(x)" if @_ != 1;
446 usage "exp(x)" if @_ != 1;
451 usage "fabs(x)" if @_ != 1;
456 usage "log(x)" if @_ != 1;
461 usage "pow(x,exponent)" if @_ != 2;
466 usage "sin(x)" if @_ != 1;
471 usage "sqrt(x)" if @_ != 1;
476 usage "getpwnam(name)" if @_ != 1;
481 usage "getpwuid(uid)" if @_ != 1;
486 unimpl "longjmp() is C-specific: use die instead";
490 unimpl "setjmp() is C-specific: use eval {} instead";
494 unimpl "siglongjmp() is C-specific: use die instead";
498 unimpl "sigsetjmp() is C-specific: use eval {} instead";
502 usage "kill(pid, sig)" if @_ != 2;
507 usage "raise(sig)" if @_ != 1;
508 kill $$, $_[0]; # Is this good enough?
512 unimpl "offsetof() is C-specific, stopped";
516 redef "$filehandle->clearerr(filehandle)";
520 redef "$filehandle->fclose(filehandle)";
524 redef "FileHandle->new_from_fd(fd,mode)";
528 redef "$filehandle->eof()";
532 redef "$filehandle->getc()";
536 redef "$filehandle->gets()";
540 redef "$filehandle->fileno()";
544 redef "FileHandle->open()";
548 unimpl "fprintf() is C-specific--use printf instead";
552 unimpl "fputc() is C-specific--use print instead";
556 unimpl "fputs() is C-specific--use print instead";
560 unimpl "fread() is C-specific--use read instead";
564 unimpl "freopen() is C-specific--use open instead";
568 unimpl "fscanf() is C-specific--use <> and regular expressions instead";
572 redef "$filehandle->seek(pos,whence)";
576 redef "$filehandle->error()";
580 redef "$filehandle->flush()";
584 redef "$filehandle->getpos()";
588 redef "$filehandle->setpos(pos)";
592 redef "$filehandle->tell()";
596 unimpl "fwrite() is C-specific--use print instead";
600 usage "getc(handle)" if @_ != 1;
605 usage "getchar()" if @_ != 0;
610 usage "gets()" if @_ != 0;
615 print STDERR "@_: " if @_;
616 print STDERR $!,"\n";
620 usage "printf(pattern, args...)" if @_ < 1;
625 unimpl "putc() is C-specific--use print instead";
629 unimpl "putchar() is C-specific--use print instead";
633 unimpl "puts() is C-specific--use print instead";
637 usage "remove(filename)" if @_ != 1;
642 usage "rename(oldfilename, newfilename)" if @_ != 2;
643 rename($_[0], $_[1]);
647 usage "rewind(filehandle)" if @_ != 1;
652 unimpl "scanf() is C-specific--use <> and regular expressions instead";
656 usage "sprintf(pattern,args)" if @_ == 0;
661 unimpl "sscanf() is C-specific--use regular expressions instead";
665 redef "FileHandle->new_tmpfile()";
669 redef "$filehandle->ungetc(char)";
673 unimpl "vfprintf() is C-specific";
677 unimpl "vprintf() is C-specific";
681 unimpl "vsprintf() is C-specific";
685 usage "abs(x)" if @_ != 1;
690 unimpl "atexit() is C-specific: use END {} instead";
694 unimpl "atof() is C-specific, stopped";
698 unimpl "atoi() is C-specific, stopped";
702 unimpl "atol() is C-specific, stopped";
706 unimpl "bsearch(xxx)" if @_ != 123;
711 unimpl "calloc() is C-specific, stopped";
715 unimpl "div() is C-specific, stopped";
719 usage "exit(status)" if @_ != 1;
724 unimpl "free() is C-specific, stopped";
729 usage "getenv(name)" if @_ != 1;
734 unimpl "labs() is C-specific, use abs instead";
738 unimpl "ldiv() is C-specific, use / and int instead";
742 unimpl "malloc() is C-specific, stopped";
746 unimpl "qsort() is C-specific, use sort instead";
750 unimpl "rand() is non-portable, use Perl's rand instead";
754 unimpl "realloc() is C-specific, stopped";
762 unimpl "strtod() is C-specific, stopped";
766 unimpl "strtol() is C-specific, stopped";
770 unimpl "stroul() is C-specific, stopped";
774 usage "system(command)" if @_ != 1;
779 unimpl "memchr() is C-specific, use index() instead";
783 unimpl "memcmp() is C-specific, use eq instead";
787 unimpl "memcpy() is C-specific, use = instead";
791 unimpl "memmove() is C-specific, use = instead";
795 unimpl "memset() is C-specific, use x instead";
799 unimpl "strcat() is C-specific, use .= instead";
803 unimpl "strchr() is C-specific, use index() instead";
807 unimpl "strcmp() is C-specific, use eq instead";
811 unimpl "strcpy() is C-specific, use = instead";
815 unimpl "strcspn() is C-specific, use regular expressions instead";
819 usage "strerror(errno)" if @_ != 1;
825 unimpl "strlen() is C-specific, use length instead";
829 unimpl "strncat() is C-specific, use .= instead";
833 unimpl "strncmp() is C-specific, use eq instead";
837 unimpl "strncpy() is C-specific, use = instead";
841 unimpl "strpbrk() is C-specific, stopped";
845 unimpl "strrchr() is C-specific, use rindex() instead";
849 unimpl "strspn() is C-specific, stopped";
853 usage "strstr(big, little)" if @_ != 2;
858 unimpl "strtok() is C-specific, stopped";
862 usage "chmod(mode, filename)" if @_ != 2;
867 usage "fstat(fd)" if @_ != 1;
869 open(TMP, "<&$_[0]"); # Gross.
870 local(@l) = stat(TMP);
876 usage "mkdir(directoryname, mode)" if @_ != 2;
881 usage "stat(filename)" if @_ != 1;
886 usage "umask(mask)" if @_ != 1;
891 usage "times()" if @_ != 0;
896 usage "wait(statusvariable)" if @_ != 1;
897 local $result = wait();
903 usage "waitpid(pid, statusvariable, options)" if @_ != 3;
904 local $result = waitpid($_[0], $_[2]);
910 usage "gmtime(time)" if @_ != 1;
915 usage "localtime(time)" if @_ != 1;
920 unimpl "time()" if @_ != 0;
925 usage "alarm(seconds)" if @_ != 1;
930 usage "chdir(directory)" if @_ != 1;
935 usage "chown(filename, uid, gid)" if @_ != 3;
936 chown($_[0], $_[1], $_[2]);
940 unimpl "execl() is C-specific, stopped";
945 unimpl "execle() is C-specific, stopped";
950 unimpl "execlp() is C-specific, stopped";
955 unimpl "execv() is C-specific, stopped";
960 unimpl "execve() is C-specific, stopped";
965 unimpl "execvp() is C-specific, stopped";
970 usage "fork()" if @_ != 0;
976 usage "getcwd()" if @_ != 0;
982 usage "getegid()" if @_ != 0;
987 usage "geteuid()" if @_ != 0;
992 usage "getgid()" if @_ != 0;
997 usage "getgroups()" if @_ != 0;
999 grep(!$seen{$_}++, split(' ', $) ));
1003 usage "getlogin()" if @_ != 0;
1008 usage "getpgrp()" if @_ != 0;
1013 usage "getpid()" if @_ != 0;
1018 usage "getppid()" if @_ != 0;
1023 usage "getuid()" if @_ != 0;
1028 usage "isatty(filehandle)" if @_ != 1;
1033 usage "link(oldfilename, newfilename)" if @_ != 2;
1038 usage "rmdir(directoryname)" if @_ != 1;
1043 usage "setgid(gid)" if @_ != 1;
1048 usage "setuid(uid)" if @_ != 1;
1053 usage "sleep(seconds)" if @_ != 1;
1058 usage "unlink(filename)" if @_ != 1;
1063 usage "utime(filename, atime, mtime)" if @_ != 3;
1064 utime($_[1], $_[2], $_[0]);