feat(execution): update $PWD and $OLD_PWD
This commit is contained in:
parent
dd8a7165e1
commit
a0b723fb30
1 changed files with 10 additions and 5 deletions
|
|
@ -12,6 +12,7 @@
|
|||
#include "../expansion/expansion.h"
|
||||
#include "../utils/ast/ast.h"
|
||||
#include "../utils/hash_map/hash_map.h"
|
||||
#include "../utils/vars/vars.h"
|
||||
#include "execution.h"
|
||||
|
||||
static char **list_to_argv(struct list *command_list)
|
||||
|
|
@ -36,7 +37,7 @@ static char **list_to_argv(struct list *command_list)
|
|||
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)
|
||||
{
|
||||
|
|
@ -49,7 +50,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
|||
free(argv);
|
||||
return 0;
|
||||
}
|
||||
int builtin_ret = try_builtin(argv);
|
||||
int builtin_ret = try_builtin(argv, vars);
|
||||
if (builtin_ret != -1)
|
||||
{
|
||||
free(argv);
|
||||
|
|
@ -294,7 +295,7 @@ static int builtin_exit(char **argv)
|
|||
return exit_val;
|
||||
}
|
||||
|
||||
static int builtin_cd(char **argv)
|
||||
static int builtin_cd(char **argv, struct hash_map *vars)
|
||||
{
|
||||
const char *path = argv[1];
|
||||
if (!path)
|
||||
|
|
@ -306,11 +307,15 @@ static int builtin_cd(char **argv)
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
// char *pwd = getcwd("", "");
|
||||
char *pwd = get_var_or_env(pwd, "PWD");
|
||||
if (chdir(path) != 0)
|
||||
{
|
||||
perror("cd");
|
||||
return 1;
|
||||
}
|
||||
set_var_copy(vars, "OLD_PWD", pwd);
|
||||
set_var_copy(vars, "PWD", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -320,7 +325,7 @@ static int builtin_cd(char **argv)
|
|||
* @param argv Array of command arguments
|
||||
* @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])
|
||||
return 0;
|
||||
|
|
@ -334,7 +339,7 @@ static int try_builtin(char **argv)
|
|||
if (strcmp(argv[0], "exit") == 0)
|
||||
return builtin_exit(argv);
|
||||
if (strcmp(argv[0], "cd") == 0)
|
||||
return builtin_cd(argv);
|
||||
return builtin_cd(argv, vars);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue