-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
49 lines (47 loc) · 1.41 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
#include "main.h"
/**
* main - Entry point of the simple shell program.
* @argc: Number of command-line arguments.
* @argv: Array of command-line arguments.
*
* Return: 0 on success, 1 on error.
*/
int main(int argc, char **argv)
{
char *delims = " \t\n", *cmdpath, *inputline = NULL, **token_array;
int linelen = 0, toklen, status = 0;
size_t n;
if (argc > 1)
{ /* non-interactive mode (run once, exit) */
cmdpath = get_path(argv[1]);
status = run_cmd(cmdpath, &argv[1]);
free(cmdpath);
}
else
{ /* interactive mode */
if (isatty(STDIN_FILENO))/* if in user interactive mode */
printf("--------\nWelcome to $$hell!\nGo away\n--------\n");
do { /* loop indefinitely */
if (isatty(STDIN_FILENO))
printf("(ง'̀-'́)ง "); /* shell with an attitude */
linelen = getline(&inputline, &n, stdin);/* get and measure input */
toklen = count_tokens(inputline, delims);
if (linelen > 1 && toklen > 0) /* is input valid ? */
{ /* construct token array and get command path */
token_array = create_tok_array(inputline, delims, ++toklen);
cmdpath = get_path(*token_array);
if (!strcmp(*token_array, "exit")) /* is the command exit ? */
{
free(cmdpath);
free(token_array);
break;
}
status = run_cmd(cmdpath, token_array); /* attempt execute command */
free(cmdpath);
free(token_array);
}
} while (linelen > -1);
}
free(inputline);
return (status);
}