redirect file permission, new open_acces function with ft_perror
This commit is contained in:
parent
0028d5ac4d
commit
a5cb86d0a8
14 changed files with 105 additions and 43 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdlib.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 "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;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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/xattr.h>
|
||||
# include <sys/acl.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
21
42sh/libft/src/sys/is_directory.c
Normal file
21
42sh/libft/src/sys/is_directory.c
Normal 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));
|
||||
}
|
||||
38
42sh/libft/src/sys/open_access.c
Normal file
38
42sh/libft/src/sys/open_access.c
Normal 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);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,20 +6,12 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue