-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslateExec.c
171 lines (151 loc) · 3.7 KB
/
translateExec.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
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
168
169
170
#include "crikey.h"
/**
* _strcat - concatenates a string
* @dest: the string to be concatenated
* @src: the string to add to dest
*
* Return: returns char string array
*/
char *_strcat(char *dest, char *src)
{
int countDestLen = 0;
int i = 0;
while (dest[countDestLen])
countDestLen++;
for (; src[i] != '\0'; i++)
{
dest[countDestLen + i] = src[i];
}
dest[countDestLen + i] = '\0';
return (dest);
}
/**
* concatTok - concatenates a token
* @testExec: the string to be concatinated
* @param: the command, input from the user
* @enVarToken: the tokenized directory in path
* Return: returns char string array
*/
void concatTok(char *testExec, char *param, char *enVarToken)
{
_strcat(testExec, enVarToken);
_strcat(testExec, "/");
_strcat(testExec, param);
_strcat(testExec, "\0");
}
/**
* _strcpy - copies the string pointed to by src
* @dest: pointer
* @src: pointer
*
* Return: returns char string array
*/
char *_strcpy(char *dest, char *src)
{
int counter = 0;
while (src[counter])
{
dest[counter] = src[counter];
counter++;
}
dest[counter] = '\0';
return (dest);
}
/**
* translateExec - executes the file to a string
* @params: contains the parameters from the user input
* @env: contains the environment variables
* @exitStatus: Status to exit with
*
* Return: returns char string array
*/
int translateExec(char **params, char **env, int *exitStatus)
{
int i = 0, pathLen = 0, paramLen = 0, tokLen = 0, ret = 0;
char *path, *enVariable;
char *param = params[0];
char *cwd;
/*gets Parameter[0] length*/
for (paramLen = 0; param[paramLen]; paramLen++)
;
/*loop through each instance variable in environment*/
while (env[i])
{
enVariable = env[i];
for (pathLen = 0; enVariable[pathLen]; pathLen++)
;
path = malloc(sizeof(char) * pathLen + 1);
if (path == NULL)
exit(1);
_strcpy(path, enVariable);
/*check for PATH environment variable*/
enVariable = _strtok(path, '=');
cwd = malloc(sizeof(char) * 1024);
ret = checkEnvVariable(enVariable, tokLen, paramLen, param, params,
path, cwd, exitStatus, env);
free(cwd);
if (ret == 1)
{
ret = 0;
return (1);
}
i++;
free(path);
}
return (ret);
}
/**
* checkEnvVariable - a helper function to execute files if equals "PATH"
* @enVariable: environment variable
* @tokLen: token length, int
* @paramLen: param length, int
* @param: param, the first value of params
* @params: a double pointer that points to params
* @path: the path
* @cwd: Pointer to the buffer to store the current working directory
* @exitStatus: the exit Status of the program
* @env: environment variable
*
* Return: no return
*/
int checkEnvVariable(char *enVariable, int tokLen, int paramLen, char *param,
char **params, char *path, char *cwd, int *exitStatus, char **env)
{
char *testExec, *enValue, *enVariableToken;
int status, j = 0;
struct stat ret;
if (_strcmp(enVariable, "PATH") == 1)
{
enValue = _strtok(NULL, '=');
enVariableToken = _strtok(enValue, ':');
while (enVariableToken)
{
if (*enVariableToken == '\0')
enVariableToken = getcwd(cwd, 1024);
for (tokLen = 0; enVariableToken[tokLen]; tokLen++)
;
testExec = malloc(sizeof(char) * (paramLen + tokLen + 2));
for (j = 0; j < paramLen + tokLen + 2; ++j)
testExec[j] = '\0';
if (testExec == NULL)
exit(1);
concatTok(testExec, param, enVariableToken);
if (stat(testExec, &ret) == 0 && access(testExec, X_OK) != 0)
*exitStatus = 126;
if (access(testExec, X_OK) == 0)
{
if (!fork())
execve(testExec, params, env);
else
wait(&status);
*exitStatus = 0;
free(testExec);
free(path);
return (1);
}
enVariableToken = _strtok(NULL, ':');
free(testExec);
}
}
return (0);
}