-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.c
109 lines (95 loc) · 1.8 KB
/
check.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
#include "shell.h"
char *find_absolute_path(char **args, char **av);
char *find_relative_path(char **args, char **av, char *path_env);
/**
* check_spaces - checks if user input contains only spaces
* @line: user input
* Return: 0 if input contains only spaces
*/
int check_spaces(char *line)
{
int i = 0;
int flag = 1;
int len = 0;
len = strlen(line);
while (line[i] == ' ' || line[i] == '\n' || line[i] == '\t')
i++;
if (i != len)
flag = 0;
return (flag);
}
/**
* *check_path - checks if the command is in the path
*
* @args: arguments
* @av: arguments
*
* Return: path
*/
char *check_path(char **args, char **av)
{
char *path;
char *temp;
char path_env[1024];
if (args[0][0] == '/' || args[0][0] == '.')
{
path = args[0];
if ((access(path, F_OK)) == -1)
{
fprintf(stderr, "%s: %d: %s: not found\n",
av[0], 1, args[0]);
return ("Fail access");
}
}
else
{
temp = getenv("PATH");
if (temp != NULL)
{
strcpy(path_env, temp);
}
else
{
fprintf(stderr, "%s: %d: %s: not found\n", av[0], 1, args[0]);
return ("Fail access");
}
path = find_relative_path(args, av, path_env);
}
return (path);
}
/**
* *find_relative_path - find the relative path of a command
*
* @args: arguments
* @av: arguments
* @path_env: path environment
*
* Return: relative path
*/
char *find_relative_path(char **args, char **av, char *path_env)
{
char **path_arr = split_path(path_env);
char *path;
char *first;
int i = 0;
while (path_arr[i])
{
first = _strcat("/", args[0]);
path = _strcat(path_arr[i], first);
if (access(path, F_OK) == -1)
{
free(path);
free(first);
}
else
{
free(path_arr);
free(first);
return (path);
}
i++;
}
free(path_arr);
fprintf(stderr, "%s: %d: %s: not found\n", av[0], 1, args[0]);
return ("Fail access");
}