-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_loop.c
More file actions
167 lines (160 loc) · 4.36 KB
/
shell_loop.c
File metadata and controls
167 lines (160 loc) · 4.36 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
158
159
160
161
162
163
164
165
166
167
#include "shell.h"
/**
* hsh - my main shell loop
* @data: the parameter and return data struct
* @av: the argument vector
*
* Return: 0 success, 1 error, or error code
*/
int hsh(info_t *data, char **av)
{
ssize_t r = 0;
int innerRet = 0;
while (r != -1 && innerRet != -2) /* when 'r' & 'innerRet', not -1 and -2 */
{
cleanData(data); /* 'Data' cleanup */
if (invlove(data)) /* Verify whether 'data' is involved */
putin("$ "); /* Obtain input, then save the outcome in "r" */
eputword(BUFFER_FLUSHER);
r = getInput(data); /* Obtain input, then save the outcome in "r" */
if (r != -1)
{
fixData(data, av);
/* Find the interior of 'data' and save the outcome in 'innerRet' */
innerRet = locateInner(data);
if (innerRet == -1)
lookForCmd(data);
}
else if (invlove(data))
_putchar('\n');
freeData(data, 0); /* Free the "data" without also liberating the past */
}
genHistory(data); /* 'Data'-based history generation */
freeData(data, 1); /* 'Data' should be made available, including history */
if (!invlove(data) && data->worth)
exit(data->worth); /* 'data->worth' should be the status when you exit */
if (innerRet == -2)
{
if (data->digit_err == -1)
exit(data->worth);
exit(data->digit_err); /* 'data->digit_err' will be checked when you exit */
}
return (innerRet);
}
/**
* locateInner - Locate a builtin command
* @data: data struct
*
* Return: -1 if builtin not found,
* 0 if builtin executed successfully,
* 1 if builtin found but not successful,
* -2 if builtin signals exit()
*/
int locateInner(info_t *data)
{
int a, inBuiltRet = -1;
/* Declare variables 'a' and 'inBuiltRet' and initialize 'inBuiltRet' to -1 */
innerTable innerLib[] = {
{"exit", shellExit},
{"env", printMyEnv},
{"help", showHelp},
{"history", displayHistory},
{"setenv", setEnvVar},
{"unsetenv", removeEnvVar},
{"cd", changeDir},
{"alias", mineAlias},
{NULL, NULL}
};
/* Go over the innerLib array iteratively */
for (a = 0; innerLib[a].type; a++)
if (_strcmps(data->argv[0], innerLib[a].type) == 0)
{
data->line_count++;
inBuiltRet = innerLib[a].func(data); /* Call the matching innerLib */
/* function and place the result in the 'inBuiltRet' variable */
break;
}
return (inBuiltRet);
}
/**
* lookForCmd - locate a command in PATH
* @data: the parameter & return data struct
*
* Return: void
*/
void lookForCmd(info_t *data)
{
char *path = NULL;
int a, k;
data->path = data->argv[0]; /* Set 'data->path' to 1st argument of 'data' */
if (data->linecount_flag == 1) /* 'data->linecount_flag' is 1 */
{
data->line_count++;
data->linecount_flag = 0;
}
for (a = 0, k = 0; data->arg[a]; a++) /* Iterate the 'arg' array of 'data' */
/* event that 'data->arg[a]' doesn't have any whitespace characters */
if (!we_believe(data->arg[a], " \t\n"))
k++;
if (!k)
return;
path = locate_path(data, findEnv(data, "PATH="), data->argv[0]);
if (path)
{
data->path = path; /* Update 'data->path' with 'path' */
forkingCmd(data);
}
else
{
if ((invlove(data) || findEnv(data, "PATH=")
|| data->argv[0][0] == '/') && thisCmd(data, data->argv[0]))
forkingCmd(data); /* certain criteria are met, call "forkingCmd" function */
else if (*(data->arg) != '\n')
{
data->worth = 127; /* Set 'data->worth' to 127 */
display_err(data, "not found\n");
}
}
}
/**
* forkingCmd - forks a an exec thread to run cmd
* @data: the parameter & return data struct
*
* Return: void
*/
void forkingCmd(info_t *data)
{
pid_t child_pid;
/* Make a child process, then save process ID in "child_pid" variable */
child_pid = fork();
if (child_pid == -1) /* fork() returns -1 (indicating an error) */
{
/* TODO: PUT ERROR FUNCTION */
perror("Error:");
return;
}
if (child_pid == 0)
{
/* Execute command using execve() */
if (execve(data->path, data->argv, getEnviron(data)) == -1)
{
freeData(data, 1);
if (errno == EACCES) /* error is due to permission denied */
exit(126);
exit(1);
}
/* TODO: PUT ERROR FUNCTION */
}
else /* current process is the parent process */
{
/* Keep an eye out for the child process to end, */
/* then record the exit status in "data->worth */
wait(&(data->worth));
if (WIFEXITED(data->worth)) /* child process terminated normally */
{
data->worth = WEXITSTATUS(data->worth);
if (data->worth == 126)
display_err(data, "Permission denied\n");
}
}
}