feat(execution): update $PWD and $OLD_PWD

This commit is contained in:
william.valenduc 2026-01-29 11:29:03 +00:00
parent fe3c4243c8
commit 988d8ef298

View file

@ -12,6 +12,7 @@
#include "../expansion/expansion.h" #include "../expansion/expansion.h"
#include "../utils/ast/ast.h" #include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.h" #include "../utils/hash_map/hash_map.h"
#include "../utils/vars/vars.h"
#include "execution.h" #include "execution.h"
static char **list_to_argv(struct list *command_list) static char **list_to_argv(struct list *command_list)
@ -36,7 +37,7 @@ static char **list_to_argv(struct list *command_list)
return argv; return argv;
} }
static int try_builtin(char **argv); static int try_builtin(char **argv, struct hash_map *vars);
int exec_ast_command(struct ast_command *command, struct hash_map *vars) int exec_ast_command(struct ast_command *command, struct hash_map *vars)
{ {
@ -49,7 +50,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
free(argv); free(argv);
return 0; return 0;
} }
int builtin_ret = try_builtin(argv); int builtin_ret = try_builtin(argv, vars);
if (builtin_ret != -1) if (builtin_ret != -1)
{ {
free(argv); free(argv);
@ -294,7 +295,7 @@ static int builtin_exit(char **argv)
return exit_val; return exit_val;
} }
static int builtin_cd(char **argv) static int builtin_cd(char **argv, struct hash_map *vars)
{ {
const char *path = argv[1]; const char *path = argv[1];
if (!path) if (!path)
@ -306,11 +307,15 @@ static int builtin_cd(char **argv)
return 1; return 1;
} }
} }
// char *pwd = getcwd("", "");
char *pwd = get_var_or_env(pwd, "PWD");
if (chdir(path) != 0) if (chdir(path) != 0)
{ {
perror("cd"); perror("cd");
return 1; return 1;
} }
set_var_copy(vars, "OLD_PWD", pwd);
set_var_copy(vars, "PWD", path);
return 0; return 0;
} }
@ -320,7 +325,7 @@ static int builtin_cd(char **argv)
* @param argv Array of command arguments * @param argv Array of command arguments
* @return int Exit status of the builtin command, or -1 if not a builtin * @return int Exit status of the builtin command, or -1 if not a builtin
*/ */
static int try_builtin(char **argv) static int try_builtin(char **argv, struct hash_map *vars)
{ {
if (!argv || !argv[0]) if (!argv || !argv[0])
return 0; return 0;
@ -334,7 +339,7 @@ static int try_builtin(char **argv)
if (strcmp(argv[0], "exit") == 0) if (strcmp(argv[0], "exit") == 0)
return builtin_exit(argv); return builtin_exit(argv);
if (strcmp(argv[0], "cd") == 0) if (strcmp(argv[0], "cd") == 0)
return builtin_cd(argv); return builtin_cd(argv, vars);
return -1; return -1;
} }