-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
133 lines (105 loc) · 3.16 KB
/
main.c
File metadata and controls
133 lines (105 loc) · 3.16 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
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
#include "main.h"
int status = 0;
t_builtin g_builtin[] =
{
{.builtin_name = "echo", .foo=call_echo},
{.builtin_name = "env", .foo=call_env}, /* Print environment */
{.builtin_name = "exit", .foo=call_exit}, /* Exit the shell */
{.builtin_name = NULL},
};
int main(int ac, char **av)
{
char *line;
char **args;
printbanner();
while (line = read_line()){
args = split_line(line);
if (args[0] && !strcmp(args[0], "cd")){
if (args[1]){
if (chdir(args[1]) != 0){
perror("chdir failed");
}
} else {
fprintf(stderr, "cd: expected argument\n");
// return EXIT_FAILURE;
}
}
call_exec(args);
free(line);
free(args);
}
(void)ac;
int status;
pid_t child = fork();
if (child == 0){
execvp(av[1], av+1);
}
wait(&status);
return EXIT_SUCCESS;
}
void call_exec(char **args){
int i = 0;
const char *curr;
if (!args || !args[0])
return;
while ((curr = g_builtin[i].builtin_name)){
if (!strcmp(args[0], curr))
{
if ((status = (g_builtin[i].foo)(args)))
printf("%s failed\n", curr);
return ;
}
i++;
}
call_launch(args);
}
void call_launch(char **args)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (pid == 0)
{
// Execvp;
if (!args[0] || !args)
{
fprintf(stderr, "Execvp: invalid arguments\n");
exit(EXIT_FAILURE);
}
if (execvp(args[0], args) == -1)
{
perror("execvp failed");
exit(EXIT_FAILURE);
}
}
else
{
// Wait();
pid_t result;
// if (!status)
// {
// fprintf(stderr, "Wait: status argument required\n");
// return (-1);
// }
result = wait(&status);
if (result == -1)
perror("Wait failed");
if (WIFEXITED(status))
status = WEXITSTATUS(status);
// return (result);
}
}
void printbanner(void){
const char *logo =
"███████╗██╗ ██╗██████╗ █████╗ ███████╗██╗ ██╗███████╗██╗ ██╗ \n"
"██╔════╝██║ ██║██╔══██╗██╔══██╗ ██╔════╝██║ ██║██╔════╝██║ ██║ \n"
"███████╗██║ ██║██████╔╝███████║ ███████╗███████║█████╗ ██║ ██║ \n"
"╚════██║██║ ██║██╔═══╝ ██╔══██║ ╚════██║██╔══██║██╔══╝ ██║ ██║ \n"
"███████║╚██████╔╝██║ ██║ ██║ ███████║██║ ██║███████╗███████╗███████╗\n"
"╚══════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝\n";
printf("%s\n", logo);
}