Module

std::libc::io

LibC I/O

POSIX file-descriptor operations from unistd.h and fcntl.h, exposed as LibC::File.

Constants

Constant Description
SEEK_SET Seek from beginning of file
SEEK_CUR Seek from current position
SEEK_END Seek from end of file
STDIN_FILENO Standard input fd (0)
STDOUT_FILENO Standard output fd (1)
STDERR_FILENO Standard error fd (2)
O_RDONLY Open for reading only
O_WRONLY Open for writing only
O_RDWR Open for reading and writing
O_CREAT Create file if it does not exist
O_TRUNC Truncate file to zero length
O_APPEND Append on each write
F_GETFD fcntl request: get fd flags
F_SETFD fcntl request: set fd flags
FD_CLOEXEC Close fd on successful exec

Functions

Function Description
open Open file by pathname
close Close file descriptor
read Read bytes from file descriptor
write Write bytes to file descriptor
lseek Reposition file offset
unlink Delete filesystem name
rmdir Remove empty directory
getcwd Get current working directory
chdir Change working directory
dup Duplicate file descriptor
dup2 Duplicate fd to specific number
isatty Test if fd refers to a terminal
fcntl Run fcntl(fd, cmd)
fcntlArg Run fcntl(fd, cmd, arg)

Most functions return -1 on error. read returns 0 on EOF.

Example

import LibC, CType from "std::libc";

function main(): i32 {
  let fd: CType::CInt = LibC::File::open(
    "hello.txt",
    LibC::File::O_WRONLY | LibC::File::O_CREAT | LibC::File::O_TRUNC,
  );
  if fd == -1 {
    return 1;
  }
  let data: *u8 = "hello, world\n";
  LibC::File::write(fd, data as CType::CConstVoidPtr, 13);
  LibC::File::close(fd);
  return 0;
}
namespace __fcntl

Functions

function fcntl(fd: i32, cmd: i32): i32

POSIX fcntl(fd, cmd) for commands without a third argument.

function fcntlArg(fd: i32, cmd: i32, arg: i32): i32

POSIX fcntl(fd, cmd, arg) for integer descriptor-flag commands.

Constants

const F_GETFD: i32
const F_SETFD: i32
const FD_CLOEXEC: i32
const O_APPEND: i32
const O_CREAT: i32
const O_RDONLY: i32
const O_RDWR: i32
const O_TRUNC: i32
const O_WRONLY: i32
namespace __unistd_io

Functions

function chdir(path: *u8): i32

POSIX chdir; changes the current working directory.

function close(fd: i32): i32

POSIX close; returns 0 on success or -1.

function dup(oldfd: i32): i32

POSIX dup; duplicates a file descriptor.

function dup2(oldfd: i32, newfd: i32): i32

POSIX dup2; duplicates a descriptor to a requested number.

function getcwd(buf: *mut u8, size: u64): *mut u8

POSIX getcwd; writes the current directory into buf.

function isatty(fd: i32): i32

POSIX isatty; returns nonzero when fd is a terminal.

function lseek(fd: i32, offset: i64, whence: i32): i64

POSIX lseek; returns the resulting offset or -1.

function open(pathname: *u8, flags: i32): i32

POSIX open with flags; returns a file descriptor or -1.

function read(fd: i32, buf: *mut void, count: u64): i64

POSIX read; returns bytes read, 0 on EOF, or -1 on error.

function rmdir(pathname: *u8): i32

POSIX rmdir; removes an empty directory.

function write(fd: i32, buf: *void, count: u64): i64

POSIX write; returns bytes written or -1 on error.

Constants

const SEEK_CUR: i32
const SEEK_END: i32
const SEEK_SET: i32
const STDERR_FILENO: i32
const STDIN_FILENO: i32
const STDOUT_FILENO: i32
namespace File

POSIX file descriptors (unistd.h / fcntl.h)

Functions

function chdir(path: *u8): i32

Changes the current working directory to path.

function close(fd: i32): i32

Closes file descriptor fd. Returns 0 on success, -1 on error.

function dup(oldfd: i32): i32

Duplicates file descriptor oldfd. Returns the new fd or -1.

function dup2(oldfd: i32, newfd: i32): i32

Duplicates oldfd to newfd, closing newfd first if open.

function fcntl(fd: i32, cmd: i32): i32

Performs fcntl(fd, cmd) for commands without a third argument.

function fcntlArg(fd: i32, cmd: i32, arg: i32): i32

Performs fcntl(fd, cmd, arg) for commands that require an integer value.

function getcwd(buf: *mut u8, size: u64): *mut u8

Writes the current working directory into buf (up to size bytes).

function isatty(fd: i32): i32

Tests whether fd refers to a terminal. Returns 1 if true.

function lseek(fd: i32, offset: i64, whence: i32): i64

Repositions the file offset of fd. Use SEEK_SET/CUR/END for whence.

Returns the new offset, or -1 on error.

function open(pathname: *u8, flags: i32): i32

Opens the file at pathname with the given flags.

Returns a file descriptor, or -1 on error.

function read(fd: i32, buf: *mut void, count: u64): i64

Reads up to count bytes from file descriptor fd into buf.

Returns the number of bytes read, 0 on EOF, or -1 on error.

function rmdir(pathname: *u8): i32

Removes an empty directory. Returns 0 on success.

function write(fd: i32, buf: *void, count: u64): i64

Writes up to count bytes from buf to file descriptor fd.

Returns the number of bytes written, or -1 on error.

Constants

const F_GETFD: i32
const F_SETFD: i32
const FD_CLOEXEC: i32
const O_APPEND: i32
const O_CREAT: i32
const O_RDONLY: i32
const O_RDWR: i32
const O_TRUNC: i32
const O_WRONLY: i32
const SEEK_CUR: i32
const SEEK_END: i32
const SEEK_SET: i32
const STDERR_FILENO: i32
const STDIN_FILENO: i32
const STDOUT_FILENO: i32