-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.c
112 lines (102 loc) · 2.61 KB
/
builtins.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qcesar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 18:39:39 by qcesar #+# #+# */
/* Updated: 2021/11/16 20:52:33 by qcesar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void env_builtin(char **cmd_array, t_env *start, int fdout)
{
t_env *temp;
temp = start;
while (temp != NULL)
{
if (temp->value)
{
ft_putstr_fd(temp->key, fdout);
ft_putchar_fd('=', fdout);
ft_putstr_fd(temp->value, fdout);
ft_putchar_fd('\n', fdout);
}
temp = temp->next;
}
free_array(cmd_array);
set_exit(0);
}
void echo_builtin_2(char **cmd_array, int fdout, int *i, int endl)
{
if (ft_strcmp(cmd_array[*i], "$?") == 0)
ft_putnbr_fd(get_exit(), fdout);
else
{
while (cmd_array[*i])
{
ft_putstr_fd(cmd_array[*i], fdout);
if (cmd_array[*i + 1])
ft_putstr_fd(" ", fdout);
(*i)++;
}
}
if (endl != 1)
ft_putstr_fd("\n", fdout);
free_array(cmd_array);
set_exit(0);
}
void echo_builtin(char **cmd_array, int fdout)
{
int endl;
int i;
endl = 0;
i = 1;
if (ft_arraylen(cmd_array) == 1)
{
ft_putendl_fd("", fdout);
return ;
}
if (cmd_array[1] && ft_strequal(cmd_array[1], "-n"))
{
endl = 1;
while (cmd_array[i] && ft_strequal(cmd_array[i], "-n"))
i++;
}
echo_builtin_2(cmd_array, fdout, &i, endl);
}
void pwd_builtin(char **cmd_array, int fdout)
{
char *pwd;
pwd = getcwd(NULL, 0);
ft_putendl_fd(pwd, fdout);
free(pwd);
free_array(cmd_array);
set_exit(0);
}
void unset_builtin(char **cmd_array, t_env **env_list, int fdout)
{
t_env *deleted;
int i;
i = 1;
while (cmd_array[i])
{
if (unset_arg_isok(cmd_array[i]))
{
deleted = env_exists(env_list, cmd_array[i]);
if (deleted)
delete_env(deleted, env_list);
}
else
{
ft_putstr_fd("minishell: unset: `", STDERR_FILENO);
ft_putstr_fd(cmd_array[i], STDERR_FILENO);
ft_putendl_fd("\': not a valid identifier", STDERR_FILENO);
set_exit(1);
}
i++;
}
free_array(cmd_array);
set_exit(0);
}