maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
builtins.h File Reference
#include "shell.h"
Include dependency graph for builtins.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int builtin_cd (t_ms_data *data)
 
int builtin_echo (t_ms_data *data)
 
int builtin_env (t_ms_data *data)
 
int builtin_exit (t_ms_data *data)
 
int builtin_export (t_ms_data *data)
 
int builtin_pwd (t_ms_data *data)
 
int builtin_unset (t_ms_data *data)
 
void free_signal_context (void)
 

Function Documentation

◆ builtin_cd()

int builtin_cd ( t_ms_data data)

Definition at line 28 of file cd.c.

29{
30 char *target_dir;
31 char *home_dir;
32 char *error_message;
33 char cwd[4096];
34
35 home_dir = get_env(data->envp, "HOME");
36 target_dir = (char *)data->args[1];
37 if (!target_dir)
38 target_dir = home_dir;
39 if (chdir(target_dir) == -1)
40 {
41 error_message = ft_strjoin("cd: ", target_dir);
42 if (errno == EACCES)
43 exit_status_handler(data, PERMISSION_DENIED, error_message);
44 if (errno == ENOENT)
45 exit_status_handler(data, IS_DIRECTORY, error_message);
46 free(error_message);
47 return (EXIT_FAILURE);
48 }
49 set_env(&data->envp, "OLDPWD", get_env(data->envp, "PWD"));
50 if (getcwd(cwd, sizeof(cwd)) != NULL)
51 set_env(&data->envp, "PWD", cwd);
52 return (EXIT_SUCCESS);
53}
void set_env(t_env **env, const char *key, const char *value)
Definition utils_utils.c:44
char * get_env(t_env *envp, const char *key)
Definition env.c:81
#define EXIT_SUCCESS
Definition exit_status.h:16
#define EXIT_FAILURE
Definition exit_status.h:17
#define PERMISSION_DENIED
Definition exit_status.h:25
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg)
Definition exit_status.c:25
#define IS_DIRECTORY
Definition exit_status.h:20
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23
t_env * envp
Definition shell.h:24
char ** args
Definition shell.h:23

References s_ms_data::args, s_ms_data::envp, EXIT_FAILURE, exit_status_handler(), EXIT_SUCCESS, ft_strjoin(), get_env(), IS_DIRECTORY, PERMISSION_DENIED, and set_env().

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_echo()

int builtin_echo ( t_ms_data data)

Definition at line 23 of file echo.c.

24{
25 int newline;
26 int i;
27 char **args;
28
29 newline = 1;
30 i = 1;
31 args = data->args;
32 if (!data || !data->args || !data->args[0])
33 return (EXIT_FAILURE);
34 handle_std_io(&data->std_out, STDOUT_FILENO);
35 if (args[1] && ft_strcmp(args[1], "-n") == 0)
36 {
37 newline = 0;
38 i = 2;
39 }
40 while (args[i])
41 {
42 ft_putstr_fd(args[i], STDOUT_FILENO);
43 if (args[i + 1])
44 ft_putstr_fd(" ", STDOUT_FILENO);
45 i++;
46 }
47 if (newline)
48 ft_putchar_fd('\n', STDOUT_FILENO);
49 return (EXIT_SUCCESS);
50}
void handle_std_io(int *std_io, int std_fileno)
Definition utils_0.c:50
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
void ft_putchar_fd(char c, int fd)
void ft_putstr_fd(char *s, int fd)
int std_out
Definition shell.h:27

References s_ms_data::args, EXIT_FAILURE, EXIT_SUCCESS, ft_putchar_fd(), ft_putstr_fd(), ft_strcmp(), handle_std_io(), and s_ms_data::std_out.

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_env()

int builtin_env ( t_ms_data data)

Definition at line 35 of file env.c.

36{
37 if (ft_strcmp(data->args[0], "env") == 0 && data->args[1] == NULL)
38 print_env_stack(data->envp);
39 else
40 {
41 ft_putstr_fd("env: '", STDERR_FILENO);
42 ft_putstr_fd(data->args[1], STDERR_FILENO);
43 ft_putstr_fd("': No such file or directory\n", STDERR_FILENO);
46 return (IS_DIRECTORY);
47 }
48 return (EXIT_SUCCESS);
49}
void print_env_stack(t_env *envp)
Definition env.c:23
void set_shell_var(t_env **shell_var, const char *key, const char *value)
void set_exit_status(int *exit_status, int status_code)
Definition exit_status.c:79
char * ft_itoa(int n)
Definition ft_itoa.c:40
int exit_status
Definition shell.h:30
t_env * shell_variables
Definition shell.h:25

References s_ms_data::args, s_ms_data::envp, s_ms_data::exit_status, EXIT_SUCCESS, ft_itoa(), ft_putstr_fd(), ft_strcmp(), IS_DIRECTORY, print_env_stack(), set_exit_status(), set_shell_var(), and s_ms_data::shell_variables.

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_exit()

int builtin_exit ( t_ms_data data)

Definition at line 61 of file exit.c.

62{
63 int number;
64
65 number = 0;
66 if (!data->args || !data->args[1])
67 {
68 ft_free_2d_arr(data->args);
69 data->args = NULL;
70 ft_putendl_fd("exit", STDOUT_FILENO);
71 handle_exit(data, 0);
72 return (0);
73 }
74 if (ft_isnumber(data->args[1]) == 0)
75 handle_numeric_error(data, data->args[1]);
76 else if (data->args[2])
78 else
79 {
80 number = ft_atoi(data->args[1]);
81 ft_free_2d_arr(data->args);
82 data->args = NULL;
83 handle_exit(data, number);
84 }
85 return (EXIT_SUCCESS);
86}
void ft_free_2d_arr(char **arr)
void handle_exit(t_ms_data *data, int status)
Definition exit.c:53
void handle_too_many_args_error(t_ms_data *data)
Definition exit.c:47
void handle_numeric_error(t_ms_data *data, const char *arg)
Definition exit.c:37
int ft_isnumber(char *str)
void ft_putendl_fd(char *s, int fd)
int ft_atoi(const char *str)

References s_ms_data::args, EXIT_SUCCESS, ft_atoi(), ft_free_2d_arr(), ft_isnumber(), ft_putendl_fd(), handle_exit(), handle_numeric_error(), and handle_too_many_args_error().

Referenced by execute(), and main_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_export()

int builtin_export ( t_ms_data data)

Definition at line 30 of file export.c.

31{
32 if (data->args == NULL || data->args[1] == NULL)
33 {
34 print_env(data->envp);
35 return (EXIT_SUCCESS);
36 }
37 if (ft_strcmp(data->args[1], "-p") != 0 && data->args[1][0] == '-')
38 {
40 ft_strjoin("export: ", data->args[1]));
41 ft_putendl_fd("export: usage: export [-p]" \
42 "[name[=value] ...] or export -p", STDERR_FILENO);
43 return (INVALID_OPTION);
44 }
45 else if (ft_strcmp(data->args[1], "-p") == 0)
46 {
47 print_env(data->envp);
48 return (EXIT_SUCCESS);
49 }
50 add_env(data);
51 return (EXIT_SUCCESS);
52}
#define INVALID_OPTION
Definition exit_status.h:28
static void add_env(t_ms_data *data)
Definition export.c:70
static void print_env(t_env *env)
Definition export.c:54

References add_env(), s_ms_data::args, s_ms_data::envp, exit_status_handler(), EXIT_SUCCESS, ft_putendl_fd(), ft_strcmp(), ft_strjoin(), INVALID_OPTION, and print_env().

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_pwd()

int builtin_pwd ( t_ms_data data)

Definition at line 24 of file pwd.c.

25{
26 char cwd[4096];
27
28 (void)data;
29 if (getcwd(cwd, sizeof(cwd)) != NULL)
30 ft_putendl_fd(cwd, STDOUT_FILENO);
31 return (EXIT_SUCCESS);
32}

References EXIT_SUCCESS, and ft_putendl_fd().

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ builtin_unset()

int builtin_unset ( t_ms_data data)

Definition at line 29 of file unset.c.

30{
31 int i;
32 char *key;
33
34 i = 0;
35 while (data->args[++i])
36 {
37 key = data->args[i];
38 if (unset_env(&data->envp, key) == -1)
39 {
40 return (EXIT_SUCCESS);
41 }
42 }
43 return (EXIT_SUCCESS);
44}
int unset_env(t_env **env, const char *name)
Definition env.c:95

References s_ms_data::args, s_ms_data::envp, EXIT_SUCCESS, and unset_env().

Referenced by execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_signal_context()

void free_signal_context ( void  )

Definition at line 23 of file builtins_utils.c.

24{
25 t_signal_context *context;
26
27 context = get_context(NULL);
28 free(context);
29}
t_signal_context * get_context(t_ms_data *data)

References get_context().

Referenced by cleanup_exit_resources().

Here is the call graph for this function:
Here is the caller graph for this function: