-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_path2.c
More file actions
118 lines (109 loc) · 2.32 KB
/
Copy path_path2.c
File metadata and controls
118 lines (109 loc) · 2.32 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
#include "shell.h"
/**
* _findBuiltin - finds a builtin command
* @info: the parameter & return info 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 _findBuiltin(info_t *info)
{
int i, builtin_ret = -1;
builtin_table builtin_tbl[] = {
{"exit", _myExit},
{"env", _myEnv},
{"help", _myHelp},
{"history", _myHistory},
{"setenv", _mySetEnv},
{"unsetenv", _myUnsetEnv},
{"cd", _myCd},
{"alias", _myAlias},
{NULL, NULL}
};
for (i = 0; builtin_tbl[i].type; i++)
{
if (_strcmp(info->_argv[0], builtin_tbl[i].type) == 0)
{
info->_lineCount++;
builtin_ret = builtin_tbl[i].func(info);
break;
}
}
return (builtin_ret);
}
/**
* _findCommand - Function finds a command in PATH
* @info: the parameter & return info struct
* Return: void
*/
void _findCommand(info_t *info)
{
char *path = NULL;
int i, num_args;
info->_path = info->_argv[0];
if (info->_countLine == 1)
{
info->_lineCount++;
info->_countLine = 0;
}
for (i = 0, num_args = 0; info->_args[i]; i++)
if (!_isDelimeter(info->_args[i], " \t\n"))
num_args++;
if (!num_args)
return;
path = _findCommandPath(info, _getEnv(info, "PATH="), info->_argv[0]);
if (path)
{
info->_path = path;
_forkCommand(info);
}
else
{
if ((_isInteractive(info) || _getEnv(info, "PATH=") || info->_argv[0][0]
== '/') && _isExecutableCommand(info, info->_argv[0]))
_forkCommand(info);
else if (*(info->_args) != '\n')
{
info->_status = 127;
_printError(info, "Not Found\n");
}
}
}
/**
* _forkCommand - Function forks an exec thread to run a command
* @info: the parameter & return info struct
* Return: void
*/
void _forkCommand(info_t *info)
{
pid_t child_pid;
child_pid = fork();
if (child_pid == -1)
{
/* TODO: PUT ERROR FUNCTION */
perror("Error:");
return;
}
if (child_pid == 0)
{
if (execve(info->_path, info->_argv, _getEnvironmentStrings(info)) == -1)
{
_freeInfoStruct(info, 1);
if (errno == EACCES)
exit(126);
exit(1);
}
/* TODO: PUT ERROR FUNCTION */
}
else
{
wait(&(info->_status));
if (WIFEXITED(info->_status))
{
info->_status = WEXITSTATUS(info->_status);
if (info->_status == 126)
_printError(info, "Permission denied\n");
}
}
}