maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
export.c File Reference
#include "env.h"
#include "libft.h"
#include <stdlib.h>
#include "shell.h"
#include <stdio.h>
#include "exit_status.h"
Include dependency graph for export.c:

Go to the source code of this file.

Functions

int builtin_export (t_ms_data *data)
 
static void print_env (t_env *env)
 
static void add_env (t_ms_data *data)
 

Function Documentation

◆ add_env()

static void add_env ( t_ms_data data)
static

Definition at line 70 of file export.c.

71{
72 int i;
73 char *key;
74 char *curr_arg;
75
76 i = 0;
77 key = NULL;
78 curr_arg = NULL;
79 while (data->args[++i])
80 {
81 if (ft_strchr(data->args[i], '='))
82 {
83 curr_arg = data->args[i];
84 key = ft_strcdup(curr_arg, '=');
85 set_env(&data->envp, key, \
86 ft_strchr(curr_arg, '=') + 1);
87 free(key);
88 }
89 else
90 {
91 curr_arg = data->args[i];
92 key = curr_arg;
93 set_env(&data->envp, key, "");
94 }
95 }
96}
void set_env(t_env **env, const char *key, const char *value)
Definition env.c:73
char * ft_strcdup(const char *s, int c)
Definition utils.c:52
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
t_env * envp
Definition shell.h:24
char ** args
Definition shell.h:23

References s_ms_data::args, s_ms_data::envp, ft_strcdup(), ft_strchr(), and set_env().

Referenced by builtin_export().

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
#define EXIT_SUCCESS
Definition exit_status.h:16
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg)
Definition exit_status.c:25
static void add_env(t_ms_data *data)
Definition export.c:70
static void print_env(t_env *env)
Definition export.c:54
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23
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:

◆ print_env()

static void print_env ( t_env env)
static

Definition at line 54 of file export.c.

55{
56 t_env *curr_node;
57
58 curr_node = env;
59 while (curr_node)
60 {
61 if (!ft_strcmp(curr_node->value, ""))
62 ft_printf("declare -x %s\n", curr_node->key);
63 else
64 ft_printf("declare -x %s=\"%s\"\n", \
65 curr_node->key, curr_node->value);
66 curr_node = curr_node->next;
67 }
68}
int ft_printf(const char *format,...)
Definition ft_printf.c:37
Definition env.h:17
struct s_env * next
Definition env.h:20
char * key
Definition env.h:18
char * value
Definition env.h:19

References ft_printf(), ft_strcmp(), s_env::key, s_env::next, and s_env::value.

Referenced by builtin_export().

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