Skip to content

Commit

Permalink
Merge pull request #30 from Ofacy/error-wd
Browse files Browse the repository at this point in the history
Edit cd and pwd
  • Loading branch information
Ofacy authored Apr 9, 2024
2 parents d0d2d8e + 05b7164 commit 4737c3a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
66 changes: 56 additions & 10 deletions srcs/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,43 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/14 13:57:43 by bwisniew #+# #+# */
/* Updated: 2024/04/03 22:35:17 by lcottet ### ########.fr */
/* Updated: 2024/04/09 15:37:39 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "libft.h"
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int cd_change_env(t_mshell *sh)
int cd_getcwd_err(t_mshell *sh, char *argv)
{
char *new_pwd;
char *pwd_slash;
t_env *pwd;

custom_error("getcwd", "cannot access parent directories");
if (argv)
{
pwd = env_get(sh, "PWD", false);
if (!pwd)
return (1);
pwd_slash = ft_strjoin(pwd->value, "/");
if (!pwd_slash)
return (1);
new_pwd = ft_strjoin(pwd_slash, argv);
free(pwd_slash);
if (!new_pwd)
return (1);
env_set(sh, "PWD", new_pwd);
free(new_pwd);
}
return (0);
}

int cd_change_env(t_mshell *sh, char *argv)
{
t_env *pwd;
t_env *oldpwd;
Expand All @@ -27,10 +53,7 @@ int cd_change_env(t_mshell *sh)
{
oldpwd = env_get(sh, "OLDPWD", true);
if (oldpwd && env_set(sh, "OLDPWD", pwd->value) != 0)
{
custom_error("cd", "couldn't change OLDPWD");
return (1);
}
return (custom_error("cd", "couldn't change OLDPWD"), 1);
new_pwd = getcwd(NULL, 0);
if (new_pwd && env_set(sh, "PWD", new_pwd) != 0)
{
Expand All @@ -39,7 +62,10 @@ int cd_change_env(t_mshell *sh)
free(new_pwd);
return (1);
}
free(new_pwd);
if (new_pwd)
free(new_pwd);
else
cd_getcwd_err(sh, argv);
}
return (0);
}
Expand All @@ -59,7 +85,25 @@ int cd_home(t_mshell *sh)
error("cd");
return (1);
}
return (cd_change_env(sh));
return (cd_change_env(sh, NULL));
}

int cd_oldpwd(t_mshell *sh)
{
t_env *oldpwd;

oldpwd = env_get(sh, "OLDPWD", false);
if (!oldpwd)
{
custom_error("cd", "OLDPWD not set");
return (1);
}
if (chdir(oldpwd->value) == -1)
{
error("cd");
return (1);
}
return (cd_change_env(sh, NULL));
}

int cd(t_mshell *sh, t_execute *exec)
Expand All @@ -68,8 +112,10 @@ int cd(t_mshell *sh, t_execute *exec)

(void)sh;
args = exec->args.tab;
if (args[1] == NULL)
if (args[1] == NULL || (args[1] != NULL && args[1][0] == '~'))
return (cd_home(sh));
if (args[1] == NULL || (args[1] != NULL && args[1][0] == '-'))
return (cd_oldpwd(sh));
else if (args[2] != NULL)
{
custom_error("cd", "too many arguments");
Expand All @@ -80,5 +126,5 @@ int cd(t_mshell *sh, t_execute *exec)
error("cd");
return (1);
}
return (cd_change_env(sh));
return (cd_change_env(sh, args[1]));
}
10 changes: 8 additions & 2 deletions srcs/builtins/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/14 13:57:34 by bwisniew #+# #+# */
/* Updated: 2024/03/21 12:15:35 by lcottet ### ########.fr */
/* Updated: 2024/04/08 15:49:06 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,9 +18,15 @@
int pwd(t_mshell *sh, t_execute *exec)
{
char *pwd;
t_env *env;

(void)exec;
(void)sh;
env = env_get(sh, "PWD", false);
if (env)
{
printf("%s\n", env->value);
return (0);
}
pwd = getcwd(NULL, 0);
if (!pwd)
{
Expand Down

0 comments on commit 4737c3a

Please sign in to comment.