-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
74 lines (65 loc) · 1.5 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
/*
** EPITECH PROJECT, 2022
** Untitled (Workspace)
** File description:
** main
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
//Recode function usefull like strtowordarray strcat etc...
void print_prompt(void)
{
printf("\033[1;32m");
if (isatty(STDIN_FILENO) == 1)
printf("$> ");
printf("\033[0m");
}
void my_pipe() {
int fd[2];
pid_t pid;
pipe(fd);
pid = fork();
if (pid == 0) {
// use close to close the read end of the pipe
// dup2 to redirect the write end of the pipe to stdout
// execute the first command (ls | cat -e == ls)
} else {
// use close to close the write end of the pipe
// dup2 to redirect the read end of the pipe to stdin
// execute the second command (ls | cat -e == cat -e)
}
}
void exec_cmd(char *buffer, char **env)
{
char **cmd = my_str_to_word_array(buffer, " \t\n");
if (//si il y a un pipe)
my_pipe();
else
printf("%s: command not found\n", cmd[0]);
exit(0);
}
int main(int argc, char **argv, char **env)
{
char *buffer = NULL;
size_t len = 0;
print_prompt();
while (getline(&buffer, &len, stdin) != -1) {
pid_t pid = fork();
if (pid == 0) {
exec_cmd(buffer, env);
}
else {
wait(NULL);
}
print_prompt();
}
return (0);
}