Commit 18ea4e58 authored by pêle-mêle's avatar pêle-mêle
Browse files

write errors

parent 06349ffc
......@@ -6,13 +6,13 @@
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 13:46:48 by abenoit #+# #+# */
/* Updated: 2021/01/12 00:05:04 by mvidal-a ### ########.fr */
/* Updated: 2021/01/31 13:20:46 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_printarray_fd(char **array, int fd)
int ft_printarray_fd(char **array, int fd)
{
size_t i;
......@@ -20,8 +20,11 @@ void ft_printarray_fd(char **array, int fd)
if (array != NULL)
while (array[i] != NULL)
{
ft_putstr_fd(array[i], fd);
ft_putchar_fd('\n', fd);
if (ft_putstr_fd(array[i], fd) != WRITE_SUCCESS)
return (FAILURE);
if (ft_putchar_fd('\n', fd) != SUCCESS)
return (FAILURE);
i++;
}
return (SUCCESS);
}
......@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/04 12:29:06 by mvidal-a #+# #+# */
/* Updated: 2021/01/28 17:14:54 by mvidal-a ### ########.fr */
/* Updated: 2021/01/31 19:00:44 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
......@@ -17,11 +17,14 @@
# include <stdint.h>
# include <sys/types.h>
# define ERROR -1
# define ERROR -1
# define TRUE 1
# define FALSE 0
# define SUCCESS 1
# define FAILURE 0
# define WRITE_SUCCESS -2
typedef struct s_list
......@@ -112,7 +115,7 @@ size_t ft_varnamelen(char *str);
char *get_var_value(char **vars, char *name, size_t name_len);
ssize_t get_var_pos(char **vars, char *name, size_t name_len);
void ft_printarray_fd(char **array, int fd);
int ft_printarray_fd(char **array, int fd);
size_t ft_arraylen(char **array);
void free_str_array(char **array);
char **dup_str_array(char **array);
......
......@@ -35,6 +35,10 @@ echo '"'
masaki:
a la fin avec rgrep:
erreurs de write et de ft_put* a terminer
gestion d'erreurs des fonctions autorisees une a une
......@@ -48,6 +52,8 @@ cd: leaks in getcwd
"exit 42" doit exit en renvoyant 42
tester sur bash "exit 42 1431" ou "exit fds"
strerror au lieu de perror qui est pas autorise
......
......@@ -22,7 +22,7 @@ int ft_cd(char **args, t_xe *xe)
oldpwd = getcwd(NULL, 0); // error
if (chdir(path) == ERROR)
{
perror("cd");
perror("cd"); // -> strerror
free(path);
free(oldpwd);
xe->stat_loc = 1;
......
......@@ -13,12 +13,15 @@ int ft_echo(char **args, t_xe *xe)
}
while (*args != NULL)
{
ft_putchar_fd(' ', STDOUT_FILENO);
ft_putstr_fd(*args, STDOUT_FILENO);
if (ft_putchar_fd(' ', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (ft_putstr_fd(*args, STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
args++;
}
if (!(options & ECHO_N_OPTION))
ft_putchar_fd('\n', STDOUT_FILENO);
if (ft_putchar_fd('\n', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
xe->stat_loc = 0;
return (SUCCESS);
}
......@@ -3,7 +3,8 @@
int ft_env(char **args, t_xe *xe)
{
(void)args;
ft_printarray_fd(xe->env, STDOUT_FILENO);
if (ft_printarray_fd(xe->env, STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
xe->stat_loc = 0;
return (SUCCESS);
}
......@@ -18,15 +18,31 @@ char **sort_variables(char **env, char **exported)
return (sorted_array);
}
void print_export_var_value(char *value)
int print_export_var_value(char *value)
{
while (*value != '\0')
{
if (ft_isset(*value, "$\"\\`") == TRUE)
ft_putchar_fd('\\', STDOUT_FILENO);// change fd
ft_putchar_fd(*value, STDOUT_FILENO);// change fd
if (ft_putchar_fd('\\', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd(*value, STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
value++;
}
return (SUCCESS);
}
int print_export_one_var(char *variable, ssize_t equalsign_pos)
{
if (write(STDOUT_FILENO, variable, equalsign_pos + 1) != equalsign_pos + 1)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (print_export_var_value(variable + equalsign_pos + 1) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
return (SUCCESS);
}
int print_export(char **env, char **exported)
......@@ -34,6 +50,7 @@ int print_export(char **env, char **exported)
char **variables;
size_t i;
ssize_t equalsign_pos;
int ret;
variables = sort_variables(env, exported);
if (variables == NULL)
......@@ -41,22 +58,20 @@ int print_export(char **env, char **exported)
i = 0;
while (variables[i] != NULL)
{
ft_putstr_fd("declare -x ", STDOUT_FILENO);// change fd
ret = ft_putstr_fd("declare -x ", STDOUT_FILENO);
equalsign_pos = ft_index(variables[i], '=');
if (equalsign_pos != NOT_FOUND)
{
write(STDOUT_FILENO, variables[i], equalsign_pos + 1);// idem
ft_putchar_fd('"', STDOUT_FILENO); // idem
print_export_var_value(variables[i] + equalsign_pos + 1);
ft_putchar_fd('"', STDOUT_FILENO); // id
}
else
ft_putstr_fd(variables[i], STDOUT_FILENO); //id
ft_putchar_fd('\n', STDOUT_FILENO); // id
if (equalsign_pos != NOT_FOUND && ret == WRITE_SUCCESS)
ret = print_export_one_var(variables[i], equalsign_pos);
if (ret == SUCCESS || ret == WRITE_SUCCESS)
ret = ft_putstr_fd(variables[i], STDOUT_FILENO);
if (ret == WRITE_SUCCESS)
ret = ft_putchar_fd('\n', STDOUT_FILENO);
if (ret != SUCCESS)
break ;
i++;
}
free(variables);
return (SUCCESS);
return (ret);
}
static int ft_env_append_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
......@@ -208,16 +223,12 @@ static int ft_export_checks(char **args, t_xe *xe)
int ft_export(char **args, t_xe *xe)
{
int ret;
if (ft_arraylen(args) == 1)
{
if (print_export(xe->env, xe->exported) != SUCCESS)
return (MALLOC_ERR);
}
ret = print_export(xe->env, xe->exported);
else
{
if (ft_export_checks(args + 1, xe) != SUCCESS)
return (MALLOC_ERR);
}
ret = ft_export_checks(args + 1, xe);
xe->stat_loc = 0;
return (SUCCESS);
return (ret);
}
......@@ -13,7 +13,11 @@ int ft_pwd(char **args, t_xe *xe)
xe->stat_loc = 1;
return (SUCCESS); // other code!
}
ft_putendl_fd(buf, STDOUT_FILENO);
if (ft_putendl_fd(buf, STDOUT_FILENO) != WRITE_SUCCESS)
{
free(buf);
return (WRITE_ERR);
}
free(buf);
xe->stat_loc = 0;
return (SUCCESS);
......
......@@ -239,7 +239,7 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
if (cmd_code == M_ERROR)
return (MALLOC_ERR);
ret = command[cmd_code](args, xe);
if (ret >= HOME_NOT_SET)
return (ret);
if (ret >= HOME_NOT_SET)// ?
return (ret);// ?
return (ret);
}
#include "minishell.h"
#include "execution.h"
#include "parsing.h"
#include <stdio.h> // perror
#include <string.h> // strerror
int parsing_error(int err_code, t_xe *xe)
{
......@@ -13,7 +14,6 @@ int parsing_error(int err_code, t_xe *xe)
"No redirection path specified",
"Empty command before ; or |"};
(void)err_msg;
ft_putstr_fd("minishell: syntax error: ", STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _PARSING_ERROR_ - 1], STDERR_FILENO);
xe->stat_loc = 2;
......@@ -28,7 +28,6 @@ int exec_error(int err_code, t_xe *xe)
"HOME not set",
"No such file or directory"};
(void)err_msg;
ft_putstr_fd("minishell: command error: ", STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
// have to set stat_loc as well !!!
......@@ -42,20 +41,17 @@ int exec_error(int err_code, t_xe *xe)
return (SUCCESS);
}
static const char *err_msg(int err_code)
static int err_output(int err_code)
{
const char *msg[] = {
const char *err_msg[] = {
"Memory allocation failure",
"Cannot write on standard output",
"Cannot read standard input (GNL error)"};
return (msg[err_code]);
}
static int err_output(int err_code)
{
ft_putstr_fd("Error: ", STDERR_FILENO);
perror(err_msg(err_code - _ERRNO_MSG_));
ft_putstr_fd(err_msg[err_code - _ERRNO_MSG_], STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO); // besoin de faire comme perror?
ft_putendl_fd(strerror(errno), STDERR_FILENO); // strerror error?
return (SUCCESS);
}
......@@ -68,7 +64,8 @@ int clean_and_exit(int ret, t_xe *xe)
ft_putstr_fd("Minishell takes no argument", STDERR_FILENO);
if (ret != CHILD_EXIT)
if (isatty(STDIN_FILENO)) // temp pour le testeur
write(1, "exit\n", 5);
if (ft_putstr_fd("exit\n", STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR); // possible?
exit(EXIT_SUCCESS);
}
......
......@@ -26,7 +26,7 @@ static int get_input(char **line)
if (isatty(STDIN_FILENO)) // temp pour le testeur
{
if (ft_putstr_fd(FT_PS1, STDOUT_FILENO) == ERROR)
if (ft_putstr_fd(FT_PS1, STDOUT_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
ret = get_next_line(STDIN_FILENO, line);
......
No preview for this file type
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment