io. handle

Represents a handle for interacting with a file descriptor.

Example
const handle = io.open(…);

handle.read(…);
handle.write(…);

handle.seek(…);
handle.tell();

handle.fileno();

handle.close();

handle.error();

Methods

close() → {boolean}nullable

Closes the file descriptor.

Closes the underlying file descriptor. Further operations on this handle will fail.

Returns true if the descriptor was successfully closed.

Returns null if an error occurred.

Returns: boolean
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
handle.close();

dup() → {handle}nullable

Duplicates the file descriptor.

Creates a duplicate of the file descriptor using dup(2).

Returns a new io.handle for the duplicated descriptor.

Returns null if an error occurred.

Returns: handle
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
const dup_handle = handle.dup();

dup2(newfd) → {boolean}nullable

Duplicates the file descriptor to a specific descriptor number.

Creates a duplicate of the file descriptor to the specified descriptor number using dup2(2). If newfd was previously open, it is silently closed.

Returns true on success.

Returns null if an error occurred.

Parameters:
NameTypeDescription
newfdnumber

The target file descriptor number.

Returns: boolean
Example
const handle = io.open('/tmp/test.txt', O_WRONLY);
handle.dup2(2);  // Redirect stderr to the file

error() → {string}nullable

Query error information.

Returns a string containing a description of the last occurred error or null if there is no error information.

Returns: string
Example
// Trigger an error
io.open('/path/does/not/exist');

// Print error (should yield "No such file or directory")
print(io.error(), "\n");

fcntl(cmd, argopt) → {number|handle}nullable

Performs fcntl() operations on the file descriptor.

Performs the specified fcntl() command on the file descriptor with an optional argument.

Returns the result of the fcntl() call. For F_DUPFD and F_DUPFD_CLOEXEC, returns a new io.handle wrapping the duplicated descriptor. For other commands, returns a number (interpretation depends on cmd).

Returns null if an error occurred.

Parameters:
NameTypeDescription
cmdnumber

The fcntl command (e.g., F_GETFL, F_SETFL, F_GETFD, F_SETFD, F_DUPFD).

argnumber(optional)

Optional argument for the command.

Returns: number | handle
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
const flags = handle.fcntl(F_GETFL);
handle.fcntl(F_SETFL, flags | O_NONBLOCK);
const dup_handle = handle.fcntl(F_DUPFD, 10);  // Returns io.handle

fileno() → {number}nullable

Gets the file descriptor number.

Returns the underlying file descriptor number.

Returns null if the handle is closed.

Returns: number
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
print(handle.fileno(), "\n");

ioctl(direction, type, num, valueopt) → {number|string}

Performs an ioctl operation on the file descriptor.

The direction parameter specifies who is reading and writing, from the user's point of view. It can be one of the following values:

DirectionDescription
IOC_DIR_NONEneither userspace nor kernel is writing, ioctl is executed without passing data.
IOC_DIR_WRITEuserspace is writing and kernel is reading.
IOC_DIR_READkernel is writing and userspace is reading.
IOC_DIR_RWuserspace is writing and kernel is writing back into the data structure.

Returns the result of the ioctl operation; for IOC_DIR_READ and IOC_DIR_RW this is a string containing the data, otherwise a number as return code.

Returns null if an error occurred.

Parameters:
NameTypeDescription
directionnumber

The direction of the ioctl operation. Use constants IOC_DIR_*.

typenumber

The ioctl type (see https://www.kernel.org/doc/html/latest/userspace-api/ioctl/ioctl-number.html)

numnumber

The ioctl sequence number.

valuenumber | string(optional)

The value to pass to the ioctl system call. For IOC_DIR_NONE, this argument is ignored. With IOC_DIR_READ, the value should be a positive integer specifying the number of bytes to expect from the kernel. For the other directions, IOC_DIR_WRITE and IOC_DIR_RW, that value parameter must be a string, serving as buffer for the data to send.

Returns: number | string
Example
const handle = io.open('/dev/tty', O_RDWR);
const size = handle.ioctl(IOC_DIR_READ, 0x54, 0x13, 8);  // TIOCGWINSZ

isatty() → {boolean}nullable

Checks if the file descriptor refers to a terminal.

Returns true if the descriptor refers to a terminal device.

Returns false otherwise.

Returns null if an error occurred.

Returns: boolean
Example
const handle = io.new(0);  // stdin
if (handle.isatty())
    print("Running in a terminal\n");

read(length) → {string}nullable

Reads data from the file descriptor.

Reads up to the specified number of bytes from the file descriptor.

Returns a string containing the read data.

Returns an empty string on EOF.

Returns null if a read error occurred.

Parameters:
NameTypeDescription
lengthnumber

The maximum number of bytes to read.

Returns: string
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
const data = handle.read(1024);

seek(offsetopt, whenceopt) → {boolean}nullable

Sets the file descriptor position.

Sets the file position of the descriptor to the given offset and whence.

Returns true if the position was successfully set.

Returns null if an error occurred.

Parameters:
NameTypeDescription
offsetnumber(optional, default: 0)

The offset in bytes.

whencenumber(optional, default: 0)

The position reference.

WhenceDescription
0The offset is relative to the start of the file (SEEK_SET).
1The offset is relative to the current position (SEEK_CUR).
2The offset is relative to the end of the file (SEEK_END).
Returns: boolean
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
handle.seek(100, 0);  // Seek to byte 100 from start

tell() → {number}nullable

Gets the current file descriptor position.

Returns the current file position as an integer.

Returns null if an error occurred.

Returns: number
Example
const handle = io.open('/tmp/test.txt', O_RDONLY);
const pos = handle.tell();

write(data) → {number}nullable

Writes data to the file descriptor.

Writes the given data to the file descriptor. Non-string values are converted to strings before being written.

Returns the number of bytes written.

Returns null if a write error occurred.

Parameters:
NameTypeDescription
data*

The data to write.

Returns: number
Example
const handle = io.open('/tmp/test.txt', O_WRONLY | O_CREAT);
handle.write('Hello World\n');