forked from kedpak/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_path.c
More file actions
88 lines (80 loc) · 1.54 KB
/
Copy path_path.c
File metadata and controls
88 lines (80 loc) · 1.54 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
#include "holberton.h"
/**
* free_pointers - free all tokens
* @ptr: all the tokens (aka characters)
* Return: void
*/
void free_pointers(char **ptr)
{
int i;
for (i = 0; ptr[i] != NULL; i++)
free(ptr[i]);
free(ptr);
}
/**
* _path - handles concatination of input and path directories
* @t: string inputed into command line
* Return: void
*/
void _path(char **t)
{
list_tt *tmp_node, *head;
char *path;
int file, buffer;
buffer = BUFSIZE;
head = _build_list();
path = malloc(sizeof(char) * buffer);
tmp_node = head;
while (1)
{
_pathhelper(file, t, head, path);
path = _strcpy(path, tmp_node->str);
path = _strcat(path, t[0]);
file = access(path, X_OK);
if (file == 0)
{
if (execve(path, t, environ) == -1)
perror("error");
free(path);
free(head);
}
tmp_node = tmp_node->next;
if (tmp_node->next == NULL)
{
_errorstring(t[0]);
_errorstring(": command not found\n");
free_pointers(t);
free(path);
free_list(head);
_exit(98);
}
}
free_list(head);
free(path);
}
/**
* _pathhelper - handles case if input begins with '/'
* @file: checks if specific path file is found
* @t: inputed string in command line
* @list: build list from the environment
* @path: each directory in path
* Return: void
*/
void _pathhelper(int file, char **t, list_tt *list, char *path)
{
if (t[0][0] == '/')
{
file = access(t[0], X_OK);
if (file == 0)
execve(t[0], t, environ);
else
{
free_pointers(t);
free(path);
free(list);
_errorstring("-bash: ");
perror(t[0]);
_exit(98);
}
}
}