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)
 

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 env.c:73
char * get_env(t_env *envp, const char *key)
Definition env.c:59
#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 22 of file echo.c.

23{
24 int newline;
25 int i;
26
27 newline = 1;
28 if (data->args[1] && (ft_strcmp(data->args[1], "-n") == 0))
29 {
30 newline = 0;
31 data->args++;
32 }
33 i = 0;
34 while (data->args[++i])
35 {
36 ft_putstr_fd(data->args[i], STDOUT_FILENO);
37 if (data->args[i + 1])
38 ft_putstr_fd(" ", STDOUT_FILENO);
39 }
40 if (newline)
41 write(STDOUT_FILENO, "\n", 1);
42 return (EXIT_SUCCESS);
43}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
void ft_putstr_fd(char *s, int fd)

References s_ms_data::args, EXIT_SUCCESS, ft_putstr_fd(), and ft_strcmp().

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:78
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 62 of file exit.c.

63{
64 int number;
65
66 number = 0;
67 if (data->args[1])
68 {
69 if (ft_isnumber(data->args[1]) == 0)
70 handle_numeric_error(data, data->args[1]);
71 else if (data->args[2])
73 else
74 {
75 number = ft_atoi(data->args[1]);
76 handle_exit(data, number);
77 }
78 }
79 else
80 handle_exit(data, 0);
81 return (0);
82}
void handle_exit(t_ms_data *data, int status)
Definition exit.c:48
void handle_too_many_args_error(t_ms_data *data)
Definition exit.c:42
void handle_numeric_error(t_ms_data *data, const char *arg)
Definition exit.c:33
int ft_isnumber(char *str)
int ft_atoi(const char *str)

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

Referenced by execute().

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
void ft_putendl_fd(char *s, int fd)

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:96

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: