-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_loop.c
More file actions
66 lines (58 loc) · 1.12 KB
/
shell_loop.c
File metadata and controls
66 lines (58 loc) · 1.12 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
/*
** EPITECH PROJECT, 2025
** B-PSU-200-LYN-2-1-minishell1-pierre.baud
** File description:
** shell_loop
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "my.h"
int my_exit(char **args)
{
if (args[1] != NULL) {
write(2, "exit: Trop d'arguments\n", 22);
return 1;
}
exit(0);
}
char *read_input(void)
{
char *line = NULL;
size_t len = 0;
ssize_t entry;
write(1, "$> ", 3);
entry = getline(&line, &len, stdin);
if (entry == -1) {
free(line);
return NULL;
}
return line;
}
void process_input(char *line, char ***envc)
{
char *cmd;
char *saveptr;
char **args;
cmd = strtok_r(line, ";", &saveptr);
while (cmd != NULL) {
args = parse_command(cmd);
if (args[0] != NULL) {
execute_command(args, envc);
}
free(args);
cmd = strtok_r(NULL, ";", &saveptr);
}
}
void shell_loop(char ***envc)
{
char *line;
while (1) {
line = read_input();
if (line == NULL) {
break;
}
process_input(line, envc);
}
}