Fcntl#

📦 min

Symbolic constants for fcntl(2), flock(2), lseek(2), and file-mode bits - everything you need to call sysopen, fcntl, flock, seek, and to interpret the mode word returned by stat.

Import individual names, or a named group via one of the export tags: :DEFAULT (open and file-descriptor flags), :flock (advisory locks), :seek (whence positions), :mode (mode bits and S_IS* predicates), :Fcompat (BSD-style flock aliases).

Open flags (sysopen)#

  • O_RDONLY, O_WRONLY, O_RDWR - access mode.

  • O_CREAT - create the file if it does not exist.

  • O_EXCL - with O_CREAT, fail if the file already exists.

  • O_TRUNC - truncate to zero length on open.

  • O_APPEND - every write seeks to end of file first.

  • O_NONBLOCK - open without blocking; I/O returns EAGAIN instead.

  • O_NOCTTY, O_NOFOLLOW, O_BINARY - additional platform flags (O_BINARY is a no-op on Linux).

File-descriptor control (fcntl)#

  • F_DUPFD - duplicate a descriptor.

  • F_GETFD, F_SETFD - read / set the close-on-exec flag FD_CLOEXEC.

  • F_GETFL, F_SETFL - read / set the open-file status flags.

Lock types (flock)#

  • LOCK_SH - shared lock.

  • LOCK_EX - exclusive lock.

  • LOCK_UN - release lock.

  • LOCK_NB - OR with the above to request a non-blocking attempt.

Seek whence#

  • SEEK_SET - from start of file.

  • SEEK_CUR - from current position.

  • SEEK_END - from end of file.

File mode bits (stat)#

  • Type mask and values: S_IFMT, S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFIFO, S_IFLNK, S_IFSOCK.

  • Permission triplets: S_IRWXU, S_IRWXG, S_IRWXO and the per-bit S_IRUSR / S_IWUSR / S_IXUSR (and the GRP, OTH variants).

  • Setuid / setgid / sticky bits: S_ISUID, S_ISGID, S_ISVTX.

Mode test helpers#

Function-form predicates over a stat-mode value: S_ISREG, S_ISDIR, S_ISCHR, S_ISBLK, S_ISFIFO, S_ISLNK, S_ISSOCK, plus S_IFMT (extract file-type bits) and S_IMODE (extract permission bits).

Performance#

Every constant in this module is resolved at compile time - using O_CREAT in an expression costs exactly what writing the integer literal would cost. No per-call lookup.

A constant this system does not have dies when called: a name Fcntl knows but the platform lacks (O_EXLOCK, S_IFWHT) with “Your vendor has not defined Fcntl macro O_EXLOCK, used at FILE line N”, any other name with “FOO is not a valid Fcntl macro at FILE line N”.

use Fcntl qw(:DEFAULT :flock :seek :mode);

sysopen(my $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0644) or die "sysopen: $!";
flock($fh, LOCK_EX | LOCK_NB) or die "flock: $!";
syswrite $fh, "data\n";
sysseek $fh, 0, SEEK_SET;
my $mode = (stat $fh)[2];
printf "%s, permissions %04o\n", S_ISREG($mode) ? "regular file" : "other", S_IMODE($mode);
# regular file, permissions 0644

This is a native implementation of Fcntl 1.20; its Fcntl.pm is used unchanged, and use Fcntl alone exports the :DEFAULT set.

Differences from upstream#

None.

Functions#

Mode test helpers#

S_IMODE#

Extract the permission bits from a stat-mode value.

S_IFMT#

Extract the file-type bits from a stat-mode value.

S_ISREG#

True if a stat-mode value names a file of the predicate’s type: S_ISREG a regular file, S_ISDIR a directory, S_ISLNK a symbolic link, S_ISSOCK a socket, S_ISBLK a block device, S_ISCHR a character device, S_ISFIFO a named pipe.

Also available as Fcntl::S_ISDIR, Fcntl::S_ISLNK, Fcntl::S_ISSOCK, Fcntl::S_ISBLK, Fcntl::S_ISCHR, Fcntl::S_ISFIFO.