redirect file permission, new open_acces function with ft_perror

This commit is contained in:
Jack Halford 2017-03-25 01:50:27 +01:00
parent 0028d5ac4d
commit a5cb86d0a8
14 changed files with 105 additions and 43 deletions

View file

@ -197,6 +197,8 @@ sys/fd_replace.c\
sys/ft_getenv.c\ sys/ft_getenv.c\
sys/ft_xattr_count.c\ sys/ft_xattr_count.c\
sys/ft_xattr_print.c\ sys/ft_xattr_print.c\
sys/is_directory.c\
sys/open_access.c\
time/ft_mytime_free.c\ time/ft_mytime_free.c\
time/ft_mytime_get.c\ time/ft_mytime_get.c\
time/ft_time_isrecent.c time/ft_time_isrecent.c

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 15:34:21 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_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__
# define DG(s, ...) ft_dprintf(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__) # 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 ERR_SET(n, ...) error_set(n, ##__VA_ARGS__)
# define ERRMSG_MAX_SIZE 150 # define ERRMSG_MAX_SIZE 150
@ -32,6 +33,9 @@ enum e_errors
E_CO_MULT, E_CO_MULT,
E_CO_MISS, E_CO_MISS,
E_CO_MISSL, E_CO_MISSL,
E_SYS_NOFILE,
E_SYS_ISDIR,
E_SYS_NOPERM,
E_MAX, E_MAX,
}; };

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 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 <stdio.h> # include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
# include <time.h> # include <time.h>
# include <sys/xattr.h>
# include <sys/acl.h> 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 "error.h"
# include "color.h" # include "color.h"
@ -37,10 +41,6 @@
# include "get_next_line.h" # include "get_next_line.h"
# include "sys.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 struct s_stos
{ {
char *key; char *key;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 17:24:23 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 <sys/types.h> # include <sys/types.h>
# include <sys/xattr.h> # include <sys/xattr.h>
# include <sys/acl.h>
# include <sys/stat.h>
# include <fcntl.h>
int ft_xattr_print(char *path); int ft_xattr_print(char *path);
int ft_xattr_count(char *path); int ft_xattr_count(char *path);
char *ft_getenv(char **env, char *key); 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 dup2_close(int fd1, int fd2);
int fd_replace(int fd1, int fd2); int fd_replace(int fd1, int fd2);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 16:47:00 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] = char g_error_msglist[E_MAX][ERRMSG_MAX_SIZE] =
{ {
"no error", "unknown error 0",
"invalid option -%c", "invalid option -%c",
"invalid option --%s", "invalid option --%s",
"option '%c' awaits argument(s): please don't combine", "option '%c' awaits argument(s): please don't combine",
"option '%c': missing argument", "option '%c': missing argument",
"option '%s': missing argument", "option '%s': missing argument",
"%s: no such file or directory",
"%s: Is a directory",
"%s: Permission denied",
}; };
int g_errnum = 0; int g_errnum = 0;
@ -39,7 +42,7 @@ int error_set(int n, ...)
int ft_perror(char *utility) int ft_perror(char *utility)
{ {
ft_dprintf(2, "{red}%s: %s{eoc}\n", utility ? utility : g_argv[0], utility = utility ? utility : g_argv[0];
g_errmsg); ERR_MSG(utility, g_errmsg);
return (g_errnum); return (g_errnum);
} }

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_directory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open_access.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:07:37 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; int fdnew;
fdnew = redir->n; fdnew = redir->n;
if ((fdold = open(redir->word, if ((fdold = open(redir->word, R_OK,
O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) O_WRONLY | O_CREAT | O_APPEND, 0644)) != 0)
exit(1); return (ft_perror(NULL));
dup2(fdold, fdnew); dup2(fdold, fdnew);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 12:36:10 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:03:53 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; int fdnew;
fdnew = redir->n; fdnew = redir->n;
if ((fdold = open(redir->word, if ((fdold = open_access(redir->word, R_OK,
O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
{ return (ft_perror(NULL));
SH_ERR("open(): %s", strerror(errno));
exit(1);
}
fd_replace(fdold, fdnew); fd_replace(fdold, fdnew);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:12:31 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:09:53 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; int fdnew;
fdnew = redir->n; fdnew = redir->n;
if ((fdold = open(redir->word, O_RDONLY)) < 0) if ((fdold = open_access(redir->word, W_OK,
{ O_RDONLY, 0)) != 0)
ft_dprintf(2, "{red}%s: %s: no such file or directory\n", return (ft_perror(NULL));
SHELL_NAME, redir->word);
return (1);
}
dup2(fdold, fdnew); dup2(fdold, fdnew);
return (0); return (0);
} }

View file

@ -6,20 +6,12 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 12:07:16 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" #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, static void dir_list_content(t_glob *gl, char **str, char *pat,
int recursive) int recursive)
{ {

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 17:43:01 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */