-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprompt_util.c
More file actions
166 lines (157 loc) · 3.49 KB
/
Copy pathprompt_util.c
File metadata and controls
166 lines (157 loc) · 3.49 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "shell.h"
/**
* print_ps1 - prints the primary shell prompt
* @num: number that determines which errno value should be set
*/
void print_ps1(int num)
{
char ps1[] = "MARSU$ ";
size_t len_ps1;
int org_error;
org_error = errno;
len_ps1 = _strlen(ps1);
if (isatty(0))
write(STDOUT_FILENO, ps1, len_ps1);
if (num == 0)
errno = 0;
else
errno = org_error;
}
/**
* get_path - get the env path and tokenizes it into a array.
* @modify_path: pointer to store modified path string into
*
* Return: array / double pointer.
*/
char **get_path(char **modify_path)
{
char **token_ptr;
char *path, *delim;
unsigned int i, j, num_char;
delim = ":";
path = _getenv("PATH");
num_char = _strlen(path);
*modify_path = malloc(sizeof(char) * (num_char + 2));
if (num_char == 0)
{
(*modify_path)[0] = '.';
(*modify_path)[1] = '\0';
}
else
{
for (i = 0, j = 0; path[i] != '\0'; i++)
{
if (i == 0 && path[i] == ':')
{
(*modify_path)[j++] = '.';
(*modify_path)[j++] = path[i];
}
else if (i == num_char - 1 && path[i] == ':')
{
(*modify_path)[j++] = path[i];
(*modify_path)[j++] = '.';
}
else if (path[i] == ':' && path[i + 1] == ':')
{
(*modify_path)[j++] = path[i];
(*modify_path)[j++] = '.';
}
else
(*modify_path)[j++] = path[i];
}
for (; j < num_char + 2; j++)
(*modify_path)[j] = '\0';
}
token_ptr = tokenize_str(*modify_path, delim);
return (token_ptr);
}
/**
* find_pathname - finds the pathname attached to the associated command
* @path: pointer to 2d array of tokenized directories in PATH
* @input: input to find
* Return: Path to the input file.
*/
char *find_pathname(char **path, char *input)
{
unsigned int i;
DIR *directory;
struct dirent *filename;
int str_cmp, match_found;
char *result;
filename = NULL;
match_found = 0;
for (i = 0; path[i] != NULL; i++)
{
directory = opendir(path[i]);
if (directory == NULL)
{
errno = EBADF;
return (NULL);
}
while ((filename = readdir(directory)) != NULL)
{
/* TODO make strcmp function */
str_cmp = _strcmp(filename->d_name, input);
if (str_cmp == 0)
{
match_found = 1;
break;
}
}
if (match_found == 1)
break;
closedir(directory);
}
if (match_found == 1)
{
result = make_pathname(path[i], input);
if (access(result, R_OK) != -1)
errno = EACCES;
closedir(directory);
return (result);
}
errno = EBADF;
return (NULL);
}
/**
* make_pathname - takes result from path_name and creates a string.
* string contains the full pathname.
* @path: pathname
* @file: file in the path
* Return: pointer to an allocated string that contains the full pathname.
*/
char *make_pathname(char *path, char *file)
{
size_t num_path, num_file;
char *result;
unsigned int j, k;
num_path = _strlen(path);
num_file = _strlen(file);
result = malloc(sizeof(char) * (num_path + num_file + 2));
if (result == NULL)
return (NULL);
for (j = 0; j < num_path; j++)
result[j] = path[j];
result[j++] = '/';
for (k = 0; k < num_file; k++)
result[j + k] = file[k];
result[j + k] = '\0';
return (result);
}
/**
* _getenv - gets the value of the environment variable
* @name: variable to find
* Return: pointer to the value of the environment variable.
*/
char *_getenv(const char *name)
{
unsigned int i, j;
for (i = 0; environ[i] != NULL; i++)
{
for (j = 0; environ[i][j] != '=' && environ[i][j] == name[j] && name[j]; j++)
;
if (environ[i][j] == '=' && name[j] == '\0')
return (&environ[i][++j]);
}
return (NULL);
}