-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexit_shell.c
More file actions
59 lines (52 loc) · 1.23 KB
/
Copy pathexit_shell.c
File metadata and controls
59 lines (52 loc) · 1.23 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
#include "main.h"
/**
* compare - function that compares strings
* @command: commands from user
* @str: the string to compare with
* Return: 1 if true, 0 if false
*/
int compare(char *command, char *str)
{
int val = _strcmp(command, str);
if (val == 0)
return (1);
else
return (0);
}
/**
* in_env_path - get the file path to a command
* @command: the command entered by the user
* Return: 1 if true and 0 if false
*/
char *in_env_path(char *command, char **env)
{
char *file_path = NULL;
char *path_env = getpath(env);
char *path_clone = strdup(path_env);
char *directory = strtok(path_clone, ":");
if (command == NULL || env == NULL || path_env == NULL)
return (NULL);
while (directory != NULL)
{
size_t len1 = _strlen(directory);
int len2 = _strlen(command);
file_path = malloc(len1 + len2 + 2);
if (!file_path)
{
free(path_clone);
return (NULL);
}
_strcpy(file_path, directory);
_strcat(file_path, "/");
_strcat(file_path, command);
if (access(file_path, F_OK) == 0 && access(file_path, X_OK) == 0)
{
free(path_clone);
return (file_path);
}
free(file_path);
directory = strtok(NULL, ":");
}
free(path_clone);
return (NULL);
}