forked from Katalyst99/Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
63 lines (56 loc) · 878 Bytes
/
shell.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
#include "main.h"
/**
* main - shell entry point
* Return: Always 0
*/
int main(int __attribute__((unused))argc, char __attribute__((unused))**argv)
{
size_t i = 0;
char *lineptr = NULL;
char **args;
ssize_t rc = 0;
int r_status = 0;
while (1)
{
if(isatty(STDIN_FILENO) == 1)
{
write(STDOUT_FILENO, "$ ", 2);
}
rc = getline(&lineptr, &i, stdin);
if (rc == -1)
{
if (isatty(STDIN_FILENO) == 1)
{
write(STDOUT_FILENO, "\n", 1);
}
free(lineptr);
break;
}
if (_strcmp(lineptr, "exit") == 0)
{
exit_sshell();
}
if (_strcmp(lineptr, "env") == 0)
{
print_env();
}
args = split_tok(lineptr, rc);
if (args[0] != NULL)
{
r_status = exec(args);
}
else
{
perror ("Error");
}
free(args);
}
return (r_status);
}
/**
* exit_sshell - exits the simple shell
*/
void exit_sshell(void)
{
exit(0);
}