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
3 changes: 2 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/04 18:49:36 by ttsubo ### ########.fr */
/* Updated: 2025/05/17 12:40:38 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,7 @@ void handle_error_and_exit(const char *func_name, t_minish *minish);
void add_env_back(t_env **lst, t_env *new);
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);

#endif
65 changes: 57 additions & 8 deletions src/builtin/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,70 @@
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 11:12:37 by ttsubo #+# #+# */
/* Updated: 2025/05/01 15:49:07 by dayano ### ########.fr */
/* Updated: 2025/05/17 13:36:48 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

int builtin_cd(int argc, char *argv[], t_minish *minish)
static int _cd_common(t_minish *minish, char *path)
{
(void)minish;
if (argc != 2)
return (error_mes(argv[0], "wrong argument"), EXIT_FAILURE);
if (chdir(argv[1]) < 0)
return (perror(argv[1]), 1);
char *pwd;
char *newpwd;

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);
}

static int _cd_home(t_minish *minish)
{
char *home;

home = get_env_value(minish->env, "HOME");
if (!home)
return (error_mes("bash: cd", "HOME not set"), EXIT_FAILURE);
return (_cd_common(minish, home));
}

static int _cd_oldpwd(t_minish *minish)
{
char *oldpwd;
int status;

oldpwd = 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);
}

static int _cd_normal(char *path, t_minish *minish)
{
return (_cd_common(minish, path));
}

int builtin_cd(int argc, char *argv[], t_minish *minish)
{
if (argc == 1)
return (_cd_home(minish));
else if (argc == 2 && ft_strcmp("-", argv[1]) == 0)
return (_cd_oldpwd(minish));
else if (argc == 2)
return (_cd_normal(argv[1], minish));
else
return (error_mes(argv[0], "too many arguments"), EXIT_FAILURE);
}
19 changes: 18 additions & 1 deletion src/builtin/env_utils_3.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 11:17:22 by dayano #+# #+# */
/* Updated: 2025/04/29 13:53:40 by ttsubo ### ########.fr */
/* Updated: 2025/05/17 13:13:50 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -69,6 +69,23 @@ char *get_env_value(t_env *current, const char *key)
return (NULL);
}

int set_env_value(t_env *current, const char *key, char *value)
{
if (!current || !key)
return (1);
while (current)
{
if (!ft_strcmp(current->key, key))
{
free_str(&current->value);
current->value = ft_strdup(value);
return (0);
}
current = current->next;
}
return (1);
}

bool has_env_key(t_env *current, const char *key)
{
if (!current || !key)
Expand Down