-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
79 lines (76 loc) · 1.56 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
75
76
77
78
79
#include "shell.h"
char **argv;
char *cmdline_copy = NULL;
int argc;
char *commandline = NULL;
char *prog;
int execution_count = 0;
/**
* main - Get user input
*
* Return: 0 (success)
*/
int main(int ac, char **av)
{
int cont = 1, numtokens = 0, i;
char *prompt = "", exit_msg[] = "exit\n";
size_t n = 0;
ssize_t nchars_read;
prog = av[(ac - ac)];
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
prompt = "($) ";
while (cont)
{
execution_count++;
write(STDIN_FILENO, prompt, strlen(prompt));
nchars_read = getline(&commandline, &n, stdin);
if (nchars_read == -1)
{
if (feof(stdin))
{
free(commandline);
return (-1);
}
write(STDOUT_FILENO, exit_msg, strlen(exit_msg));
free(commandline);
return (-1);
}
if (commandline[nchars_read - 1] == '\n')
commandline[nchars_read - 1] = '\0';
cmdline_copy = malloc(sizeof(char) * nchars_read);
if (cmdline_copy == NULL)
{
perror("");
free(commandline);
return (-1);
}
strcpy(cmdline_copy, commandline);
numtokens = count_tokens(commandline);
argc = numtokens + 1;
argv = malloc(sizeof(char *) * argc);
if (argv == NULL)
{
perror("");
free(cmdline_copy);
free(commandline);
return (-1);
}
store_tokens(cmdline_copy, argv);
if (strcmp(argv[0], "exit") == 0)
{
handle_exit();
cont = 0;
}
if (strcmp(argv[0], "cd") == 0)
changeDirectory(argv);
if (strcmp(argv[0], "env") == 0)
handle_env();
execute_cmd(argv);
for (i = 0; i < argc; i++)
free(argv[i]);
free(cmdline_copy);
}
free(argv);
free(commandline);
return (0);
}