Scroll to navigation

POSIX::2008(3pm) User Contributed Perl Documentation POSIX::2008(3pm)

NAME

POSIX::2008 - Perl interface to POSIX.1-2008

SYNOPSIS

  use Fcntl;
  use POSIX::2008 qw(openat pwrite);
  sysopen my $dh, '/tmp', O_RDONLY|O_DIRECTORY|O_NOFOLLOW or die 'Dafuq?';
  my $fh = openat($dh, 'foobar', O_RDWR|O_CREAT);
  pwrite($fh, 'fuckyounsa', 10, 0);

DESCRIPTION

POSIX::2008 contains many of the interfaces specified by POSIX.1-2008 <http://pubs.opengroup.org/onlinepubs/9699919799/> that the core POSIX module withholds, implements in Perl or fucked up.

This module is provided "as is" unless someone volunteers to maintain it.

INCOMPATIBLE PARAMETER CHANGE

Since version 0.13, the parameter order of pread/pwrite is (count, offset) as with the actual system calls instead of (offset, count). Good luck.

FILE DESCRIPTORS AND HANDLES

Since version 0.05, all I/O functions that take numeric file descriptors also accept Perl file or directory handles. "openat()" even returns a handle if you passed it one.

SYSTEM CALL RETURN VALUES

A system call return value of -1 meaning "error" is mapped to undef.

A system call return value of 0 meaning "success" is mapped to "0 but true".

For system calls where 0 does not just mean "success", 0 is returned unchanged. These are "open()", "read()", "write()", "readv()", "writev()", "pread()", "pwrite()", "preadv()", "pwritev()". "openat()" gets a special treatment in this module, see below.

FUNCTIONS

l = a64l(s);
abort();
ui = abs(i);
ret = access(path, mode);
y = acos(x);
y = acosh(x);
remaining_sec = alarm(sec);
y = asin(x);
y = asinh(x);
z = atan2(y, x);
y = atan(x);
y = atanh(x);
f = atof(s);
i = atoi(s);
l = atol(s);
s = basename(path);
r = cabs(re, im);
(re, im) = cacos(re, im);
(re, im) = cacosh(re, im);
phi = carg(re, im);
(re, im) = casinh(re, im);
(re, im) = catan(re, im);
(re, im) = catanh(re, im);
ret = catclose(catd);
s = catgets(catd, set_id, msg_id, dflt_string);
catd = catopen(name, oflag);
y = cbrt(x);
(re, im) = ccos(re, im);
(re, im) = ccosh(re, im);
y = ceil(x);
(re, im) = cexp(re, im);
ret = chdir(path);
ret = chmod(path, mode);
ret = chown(path, uid, gid);
im = cimag(re, im);
t = clock()
clock_id = clock_getcpuclockid(pid);

pid defaults to $$. Returns undef on error.

(sec, nsec) = clock_getres(clock_id);

clock_id defaults to CLOCK_REALTIME. Returns empty list on error.

(sec, nsec) = clock_gettime(clock_id);

clock_id defaults to CLOCK_REALTIME. Returns empty list on error.

(rem_sec, rem_nsec) = clock_nanosleep(clock_id, flags, sec, nsec);

In scalar context returns the remaining seconds as a floating point number.

ret = clock_settime(clock_id, sec, nsec);
(re, im) = clog(re, im);
ret = close(fd);
s = confstr(name);

name is one of the _CS_ integer constants.

(re, im) = conj(re, im);
xs = copysign(x, y);
y = cos(x);
y = cosh(x);
(re, im) = cpow(re_x, im_x, re_y, im_y);
(re, im) = cproj(re, im);
re = creal(re, im);
(re, im) = csin(re, im);
(re, im) = csinh(re, im);
(re, im) = csqrt(re, im);
(re, im) = ctan(re, im);
(re, im) = ctanh(re, im);
name = dirname(path);
(quot, rem) = div(numer, denom);
dlclose(dlhandle);
dlerror();
dlhandle = dlopen(file, mode);
addr = dlsym(dlhandle, name);
r = drand48();
endutxent();
(r, X0, X1, X2) = erand48(X0, X1, X2);
y = erf(x);
y = erfc(x);
y = exp2(x);
y = expm1(x);
ret = faccessat(dirfd, path, amode, flags=0);

flags is the bitwise OR of zero or more of AT_EACCESS, AT_SYMLINK_NOFOLLOW.

ret = fchdir(dirfd);
ret = fchmod(fd, mode);
ret = fchmodat(dirfd, path, mode, flags=0);

flags can be 0 or AT_SYMLINK_NOFOLLOW.

ret = fchown(fd, uid, gid);
ret = fchownat(dirfd, path, uid, gid, flags=0);

flags can be 0 or AT_SYMLINK_NOFOLLOW.

ret = fdatasync(fd);
ret = fdopen(fd, mode)

Returns a file handle associated with the file descriptor fd or undef on error. mode is one of the values "r", "w", "a" with an optional "+" and/or "b".

It's similar to "IO::Handle::new_from_fd()" with the following improvements:

  • It really calls fdopen(3).
  • It expects POSIX mode strings (e.g. "r", not "<").
  • It fails mode is not compatible with the flags of fd.
ret = fdopendir(fd)

Returns a directory handle associated with the file descriptor fd or undef on error. Usage example:

  my $dh = do {
    sysopen my $fh, '/tmp', O_RDONLY|O_DIRECTORY|O_NOFOLLOW;
    fdopendir($fh); # or fdopendir(fileno $fh) but the former also works
                    # with handles from opendir() for which fileno does
                    # not work before Perl 5.22
  };
  my @files = readdir $dh;  # this would fail with $fh from sysopen
    
d = fdim(double x, double y);
round = fegetround();
ret = fesetround(round);
pos = ffs(i);
y = floor(x);
r = fma(x, y, z);
m = fmax(x, y);
m = fmin(x, y);
m = fmod(x, y);
ret = fnmatch(pattern, string, flags);

Returns 0 if string matches pattern, FNM_NOMATCH if there is no match, undef if there is an error.

flags is the bitwise OR of zero or more of FNM_NOESCAPE, FNM_PATHNAME, FNM_PERIOD, FNM_FILE_NAME, FNM_LEADING_DIR, FNM_CASEFOLD.

fpclassify(x);

Returns one of FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL.

(dev, ino, mode, nlink, uid, gid, rdev, size, atim_sec, mtim_sec, ctim_sec, blksize, blocks, atim_nsec, mtim_nsec, ctim_nsec) = fstatat(dirfd, path, flags = 0);

flags is the bitwise OR of zero or more of AT_SYMLINK_NOFOLLOW, AT_NO_AUTOMOUNT.

ret = fsync(fd);
ret = ftruncate(fd, length);
ret = futimens(fd, atime_sec, atime_nsec, mtime_sec, mtime_nsec);

atime_sec and mtime_sec default to 0, atime_nsec and mtime_nsec default to UTIME_NOW.

(sec, min, hour, mday, mon, year, wday, yday, isdst) = getdate(string);
getdate_err() returns the value of the getdate_err variable.
egid = getegid();
euid = geteuid();
gid = getgid();
hostid = gethostid();
hostname = gethostname();
(int_sec, int_usec, val_sec, val_usec) = getitimer(which);

which is one of ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF.

prio = getpriority(which, who);

which can be one of PRIO_PROCESS, PRIO_PGRP, PRIO_USER, defaults to PRIO_PROCESS.

who defaults to 0.

Returns undef on error.

sid = getsid(pid);

pid defaults to 0.

uid = getuid();
(user, id, line, pid, type, sec, usec) = getutxent();

getutxent() reads a line from the current file position in the utmp file.

(user, id, line, pid, type, sec, usec) = getutxid(ut_type, ut_id);

getutxid() searches forward from the current file position in the utmp file based upon ut_type and ut_id. If ut_type is one of RUN_LVL, BOOT_TIME, NEW_TIME, or OLD_TIME, getutxid() will find the first entry whose ut_type field matches ut_type. If ut_type is one of INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, getutxid() will find the first entry whose ut_id field matches ut_id.

(user, id, line, pid, type, sec, usec) = getutxline(ut_line);

getutxline() searches forward from the current file position in the utmp file. It scans entries whose ut_type is USER_PROCESS or LOGIN_PROCESS and returns the first one whose ut_line field matches ut_line.

r = hypot(x, y);
y = ilogb(x);
Like POSIX::isalpha() but returns 0 for the empty string.
ret = isatty(fd)
ret = isblank(charstring)

Returns 1 if charstring consists only of blank characters (i.e. spaces and/or tabs). Returns 0 otherwise (also for the empty string).

Like POSIX::iscntrl() but returns 0 for the empty string.
Like POSIX::isdigit() but returns 0 for the empty string.
isfinite(x);
Like POSIX::isgraph() but returns 0 for the empty string.
isinf(x);
Like POSIX::islower() but returns 0 for the empty string.
isnan(x);
isnormal(x);
Like POSIX::isprint() but returns 0 for the empty string.
Like POSIX::ispunct() but returns 0 for the empty string.
Like POSIX::isspace() but returns 0 for the empty string.
Like POSIX::isupper() but returns 0 for the empty string.
Like POSIX::isxdigit() but returns 0 for the empty string.
y = j0(x);

j0() is the Bessel function of the first kind of order 0.

y = j1(x);

j1() is the Bessel function of the first kind of order 1.

y = jn(n, x);

jn() is the Bessel function of the first kind of order n.

(r, X0, X1, X2) = jrand48(X0, X1, X2);
ret = killpg(pgrp, sig);
s = l64a(n);
ret = lchown(path, uid, gid)
y = ldexp(x, exp);
(quot, rem) = ldiv(numer, denom);
y = lgamma(x);
ret = link(path1, path2);
ret = linkat(fd1, path1, fd2, path2, flags=0);

flags can be 0 or AT_SYMLINK_FOLLOW.

y = log1p(x);
y = log2(x);
y = logb(x);
r = lrand48();
l = lround(x)
ret = lstat(path);
ret = mkdir(path, mode);
ret = mkdirat(fd, path, mode);
name = mkdtemp(template);
ret = mkfifo(path, mode);
ret = mkfifoat(fd, path, mode);
ret = mknod(path, mode, dev);
ret = mknodat(fd, path, mode, dev);
(fd, name) = mkstemp(template);
mrand48();
(rem_sec, rem_nsec) = nanosleep(sec, nsec);

In scalar context returns the remaining seconds as a floating point number.

y = nearbyint(x);
z = nextafter(x, y);
r = nrand48()
ret = open(path, oflag[, mode]);

oflag defaults to O_RDONLY, mode defaults to 0600.

ret = openat(fd, path, oflag[, mode]);

oflag defaults to O_RDONLY, mode defaults to 0600.

If fd is numeric (i.e. a file descriptor), "openat()" returns a file descriptor. If fd is a file or directory handle the return value is also a handle whose type depends on the file type of path: If path is a directory, the return value is a directory handle, otherwise it's a file handle.

Returns undef on error.

ret = posix_fadvise(fd, offset, len, advice);

advice is one of the "POSIX_FADV_" constants.

Returns undef on error

ret = posix_fallocate(fd, offset, len);
bytes_read = pread(fd, buf, count, [offset, buf_offset]);

"pread()" reads count bytes (not characters) of data from the file descriptor fd at file offset offset into the scalar buf without changing the file offset. buf will be enlarged automatically if necessary.

offset and buf_offset are set to 0 if omitted or undef.

"pread()" treats buf just like "sysread()" does: buf_offset may be specified to place the read data at that position in buf. If buf_offset is past the end of buf, buf will be padded with zeros before appending the data. If buf_offset is negative, it is counted from the end of the string. buf will be grown or shrunk so that the last byte actually read is the last byte of buf after the read.

Returns the number of bytes read, 0 at EOF, undef on error.

bytes_read = preadv(fd, buffers, sizes, [offset])

"preadv()" behaves like "readv()" but adds an optional offset argument, which specifies the file position at which the data is to be read. offset is set to 0 if omitted or undef.

The file offset is not changed by this system call. The file referred to by fd must be capable of seeking.

name = ptsname(fd);
bytes_written = pwrite(fd, buf, [count, offset, buf_offset]);

pwrite() writes count bytes of data from the scalar buf to the file descriptor fd at file offset offset without changing the file offset. The file referenced by fd must be capable of seeking.

If count is omitted or undef, everything from buf_offset up to the end of buf is written.

buf_offset may be specified to write data from that position in buf. If buf_offset is negative it is counted from the end of the string.

offset and buf_offset are set to 0 if omitted or undef.

Returns the number of bytes written, undef on error.

On Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset (in violation of POSIX).

bytes_written = pwritev(fd, buffers, [offset])

"pwritev()" behaves like "writev()" but adds an optional offset argument, which specifies the file position at which the data is to be written. offset is set to 0 if omitted or undef.

The file offset is not changed by this system call. The file referred to by fd must be capable of seeking.

r = random();
bytes_read = read(fd, buf, count);

Like POSIX::read() but returns 0 at EOF instead of "0 but true".

bytes_read = readv(fd, buffers, sizes);

Example:

  sysopen my $fh, '/tmp/foobar', O_RDWR|O_CREAT|O_TRUNC;
  pwrite($fh, 'foobar', 6, 0);
  readv($fh, my @buf, [4, 0, 4, 4]);
  # -> @buf is ('foob', '', 'ar')
    

"readv()" reads from the file descriptor fd into buffers as many strings as there are elements in sizes. buffers must be a variable holding an array or an array reference. sizes must be an array reference.

sizes is expected to hold unsigned integers that specify how many bytes are to be read into each buffer. A byte count of 0 or undef creates an empty string. sizes is processed in array order.

buffers will be extended if necessary, but it will never be shrunk. If buffers is not empty, any existing elements are replaced as long as sufficient data was read from fd.

If the total byte count of sizes exceeds the number of bytes actually read from fd, there may be one partly filled buffer and the rest of sizes is skipped, so you may end up with less strings in buffers than there are elements in sizes.

"readv()" returns the number of bytes read (which may be less than the total bytes in sizes) or undef on error.

name = readlink(path);

Returns undef on error.

name = readlinkat(dirfd, path);

Returns undef on error.

r = remainder(x, y);
ret = remove(path);

Calls the actual C library function "remove()".

Note that POSIX::remove() fails if path is a symlink to a directory because someone "couldn't read the plans right and did a piss-poor job of putting it together" as "(-d $_[0]) ? CORE::rmdir($_[0]) : CORE::unlink($_[0])". Quote from Armageddon.

ret = rename(old, new);
ret = renameat(olddirfd, oldpath, newdirfd, newpath);
r = round(x);
y = scalbn(x, n);
(old_seed1, old_seed2, old_seed3) = seed48(seed1, seed2, seed3);
ret = setegid(gid);
ret = seteuid(uid);
ret = setgid(gid);
(old_int_sec, old_int_usec, old_val_sec, old_val_usec) = setitimer(which, int_sec, int_usec, val_sec, val_usec);

which is one of ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF.

ret = setpriority(value, which, who);

which can be one of PRIO_PROCESS, PRIO_PGRP, PRIO_USER, defaults to PRIO_PROCESS.

Returns true on success, undef on error.

who defaults to 0.

ret = setregid(rgid, egid);
ret = setreuid(ruid, euid);
ret = setuid(uid);
setutxent();
ret = sighold(sig);
ret = sigignore(sig);
b = signbit(x);
sigpause(sig);
ret = sigrelse(sig);
y = sinh(x);
srand48(seedval);
srandom(seed);
(dev, ino, mode, nlink, uid, gid, rdev, size, atim_sec, mtim_sec, ctim_sec, blksize, blocks, atim_nsec, mtim_nsec, ctim_nsec) = stat(path);
(sec, min, hour, mday, mon, year, wday, yday, isdst) = strptime(s, format[, sec, min, hour, mday, mon, year, wday, yday, isdst]);

strptime() converts the string s into a broken-down time according to the format string format. The time fields may optionally be initialized in whole or in part and will be returned as initialized if they are not affected by the format string. Unprocessed uninitialized or undef fields are returned as undef.

Returns an empty list on error.

In scalar context returns the index of the first byte in s that was not processed or the byte length of s if the whole string was consumed or undef on error.

As strptime() acts on null-terminated strings, strings containing NUL bytes will only be processed up to the first NUL byte.

symlink(old, new);
ret = symlinkat(old, dirfd, new);
sync();
y = tan(x);
y = tanh(x);
y = tgamma(x);
timerid = timer_create(clockid, signal);

Returns undef on error.

timer_delete(timerid);

Returns '0 but true' on success, undef on error.

count = timer_getoverrun(timerid);

Returns undef on error.

(interval_sec, interval_nsec, initial_sec, initial_nsec) = timer_gettime(timerid);

Returns an empty list on error.

(old_int_sec, old_int_nsec, old_init_sec, old_init_nsec) = timer_settime(timerid, flags, int_sec, int_nsec, [init_sec, init_nsec]);

flags may be 0 or "TIMER_ABSTIME". If the init values are omitted, they are set to the int values.

ret = truncate(path, length);
y = trunc(x);
ret = unlinkat(dirfd, path, flags=0);

flags can be 0 or AT_REMOVEDIR.

ret = unlink(path);
ret = utimensat(dirfd, path, flags, atime_sec, atime_nsec, mtime_sec, mtime_nsec);

flags can be 0 or AT_SYMLINK_NOFOLLOW, defaults to 0.

atime_sec and mtime_sec default to 0. atime_nsec and mtime_nsec default to UTIME_NOW.

bytes_written = write(fd, buf[, count]);

Like POSIX::write() but returns 0 instead of "0 but true" if 0 bytes were written, and it does not write more bytes than buf contains.

If count is omitted or undef, it defaults to the length of buf.

bytes_written = writev(fd, buffers);

writev() writes multiple buffers from buffers to the file associated with the file descriptor fd. buffers must be an array reference. The buffers are processed in array order. Undefined or empty elements are skipped.

Returns the number of bytes written or undef on error.

y = y0(x);

y0() is the Bessel function of the second kind of order 0.

y = y1(x);

y1() is the Bessel function of the second kind of order 1.

y = yn(n, x);

yn() is the Bessel function of the second kind of order n.

EXPORTS

This module does not export anything by default. The following export tags are available:

 :at     All *at() functions like openat() etc. plus all AT_ constants
 :id     All get/set*id() functions like getuid() etc.
 :is     All is* functions like isdigit() etc.
 :rw     read(), readv(), write(), writev()
 :prw    pread(), preadv(), pwrite(), pwritev()
 :clock  All clock* functions and all CLOCK* constants
 :fcntl  All F_, FD_, O_, POSIX_FADV_ and SEEK_ constants (for AT_ use :at)
 :fnm    C<fnmatch()> and all FNM_ constants
 :time_h All CLOCK_ and TIMER_ constants
 :timer  All timer_ functions and TIMER_ constants

CONSTANTS

"AT_EACCESS" "AT_EMPTY_PATH" "AT_FDCWD" "AT_NO_AUTOMOUNT" "AT_REMOVEDIR" "AT_SYMLINK_FOLLOW" "AT_SYMLINK_NOFOLLOW"

"BOOT_TIME" "NEW_TIME" "OLD_TIME" "DEAD_PROCESS" "INIT_PROCESS" "LOGIN_PROCESS" "USER_PROCESS" "RUN_LVL"

"CLOCK_BOOTTIME" "CLOCK_HIGHRES" "CLOCK_MONOTONIC" "CLOCK_MONOTONIC_COARSE" "CLOCK_MONOTONIC_FAST" "CLOCK_MONOTONIC_PRECISE" "CLOCK_MONOTONIC_RAW" "CLOCK_PROCESS_CPUTIME_ID" "CLOCK_REALTIME" "CLOCK_REALTIME_COARSE" "CLOCK_REALTIME_FAST" "CLOCK_REALTIME_PRECISE" "CLOCK_SOFTTIME" "CLOCK_THREAD_CPUTIME_ID" "CLOCK_UPTIME" "CLOCK_UPTIME_FAST" "CLOCK_UPTIME_PRECISE"

"_CS_GNU_LIBC_VERSION" "_CS_GNU_LIBPTHREAD_VERSION" "_CS_PATH"

"F_DUPFD" "F_DUPFD_CLOEXEC" "F_GETFD" "F_SETFD" "F_GETFL" "F_SETFL" "F_GETLK" "F_SETLK" "F_SETLKW" "F_GETOWN" "F_SETOWN" "F_RDLCK" "F_UNLCK" "F_WRLCK"

"FD_CLOEXEC"

"FNM_CASEFOLD" "FNM_FILE_NAME" "FNM_LEADING_DIR" "FNM_NOESCAPE" "FNM_NOMATCH" "FNM_PATHNAME" "FNM_PERIOD"

"FP_INFINITE" "FP_NAN" "FP_NORMAL" "FP_SUBNORMAL" "FP_ZERO"

"TIMER_ABSTIME" "ITIMER_PROF" "ITIMER_REAL" "ITIMER_VIRTUAL"

"O_ACCMODE" "O_APPEND" "O_CLOEXEC" "O_CREAT" "O_DIRECTORY" "O_DSYNC" "O_EXEC" "O_NOCTTY" "O_NOFOLLOW" "O_NONBLOCK" "O_RDONLY" "O_RDWR" "O_RSYNC" "O_SEARCH" "O_SYNC" "O_TMPFILE" "O_TRUNC" "O_TTY_INIT" "O_WRONLY"

"POSIX_FADV_NORMAL" "POSIX_FADV_SEQUENTIAL" "POSIX_FADV_RANDOM" "POSIX_FADV_NOREUSE" "POSIX_FADV_WILLNEED" "POSIX_FADV_DONTNEED"

"RTLD_GLOBAL" "RTLD_LAZY" "RTLD_LOCAL" "RTLD_NOW"

"SEEK_SET" "SEEK_CUR" "SEEK_END"

"UTIME_NOW" "UTIME_OMIT"

NOTES

For whatever reason, "preadv()" and "pwritev()" are not part of POSIX. They are included anyway.

AUTHOR

Initially hacked together by Carsten Gaebler.

LICENSE

This library is free software. You can redistribute and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file or http://www.wtfpl.net/ for more details.

2022-06-17 perl v5.34.0