-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_exist_and_permission_inchild.c
89 lines (81 loc) · 2.25 KB
/
check_exist_and_permission_inchild.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_exist_and_permission_inchild.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aouchaad <aouchaad@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 15:24:10 by aouchaad #+# #+# */
/* Updated: 2023/06/23 15:09:33 by aouchaad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int read_fromf_case(char *file)
{
int cread;
int fd;
int exist;
exist = access(file, F_OK);
if (exist == -1)
{
print_error(file, strerror(errno));
exit (1);
}
else
{
cread = access(file, R_OK);
if (cread == -1)
{
print_error(file, strerror(errno));
exit (1);
}
}
fd = open(file, O_RDONLY, 0777);
return (fd);
}
static int file_not_exist(char *file, int type, int fd)
{
if (file[0] == '\0')
{
print_error(file, "No such file or directory");
exit (1);
}
if (type == write_inf)
fd = open(file, O_CREAT | O_RDWR | O_TRUNC, 0777);
else
fd = open(file, O_CREAT | O_APPEND | O_RDWR, 0777);
return (fd);
}
static int write_inf_and_append_case(char *file, int type, int fd, int exist)
{
int cwrite;
exist = access(file, F_OK);
if (exist == -1)
return (file_not_exist(file, type, fd));
else
{
cwrite = access(file, W_OK);
if (cwrite == -1)
{
print_error(file, strerror(errno));
exit (1);
}
}
if (type == write_inf)
fd = open(file, O_RDWR | O_TRUNC, 0777);
else
fd = open(file, O_RDWR | O_APPEND, 0777);
return (fd);
}
int check_exist_and_permission_inchild(char *file, int type)
{
int fd;
int exist;
fd = -2;
exist = -2;
if (type == read_fromf)
return (read_fromf_case(file));
else if (type == write_inf || type == append)
return (write_inf_and_append_case(file, type, fd, exist));
return (-1);
}