std.os.posix Package

Function Description

The os.posix package is used to adapt to POSIX APIs.

This package provides unified control capabilities for multiple platforms, including Linux, macOS, Windows, and HarmonyOS.

API List

Functions

NameDescriptionSupported on
open(String, Int32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, Windows, macOS, and HarmonyOS
open(String, Int32, UInt32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, Windows, macOS, and HarmonyOS
access(String, Int32)Checks whether a file has certain permission. If yes, 0 is returned; otherwise, -1 is returned.Linux, Windows, macOS, and HarmonyOS
chdir(String)Changes the current working directory of the calling process by specifying a path.Linux, Windows, macOS, and HarmonyOS
chmod(String, UInt32)Modifies the file access permission.Linux, Windows, macOS, and HarmonyOS
chown(String, UInt32, UInt32)Modifies the file owner and the group to which the file owner belongs.Linux, macOS, and HarmonyOS
close(Int32)Closes a file. close causes data to be written back to the disk and resources occupied by the file to be released.Linux, Windows, macOS, and HarmonyOS
creat(String, UInt32)Creates a file and returns a file descriptor, or returns -1 when the operation fails.Linux, Windows, macOS, and HarmonyOS
dup(Int32)Copies and returns the file descriptor specified by the original fd parameter.Linux, Windows, macOS, and HarmonyOS
dup2(Int32, Int32)Copies the file descriptor specified by oldfd and returns it to the newfd parameter.Linux, Windows, macOS, and HarmonyOS
faccessat(Int32, String, Int32, Int32)Checks whether the file specified by fd has certain permission. If yes, 0 is returned; otherwise, -1 is returned.Linux, macOS, and HarmonyOS
fchdir(Int32)Changes the current working directory of the calling process by specifying the descriptor of a file path.Linux, macOS, and HarmonyOS
fchmod(Int32, UInt32)Modifies the file access permission corresponding to the file descriptor.Linux, Windows, macOS, and HarmonyOS
fchmodat(Int32, String, UInt32, Int32)Modifies the file access permission corresponding to the file descriptor.Linux, Windows, macOS, and HarmonyOS
fchown(Int32, UInt32, UInt32)Modifies the file owner corresponding to fd and the group to which the file owner belongs.Linux, macOS, and HarmonyOS
fchownat(Int32, String, UInt32, UInt32, Int32)Modifies the file owner corresponding to the file descriptor and the group to which the file owner belongs.Linux, macOS, and HarmonyOS
getcwd()Obtains the absolute path of the working directory of the current process.Linux, Windows, macOS, and HarmonyOS
getgid()Obtains the user group ID.Linux, macOS, and HarmonyOS
getgroups(Int32, CPointer<UInt32>)Obtains the code of the group to which the current user belongs.Linux, macOS, and HarmonyOS
gethostname()Obtains the host name, which is usually the name of a host on the TCP/IP network.Linux, macOS, and HarmonyOS
getlogin()Obtains the current login name.Linux, macOS, and HarmonyOS
getos()Obtains the Linux system information from the file in /proc/version.Linux
getpgid(Int32)Obtains PGID of the process specified by pid. If pid is 0, ID of the calling process is returned.Linux, macOS, and HarmonyOS
getpgrp()Obtains ID of the parent process of the calling process.Linux, macOS, and HarmonyOS
getpid()Obtains ID(PID) of the calling process.Linux, Windows, macOS, and HarmonyOS
getppid()Obtains ID of the parent process of the calling process.Linux, macOS, and HarmonyOS
getuid()Obtains the real user ID of the calling process.Linux, macOS, and HarmonyOS
isBlk(String)Checks whether the input object is a block device and returns a Boolean value.Linux, Windows, macOS, and HarmonyOS
isChr(String)Checks whether the input object is a character device and returns a Boolean value.Linux, Windows, macOS, and HarmonyOS
isDir(String)Checks whether the input object is a folder and returns a Boolean value.Linux, Windows, macOS, and HarmonyOS
isFIFO(String)Checks whether the input object is a FIFO file and returns a Boolean value.Linux, macOS, and HarmonyOS
isLnk(String)Checks whether the input object is a soft link and returns a Boolean value.Linux, macOS, and HarmonyOS
isReg(String)Checks whether the input object is a regular file and returns a Boolean value.Linux, Windows, macOS, and HarmonyOS
isSock(String)Checks whether the input object is a socket file and returns a Boolean value.Linux, macOS, and HarmonyOS
isType(String, UInt32)Checks whether a file is in the specified mode.Linux, macOS, and HarmonyOS
isatty(Int32)Tests whether the file descriptor references the terminal. If the test is successful, true is returned; otherwise, false is returned.Linux, Windows, macOS, and HarmonyOS
kill(Int32, Int32)Sends any signal to any process group or process.Linux, macOS, and HarmonyOS
killpg(Int32, Int32)Sends sig to the process group pgrp. If pgrp is 0, killpg() sends the signal to the process group to which the calling process belongs.Linux, macOS, and HarmonyOS
lchown(String, UInt32, UInt32)Modifies the file link owner and the group to which the owner belongs.Linux and macOS
link(String, String)Creates a link for an existing file. A file can have multiple directory entries pointing to i-node.Linux, macOS, and HarmonyOS
linkat(Int32, String, Int32, String, Int32)Creates a file link relative to the directory file descriptor.Linux, macOS, and HarmonyOS
lseek(Int32, Int64, Int32)Increases the read or write location accordingly in file reading or writing.Linux, Windows, macOS, and HarmonyOS
nice(Int32)Changes the priority of the current thread.Linux, macOS, and HarmonyOS
open64(String, Int32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux and HarmonyOS
open64(String, Int32, UInt32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux and HarmonyOS
openat(Int32, String, Int32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, macOS, and HarmonyOS
openat(Int32, String, Int32, UInt32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, macOS, and HarmonyOS
openat64(Int32, String, Int32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, macOS, and HarmonyOS
openat64(Int32, String, Int32, UInt32)Opens a file and returns a new file descriptor, or returns -1 when the operation fails.Linux, macOS, and HarmonyOS
pread(Int32, CPointer<UInt8>, UIntNative, Int32)Transfers nbyte of the file specified by fd to the memory specified by buffer.Linux, macOS, and HarmonyOS
pwrite(Int32, CPointer<UInt8>, UIntNative, Int32)Writes nbyte in the memory specified by buffer to the file specified by fd, starting from the specified offset location.Linux, macOS, and HarmonyOS
read(Int32, CPointer<UInt8>, UIntNative)Transfers nbyte of the file specified by fd to the memory specified by buffer.Linux, Windows, macOS, and HarmonyOS
remove(String)Deletes a file or directory.Linux, Windows, macOS, and HarmonyOS
rename(String, String)Renames a file. If necessary, the directory where the file is located is moved.Linux, Windows, macOS, and HarmonyOS
renameat(Int32, String, Int32, String)Renames a file. If necessary, the directory where the file is located is moved.Linux, macOS, and HarmonyOS
setgid(UInt32)Sets the valid group ID of the calling process. Permissions are required to run this function.Linux and macOS
sethostname(String)Sets the host name. Only the super user can call this function.Linux and macOS
setpgid(Int32, Int32)Sets the group ID specified by pid to the group ID specified by pgrp.Linux, macOS, and HarmonyOS
setpgrp()Sets ID of the group to which the current process belongs to ID of the process of the current process. This function is equivalent to calling setpgid(0, 0).Linux, macOS, and HarmonyOS
setuid(UInt32)Sets the valid user ID of the calling process. Permissions are required to run this function.Linux and macOS
symlink(String, String)Creates a link named symPath to the file specified by path.Linux, macOS, and HarmonyOS
symlinkat(String, Int32, String)Creates a link named symPath to the file specified by path and fd.Linux, macOS, and HarmonyOS
ttyname(Int32)Returns the terminal name.Linux, Windows, macOS, and HarmonyOS
umask(UInt32)Sets the permission mask.Linux, Windows, macOS, and HarmonyOS
unlink(String)Deletes a file from the file system.Linux, macOS, and HarmonyOS
unlinkat(Int32, String, Int32)Deletes a file from the file system.Linux, macOS, and HarmonyOS
write(Int32, CPointer<UInt8>, UIntNative)Writes nbyte in the memory specified by buffer to the file specified by fd.Linux, Windows, macOS, and HarmonyOS

Constants

NameDescriptionSupported on
AT_EMPTY_PATHSpecifies the file descriptor returned when no file or directory is specified in the file system. This constant is applicable to the open, open64, openat, and openat64 functions, and belongs to the function parameter oflag.Linux, Windows, and HarmonyOS
AT_REMOVEDIRPerforms an operation equivalent to rmdir(2) on pathname if the AT_REMOVEDIR flag is specified. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_CLOEXECMandatory in some multi-thread programs. This is because when one thread opens the file descriptor and another thread executes fork(2) followed by execve(2), using a separate fcntl(2) F_SETFD operation to set the FD_CLOEXEC flag is not enough to prevent contention. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_DIRECTORYIf the file specified by pathname is not a directory, the file fails to be opened. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_CREATIf the file to be opened does not exist, automatically creates the file. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_DSYNCWaits for the physical I/O to complete upon each write operation. However, if the write operation does not affect reading of the data just written, it is unnecessary to wait for the file attribute to update. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_EXCLIf O_CREAT is also set, checks whether the file exists; if the file does not exist, creates the file. Otherwise, a file opening error is displayed. In addition, if both O_CREAT and O_EXCL are set and the file to be opened is a symbolic link, the file fails to be opened. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_NOCTTYIf the file to be opened is a terminal device, the file is not the control terminal of the process. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_NOFOLLOWIf the file specified by pathname is a single symbolic link, the file fails to be opened. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_NONBLOCKOpens a file in non-blocking mode. That is, the I/O operation does not cause the calling process to wait. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_SYNCOpens a file synchronously. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, macOS, and HarmonyOS
O_RDONLYOpens a file in read-only mode. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_RDWROpens a file in read/write mode. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_WRONLYOpens a file in write-only mode. This constant is applicable to the open, open64, openat, and openat64 functions, and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_APPENDMoves data from the end of a file in file reading or writing, that is, appends the data to be written to the end of the file. This constant is applicable to the open, open64, openat, and openat64 functions, and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
O_RSYNCThis flag affects only the read operation and needs to be used together with O_SYNC or O_DSYNC. If necessary, this flag blocks the read call until the data being read (and possibly metadata) is flushed to the disk. This constant is applicable to the open, open64, openat, and openat64 functions and belongs to the function parameter oflag.Linux and HarmonyOS
O_TRUNCIf the file exists and is writable, clears the file length to 0, making the data previously stored in the file disappear. This constant is applicable to the open, open64, openat, and openat64 functions, and belongs to the function parameter oflag.Linux, Windows, macOS, and HarmonyOS
R_OKTests the read permission on a file. This constant is applicable to the access and faccessat functions and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
W_OKTests the write permission on a file. This constant is applicable to the access and faccessat functions and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
X_OKTests the execute permission on a file. This constant is applicable to the access and faccessat functions and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
F_OKTests whether a file exists. This constant is applicable to the access and faccessat functions and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
SEEK_SETSpecifies the new read/write location. This constant is applicable to the lseek function and belongs to the function parameter whence.Linux, Windows, macOS, and HarmonyOS
SEEK_CURAdds an offset to the current read or write location. This constant is applicable to the lseek function and belongs to the function parameter whence.Linux, Windows, macOS, and HarmonyOS
SEEK_ENDSets the read/write location to the end of the file and adds an offset. This constant is applicable to the lseek function and belongs to the function parameter whence.Linux, Windows, macOS, and HarmonyOS
SIGABRTSpecifies an abnormal abortion. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGBUSSpecifies a hardware fault. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGFPESpecifies an arithmetic error. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGKILLSpecifies a kill signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGCONTContinues a stopped process. The default action is continue or ignore. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGHUPSpecifies that the connection is broken. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGINTSpecifies a terminal interrupt character. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGQUITSpecifies a terminal quit character. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGILLSpecifies that the hardware instruction is invalid. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGTRAPSpecifies a hardware fault. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGIOTSpecifies a hardware fault. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGIOSpecifies an asynchronous IO. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGPIPEWrites data to the pipe of an unread process. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGALRMSpecifies that the timer expires. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGPWRSpecifies power failure or restart. The system call is invalid. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, and HarmonyOS
SIGSEGVSpecifies invalid memory reference. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGSTOPSpecifies a stop signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGTERMSpecifies a terminate signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGSTKFLTSpecifies a coprocessor stack fault. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, and HarmonyOS
SIGCHLDSpecifies that the subprocess status is changed. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGTSTPSpecifies a terminal stop signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGTTINReads the control tty on the background. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGTTOUWrites the control tty on the background. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGURGSpecifies an emergency (socket). The default action is ignore. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGUSR1Specifies a user-defined signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGUSR2Specifies a user-defined signal. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGVTALRMSpecifies a virtual time alarm. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGPROFSpecifies digest timeout. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGWINCHChanges the terminal window size. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGXCPUSpecifies that CPU usage exceeds the upper limit. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
SIGXFSZSpecifies that the file length exceeds the upper limit. The default action is terminate. This constant is applicable to the kill and killpg functions and belongs to the function parameter sig.Linux, Windows, macOS, and HarmonyOS
S_IRUSRSpecifies that the file owner has read permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IWUSRSpecifies that the file owner has write permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IRGRPSpecifies that the file user group has read permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IWGRPSpecifies that the file user group has write permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IFREGSpecifies that the file type is regular. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFBLKSpecifies that the file type is block device. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFDIRSpecifies that the file type is directory. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFCHRSpecifies that the file type is character device. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFIFOSpecifies that the file type is FIFO. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFLNKSpecifies that the file type is soft link. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IFSOCKSpecifies that the file type is socket. This constant is applicable to the isType function and belongs to the function parameter mode.Linux, Windows, macOS, and HarmonyOS
S_IROTHSpecifies that other users have read permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IRWXGSpecifies that the file user group has read, write, and execute permissions on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IRWXUSpecifies that the file owner has read, write, and execute permissions on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IWOTHSpecifies that other users have write permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IXOTHSpecifies that other users have execute permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IRWXOSpecifies that other users have read, write, and execute permissions on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IXGRPSpecifies that the file user group has execute permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS
S_IXUSRSpecifies that the file owner has execute permission on the file. This constant is applicable to the open, open64, openat, openat64, chmod(mode), fchmod(mode), fchmodat(mode), and creat functions, and belongs to the function parameter flag.Linux, Windows, macOS, and HarmonyOS