-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.c
77 lines (62 loc) · 1.33 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
#include "shell.h"
/**
* handle_sigkill - Prevents the shell from exiting using ctrl C
* @sig: Ctrl C signal
*
* Return: Nothing
*/
void handle_sigkill(__attribute__((unused)) int sig)
{
signal(SIGINT, handle_sigkill);
}
/**
* main - Creates a unix command line interpreter
* @argc: Number function arguments
* @argv: Arguments passed to the main function
*
* Return: Always 0 (success)
*/
int main(__attribute__((unused)) int argc, char **argv)
{
char *commands, *exit_cond = "exit";
char **path;
char *prog;
size_t bytes_read = 1;
ssize_t chars_read;
int interactive = 1;
signal(SIGINT, handle_sigkill);
path = getpath();
if (!isatty(STDIN_FILENO))
interactive = 0;
prog = argv[0];
while (1 && argc == 1)
{
if (interactive == 1)
write(STDOUT_FILENO, "$ ", 2);
commands = malloc(sizeof(char *));
chars_read = getline(&commands, &bytes_read, stdin);
if (chars_read == -1)
perror("Error: Could not read file_stream");
if (commands[0] == '\n')
continue;
commands[_strlen(commands) - 1] = '\0';
argv = _strtok(commands, " \t");
if (_strcmp(argv[0], exit_cond) == 0)
{
break;
}
exec_func(argv, path, prog);
if (interactive == 0)
{
break;
}
free(commands);
free(argv);
}
free(path);
if (commands != NULL)
free(commands);
if (argv != NULL)
free(argv);
return (0);
}