Skip to content

Commit 0f55d27

Browse files
authored
Merge pull request #30 from cuajarsaki/fix/#29-export-unset-issue
Fix/#29 export issue
2 parents c70aed8 + 1737fbf commit 0f55d27

29 files changed

+547
-449
lines changed

shell.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ void run_shell(t_env *env_list, char **envp);
7272
char *get_env_value(t_env *env_list, const char *key);
7373
void set_env_value(t_env **env_list, const char *key, const char *value);
7474
void unset_env_value(t_env **env_list, const char *key);
75-
void free_env_list(t_env *env_list);
75+
void free_env_list(t_env **env_list);
7676
int exec_ast(t_ast *ast, t_env *env_list, char **envp);
7777
char **convert_env_list_to_array(t_env *env_list);
7878
void free_env_array(char **envp);;
7979
void shell_unknown_command(char *cmd);
80+
int is_parent_builtin(t_cmd *cmd);
8081

8182
// FREE FUNCTIONS
8283
void free_ast(t_ast *ast);
@@ -90,7 +91,7 @@ int shell_env(t_env *env_list);
9091
int shell_export(char **args, t_env *env_list);
9192
int shell_unset(char **args, t_env *env_list);
9293
int shell_clear(void);
93-
int shell_cd(char **argv);
94+
int shell_cd(char **argv, t_env *env_list);
9495
int shell_pwd(void);
9596
int shell_echo(char **args);
9697
int shell_exit(char **argv);

src/buildin_commands/shell_cd.c

+32-28
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,56 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/02/09 15:41:50 by jidler #+# #+# */
9-
/* Updated: 2025/03/04 01:40:37 by pchung ### ########.fr */
9+
/* Updated: 2025/03/08 21:04:33 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "../../shell.h"
1414

15-
int shell_cd(char **argv)
15+
void print_cd_error(const char *msg)
1616
{
17-
const char *path = argv[1];
17+
perror(msg);
18+
}
1819

19-
if (path == NULL || ft_strcmp(path, "") == 0)
20+
void print_cd_result(void)
21+
{
22+
char cwd[1024];
23+
24+
if (getcwd(cwd, sizeof(cwd)) != NULL)
2025
{
21-
char *home = getenv("HOME");
26+
write(1, "Changed directory to: ", 22);
27+
write(1, cwd, strlen(cwd));
28+
write(1, "\n", 1);
29+
}
30+
else
31+
print_cd_error("getcwd");
32+
}
33+
34+
int shell_cd(char **argv, t_env *env_list)
35+
{
36+
const char *path = argv[1];
37+
char *home;
38+
39+
if (path == NULL || strcmp(path, "") == 0)
40+
{
41+
home = get_env_value(env_list, "HOME");
2242
if (home == NULL)
2343
{
2444
write(2, "cd: HOME not set\n", 17);
25-
return 1;
45+
return (1);
2646
}
2747
path = home;
2848
}
2949
else if (argv[2])
3050
{
3151
write(2, "cd: too many arguments\n", 24);
32-
return 1;
52+
return (1);
3353
}
34-
3554
if (chdir(path) != 0)
3655
{
37-
perror("cd");
38-
return 1;
56+
print_cd_error("cd");
57+
return (1);
3958
}
40-
else
41-
{
42-
char cwd[1024];
43-
if (getcwd(cwd, sizeof(cwd)) != NULL)
44-
{
45-
write(1, "Changed directory to: ", 22);
46-
write(1, cwd, ft_strlen(cwd));
47-
write(1, "\n", 1);
48-
}
49-
else
50-
{
51-
perror("getcwd");
52-
return 1;
53-
}
54-
}
55-
56-
return 0;
57-
}
59+
print_cd_result();
60+
return (0);
61+
}

src/buildin_commands/shell_commands.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#include "../../shell.h"
1414

15-
void shell_unknown_command(char *cmd) {
15+
void shell_unknown_command(char *cmd)
16+
{
1617
write(2, "unknown command: ", 18);
1718
write(2, cmd, ft_strlen(cmd));
1819
write(2, "\n", 1);
1920
}
2021

21-
void shell_clear(void)
22+
void shell_clear(void)
2223
{
2324
write(STDOUT_FILENO, "\033[2J\033[H", 7);
2425
}

src/buildin_commands/shell_echo.c

+15-16
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,51 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/02/09 15:41:50 by jidler #+# #+# */
9-
/* Updated: 2025/03/04 02:47:09 by pchung ### ########.fr */
9+
/* Updated: 2025/03/06 12:58:43 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "../../shell.h"
1414

15-
static bool is_n_option(const char *arg)
15+
static bool is_n_option(const char *arg)
1616
{
17-
int i = 0;
17+
int i;
1818

19+
i = 0;
1920
if (!arg || arg[0] != '-')
20-
return false;
21-
21+
return (false);
2222
i = 1;
2323
if (!arg[i])
24-
return false;
24+
return (false);
2525
while (arg[i])
2626
{
2727
if (arg[i] != 'n')
28-
return false;
28+
return (false);
2929
i++;
3030
}
31-
return true;
31+
return (true);
3232
}
3333

34-
int shell_echo(char **args)
34+
int shell_echo(char **args)
3535
{
36-
bool _newline = true;
37-
int i = 1;
36+
bool _newline;
37+
int i;
3838

39+
i = 1;
40+
_newline = true;
3941
while (args[i] && is_n_option(args[i]))
4042
{
4143
_newline = false;
4244
i++;
4345
}
44-
4546
while (args[i])
4647
{
4748
write(1, args[i], ft_strlen(args[i]));
4849
if (args[i + 1])
4950
write(1, " ", 1);
5051
i++;
5152
}
52-
5353
if (_newline)
5454
write(1, "\n", 1);
55-
56-
return 0;
57-
}
55+
return (0);
56+
}

src/buildin_commands/shell_env.c

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* shell_env.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: pchung <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/03/06 11:54:09 by pchung #+# #+# */
9+
/* Updated: 2025/03/06 11:54:43 by pchung ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
113
#include "../../shell.h"
214

3-
int shell_env(t_env *env_list)
15+
int shell_env(t_env *env_list)
416
{
5-
if (!env_list)
6-
{
7-
printf("No environment variables to display.\n");
8-
return 1;
9-
}
10-
11-
while (env_list)
12-
{
13-
printf("%s=%s\n", env_list->key, env_list->value);
14-
env_list = env_list->next;
15-
}
16-
return 0;
17-
}
17+
if (!env_list)
18+
{
19+
printf("No environment variables to display.\n");
20+
return (1);
21+
}
22+
while (env_list)
23+
{
24+
printf("%s=%s\n", env_list->key, env_list->value);
25+
env_list = env_list->next;
26+
}
27+
return (0);
28+
}

src/buildin_commands/shell_exit.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/03/02 16:13:52 by pchung #+# #+# */
9-
/* Updated: 2025/03/05 13:40:31 by pchung ### ########.fr */
9+
/* Updated: 2025/03/06 12:53:38 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -27,7 +27,7 @@ static bool is_strnum(char *str)
2727
return (true);
2828
}
2929

30-
int shell_exit(char **argv)
30+
int shell_exit(char **argv)
3131
{
3232
long int res;
3333

@@ -44,12 +44,9 @@ int shell_exit(char **argv)
4444
if (is_strnum(argv[1]))
4545
{
4646
res = ft_strtol(argv[1]);
47-
printf("exit\n");
48-
// This is debugs
49-
// printf("%ld", res& 0xFF);
50-
exit((int)(res & 0xFF));
47+
printf("exit\n");
48+
exit((int)(res & 0xFF));
5149
}
52-
// when the argument is not a number
5350
perror("exit: numeric argument required");
5451
exit(2);
5552
}

src/buildin_commands/shell_export.c

+45-33
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,60 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/02/09 15:41:50 by jidler #+# #+# */
9-
/* Updated: 2025/03/04 02:17:38 by pchung ### ########.fr */
9+
/* Updated: 2025/03/06 13:01:48 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "../../shell.h"
1414

15-
static void print_env_list(t_env *env_list)
15+
static void print_env_list(t_env *env_list)
1616
{
17-
while (env_list)
18-
{
19-
printf("declare -x %s", env_list->key);
20-
if (env_list->value)
21-
printf("=\"%s\"", env_list->value);
22-
printf("\n");
23-
env_list = env_list->next;
24-
}
17+
while (env_list)
18+
{
19+
printf("declare -x %s", env_list->key);
20+
if (env_list->value)
21+
printf("=\"%s\"", env_list->value);
22+
printf("\n");
23+
env_list = env_list->next;
24+
}
2525
}
2626

27-
int shell_export(char **args, t_env *env_list)
27+
static int export_arg(char *arg, t_env **env_list)
2828
{
29-
int i = 1;
29+
char *eq;
3030

31-
if (!args || !args[1])
32-
{
33-
print_env_list(env_list);
34-
return 0;
35-
}
31+
if (ft_strlen(arg) == 0 || arg[0] == '=')
32+
{
33+
printf("export: `%s': not a valid identifier\n", arg);
34+
return (1);
35+
}
36+
eq = ft_strchr(arg, '=');
37+
if (eq)
38+
{
39+
*eq = '\0';
40+
set_env_value(env_list, arg, eq + 1);
41+
*eq = '=';
42+
}
43+
else
44+
set_env_value(env_list, arg, NULL);
45+
return (0);
46+
}
47+
48+
int shell_export(char **args, t_env *env_list)
49+
{
50+
int i;
3651

37-
while (args[i])
38-
{
39-
char *eq = ft_strchr(args[i], '=');
40-
if (!eq)
41-
{
42-
set_env_value(&env_list, args[i], NULL);
43-
}
44-
else
45-
{
46-
*eq = '\0';
47-
set_env_value(&env_list, args[i], eq + 1);
48-
*eq = '=';
49-
}
50-
i++;
51-
}
52-
return 0;
52+
i = 1;
53+
if (!args || !args[1])
54+
{
55+
print_env_list(env_list);
56+
return (0);
57+
}
58+
while (args[i])
59+
{
60+
if (export_arg(args[i], &env_list))
61+
return (1);
62+
i++;
63+
}
64+
return (0);
5365
}

src/buildin_commands/shell_pwd.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212

1313
#include "../../shell.h"
1414

15-
int shell_pwd(void)
15+
int shell_pwd(void)
1616
{
17-
char cwd[1024];
18-
if (getcwd(cwd, sizeof(cwd)) != NULL) {
17+
char cwd[1024];
18+
19+
if (getcwd(cwd, sizeof(cwd)) != NULL)
20+
{
1921
write(1, cwd, ft_strlen(cwd));
2022
write(1, "\n", 1);
21-
return 0;
22-
} else {
23+
return (0);
24+
}
25+
else
26+
{
2327
perror("pwd");
24-
return 1;
25-
}
28+
return (1);
29+
}
2630
}

0 commit comments

Comments
 (0)