-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_shell.c
60 lines (58 loc) · 1.12 KB
/
run_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
#include "shell.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/**
* run_shell - run the shell
*
* @prog_name: the programe name
* @sign: the prompt sign
* @line: the command line
* @nread: chars read by getline
* @lines: lines run
* @last_exit_code: lines run
*
* Return: void
*/
void run_shell(char *prog_name, char *sign, char *line, size_t nread,
size_t lines, int *last_exit_code)
{
/*tokens array */
char *tokens[TOKEN_NUM];
char *command;
ssize_t is_f;
if (nread == 0)
prompt(sign);
else
{
line[nread - 1] = '\0';
tokenize(line, tokens);
command = tokens[0];
is_f = is_exec(command);
if (!command || is_whitespace(*command))
;
else if (comp_str(command, "exit") == 0)
{
free(line);
my_exit(*last_exit_code);
}
else if (comp_str(command, "env") == 0)
{
_env(environ);
*last_exit_code = 0;
}
else if (is_f == 1)
*last_exit_code = execmd(prog_name, tokens);
else if (in_path(command, tokens) == 1)
{
execmd(prog_name, tokens);
free(tokens[0]);
}
else
{
printerr(prog_name, lines, command);
*last_exit_code = 127;
}
}
prompt(sign);
}