Skip to content

4. Functions used

Ilia Munaev edited this page May 21, 2025 · 2 revisions

Functions

We use functions from a custom standard C library 'libft' with listed below.

Terminal input management

  • readline
char *readline (const char *prompt);

The function reads a line from the terminal and return it, using prompt as a prompt.

  • rl_clear_history
void rl_clear_history (void);

The function clears the history list by deleting all of the entries, in the same manner as the History library’s clear_history() function.

  • rl_replace_line
void rl_replace_line (const char *text, int clear_undo);

The function replaces the contents of rl_line_buffer with text.

  • rl_redisplay
void rl_redisplay (void);

The function changes what’s displayed on the screen to reflect the current contents of rl_line_buffer.

  • add_history
void add_history (const char *string);

The function places string at the end of the history list.

Standard I/O

  • printf
int printf(const char *format, ...);

The function produces output according to a format; writes output to stdout, the standard output stream.

  • write
ssize_t write(int fd, const void *buf, size_t nbyte);

The function writes nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fd.

File Handling

  • open
int open(const char *path, int oflag, ... );

The function creates an open file description that refers to a file and a file descriptor that refers to that open file description.

  • read
ssize_t read(int fd, void *buf, size_t nbyte);

The function reads up to count bytes from file descriptor fd into the buffer starting at buf.

  • close
int close(int fd);

The function closes a file descriptor fd, so that it no longer refers to any file and may be reused.

  • access
int access(const char *pathname, int mode);

The function checks whether the calling process can access the file pathname.

Directory & Path Management

  • getcwd
char *getcwd(char *buf, size_t size);

The function copies an absolute pathname of the current working directory to the array pointed to by buf which is of length size.

  • chdir
int chdir(const char *path);

The function changes the current working directory of the calling process to the directory specified in path.

  • opendir
DIR *opendir(const char *name);

The function function opens a directory stream corresponding to the directory name, and returns a pointer to the directory stream.

Process Management

  • fork
pid_t fork(void);

The function creates a new process by duplicating the calling process.

  • waitpid
pid_t waitpid(pid_t pid, int *wstatus, int options);

The system call suspends execution of the calling thread until a child specified by pid argument has changed state.

  • execve
 int execve(const char *pathname, char *const argv[], char *const envp[]);

The function executes the program referred to by pathname. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

  • exit
void exit(int status);

The function causes normal process termination and the least significant byte of status (i.e., status & 0xFF) is returned to the parent.

Signal Handling

  • signal
sighandler_t signal(int signum, sighandler_t handler);

Avoid its use: use sigaction(2) instead.

The function sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the address of a programmer defined function (a "signal handler").

  • sigaction
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);

The system call is used to change the action taken by a process on receipt of a specific signal.

  • sigemptyset
int sigemptyset(sigset_t *set);

The function initializes the signal set given by set to empty with all signals excluded from the set.

Pipe & Terminal Interaction

  • pipe
struct fd_pair pipe();

The funcrion creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe.

pipefd[0] refers to the read end of the pipe.

pipefd[1] refers to the write end of the pipe.

Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe.

  • dup
int dup(int oldfd);

The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor.

  • dup2
int dup2(int oldfd, int newfd);

The dup2() system call performs the same task as dup(), but instead of using the lowest-numbered unused file descriptor, it uses the file descriptor number specified in newfd. If the file descriptor newfd was previously open, it is silently closed before being reused.

  • isatty
int isatty(int fd);

The function tests whether fd is an open file descriptor referring to a terminal.

Environment Variables

  • getenv
char *getenv(const char *name);

The function searches the environment list to find the environment variable name, and returns a pointer to the corresponding value string.

Terminal Settings & Formatting

  • tcsetattr
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);

The function sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from the termios structure referred to by termios_p.

  • tcgetattr
int tcgetattr(int fd, struct termios *termios_p);

The function gets the parameters associated with the object referred by fd and stores them in the termios structure referenced by termios_p.

Memory managment

  • malloc
void *malloc(size_t size);

The function allocates size bytes and returns a pointer to the allocated memory.

  • free
void free(void *ptr);

The function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation.

Error output

  • strerror
char *strerror(int errnum);

The function returns a pointer to a string that describes the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language.

  • perror
void perror(const char *s);

The function produces a message on standard error describing the last error encountered during a call to a system or library function.