builtins cd, pwd (todo, refactoring cd)

This commit is contained in:
LuckyLaszlo
2021-12-05 01:01:27 +01:00
parent 27c00a78a0
commit c3a9035622
5 changed files with 78 additions and 14 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/12/04 15:46:00 by lperrey ### ########.fr */
/* Updated: 2021/12/04 19:09:42 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -72,6 +72,7 @@ typedef void (*t_free_f)(void *); // generic
// Error wrappers
void shell_error(char *s1, char *s2);
void shell_perror(char *s1, char *s2, char *s3);
// Generic
char *ft_strjoinfree(char *s1, char *s2);

View File

@@ -1,12 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */
/* Updated: 2021/12/04 20:01:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_cd(int argc, char *argv[], t_all *c)
{
(void)argc;
chdir(argv[1]);
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt() error"));
return (0);
char *tmp;
if (argc == 2)
tmp = argv[1];
else if (argc < 2)
{
tmp = getenv("HOME");
if (!tmp)
{
shell_error("cd: ", "HOME not set");
return (EXIT_FAILURE);
}
}
else if (argc > 2)
{
shell_error("cd: ", "too many arguments");
return (EXIT_FAILURE);
}
if (chdir(tmp) == -1)
{
shell_perror("cd: ", tmp, ": ");
return (EXIT_FAILURE);
}
tmp = init_prompt(c->prompt_base);
if (!tmp)
return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()"));
free(c->prompt);
c->prompt = tmp;
return (EXIT_SUCCESS);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/03 13:36:54 by lperrey #+# #+# */
/* Updated: 2021/12/04 16:07:21 by lperrey ### ########.fr */
/* Updated: 2021/12/04 18:47:15 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,7 @@ size_t ft_getenv_position(char *env_var);
static int change_env_value(char *arg);
// in complete shell, must mark all arguments for export
// in complete shell, must mark arguments for export
// (Not implemented in minishell)
int builtin_export(int argc, char *argv[], t_all *c)
{

View File

@@ -1,15 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 19:15:36 by lperrey #+# #+# */
/* Updated: 2021/12/04 20:22:49 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_pwd(int argc, char *argv[], t_all *c)
{
char *pwd;
char *working_dir;
(void)argc;
(void)argv;
(void)c;
pwd = getcwd(NULL, 0);
write(1, pwd, ft_strlen(pwd));
working_dir = getcwd(NULL, 0);
if (!working_dir)
return (ft_reti_perror(EXIT_FAILURE, "builtin_pwd, getcwd()"));
write(1, working_dir, ft_strlen(working_dir));
write(1, "\n", 1);
return (0);
free(working_dir);
return (EXIT_SUCCESS);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */
/* Updated: 2021/12/01 17:19:02 by lperrey ### ########.fr */
/* Updated: 2021/12/04 19:09:34 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,3 +24,18 @@ void shell_error(char *s1, char *s2)
write(STDERR_FILENO, s2, ft_strlen(s2));
write(STDERR_FILENO, "\n", 1);
}
void shell_perror(char *s1, char *s2, char *s3)
{
char *prefix;
prefix = "minishell: ";
write(STDERR_FILENO, prefix, ft_strlen(prefix));
if (s1)
write(STDERR_FILENO, s1, ft_strlen(s1));
if (s2)
write(STDERR_FILENO, s2, ft_strlen(s2));
if (s3)
write(STDERR_FILENO, s3, ft_strlen(s3));
perror(NULL);
}