-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_access_update_token.c
64 lines (62 loc) · 1.38 KB
/
check_access_update_token.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
#include "main.h"
int check_access_update_token(char **token, char **PathTok, int *ExitStat);
/**
* check_access_update_token - completes the command in
* needed and checks if its in lubrary
* @token: line input from user
* @PathTok: tokenised PATH
* @ExitStat: update status
* Return: 0 on success 1 if no access or just library like "/bin/"
*/
int check_access_update_token(char **token, char **PathTok, int *ExitStat)
{
char *CheckTok;
int PathTokIndex;
PathTokIndex = 0;
if (token == NULL || *token == NULL)
return (1); /*error*/
if (PathTok == NULL || PathTok[PathTokIndex] == NULL)
{
if (token[0][0] != '.' && token[0][0] != '/')
{
*ExitStat = 127;
return (1);
}
if (!access(token[0], F_OK | X_OK))
{
return (0);
}
else
{
*ExitStat = 127;
return (1);
}
}
for (PathTokIndex = 0; PathTok[PathTokIndex]; PathTokIndex++)
{
if (token[0][0] != '/' && token[0][0] != '.')
{
CheckTok = malloc(_strlen(token[0]) + _strlen(PathTok[PathTokIndex]) + 2);
/*added space for '/' and'\0'*/
_strcpy(CheckTok, PathTok[PathTokIndex]);
_strcat(CheckTok, "/");
_strcat(CheckTok, token[0]);
if (!access(CheckTok, F_OK | X_OK))
{
free(token[0]);
token[0] = CheckTok;
return (0);
}
free(CheckTok);
}
else
{
CheckTok = token[0];
if (!access(CheckTok, F_OK | X_OK))
return (0);
else
return (1);
}
}
return (1);
}