cd refactoring, error_wrappers

This commit is contained in:
LuckyLaszlo
2021-12-05 16:28:41 +01:00
parent c3a9035622
commit 66183ab441
4 changed files with 16 additions and 21 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/12/04 19:09:42 by lperrey ### ########.fr */ /* Updated: 2021/12/05 15:55:07 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -71,8 +71,8 @@ void close_pipeline_fd(t_cmd *pipeline[]);
typedef void (*t_free_f)(void *); // generic typedef void (*t_free_f)(void *); // generic
// Error wrappers // Error wrappers
void shell_error(char *s1, char *s2); int shell_error(char *s1, char *s2, char *s3, int ret_val);
void shell_perror(char *s1, char *s2, char *s3); int shell_perror(char *s1, char *s2, char *s3, int ret_val);
// Generic // Generic
char *ft_strjoinfree(char *s1, char *s2); char *ft_strjoinfree(char *s1, char *s2);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */ /* Created: 2021/12/04 19:31:19 by lperrey #+# #+# */
/* Updated: 2021/12/04 20:01:37 by lperrey ### ########.fr */ /* Updated: 2021/12/05 16:21:41 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -22,21 +22,12 @@ int builtin_cd(int argc, char *argv[], t_all *c)
{ {
tmp = getenv("HOME"); tmp = getenv("HOME");
if (!tmp) if (!tmp)
{ return (shell_error("cd: ", "HOME not set", "", EXIT_FAILURE));
shell_error("cd: ", "HOME not set");
return (EXIT_FAILURE);
}
} }
else if (argc > 2) else if (argc > 2)
{ return (shell_error("cd: ", "too many arguments", "", EXIT_FAILURE));
shell_error("cd: ", "too many arguments");
return (EXIT_FAILURE);
}
if (chdir(tmp) == -1) if (chdir(tmp) == -1)
{ return (shell_perror("cd: ", tmp, ": ", EXIT_FAILURE));
shell_perror("cd: ", tmp, ": ");
return (EXIT_FAILURE);
}
tmp = init_prompt(c->prompt_base); tmp = init_prompt(c->prompt_base);
if (!tmp) if (!tmp)
return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()")); return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()"));

View File

@@ -6,13 +6,13 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */ /* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */
/* Updated: 2021/12/04 19:09:34 by lperrey ### ########.fr */ /* Updated: 2021/12/05 16:26:48 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void shell_error(char *s1, char *s2) int shell_error(char *s1, char *s2, char *s3, int ret_val)
{ {
char *prefix; char *prefix;
@@ -22,10 +22,13 @@ void shell_error(char *s1, char *s2)
write(STDERR_FILENO, s1, ft_strlen(s1)); write(STDERR_FILENO, s1, ft_strlen(s1));
if (s2) if (s2)
write(STDERR_FILENO, s2, ft_strlen(s2)); write(STDERR_FILENO, s2, ft_strlen(s2));
if (s3)
write(STDERR_FILENO, s3, ft_strlen(s3));
write(STDERR_FILENO, "\n", 1); write(STDERR_FILENO, "\n", 1);
return (ret_val);
} }
void shell_perror(char *s1, char *s2, char *s3) int shell_perror(char *s1, char *s2, char *s3, int ret_val)
{ {
char *prefix; char *prefix;
@@ -38,4 +41,5 @@ void shell_perror(char *s1, char *s2, char *s3)
if (s3) if (s3)
write(STDERR_FILENO, s3, ft_strlen(s3)); write(STDERR_FILENO, s3, ft_strlen(s3));
perror(NULL); perror(NULL);
return (ret_val);
} }

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/12/01 17:16:55 by lperrey ### ########.fr */ /* Updated: 2021/12/05 16:20:55 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -48,7 +48,7 @@ int cmd_find_access(t_cmd *cmd, char *path[])
if (!cmd->path) if (!cmd->path)
{ {
cmd->error = EXIT_CMD_NOT_FOUND; cmd->error = EXIT_CMD_NOT_FOUND;
shell_error(cmd->argv[0], ": command not found"); shell_error(cmd->argv[0], ": ", "command not found", 0);
} }
} }
return (1); return (1);