/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_errno.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:40:46 by khais #+# #+# */ /* Updated: 2025/03/21 16:18:57 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_errno.h" #include "libft.h" #include #include /* ** get or set ft_errno ** ** if errno is FT_EGET, return ft_errno and do not set it ** ** else set ft_errno to errno, and return the previous value of ft_errno */ t_errno ft_errno(t_errno errno) { static t_errno ft_errno = FT_ESUCCESS; t_errno ft_errno_backup; if (errno != FT_EGET) { ft_errno_backup = ft_errno; ft_errno = errno; return (ft_errno_backup); } return (ft_errno); } /* ** get the current value of ft_errno */ t_errno ft_errno_get(void) { return (ft_errno(FT_EGET)); } char *ft_strerror(t_errno errno) { static char *strs[] = { [FT_ESUCCESS] = "Success", [FT_EINVAL] = "Invalid argument", [FT_EBADID] = "Bad identifier", [FT_EUNEXPECTED_PIPE] = "minishell: syntax error near unexpected token `|'", [FT_EMALFORMED_REDIRECTION] = "minishell: malformed redirection (perhaps a \ missing filename)", [FT_ENOMEM] = "Cannot allocate memory", [FT_STATFAIL] = "minishell: stat: internal failure", [FT_ISDIR] = "Is a directory", }; if (errno >= 0 && errno < FT_EMAXERRNO) return (strs[errno]); return ("Invalid error code"); } t_errno ft_perror(char *desc) { if (ft_errno_get() == FT_EERRNO) perror(desc); else { if (desc != NULL && desc[0] != '\0') ft_dprintf(STDERR_FILENO, "%s: ", desc); ft_dprintf(STDERR_FILENO, "%s\n", ft_strerror(ft_errno_get())); } return (ft_errno_get()); }