-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_loop.c
More file actions
157 lines (147 loc) · 2.75 KB
/
shell_loop.c
File metadata and controls
157 lines (147 loc) · 2.75 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "shell.h"
/**
* hsh - main loop for shell
* @inf: rerurn and paremeter struct
* @av: the arg vector
* Return: 0 on success, 1 on error, or error code
*/
int hsh(info_t *inf, char **av)
{
ssize_t r = 0;
int builtin_ret = 0;
while (r != -1 && builtin_ret != -2)
{
clear_info(inf);
if (interactive(inf))
_puts("$ ");
_eputchar(BUF_FLUSH);
r = get_input(inf);
if (r != -1)
{
set_info(inf, av);
builtin_ret = find_builtin(inf);
if (builtin_ret == -1)
find_cmd(inf);
}
else if (interactive(inf))
_putchar('\n');
free_info(inf, 0);
}
write_history(inf);
free_info(inf, 1);
if (!interactive(inf) && inf->status)
exit(inf->status);
if (builtin_ret == -2)
{
if (inf->err_num == -1)
exit(inf->status);
exit(inf->err_num);
}
return (builtin_ret);
}
/**
* find_builtin - finds builtin
* @inf: return and parameter strcuct
* Return: -1 if builtin isn't found,
* 0 if builtin issuccessfully,
* 1 if unsuccessful builin
* 2 if builtin signals
*/
int find_builtin(info_t *inf)
{
int i, built_in_ret = -1;
builtin_table builtintbl[] = {
{"exit", _myexit},
{"env", _myenv},
{"help", _myhelp},
{"history", _myhistory},
{"setenv", _mysetenv},
{"unsetenv", _myunsetenv},
{"cd", _mycd},
{"alias", _myalias},
{NULL, NULL}
};
for (i = 0; builtintbl[i].type; i++)
if (_strcmp(inf->argv[0], builtintbl[i].type) == 0)
{
inf->line_count++;
built_in_ret = builtintbl[i].func(inf);
break;
}
return (built_in_ret);
}
/**
* find_cmd - command in PATH
* @inf: return and parameter struct
*
* Return: void
*/
void find_cmd(info_t *inf)
{
char *path = NULL;
int i, k;
inf->path = inf->argv[0];
if (inf->linecount_flag == 1)
{
inf->line_count++;
inf->linecount_flag = 0;
}
for (i = 0, k = 0; inf->arg[i]; i++)
if (!is_delim(inf->arg[i], " \t\n"))
k++;
if (!k)
return;
path = find_path(inf, _getenv(inf, "PATH="), inf->argv[0]);
if (path)
{
inf->path = path;
fork_cmd(inf);
}
else
{
if ((interactive(inf) || _getenv(inf, "PATH=")
|| inf->argv[0][0] == '/') && is_cmd(inf, inf->argv[0]))
fork_cmd(inf);
else if (*(inf->arg) != '\n')
{
inf->status = 127;
print_error(inf, "not found\n");
}
}
}
/**
* fork_cmd - thread to run cmd
* @inf: eturn and parametr
*
* Return: void
*/
void fork_cmd(info_t *inf)
{
pid_t child_pid;
child_pid = fork();
if (child_pid == -1)
{
perror("Error:");
return;
}
if (child_pid == 0)
{
if (execve(inf->path, inf->argv, get_environ(inf)) == -1)
{
free_info(inf, 1);
if (errno == EACCES)
exit(126);
exit(1);
}
}
else
{
wait(&(inf->status));
if (WIFEXITED(inf->status))
{
inf->status = WEXITSTATUS(inf->status);
if (inf->status == 126)
print_error(inf, "Permission denied\n");
}
}
}