minishell/src/ft_errno.c

78 lines
2.3 KiB
C
Raw Normal View History

2025-02-17 16:52:17 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_errno.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:46 by khais #+# #+# */
2025-03-21 18:54:10 +01:00
/* Updated: 2025/03/21 16:18:57 by jguelen ### ########.fr */
2025-02-17 16:52:17 +01:00
/* */
/* ************************************************************************** */
#include "ft_errno.h"
#include "libft.h"
#include <stdio.h>
#include <unistd.h>
/*
** 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)",
2025-03-21 18:54:10 +01:00
[FT_ENOMEM] = "Cannot allocate memory",
[FT_STATFAIL] = "minishell: stat: internal failure",
[FT_ISDIR] = "Is a directory",
2025-02-17 16:52:17 +01:00
};
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());
}