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 __fcntlFunctions
function fcntl(fd: i32, cmd: i32): i32POSIX fcntl(fd, cmd) for commands without a third argument.
function fcntlArg(fd: i32, cmd: i32, arg: i32): i32POSIX fcntl(fd, cmd, arg) for integer descriptor-flag commands.
Constants
const F_GETFD: i32const F_SETFD: i32const FD_CLOEXEC: i32const O_APPEND: i32const O_CREAT: i32const O_RDONLY: i32const O_RDWR: i32const O_TRUNC: i32const O_WRONLY: i32namespace __unistd_ioFunctions
function chdir(path: *u8): i32POSIX chdir; changes the current working directory.
function close(fd: i32): i32POSIX close; returns 0 on success or -1.
function dup(oldfd: i32): i32POSIX dup; duplicates a file descriptor.
function dup2(oldfd: i32, newfd: i32): i32POSIX dup2; duplicates a descriptor to a requested number.
function getcwd(buf: *mut u8, size: u64): *mut u8POSIX getcwd; writes the current directory into buf.
function isatty(fd: i32): i32POSIX isatty; returns nonzero when fd is a terminal.
function lseek(fd: i32, offset: i64, whence: i32): i64POSIX lseek; returns the resulting offset or -1.
function open(pathname: *u8, flags: i32): i32POSIX open with flags; returns a file descriptor or -1.
function read(fd: i32, buf: *mut void, count: u64): i64POSIX read; returns bytes read, 0 on EOF, or -1 on error.
function rmdir(pathname: *u8): i32POSIX rmdir; removes an empty directory.
function unlink(pathname: *u8): i32POSIX unlink; removes a filesystem name.
function write(fd: i32, buf: *void, count: u64): i64POSIX write; returns bytes written or -1 on error.
Constants
const SEEK_CUR: i32const SEEK_END: i32const SEEK_SET: i32const STDERR_FILENO: i32const STDIN_FILENO: i32const STDOUT_FILENO: i32namespace FilePOSIX file descriptors (unistd.h / fcntl.h)
Functions
function chdir(path: *u8): i32Changes the current working directory to path.
function close(fd: i32): i32Closes file descriptor fd. Returns 0 on success, -1 on error.
function dup(oldfd: i32): i32Duplicates file descriptor oldfd. Returns the new fd or -1.
function dup2(oldfd: i32, newfd: i32): i32Duplicates oldfd to newfd, closing newfd first if open.
function fcntl(fd: i32, cmd: i32): i32Performs fcntl(fd, cmd) for commands without a third argument.
function fcntlArg(fd: i32, cmd: i32, arg: i32): i32Performs fcntl(fd, cmd, arg) for commands that require an integer value.
function getcwd(buf: *mut u8, size: u64): *mut u8Writes the current working directory into buf (up to size bytes).
function isatty(fd: i32): i32Tests whether fd refers to a terminal. Returns 1 if true.
function lseek(fd: i32, offset: i64, whence: i32): i64Repositions 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): i32Opens 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): i64Reads 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): i32Removes an empty directory. Returns 0 on success.
function unlink(pathname: *u8): i32Deletes a name from the filesystem. Returns 0 on success.
function write(fd: i32, buf: *void, count: u64): i64Writes up to count bytes from buf to file descriptor fd.
Returns the number of bytes written, or -1 on error.
Constants
const F_GETFD: i32const F_SETFD: i32const FD_CLOEXEC: i32const O_APPEND: i32const O_CREAT: i32const O_RDONLY: i32const O_RDWR: i32const O_TRUNC: i32const O_WRONLY: i32const SEEK_CUR: i32const SEEK_END: i32const SEEK_SET: i32const STDERR_FILENO: i32const STDIN_FILENO: i32const STDOUT_FILENO: i32