Skip to main content

Unix Standard (unistd)

Functions#

Name
intclose(int fildes)
int_execve(const char path, char const argv[], char *const envp[])
void_exit(int __status)
intfcntl(int fildes, int cmd, ... )
intfstat(int fildes, struct stat * buf)
pid_tgetpid()
pid_tgetppid()
intisatty(int fildes)
intlink(const char old, const char new)
off_tlseek(int fildes, off_t offset, int whence)
intopen(const char * name, int flags, ... )
intread(int fildes, void * buf, size_t nbyte)
intrename(const char old, const char new)
intstat(const char path, struct stat buf)
intsymlink(const char old, const char new)
intunlink(const char * name)
intwrite(int fildes, const void * buf, size_t nbyte)
intaccess(const char * path, int amode)
intchmod(const char * path, mode_t mode)
intchown(const char * path, uid_t uid, gid_t gid)
voidencrypt(char block[64], int edflag)
char *crypt(const char key, const char salt)
voidsetkey(const char * key)
intfsync(int fildes)
intioctl(int fildes, int request, ... )
intlstat(const char path, struct stat buf)
intmkdir(const char * path, mode_t mode)
intrmdir(const char * path)
unsigned intsleep(unsigned int seconds)
voidsvcall_update_task_root(void * args)
uid_tgeteuid()
uid_tgetuid()
intseteuid(uid_t uid)
intsetuid(uid_t uid)
gid_tgetgid()
gid_tgetegid()
intusleep(useconds_t useconds)

Functions Documentation#

close#

int close(    int fildes)

Parameters:

  • fildes The File descriptor fildes.

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • EBADF: Invalid file descriptor

This function closes the file associated with the specified descriptor.

_execve#

int _execve(    const char * path,    char *const argv[],    char *const envp[])

_exit#

void _exit(    int __status)

Return: This function never returns

Note: In this version, named semaphores are not closed in this function.

This function causes the calling process to exit with the specified exit code.

fcntl#

int fcntl(    int fildes,    int cmd,    ... )

Parameters:

  • fildes The file descriptor
  • cmd The operation to perform

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • EBADF: invalid file descriptor
  • ENOTSUP: cmd is not supported for the file descriptor

This function performs various operations on open files such as:

  • F_DUPFD: duplicate a file descriptor
  • F_GETFD: get the file descriptor flags
  • F_SETFD: set the file descriptor flags
  • F_GETOWN: get the file descriptor owner process ID.

fstat#

int fstat(    int fildes,    struct stat * buf)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • EBADF: fildes is invalid
  • EINVAL: buf is NULL

This function gets various file statistics for the specified file descriptor.

getpid#

pid_t getpid()

Return: The process ID of the caller.

This function returns the process ID of the calling process.

getppid#

pid_t getppid()

Return: The process ID of the caller's parent process.

This function returns the process ID of the parent process.

isatty#

int isatty(    int fildes)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENOTTY: fildes is not associated with a terminal device
  • EBADF: fildes is invalid

This function checks to see if fildes is associated with a terminal device.

link#

int link(    const char * old,    const char * new)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENOTSUP: operation not supported

This function creates a hard link between old and new.

lseek#

off_t lseek(    int fildes,    off_t offset,    int whence)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • EBADF: fildes is invalid
  • EINVAL: whence is invalid

This function sets the file offset for fildes using the following values of whence:

  • SEEKSET: set the offset to _offset
  • SEEKCUR: set the offset to current location plus _offset
  • SEEKEND: set the offset to the size of the file plus _offset

open#

int open(    const char * name,    int flags,    ... )

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENAMETOOLONG: name exceeds PATHMAX or a component of _name exceeds NAME_MAX
  • ENOENT: name could not be found
  • EIO: IO error
  • EMFILE: Too many files are already open
  • EEXIST: name already exists and flags is not set to overwrite
  • ENOMEM: not enough memory to open another file
  • ENOTDIR: the path to name does not exist
  • EFBIG: size error with the file (file is likely corrupt)

This function opens a file with flags being the OR'd combination of:

  • O_RDONLY, O_WRONLY or O_RDWR

  • O_NONBLOCK, O_CREAT, O_EXCL, O_TRUNC If the O_CREAT flag is set, the third argument should specify the mode as a mode_t. The bits used with the mode are:

  • S_IRWXU: User read/write/execute

  • S_IRUSR: User read

  • S_IWUSR: User write

  • S_IXUSR: User execute

  • S_IRWXG: Group read/write/execute

  • S_IRGRP: Group read (groups not implemented)

  • S_IWGRP: Group write (groups not implemented)

  • S_IXGRP: Group execute (groups not implemented)

  • S_IRWXO: Other read/write/execute

  • S_IROTH: Other read

  • S_IWOTH: Other write

  • S_IXOTH: Other execute

read#

int read(    int fildes,    void * buf,    size_t nbyte)

Parameters:

  • fildes The file descriptor returned by open()
  • buf A pointer to the destination memory (process must have write access)
  • nbyte The number of bytes to read

Return: The number of bytes actually read of -1 with errno (see Errno) set to:

  • EBADF: fildes is bad
  • EACCESS: fildes is on in O_WRONLY mode
  • EIO: IO error
  • EAGAIN: ONONBLOCK is set for _fildes and no new data is available

This function reads nbyte bytes from fildes to the memory location pointed to by buf.

read() is always a synchronous call which is either blocking or non-blocking depending on the value of ONONBLOCK for _fildes.

rename#

int rename(    const char * old,    const char * new)

Return: Zero on success or -1 with errno set to:

  • EEXIST: new already exists
  • EIO: IO error
  • ENOENT: old does not exist
  • EACCESS: old or new cannot be written

This functions renames old to new.

stat#

int stat(    const char * path,    struct stat * buf)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENAMETOOLONG: path exceeds PATHMAX or a component of _path exceeds NAME_MAX
  • ENOENT: path does not exist
  • EACCES: search permission is denied for a component of path

This function gets various file statistics for a given file name.

symlink#

int symlink(    const char * old,    const char * new)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENOTSUP: operation not supported

This function creates a hard link between old and new.

unlink#

int unlink(    const char * name)

Deletes a file or directory from the filesystem.

write#

int write(    int fildes,    const void * buf,    size_t nbyte)

Parameters:

  • fildes The file descriptor returned by open()
  • buf A pointer to the destination memory (process must have write access)
  • nbyte The number of bytes to read

Return: The number of bytes actually read of -1 with errno (see Errno) set to:

  • EBADF: fildes is bad
  • EACCES: fildes is on in O_RDONLY mode
  • EIO: IO error
  • EAGAIN: ONONBLOCK is set for _fildes and the device is busy

This function writes nbyte bytes fildes from the memory location pointed to by buf.

write() is always a synchronous call which is either blocking or non-blocking depending on the value of ONONBLOCK for _fildes.

access#

int access(    const char * path,    int amode)

Return: Zero on success (ie amode is allowed) or -1 with errno (see Errno) set to:

  • ENAMETOOLONG: path exceeds PATHMAX or a component of _path exceeds NAME_MAX
  • ENOENT: path does not exist
  • EACCES: amode is not allowed for path or search permission is denied for a component of path

This function checks to see if the specified access (amode) is allowed for path.

chmod#

int chmod(    const char * path,    mode_t mode)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EIO: IO Error
  • ENAMETOOLONG: path exceeds PATHMAX or a component of _path exceeds NAME_MAX
  • ENOENT: path does not exist
  • EACCES: search permission is denied for a component of path

This function changes the mode of the specified file or directory.

chown#

int chown(    const char * path,    uid_t uid,    gid_t gid)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EIO: IO Error
  • ENAMETOOLONG: path exceeds PATHMAX or a component of _path exceeds NAME_MAX
  • ENOENT: path does not exist
  • EACCES: search permission is denied for a component of path

This function changes the mode of the specified file or directory.

encrypt#

void encrypt(    char block[64],    int edflag)

Return: No return value but errno may be set to:

  • ENOSYS: function not supported on this platform

Encrypts a block of data.

crypt#

char * crypt(    const char * key,    const char * salt)

setkey#

void setkey(    const char * key)

fsync#

int fsync(    int fildes)

Parameters:

  • fildes The file descriptor returned by open()

Return: The number of bytes actually read of -1 with errno (see Errno) set to:

  • EBADF: fildes is bad
  • EIO: IO error
  • EAGAIN: ONONBLOCK is set for _fildes and the device is busy

This function performs a control request on the device associated with fildes. request is specific to the device. The value of request determines what value should be passed as the ctl argument.

ioctl#

int ioctl(    int fildes,    int request,    ... )

Parameters:

  • fildes The file descriptor returned by open()
  • request The request to the device.

Return: The number of bytes actually read of -1 with errno (see Errno) set to:

  • EBADF: fildes is bad
  • EIO: IO error
  • EAGAIN: ONONBLOCK is set for _fildes and the device is busy

This function performs a control request on the device associated with fildes. request is specific to the device. The value of request determines what value should be passed as the ctl argument.

lstat#

int lstat(    const char * path,    struct stat * buf)

Return: Zero on success or -1 on error with errno (see Errno) set to:

  • ENAMETOOLONG: path exceeds PATHMAX or a component of _path exceeds NAME_MAX
  • ENOENT: path does not exist
  • EACCES: search permission is denied for a component of path

This function is equivalent to stat() except path refers to a symbolic link.

mkdir#

int mkdir(    const char * path,    mode_t mode)

Parameters:

  • path Path to the new directory
  • mode Ignored

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENOENT: path is an empty string or the parent directory cannot be found
  • EEXIST: path already exists
  • ENOSPC: Not enough space on the disk to add a new directory

This function creates a new directory.

rmdir#

int rmdir(    const char * path)

Return: Zero on success or -1 with errno (see Errno) set to:

  • ENOENT: path is an empty string or the parent directory cannot be found
  • EEXIST: path already exists
  • ENOTDIR: path is not a directory
  • ENOTEMPTY: path is not an empty directory

This function removes the directory specified by path.

sleep#

unsigned int sleep(    unsigned int seconds)

Return: 0

This function causes the calling thread to sleep for seconds seconds.

svcall_update_task_root#

static void svcall_update_task_root(    void * args)

geteuid#

uid_t geteuid()

Return: 0 for root and 1 for user

gets the effective user id.

The effectictive user id is not always the same as the user id. If the caller is authenticated, the user id will always be root and the effective user id can switch between root and user based on the usage [setuid()](./group__unistd#function-setuid).

getuid#

uid_t getuid()

Return: 0 for root and 1 for user

gets the user id.

If the process is authenticated, the user id is root (0) otherwise, the user id is user (1).

seteuid#

int seteuid(    uid_t uid)

Parameters:

  • uid The user id to set. Must be either SYSFS_USER or SYSFS_ROOT

Return: 0 on success or -1 with the errno set to:

  • EPERM: process is not authenticated
  • EINVAL: uid is not SYSFS_USER or SYSFS_ROOT

sets the effective user id.

When the effective

setuid#

int setuid(    uid_t uid)

functions exactly like [seteuid()](./group__unistd#function-seteuid)

getgid#

gid_t getgid()

getegid#

gid_t getegid()

usleep#

int usleep(    useconds_t useconds)

Return: 0 or -1 for an error with errno (see Errno) set to:

  • EINVAL: useconds is greater than 1 million.

Causes the calling thread to sleep for useconds microseconds.

If useconds is greater than a threshold, the calling thread will yield the processor.


Updated on 18 September 2021 at 21:44:10 MDT