MentOS  0.8.0
The Mentoring Operating System
Macros | Functions
unistd.h File Reference

Functions used to manage files. More...

Go to the source code of this file.

Macros

#define STDIN_FILENO   0
 Standard input file descriptor.
 
#define STDOUT_FILENO   1
 Standard output file descriptor.
 
#define STDERR_FILENO   2
 Standard error file descriptor.
 
#define stdin   STDIN_FILENO
 Standard input file descriptor.
 
#define stdout   STDOUT_FILENO
 Standard output file descriptor.
 
#define stderr   STDERR_FILENO
 Standard error file descriptor.
 

Functions

ssize_t read (int fd, void *buf, size_t nbytes)
 Read data from a file descriptor. More...
 
ssize_t write (int fd, const void *buf, size_t nbytes)
 Write data into a file descriptor. More...
 
int open (const char *pathname, int flags, mode_t mode)
 Opens the file specified by pathname. More...
 
int close (int fd)
 Close a file descriptor. More...
 
off_t lseek (int fd, off_t offset, int whence)
 Repositions the file offset inside a file. More...
 
int unlink (const char *path)
 Delete a name and possibly the file it refers to. More...
 
int symlink (const char *linkname, const char *path)
 Creates a symbolic link. More...
 
int readlink (const char *path, char *buffer, size_t bufsize)
 Read the symbolic link, if present. More...
 
void exit (int status)
 Wrapper for exit system call. More...
 
pid_t getpid (void)
 Returns the process ID (PID) of the calling process. More...
 
pid_t getsid (pid_t pid)
 Return session id of the given process. If pid == 0 return the SID of the calling process If pid != 0 return the SID corresponding to the process having identifier == pid. More...
 
pid_t setsid (void)
 creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session (i.e., its session ID is made the same as its process ID). The calling process also becomes the process group leader of a new process group in the session (i.e., its process group ID is made the same as its process ID). More...
 
pid_t getpgid (pid_t pid)
 returns the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used. More...
 
int setpgid (pid_t pid, pid_t pgid)
 Sets the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used. More...
 
gid_t getgid (void)
 returns the real group ID of the calling process. More...
 
gid_t getegid (void)
 returns the effective group ID of the calling process. More...
 
int setgid (gid_t gid)
 sets the group IDs of the calling process. More...
 
int setregid (gid_t rgid, gid_t egid)
 sets the real and effective group IDs of the calling process. More...
 
uid_t getuid (void)
 Returns the real User ID of the calling process. More...
 
uid_t geteuid (void)
 Returns the effective User ID of the calling process. More...
 
int setuid (uid_t uid)
 Sets the User IDs of the calling process. More...
 
int setreuid (uid_t ruid, uid_t euid)
 Sets the effective and real User IDs of the calling process. More...
 
pid_t getppid (void)
 Returns the parent process ID (PPID) of the calling process. More...
 
pid_t fork (void)
 Clone the calling process, but without copying the whole address space. The calling process is suspended until the new process exits or is replaced by a call to ‘execve’. More...
 
int execl (const char *path, const char *arg,...)
 Replaces the current process image with a new process image (argument list). More...
 
int execlp (const char *file, const char *arg,...)
 Replaces the current process image with a new process image (argument list). More...
 
int execle (const char *path, const char *arg,...)
 Replaces the current process image with a new process image (argument list). More...
 
int execlpe (const char *file, const char *arg,...)
 Replaces the current process image with a new process image (argument list). More...
 
int execv (const char *path, char *const argv[])
 Replaces the current process image with a new process image (argument vector). More...
 
int execvp (const char *file, char *const argv[])
 Replaces the current process image with a new process image (argument vector). More...
 
int execve (const char *path, char *const argv[], char *const envp[])
 Replaces the current process image with a new process image (argument vector), allows the caller to specify the environment of the executed program via envp. More...
 
int execvpe (const char *file, char *const argv[], char *const envp[])
 Replaces the current process image with a new process image (argument vector), allows the caller to specify the environment of the executed program via envp. More...
 
int nice (int inc)
 Adds inc to the nice value for the calling thread. More...
 
int reboot (int magic1, int magic2, unsigned int cmd, void *arg)
 Reboots the system, or enables/disables the reboot keystroke. More...
 
char * getcwd (char *buf, size_t size)
 Get current working directory. More...
 
int chdir (char const *path)
 Changes the current working directory to the given path. More...
 
int fchdir (int fd)
 Is identical to chdir(), the only difference is that the directory is given as an open file descriptor. More...
 
ssize_t getdents (int fd, dirent_t *dirp, unsigned int count)
 
int dup (int fd)
 Return a new file descriptor. More...
 
unsigned alarm (int seconds)
 Send signal to calling thread after desired seconds. More...
 
int chmod (const char *pathname, mode_t mode)
 Change the file's mode bits. More...
 
int fchmod (int fd, mode_t mode)
 Change the file's mode bits. More...
 
int chown (const char *pathname, uid_t owner, gid_t group)
 Change the owner and group of a file. More...
 
int fchown (int fd, uid_t owner, gid_t group)
 Change the owner and group of a file. More...
 
int lchown (const char *pathname, uid_t owner, gid_t group)
 Change the owner and group of a file. More...
 

Detailed Description

Functions used to manage files.

Function Documentation

◆ alarm()

unsigned alarm ( int  seconds)

Send signal to calling thread after desired seconds.

Parameters
secondsthe amount of seconds.
Returns
If there is a previous alarm() request with time remaining, alarm() shall return a non-zero value that is the number of seconds until the previous request would have generated a SIGALRM signal. Otherwise, alarm() shall return 0.

◆ chdir()

int chdir ( char const *  path)

Changes the current working directory to the given path.

Parameters
pathThe new current working directory.
Returns
0 on success, -1 on failure and errno is set to indicate the error.

◆ chmod()

int chmod ( const char *  pathname,
mode_t  mode 
)

Change the file's mode bits.

Parameters
pathnameThe pathname of the file to change mode.
modeThe mode bits to set.
Returns
On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ chown()

int chown ( const char *  pathname,
uid_t  owner,
gid_t  group 
)

Change the owner and group of a file.

Parameters
pathnameThe pathname of the file to change.
ownerThe new owner to set.
groupThe new group to set.
Returns
On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ close()

int close ( int  fd)

Close a file descriptor.

Parameters
fdThe file descriptor.
Returns
The result of the operation.

◆ dup()

int dup ( int  fd)

Return a new file descriptor.

Parameters
fdThe fd pointing to the opened file.
Returns
On success, a new file descriptor is returned. On error, -1 is returned, and errno is set appropriately.

◆ execl()

int execl ( const char *  path,
const char *  arg,
  ... 
)

Replaces the current process image with a new process image (argument list).

Parameters
pathThe absolute path to the binary file to execute.
argA list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
...The argument list.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execle()

int execle ( const char *  path,
const char *  arg,
  ... 
)

Replaces the current process image with a new process image (argument list).

Parameters
pathThe absolute path to the binary file to execute.
argA list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
...The argument list which contains as last argument the environment.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execlp()

int execlp ( const char *  file,
const char *  arg,
  ... 
)

Replaces the current process image with a new process image (argument list).

Parameters
fileThe name of the binary file to execute, which is searched inside the paths specified inside the PATH environmental variable.
argA list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
...The argument list.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execlpe()

int execlpe ( const char *  file,
const char *  arg,
  ... 
)

Replaces the current process image with a new process image (argument list).

Parameters
fileThe name of the binary file to execute, which is searched inside the paths specified inside the PATH environmental variable.
argA list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
...The argument list which contains as last argument the environment.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execv()

int execv ( const char *  path,
char *const  argv[] 
)

Replaces the current process image with a new process image (argument vector).

Parameters
pathThe absolute path to the binary file to execute.
argvA vector of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execve()

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

Replaces the current process image with a new process image (argument vector), allows the caller to specify the environment of the executed program via envp.

Parameters
pathThe absolute path to the binary file to execute.
argvA vector of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
envpA vector of one or more pointers to null-terminated strings that represent the environment list available to the executed program.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execvp()

int execvp ( const char *  file,
char *const  argv[] 
)

Replaces the current process image with a new process image (argument vector).

Parameters
fileThe name of the binary file to execute, which is searched inside the paths specified inside the PATH environmental variable.
argvA vector of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ execvpe()

int execvpe ( const char *  file,
char *const  argv[],
char *const  envp[] 
)

Replaces the current process image with a new process image (argument vector), allows the caller to specify the environment of the executed program via envp.

Parameters
fileThe name of the binary file to execute, which is searched inside the paths specified inside the PATH environmental variable.
argvA vector of one or more pointers to null-terminated strings that represent the argument list available to the executed program.
envpA vector of one or more pointers to null-terminated strings that represent the environment list available to the executed program.
Returns
Returns -1 only if an error has occurred, and sets errno.

◆ exit()

void exit ( int  status)

Wrapper for exit system call.

Parameters
statusThe exit status.

◆ fchdir()

int fchdir ( int  fd)

Is identical to chdir(), the only difference is that the directory is given as an open file descriptor.

Parameters
fdThe file descriptor of the open directory.
Returns
0 on success, -1 on failure and errno is set to indicate the error.

◆ fchmod()

int fchmod ( int  fd,
mode_t  mode 
)

Change the file's mode bits.

Parameters
fdThe fd pointing to the opened file.
modeThe mode bits to set.
Returns
On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ fchown()

int fchown ( int  fd,
uid_t  owner,
gid_t  group 
)

Change the owner and group of a file.

Parameters
fdThe fd pointing to the opened file.
ownerThe new owner to set.
groupThe new group to set.
Returns
On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ fork()

pid_t fork ( void  )

Clone the calling process, but without copying the whole address space. The calling process is suspended until the new process exits or is replaced by a call to ‘execve’.

Returns
Return -1 for errors, 0 to the new process, and the process ID of the new process to the old process.

◆ getcwd()

char* getcwd ( char *  buf,
size_t  size 
)

Get current working directory.

Parameters
bufThe array where the CWD will be copied.
sizeThe size of the array.
Returns
On success, returns the same pointer to buf. On failure, returnr NULL, and errno is set to indicate the error.

◆ getdents()

ssize_t getdents ( int  fd,
dirent_t dirp,
unsigned int  count 
)

Provide access to the directory entries.

Parameters
fdThe fd pointing to the opened directory.
dirpThe buffer where de data should be placed.
countThe size of the buffer.
Returns
On success, the number of bytes read is returned. On end of directory, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ getegid()

gid_t getegid ( void  )

returns the effective group ID of the calling process.

Returns
GID of the current process

◆ geteuid()

uid_t geteuid ( void  )

Returns the effective User ID of the calling process.

Returns
User ID of the current process.

◆ getgid()

gid_t getgid ( void  )

returns the real group ID of the calling process.

Returns
GID of the current process

◆ getpgid()

pid_t getpgid ( pid_t  pid)

returns the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used.

Parameters
pidprocess of which we want to know the PGID.
Returns
the PGID of the specified process.

◆ getpid()

pid_t getpid ( void  )

Returns the process ID (PID) of the calling process.

Returns
pid_t process identifier.

◆ getppid()

pid_t getppid ( void  )

Returns the parent process ID (PPID) of the calling process.

Returns
pid_t parent process identifier.

◆ getsid()

pid_t getsid ( pid_t  pid)

Return session id of the given process. If pid == 0 return the SID of the calling process If pid != 0 return the SID corresponding to the process having identifier == pid.

Parameters
pidprocess identifier from wich we want the SID
Returns
On success return SID of the session Otherwise return -1 with errno set on: EPERM or ESRCH

◆ getuid()

uid_t getuid ( void  )

Returns the real User ID of the calling process.

Returns
User ID of the current process.

◆ lchown()

int lchown ( const char *  pathname,
uid_t  owner,
gid_t  group 
)

Change the owner and group of a file.

Parameters
pathnameThe pathname of the file to change.
ownerThe new owner to set.
groupThe new group to set.
Returns
On success, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ lseek()

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

Repositions the file offset inside a file.

Parameters
fdThe file descriptor of the file.
offsetThe offest to use for the operation.
whenceThe type of operation.
Returns
Upon successful completion, returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.

◆ nice()

int nice ( int  inc)

Adds inc to the nice value for the calling thread.

Parameters
incThe value to add to the nice.
Returns
On success, the new nice value is returned. On error, -1 is returned, and errno is set appropriately.

◆ open()

int open ( const char *  pathname,
int  flags,
mode_t  mode 
)

Opens the file specified by pathname.

Parameters
pathnameA pathname for a file.
flagsfile status flags and file access modes of the open file description.
modethe file mode bits be applied when a new file is created.
Returns
file descriptor number, -1 otherwise and errno is set to indicate the error.

◆ read()

ssize_t read ( int  fd,
void *  buf,
size_t  nbytes 
)

Read data from a file descriptor.

Parameters
fdThe file descriptor.
bufThe buffer.
nbytesThe number of bytes to read.
Returns
The number of read characters.

◆ readlink()

int readlink ( const char *  path,
char *  buffer,
size_t  bufsize 
)

Read the symbolic link, if present.

Parameters
paththe file for which we want to read the symbolic link information.
bufferthe buffer where we will store the symbolic link path.
bufsizethe size of the buffer.
Returns
The number of read characters on success, -1 otherwise and errno is set to indicate the error.

◆ reboot()

int reboot ( int  magic1,
int  magic2,
unsigned int  cmd,
void *  arg 
)

Reboots the system, or enables/disables the reboot keystroke.

Parameters
magic1fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC1.
magic2fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC2.
cmdThe command to send to the reboot.
argArgument passed with some specific commands.
Returns
For the values of cmd that stop or restart the system, a successful call to reboot() does not return. For the other cmd values, zero is returned on success. In all cases, -1 is returned on failure, and errno is set appropriately.

◆ setgid()

int setgid ( gid_t  gid)

sets the group IDs of the calling process.

Parameters
gidthe Group ID to set
Returns
On success, zero is returned. Otherwise returns -1 with errno set to :EINVAL or EPERM

◆ setpgid()

int setpgid ( pid_t  pid,
pid_t  pgid 
)

Sets the Process Group ID (PGID) of the process specified by pid. If pid is zero, the process ID of the calling process is used.

Parameters
pidprocess of which we want to set the PGID.
pgidthe PGID we want to set.
Returns
returns zero. On error, -1 is returned, and errno is set appropriately.

◆ setregid()

int setregid ( gid_t  rgid,
gid_t  egid 
)

sets the real and effective group IDs of the calling process.

Parameters
rgidthe new real Group ID.
egidthe effective real Group ID.
Returns
On success, zero is returned. Otherwise returns -1 with errno set EPERM

◆ setreuid()

int setreuid ( uid_t  ruid,
uid_t  euid 
)

Sets the effective and real User IDs of the calling process.

Parameters
ruidthe new real User ID.
euidthe effective real User ID.
Returns
On success, zero is returned. Otherwise returns -1 with errno set to EPERM

◆ setsid()

pid_t setsid ( void  )

creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session (i.e., its session ID is made the same as its process ID). The calling process also becomes the process group leader of a new process group in the session (i.e., its process group ID is made the same as its process ID).

Returns
On success return SID of the session just created Otherwise return -1 with errno : EPERM

◆ setuid()

int setuid ( uid_t  uid)

Sets the User IDs of the calling process.

Parameters
uidthe new User ID.
Returns
On success, zero is returned. Otherwise returns -1 with errno set to :EINVAL or EPERM

◆ symlink()

int symlink ( const char *  linkname,
const char *  path 
)

Creates a symbolic link.

Parameters
linknamethe name of the link.
paththe entity it is linking to.
Returns
0 on success, a negative number if fails and errno is set.

◆ unlink()

int unlink ( const char *  path)

Delete a name and possibly the file it refers to.

Parameters
pathThe path to the file.
Returns
0 on success, -errno on failure.

◆ write()

ssize_t write ( int  fd,
const void *  buf,
size_t  nbytes 
)

Write data into a file descriptor.

Parameters
fdThe file descriptor.
bufThe buffer collecting data to written.
nbytesThe number of bytes to write.
Returns
The number of written bytes.