Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion inc/builtin/env_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 10:25:47 by dayano #+# #+# */
/* Updated: 2025/05/17 12:40:38 by ttsubo ### ########.fr */
/* Updated: 2025/05/22 12:30:11 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,5 +24,7 @@ t_env *get_env_node(t_env *current, const char *key);
char *get_env_value(t_env *current, const char *key);
int set_env_value(t_env *current, const char *key, char *value);
bool has_env_key(t_env *current, const char *key);
void remove_env_value(t_env *env, char *key);
int add_env_value(t_env *env, char *key, char *value);

#endif
31 changes: 21 additions & 10 deletions src/builtin/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 11:12:37 by ttsubo #+# #+# */
/* Updated: 2025/05/17 13:36:48 by ttsubo ### ########.fr */
/* Updated: 2025/05/22 12:50:37 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,17 +16,28 @@ static int _cd_common(t_minish *minish, char *path)
{
char *pwd;
char *newpwd;
char *oldpwd;

pwd = get_env_value(minish->env, "PWD");
if (chdir(path) < 0)
return (perror(path), 1);
newpwd = getcwd(NULL, 0);
if (!newpwd)
return (perror("getcwd"), 1);
set_env_value(minish->env, "OLDPWD", pwd);
set_env_value(minish->env, "PWD", newpwd);
free_str(&newpwd);
return (0);
pwd = ft_strdup(get_env_value(minish->env, "PWD"));
oldpwd = get_env_value(minish->env, "OLDPWD");
if (pwd && oldpwd)
{
set_env_value(minish->env, "PWD", newpwd);
set_env_value(minish->env, "OLDPWD", pwd);
}
if (!pwd && oldpwd)
remove_env_value(minish->env, "OLDPWD");
if (pwd && !oldpwd)
{
set_env_value(minish->env, "PWD", newpwd);
add_env_value(minish->env, "OLDPWD", pwd);
}
return (free_str(&pwd), free_str(&newpwd), 0);
}

static int _cd_home(t_minish *minish)
Expand All @@ -44,14 +55,14 @@ static int _cd_oldpwd(t_minish *minish)
char *oldpwd;
int status;

oldpwd = get_env_value(minish->env, "OLDPWD");
oldpwd = ft_strdup(get_env_value(minish->env, "OLDPWD"));
if (!oldpwd)
return (error_mes("bash: cd", "OLDPWD not set"), EXIT_FAILURE);
status = _cd_common(minish, oldpwd);
if (status == 0)
return (printf("%s\n", oldpwd), status);
else
return (status);
printf("%s\n", oldpwd);
free_str(&oldpwd);
return (status);
}

static int _cd_normal(char *path, t_minish *minish)
Expand Down
47 changes: 46 additions & 1 deletion src/builtin/env_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 10:26:59 by dayano #+# #+# */
/* Updated: 2025/04/28 16:32:43 by ttsubo ### ########.fr */
/* Updated: 2025/05/22 12:29:48 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,3 +52,48 @@ void remove_env_node(t_env **env_lst, t_env *target)
current = current->next;
}
}

void remove_env_value(t_env *env, char *key)
{
t_env *current;
t_env *prev;

current = env;
prev = NULL;
while (current)
{
if (ft_strcmp(current->key, key) == 0)
{
if (prev)
prev->next = current->next;
else
env = current->next;
free(current->key);
free(current->value);
free(current);
return ;
}
prev = current;
current = current->next;
}
}

int add_env_value(t_env *env, char *key, char *value)
{
t_env *node;

if (!is_valid_key(key))
return (export_err_invalid("minish", key, value));
node = ft_calloc(1, sizeof(t_env));
if (!node)
return (EXIT_FAILURE);
node->key = ft_strdup(key);
if (!node->key)
return (free(node), EXIT_FAILURE);
node->value = ft_strdup(value);
if (!node->value)
return (free(node->key), free(node), EXIT_FAILURE);
node->is_exported = 1;
add_env_back(&env, node);
return (0);
}
4 changes: 2 additions & 2 deletions src/invoke_cmd/create_envp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:56:59 by dayano #+# #+# */
/* Updated: 2025/05/04 22:09:48 by ttsubo ### ########.fr */
/* Updated: 2025/05/22 12:36:37 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,9 +73,9 @@ char **create_envp(t_minish *minish)
envp[i] = _get_envp_key_value(env);
if (!envp[i])
return (free_strs(&envp), NULL);
i++;
}
env = env->next;
i++;
}
envp[i] = NULL;
return (envp);
Expand Down