From a5cb86d0a846e9b7a35e416a3fc6a51a7e2cc36d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 25 Mar 2017 01:50:27 +0100 Subject: [PATCH] redirect file permission, new open_acces function with ft_perror --- 42sh/libft/Makefile | 2 ++ 42sh/libft/includes/error.h | 8 ++++-- 42sh/libft/includes/libft.h | 14 +++++------ 42sh/libft/includes/sys.h | 10 +++++++- 42sh/libft/src/error/error.c | 11 ++++++--- 42sh/libft/src/sys/is_directory.c | 21 ++++++++++++++++ 42sh/libft/src/sys/open_access.c | 38 +++++++++++++++++++++++++++++ 42sh/src/exec/redirect_dgreat.c | 8 +++--- 42sh/src/exec/redirect_dless.c | 2 +- 42sh/src/exec/redirect_great.c | 9 +++---- 42sh/src/exec/redirect_greatand.c | 2 +- 42sh/src/exec/redirect_less.c | 11 +++------ 42sh/src/glob/dir_glob.c | 10 +------- 42sh/src/job_control/builtin_jobs.c | 2 +- 14 files changed, 105 insertions(+), 43 deletions(-) create mode 100644 42sh/libft/src/sys/is_directory.c create mode 100644 42sh/libft/src/sys/open_access.c diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 30571112..8ab633b5 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -197,6 +197,8 @@ sys/fd_replace.c\ sys/ft_getenv.c\ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ +sys/is_directory.c\ +sys/open_access.c\ time/ft_mytime_free.c\ time/ft_mytime_get.c\ time/ft_time_isrecent.c diff --git a/42sh/libft/includes/error.h b/42sh/libft/includes/error.h index 896b5bcb..0f587158 100644 --- a/42sh/libft/includes/error.h +++ b/42sh/libft/includes/error.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */ -/* Updated: 2017/03/24 15:11:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:34:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ # define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ # define DG(s, ...) ft_dprintf(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__) -# define ERR_MSG(s, ...) "{red}%s: " s "{eoc}\n", PROGNAME, ##__VA_ARGS__ +# define ERR_PROTO(u, m) "{red}%s: %s{eoc}\n", u, m +# define ERR_MSG(u, m) ft_dprintf(2, ERR_PROTO(u, m)) # define ERR_SET(n, ...) error_set(n, ##__VA_ARGS__) # define ERRMSG_MAX_SIZE 150 @@ -32,6 +33,9 @@ enum e_errors E_CO_MULT, E_CO_MISS, E_CO_MISSL, + E_SYS_NOFILE, + E_SYS_ISDIR, + E_SYS_NOPERM, E_MAX, }; diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index c9baf04d..8e43ec5a 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:08:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:35:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,12 @@ # include # include # include -# include -# include + +typedef struct s_stos t_stos; +typedef struct s_stof t_stof; +typedef struct s_itof t_itof; +typedef long long t_flag; +typedef long long t_type; # include "error.h" # include "color.h" @@ -37,10 +41,6 @@ # include "get_next_line.h" # include "sys.h" -typedef struct s_stos t_stos; -typedef struct s_stof t_stof; -typedef struct s_itof t_itof; - struct s_stos { char *key; diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index 972805ae..c7a046cb 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/21 16:33:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:42:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,18 @@ # include # include +# include +# include +# include int ft_xattr_print(char *path); int ft_xattr_count(char *path); + char *ft_getenv(char **env, char *key); + +int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); +int is_directory(const char *path); + int dup2_close(int fd1, int fd2); int fd_replace(int fd1, int fd2); diff --git a/42sh/libft/src/error/error.c b/42sh/libft/src/error/error.c index 1f8b980d..971d22f9 100644 --- a/42sh/libft/src/error/error.c +++ b/42sh/libft/src/error/error.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 16:47:00 by jhalford #+# #+# */ -/* Updated: 2017/03/24 16:22:55 by gwojda ### ########.fr */ +/* Updated: 2017/03/25 01:22:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,15 @@ char g_error_msglist[E_MAX][ERRMSG_MAX_SIZE] = { - "no error", + "unknown error 0", "invalid option -%c", "invalid option --%s", "option '%c' awaits argument(s): please don't combine", "option '%c': missing argument", "option '%s': missing argument", + "%s: no such file or directory", + "%s: Is a directory", + "%s: Permission denied", }; int g_errnum = 0; @@ -39,7 +42,7 @@ int error_set(int n, ...) int ft_perror(char *utility) { - ft_dprintf(2, "{red}%s: %s{eoc}\n", utility ? utility : g_argv[0], - g_errmsg); + utility = utility ? utility : g_argv[0]; + ERR_MSG(utility, g_errmsg); return (g_errnum); } diff --git a/42sh/libft/src/sys/is_directory.c b/42sh/libft/src/sys/is_directory.c new file mode 100644 index 00000000..accd92a4 --- /dev/null +++ b/42sh/libft/src/sys/is_directory.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_directory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/25 01:40:31 by jhalford #+# #+# */ +/* Updated: 2017/03/25 01:42:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int is_directory(const char *path) +{ + struct stat path_stat; + + stat(path, &path_stat); + return (S_ISDIR(path_stat.st_mode)); +} diff --git a/42sh/libft/src/sys/open_access.c b/42sh/libft/src/sys/open_access.c new file mode 100644 index 00000000..0f978306 --- /dev/null +++ b/42sh/libft/src/sys/open_access.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open_access.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/25 01:10:56 by jhalford #+# #+# */ +/* Updated: 2017/03/25 01:50:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int is_directory(const char *path) +{ + struct stat path_stat; + + stat(path, &path_stat); + return (S_ISDIR(path_stat.st_mode)); +} + +int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm) +{ + int fd; + + if (access(file, F_OK) != 0) + return (-ERR_SET(E_SYS_NOFILE, file)); + if (is_directory(file)) + return (-ERR_SET(E_SYS_ISDIR, file)); + if (access(file, a_flag)) + return (-ERR_SET(E_SYS_NOPERM, file)); + if ((fd = open(file, o_flag, o_perm)) < 0) + { + exit(1); + } + return (fd); +} diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c index b1164f94..97200090 100644 --- a/42sh/src/exec/redirect_dgreat.c +++ b/42sh/src/exec/redirect_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:07:37 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:15:39 by gwojda ### ########.fr */ +/* Updated: 2017/03/25 01:49:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,9 @@ int redirect_dgreat(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word, - O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) - exit(1); + if ((fdold = open(redir->word, R_OK, + O_WRONLY | O_CREAT | O_APPEND, 0644)) != 0) + return (ft_perror(NULL)); dup2(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_dless.c b/42sh/src/exec/redirect_dless.c index 54aef89a..d8063a57 100644 --- a/42sh/src/exec/redirect_dless.c +++ b/42sh/src/exec/redirect_dless.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 12:36:10 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:36:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:45:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index eeb03b91..20ae5822 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/25 00:57:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:49:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,9 @@ int redirect_great(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word, + if ((fdold = open_access(redir->word, R_OK, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) - { - SH_ERR("open(): %s", strerror(errno)); - exit(1); - } + return (ft_perror(NULL)); fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index d5e2e573..10c979ac 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/03/21 17:41:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 00:58:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index 6c4e5c25..08dd067b 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:36:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 01:49:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,9 @@ int redirect_less(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word, O_RDONLY)) < 0) - { - ft_dprintf(2, "{red}%s: %s: no such file or directory\n", - SHELL_NAME, redir->word); - return (1); - } + if ((fdold = open_access(redir->word, W_OK, + O_RDONLY, 0)) != 0) + return (ft_perror(NULL)); dup2(fdold, fdnew); return (0); } diff --git a/42sh/src/glob/dir_glob.c b/42sh/src/glob/dir_glob.c index d7736c1c..97673350 100644 --- a/42sh/src/glob/dir_glob.c +++ b/42sh/src/glob/dir_glob.c @@ -6,20 +6,12 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/30 12:07:16 by wescande #+# #+# */ -/* Updated: 2017/03/22 21:59:49 by wescande ### ########.fr */ +/* Updated: 2017/03/25 01:39:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int is_directory(const char *path) -{ - struct stat path_stat; - - stat(path, &path_stat); - return (S_ISDIR(path_stat.st_mode)); -} - static void dir_list_content(t_glob *gl, char **str, char *pat, int recursive) { diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index 7ef0e117..be9419c5 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 23:33:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/25 00:58:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */