-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_shell.c
72 lines (68 loc) · 1.32 KB
/
simple_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
#include "shell.h"
/**
* exec_line - This function executes a line
* @str: Pointer to string
*/
void exec_line(char *str)
{
char **argv;
int execFlag, w_len;
execFlag = special_circ(str);
if (execFlag == -1)
{
w_len = count_input(str);
if (str[0] != '\n' && w_len > 0)
{
argv = tokenize(str, " \t", w_len);
execFlag = builtin_functions(argv, str);
if (!execFlag)
{
argv[0] = find(argv[0]);
if (argv[0] && access(argv[0], X_OK) == 0)
execute(argv[0], argv);
else
perror("./hsh");
}
frees_tokens(argv);
}
}
}
/**
* simple_shell - This function executes a commands from file
* @string: Name of file
*/
void simple_shell(char *string)
{
FILE *fp;
char buffer[128];
char *lineptr = NULL;
int n = 20;
fp = fopen(string, "r");
if (!fp)
{
fprintf(stderr, "./hsh: 0: Can't open %s\n", string);
exit(127);
}
lineptr = malloc(sizeof(char) * 20);
lineptr[0] = '\0';
while ((fgets(buffer, sizeof(buffer), fp)) != NULL)
{
if (n - _strlen(lineptr) < sizeof(buffer))
{
lineptr = _realloc(lineptr, n, n * 2);
n *= 2;
if (lineptr == NULL)
{
perror("Failed to allocate memory");
free(lineptr);
exit(127);
}
}
_strcpy(lineptr, buffer);
if ((lineptr)[_strlen(lineptr) - 1] == '\n')
exec_line(lineptr);
}
fclose(fp);
free(lineptr);
exit(exit_status);
}