-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
80 lines (70 loc) · 1.22 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "shell.h"
/**
* main - simple unix like program.
*
* @argc: Nth void arguments.
* @argv: Array of command
*
* Return: Execute the command entered by user.
*/
int main(int argc, char **argv)
{
char *buffer = NULL, *delim = " \n";
ssize_t line = 0;
size_t nread = 0;
int status = 0;
(void)argc;
do {
if (isatty(STDIN_FILENO))
printf(":) ");
line = getline(&buffer, &nread, stdin);
if (line == -1 || hsh_strcmp("exit\n", buffer) == 0)
{
free(buffer);
break;
}
buffer[line - 1] = '\0';
if (hsh_strcmp("env", buffer) == 0)
{
hsh_env();
continue;
}
if (hsh_line(buffer) == 1)
{
status = 0;
continue;
}
argv = hsh_parse(buffer, delim);
argv[0] = hsh_path(argv[0]);
if (argv[0] != NULL)
status = execute(argv);
else
perror("Error");
free(argv);
} while (1);
return (status);
}
/**
* execute - execute the given command.
*
* @argv: arguments passed by stdin
*
* Return: Always Status(success).
*/
int execute(char **argv)
{
int checked, pid;
pid = fork();
if (pid == 0)
{
if (execve(argv[0], argv, environ) == -1)
perror("Error");
}
else
{
wait(&checked);
if (WIFEXITED(checked))
checked = WEXITSTATUS(checked);
}
return (checked);
}