maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
export.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* export.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/17 11:10:52 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 13:12:35 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "env.h"
14#include "libft.h"
15#include <stdlib.h>
16#include "shell.h"
17#include <stdio.h>
18#include "exit_status.h"
19
20/*
21Functionalities:
22- If no arguments are passed or "-p", print all environment variables
23- If arguments are passed, set the environment variables
24 */
25
26int builtin_export(t_ms_data *data);
27static void print_env(t_env *env);
28static void add_env(t_ms_data *data);
29
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}
53
54static void print_env(t_env *env)
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}
69
70static void add_env(t_ms_data *data)
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
#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 builtin_export(t_ms_data *data)
Definition export.c:30
int ft_printf(const char *format,...)
Definition ft_printf.c:37
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
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
void ft_putendl_fd(char *s, int fd)
Definition env.h:17
struct s_env * next
Definition env.h:20
char * key
Definition env.h:18
char * value
Definition env.h:19
t_env * envp
Definition shell.h:24
char ** args
Definition shell.h:23