-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_resolution.c
More file actions
121 lines (109 loc) · 2.01 KB
/
path_resolution.c
File metadata and controls
121 lines (109 loc) · 2.01 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
#include "main.h"
/**
*inputFix - fixes input from \n and spaces
*@data: relevent data
*Return: must return 0 for success
*/
int inputFix(shell_data *data)
{
int cp = 0;
/*char *error_message;*/
escapeChars(data);
removeExtraSpace(data);
cp = isOnlyNull(data);
if (cp == 1)
return (1);
fixDataInput(data);
if (_strcmp(data->input, "env") == 0)
return (2);
if (!_getenv("PATH") && _strchr(data->input, '/') == NULL)
{
if (_getenv("PATH1"))
printError(data, "./shell: 1: ", ": not found\n", 127);
else
printError(data, "./hsh: 1: ", ": not found\n", 127);
return (1);
}
return (0);
}
/**
*commandChecker - functions check commands
*@data: command line
*/
void commandChecker(shell_data *data)
{
int check;
check = isExecutable(data);
if (check == 0)
commandExe(data);
else if (check == 1)
isBuildin(data);
else
free(data->args);
}
/**
*checkSlash - functions check slash command
*@data: command line
*@lepath: path from previous function
*Return: should return 0 for first case, 2 for second case
*/
int checkSlash(shell_data *data, char *lepath)
{
struct stat file_stat;
if (stat(data->input, &file_stat) == 0)
{
getCmdArgs(data, data->input);
free(lepath);
return (0);
}
else
{
errorHandler(data);
free(lepath);
return (2);
}
}
/**
*isExecutable - is file executable
*@data: shell data
*Return: must return 0 for success
*/
int isExecutable(shell_data *data)
{
char *path = isPath();
char *dir = _strtok(path, ":");
char filepath[256] = {0};
int compt = 0;
if (data->input)
{
if (_strchr(data->input, '/') != NULL)
{
return (checkSlash(data, path));
}
while (dir != NULL && compt == 0)
{
_strcpy(filepath, dir);
_strcat(filepath, "/");
_strcat(filepath, data->input);
if (access(filepath, X_OK) == 0)
{
compt++;
}
if (compt == 0)
dir = _strtok(NULL, ":");
}
if (compt > 0)
{
getCmdArgs(data, filepath);
free(path);
return (0);
}
else
{
free(path);
return (1);
}
}
free(path);
return (1);
}