-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_helper2.c
More file actions
168 lines (161 loc) · 3.51 KB
/
shell_helper2.c
File metadata and controls
168 lines (161 loc) · 3.51 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
167
168
#include "holberton.h"
/**
* num_to_str - convert integer to string
* @n: the number that need to convert
* Return: return the converted string
*/
char *num_to_str(size_t n)
{
char *str = NULL;
int len = 1, n_cp = n, i = 0, tmp;
if (n_cp / 10 != 0)
{
len++;
n_cp = n_cp / 10;
}
str = malloc(sizeof(char) * (len + 1));
if (!str)
_exit(1);
str[i + len] = '\0';
while (n >= 10)
{
tmp = n % 10;
str[i + len - 1] = tmp + '0';
len--;
n = n / 10;
}
str[i] = n + '0';
return (str);
}
/**
* path_helper - searches the correct path of command and
* concatenates the path with '/' and command
* @path: the environment variable PATH
* @buff_tk: the first argument of user input
* @argv: program name
* @input_count: number of commands processed
* @stat: exit status
* Return: return the concatenate string if found,
* otherwise, return NULL if not found.
*/
char *path_helper(char *path, char **buff_tk, char *argv,
size_t input_count, int *stat)
{
char *path_cp = NULL, *concat_path = NULL;
char **path_tk = NULL;
int i = 0;
if (path)
{
if (path[0] == ':' && path[_strlen(path) - 1] == ':')
path_cp = _strcat(".", path, ".");
else if (path[0] == ':')
path_cp = _strcat(".", path, "");
else if (path[_strlen(path) - 1] == ':')
path_cp = _strcat(path, ".", "");
else
path_cp = _strdup(path);
path_tk = create_arg_list(path_tk,
path_cp, ":");
}
else
{
path_tk = NULL;
}
while (path_tk && path_tk[i] != NULL)
{
concat_path = _strcat(path_tk[i], "/", buff_tk[0]);
if (access(concat_path, X_OK) == 0)
{
free(path_cp);
free(path_tk);
return (concat_path);
}
if (access(concat_path, F_OK) == 0)
{
error_message(argv, input_count, ": Permission denied\n", buff_tk);
free(path_cp);
free(path_tk);
*stat = 126;
free(buff_tk);
free(concat_path);
return (NULL);
}
i++;
free(concat_path);
}
free(path_cp);
free(path_tk);
*stat = 127;
error_message(argv, input_count, ": not found\n", buff_tk);
free(buff_tk);
return (NULL);
}
/**
* _getenv - searches for the environment string pointed to by name
* @name: string containing the name of the requested variable.
* @env: environment string
* Return: returns the associated value to the string,
* or NULL if that environment variable does not exist.
*/
char *_getenv(char *name, list_t **env)
{
list_t *temp = *env;
while (temp)
{
if (_strcmp(temp->key, name) == 0)
return (temp->val);
temp = temp->next;
}
return (NULL);
}
/**
* _strcat - concatenates three strings.
* @dest: the string need to concatenated
* @src: the string need to concatenated
* @str: the string need to concatenated
* Return: return a pointer to the resultring string result
*/
char *_strcat(char *dest, char *src, char *str)
{
int total_len = 0;
int i = 0, l = 0, m = 0, n = 0;
char *result = NULL;
if (!dest || !src || !str)
return (NULL);
total_len = _strlen(dest) + _strlen(src) + _strlen(str);
result = malloc(sizeof(char) * (total_len + 1));
if (!result)
_exit(1);
for (i = 0; i < total_len; i++)
{
if (dest[l] != '\0')
{
result[i] = dest[l];
l++;
}
else if (dest[l] == '\0' && src[m] != '\0')
{
result[i] = src[m];
m++;
}
else if (src[m] == '\0' && str[n] != '\0')
{
result[i] = str[n];
n++;
}
}
result[i] = '\0';
return (result);
}
/**
* _strlen - return the length of a string
* @s:the string that need to find the length
* Return: return the length of the string
*/
int _strlen(char *s)
{
int length = 0;
while (s[length] != '\0')
length++;
return (length);
}