cd refactoring, error_wrappers
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
||||
// Error wrappers
|
||||
void shell_error(char *s1, char *s2);
|
||||
void shell_perror(char *s1, char *s2, char *s3);
|
||||
int shell_error(char *s1, char *s2, char *s3, int ret_val);
|
||||
int shell_perror(char *s1, char *s2, char *s3, int ret_val);
|
||||
|
||||
// Generic
|
||||
char *ft_strjoinfree(char *s1, char *s2);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* 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");
|
||||
if (!tmp)
|
||||
{
|
||||
shell_error("cd: ", "HOME not set");
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
return (shell_error("cd: ", "HOME not set", "", EXIT_FAILURE));
|
||||
}
|
||||
else if (argc > 2)
|
||||
{
|
||||
shell_error("cd: ", "too many arguments");
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
return (shell_error("cd: ", "too many arguments", "", EXIT_FAILURE));
|
||||
if (chdir(tmp) == -1)
|
||||
{
|
||||
shell_perror("cd: ", tmp, ": ");
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
return (shell_perror("cd: ", tmp, ": ", EXIT_FAILURE));
|
||||
tmp = init_prompt(c->prompt_base);
|
||||
if (!tmp)
|
||||
return (ft_reti_perror(EXIT_FAILURE, "builtin_cd, init_prompt()"));
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
void shell_error(char *s1, char *s2)
|
||||
int shell_error(char *s1, char *s2, char *s3, int ret_val)
|
||||
{
|
||||
char *prefix;
|
||||
|
||||
@@ -22,10 +22,13 @@ void shell_error(char *s1, char *s2)
|
||||
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));
|
||||
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;
|
||||
|
||||
@@ -38,4 +41,5 @@ void shell_perror(char *s1, char *s2, char *s3)
|
||||
if (s3)
|
||||
write(STDERR_FILENO, s3, ft_strlen(s3));
|
||||
perror(NULL);
|
||||
return (ret_val);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user