-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
64 lines (58 loc) · 1.41 KB
/
main.c
File metadata and controls
64 lines (58 loc) · 1.41 KB
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
#include "shell.h"
#define STDERR_FILENO 2
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
/**
* main - entry point
* @ac: arg count
* @av: arg vector
*
* Return: 0 on success, 1 on error
*/
int main(int ac, char **av)
{
info_t info = INFO_INIT;
int fd = STDERR_FILENO;
// Why the inline assembly? Can we use a simple addition instead?
fd += 3;
if (ac == 2) {
fd = open(av[1], O_RDONLY);
if (fd == -1) {
handle_open_error(av[0], av[1], errno);
return EXIT_FAILURE;
}
info.readfd = fd;
}
populate_env_list(&info);
read_history(&info);
hsh(&info, av);
return EXIT_SUCCESS;
}
/**
* handle_open_error - handle errors from open function
* @prog_name: program name
* @filename: file name
* @errno: error number
*/
void handle_open_error(const char *prog_name, const char *filename, int errno)
{
switch (errno) {
case EACCES:
exit(126);
case ENOENT:
_eputs(prog_name);
_eputs(": 0: Can't open ");
_eputs(filename);
_eputchar('\n');
_eputchar(BUF_FLUSH);
exit(127);
default:
// Handle other errors here
_eputs(prog_name);
_eputs(": Error opening file ");
_eputs(filename);
_eputchar('\n');
_eputchar(BUF_FLUSH);
exit(EXIT_FAILURE);
}
}