-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_path.c
More file actions
152 lines (129 loc) · 3.04 KB
/
handle_path.c
File metadata and controls
152 lines (129 loc) · 3.04 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
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
#include "main.h"
/**
* _getenv - searches for env variable by name
* @var_name - env variable name to search for.
* Return: return env var value
*/
char *_getenv(const char *var_name) {
/* Declare variables */
int i, j;
/*Check if name is NULL */
if (!var_name)
return (NULL);
/* Loop through the environment variables */
for (i = 0; environ[i]; i++) {
j = 0;
while (var_name[j] && var_name[j] == environ[i][j])
j++;
if (var_name[j] == '\0')
return (environ[i] + j + 1);
}
/* Return NULL if no match is found */
return (NULL);
}
/**
* process_path - processes path
* @path - path to process
* return: head
*/
list_path *process_path(char *path) {
/* Declare variables */
list_path *head;
char *token;
char *cpath;
/* Initialize the head pointer to NULL */
head = NULL;
cpath = _strdup(path);
if (!cpath)
return (NULL);
token = strtok(cpath, ":");
printf("DEBUG: First token: %s\n", token);
while (token) {
printf("DEBUG: Extracted directory: %s\n", token);
head = adding_node(&head, token);
token = strtok(NULL, ":");
}
free(cpath);
return (head);
}
/**
* adding_node - adds a node to a linked list
* @head: head to the list
* @str: data for the new node
* Return: head to the list
*/
list_path *adding_node(list_path **head, char *str) {
list_path *new_node;
/* Check if str is NULL */
if (!str)
return (NULL);
/* Allocate memory for the new node */
new_node = malloc(sizeof(list_path));
if (!new_node)
return (NULL);
new_node->dir = _strdup(str);
new_node->p = NULL;
/* If head is NULL, make the new node the head */
if (!*head) {
*head = new_node;
} else {
list_path *tmp = *head;
while (tmp->p) {
tmp = tmp->p;
}
tmp->p = new_node;
}
/* Return a pointer to the head of the list */
return (*head);
}
/**
* _which - finds a file in a linked list
* @filename: name of file
* @head: head to the list
* Return: first path where file is found
*/
char *_which(char *filename, list_path *head) {
struct stat st;
char *string = NULL;
list_path *tmp = head;
char *full_path = NULL;
if (filename == NULL || head == NULL) {
printf("ERROR: Invalid arguments.\n");
return (NULL);
}
if (strchr(filename, '/') != NULL) {
string = str_concat(1, filename);
if (string == NULL) {
printf("ERROR: Memory allocation failed.\n");
return (NULL);
}
if (stat(full_path, &st) == 0 && st.st_mode & S_IXUSR) {
return (string);
}
free(string);
}
prints("Debug: Full Path To Check: %s", full_path)
while (tmp != NULL) {
string = _handle_trailing_slash(tmp->dir);
if (string == NULL) {
printf("ERROR: Memory allocation failed.\n");
return (NULL);
}
full_path = str_concat(2, string, filename);
free(string);
if (full_path == NULL) {
printf("ERROR: Memory allocation failed.\n");
return (NULL);
}
if (stat(full_path, &st) == 0) {
printf("DEBUG: File found in Full Path: %s\n", full_path);
return(full_path);
} else {
printf("DEBUG: File not found.\n");
return (NULL);
}
free(full_path);
tmp = tmp->p;
}
return (NULL);
}