-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Ofacy/error-wd
Edit cd and pwd
- Loading branch information
Showing
2 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
|
@@ -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); | ||
} | ||
|
@@ -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) | ||
|
@@ -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"); | ||
|
@@ -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])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
{ | ||
|