👨‍💻
Guide
Laura's GithubSimon's Github
  • What is this gitbook for?
  • 🛠️Useful tools
    • 🏁Header files
    • 🧱C Structures
    • 🔗Linked Lists (todo)
    • 📄Makefiles
    • 🔄Switch statement
    • 🗃️File descriptors (FD)
  • 🖌️MiniLibX
    • MiniLibX Helper Function
    • MiniLibX Hook Examples
  • 0️⃣ Rank 00
    • Libft
      • 📑LIBC functions
        • ft_isalpha
        • ft_isdigit
        • ft_isalnum
        • ft_isascii
        • ft_isprint
        • ft_strlen
        • ft_memset
        • ft_bzero
        • ft_memcpy
        • ft_memmove
        • ft_strlcpy
        • ft_strlcat
        • ft_toupper
        • ft_tolower
        • ft_strchr
        • ft_strrchr
        • ft_strncmp
        • ft_memchr
        • ft_memcmp
        • ft_strnstr
        • ft_atoi
        • ft_calloc
        • ft_strdup
      • 📑Additional functions
        • ft_substr
        • ft_strjoin
        • ft_strtrim
        • ft_split
        • ft_itoa
        • ft_strmapi
        • ft_striteri
        • ft_putchar_fd
        • ft_putstr_fd
        • ft_putendl_fd
        • ft_putnbr_fd
      • 📑Bonus functions
        • ft_lstnew
        • ft_lstadd_front
        • ft_lstsize
        • ft_lstlast
        • ft_lstadd_back
        • ft_lstdelone
        • ft_lstclear
        • ft_lstiter
        • ft_lstmap
  • 1️⃣ Rank 01
    • Born2beRoot
      • 📠What's a virtual machine ?
      • 📠Install your virtual machine
      • 📠P2P Evaluation - Questions
    • ft_printf
      • ▪️Variadic functions
      • ▪️Building the thing
    • get_next_line
      • ▪️open() & read()
      • ▪️Static variables
      • ▪️Building the thing
      • ▪️Commented solution
  • 2️⃣ Rank 02
    • so_long
      • ▪️Understand so_long
      • ▪️Core concepts
      • ▪️Building the thing
    • pipex
      • ▪️Understand pipex
      • ▪️Functions used
      • ▪️Building the thing
    • minitalk
      • ▪️Understand minitalk
      • ▪️Functions used
      • ▪️Building the thing
    • push_swap
      • ▪️Algorithms
      • ◾Building the thing
    • FdF
      • 🗡️Understand FdF
      • 🗡️Graphics programming
      • 🗡️Building the thing
  • 3️⃣ RANK 03
    • Philosophers
      • ▪️Understand Philosophers
      • ▪️Functions used
      • ▪️Building the thing
    • Minishell
      • ▪️Understand Minishell
      • ▪️Functions
      • ◾Building the thing
  • 4️⃣ RANK 04
    • CPP (00 - 04) (doing)
      • CPP00
      • CPP01
      • CPP02
      • CPP03
      • CPP04 (doing)
    • NetPractice
      • Theory
      • Level 1 & 2
    • MiniRT
      • Understand MiniRT
      • Building the thing
  • 5️⃣ RANK 05
    • CPP (05-09) (to-do)
      • CPP05
      • CPP06 (to-do)
      • CPP07
      • CPP08 (to-do)
      • CPP09 (to-do)
    • Inception (doing)
      • 🕓The basics (Docker, Images, etc...)
      • Project Files
    • webserv (to-do)
  • 6️⃣ rank 06
    • ft_transcendence (to-do)
  • 🛂Exams
    • Exam Rank 02
      • Level 1
        • first_word
        • fizz_buzz
        • *ft_putstr
        • *ft_strcpy
        • *ft_strlen
        • ft_swap
        • repeat_alpha
        • rev_print
        • rot_13
        • rotone
        • search_and_replace
        • ulstr
      • Level 2
        • alpha_mirror
        • camel_to_snake
        • do_op
        • *ft_atoi
        • *ft_strcmp
        • ft_strcspn
        • ft_strspn
        • *ft_strdup
        • ft_strpbrk
        • ft_strrev
        • inter
        • last_word
        • ft_is_power_2
        • max
        • print_bits
        • reverse_bits
        • wdmatch
        • swap_bits
        • union
        • snake_to_camel
      • Level 3
        • add_prime_sum
        • epur_str
        • expand_str
        • ft_atoi_base
        • ft_list_size
        • ft_range
        • ft_rrange
        • hidenp
        • lcm
        • paramsum
        • pgcd
        • print_hex
        • rstr_capitalizer
        • str_capitalizer
        • tab_mult
      • Level 4
        • flood_fil
        • fprime
        • ft_split
        • ft_itoa
        • ft_list_foreach
        • ft_list_remove
        • rev_wstr
        • rotstring
        • sort_int_tab
        • sort_list
    • Exam Rank 03
    • Exam Rank 04
    • Exam Rank 05
      • Module 0
  • 👥Team
Powered by GitBook
On this page
  • readline()
  • rl_clear_history()
  • rl_on_new_line()
  • rl_replace_line()
  • rl_redisplay()
  • add_history()
  • getcwd()
  • chdir()
  • stat() & lstat() & fstat()
  • opendir()
  • readdir()
  • closedir()
  • strerror()
  • perror()
  • isatty()
  • ttyname()
  • ttyslot()
  • ioctl()
  • getenv()
  • tcsetattr()
  • tcgetattr()
  • tgetent()

Was this helpful?

  1. 3️⃣ RANK 03
  2. Minishell

Functions

PreviousUnderstand MinishellNextBuilding the thing

Last updated 1 year ago

Was this helpful?

We can use some function that we already know (like printf, malloc, free, etc) so I will not describe them here.

You will probably not use all of these functions but at least you have somewhere where you can easily find links to the manual pages. And for some, an example on how to use them.

readline()

char *readline (const char *prompt);

The readline() function reads a line from the terminal and returns it, using prompt as a prompt. If no prompt is given as parameter, no prompt will be shown in the terminal. The line returned is allocated with malloc and we have to free it ourselves.

readline()
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

int main(void)
{
    char *rl;
    rl = readline("Prompt > ");
    printf("%s\n", rl);
    return (0);
}

Compiling this programm and running will result in the following.

$> ./minishell
Prompt > Hi ! How are you ?
Hi ! How are you ?
$>

You can find more information about readline() .

rl_clear_history()

void rl_clear_history(void);

The rl_clear_line() function clears the history list by deleting all of the entries. The rl_clear_line() function frees data that the readline library saves in the histroy list.

rl_on_new_line()

int rl_on_new_line(void);

The rl_on_new_line() function tells the update routine that we have moved onto a new empty line, usually used after outputting a line.

rl_replace_line()

I didn't find any information on that function.

rl_redisplay()

int rl_redisplay(void);

The rl_redisplay() change what's displayed on the screen to reflect the current contents of rl_line_buffer.

add_history()

void add_history(char *s);

The add_history() function saves the line passed as parameter in the history so it can be retrieved later in the terminal (like pressing the up arrow in bash).

getcwd()

#include <unistd.h>
char *getcwd(char *buf, size_t size);

The getcwd() returns a null-terminated string containing the absolute pathname that is the current working directory of the calling process. The pathname is returned as the function result and via the argument buf.

getcwd() example
#include <unistd.h>
#include <stdio.h> // for printf

int main(void)
{
    char *pwd;
    
    pwd = getcwd(NULL, 0);
    printf("pwd: %s\n", pwd);
    return (0);
}
$> pwd: /Users/saeby/Documents/tmp

chdir()

#include <unistd.h>
int chdir(const char *path);

chdir() changes the current working directory of the calling process to the directory specified in path.

chdir() example
#include <unistd.h>
#include <stdio.h> // for printf

int main(void)
{
    char *pwd;
    
    pwd = getcwd(NULL, 0);
    printf("pwd before chdir: %s\n", pwd);
    chdir("/Users/saeby/Documents/42/minishell");
    pwd = getcwd(NULL, 0);
    printf("pwd after chdir: %s\n", pwd);
    return (0);
}
$> pwd before chdir: /Users/saeby/Documents/tmp
$> pwd after chdir: /Users/saeby/Documents/42/minishell

stat() & lstat() & fstat()

#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict statbuf);
int lstat(const char *restrict pathname, struct stat *restrict statbuf);
int fstat(int fd, struct stat *statbuf);

These functions return information about a file in the structure pointed to by statbuf.

opendir()

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

The opendir() function opens a directory stream corresponding to the directory name, and returns a pointer to the directory stream. The stream is positioned at the first entry in the directory.

readdir()

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occured.

closedir()

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);

The closedir() function closes the directory stream associated with dirp. A successful call to closedir() also closes the underlying file descriptor associated with dirp. The directory stream descriptor dirp is not available after this call.

strerror()

#include <string.h>
char *strerror(int errnum);

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum. This string must not be modified by the application, but may be modified by a subsequent call to strerror() or strerror_l(). No other library function, including perror(), will modify this string.

perror()

#include <stdio.h>
void perror(const char *s);

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

isatty()

#include <unistd.h>
int isatty(int fd);

The isatty function tests wether fd is a terminal.

ttyname()

#include <unistd.h>
char **ttyname(int fd);

The ttyname() function returns a pointer to the null-terminated pathname of the terminal device that is open on the file descriptor fd, or NULL on error.

ttyslot()

#include <unistd.h>
int ttyslot(void);

ioctl()

#include <sys/ioctl.h>
int ioctl(int fd, unsigned long request, ...);

getenv()

#include <stdlib.h>
char *getenv(const char *name);

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

tcsetattr()

#include <termios.h>
int tcsetattr(int fildes, int optional_actions, const struct *termios_p);

tcgetattr()

#include <termios.h>
int tcgetattr(int fildes, struct termios *termios_p);

The tcgetattr() function shall get the parameters associated with with the terminal reffered to by fildes and store them in the termios structure referenced by termios_p.

tgetent()

#include <curses.h>
#include <term.h>
int tgetent(char *bp, const char *name);
int tgetflag(char *id);
int tgetnum(char *id);
char *tgetstr(char *id, char **area);
char *tgoto(const char *cap, int col, int row);
int tputs(const char *str, int affcnt, int (*putc)(int));

You can find more information about getcwd() .

You can find more information about chdir() .

You can find more detailed information about these functions .

You can find more information about the opendir function .

You can find more information about readdir .

You can find more information about closedir .

You can find more information about strerror .

You can find more information about perror .

You can find more information about isatty .

You can find more information about ttyname() .

This is a legacy function with some backstory, you can read all about it and how it works .

The ioctl() system call manipulates the underlying device parameters of a special files. You can find more detailed information .

You can find more information about getenv() .

The tcsetattr() function shall set the parameters associated with the terminal referred to by the open file descriptor fildes from the termios structure referenced by termios_p as described .

You can find more detailed information .

These routines are included as a conversion aid for programs that use the termcap library. You can find more information about all of them .

▪️
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here
here