-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
155 lines (141 loc) · 2.57 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "shell.h"
#include <limits.h>
/**
* init - initialize the shell's state
*
* @prog: the program name
* @env: the process' environment variables
*
* Return: the shell's state
*/
state *init(char *prog, char **env)
{
state *self = malloc(sizeof(state));
self->lineno = 1;
self->aliases = NULL;
self->env = from_strarr(env);
self->prog = prog;
self->_errno = 0;
self->content = NULL;
self->lines = NULL;
self->tokens = NULL;
self->errno_buf = malloc(NCHARS(12));
self->pid_buf = format("%d", getpid());
self->parts = NULL;
self->command = NULL;
self->fd = 0;
self->buf = NULL;
return (self);
}
/**
* deinit - destroy the shell's state
*
* @self: the shell's state
*
* Return: nothing
*/
void deinit(state *self)
{
if (!self)
return;
free_list(self->aliases);
free_list(self->env);
free(self->content);
free(self->lines);
free(self->tokens);
free(self->parts);
if (self->fd)
close(self->fd);
free(self->pid_buf);
free(self->errno_buf);
free(self->command);
free(self->buf);
free(self);
}
/**
* cleanup - routine clean up that frees up memory in the state
* @self: the shell's state
* Return: nothing
*/
void cleanup(state *self)
{
if (!self)
return;
free(self->content);
self->content = NULL;
free(self->lines);
self->lines = NULL;
free(self->parts);
self->parts = NULL;
free(self->tokens);
self->tokens = NULL;
free(self->command);
self->command = NULL;
}
/**
* open_file - used by the main function to open a file
*
* @self: the shell's state
* @path: the path of the file
*
* Return: the open file descriptor
*/
int open_file(state *self, char *path)
{
int fd;
if (access(path, F_OK) == -1)
{
fprinterr(format(
"%s: %d: cannot open %s: No such file\n",
self->prog, 0, path));
deinit(self);
exit(2);
}
if (access(path, R_OK) == -1)
{
{
fprinterr(format(
"%s: %d: cannot open %s: Permission denied\n",
self->prog, 0, path));
deinit(self);
exit(2);
}
}
fd = open(path, O_RDONLY);
if (fd == -1)
{
perror(self->prog);
deinit(self);
exit(EXIT_FAILURE);
}
self->fd = fd;
return (fd);
}
/**
* main - entry point
*
* @ac: number of argument variables
* @av: array of argument variables
* @env: array of environment variables
*
* Return: alway (0)
*/
int main(int ac, char **av, char **env)
{
int status;
state *self;
int fd;
(void)ac;
fd = STDIN_FILENO;
signal(SIGINT, SIG_IGN);
self = init(av[0], env);
if (av[1])
fd = open_file(self, av[1]);
if (isatty(fd))
interactive(self);
else
non_interactive(self, fd);
status = self->_errno;
deinit(self);
return (status);
}