-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
117 lines (105 loc) · 2.23 KB
/
helpers.c
File metadata and controls
117 lines (105 loc) · 2.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
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
#include "shell.h"
/**
* _tokount - counts number of tokens
* @str: input string
* @delim: delimiter string
* Return: delimiter count
*/
int _tokount(char *str, char *delim)
{
int i = 0, j = 0;
int c = 0;
while (delim[i])
{
while (str[j])
if (str[j++] == delim[i] && str[j] != delim[i])
c++; /*haha c ++, get it...what a joke*/
j = 0;
i++;
}
return (c);
}
/**
* _is_arg_run_ready - tests for file access
* @arg: path to check
* Return: 1 on success, 0 on failure
*/
unsigned int _is_arg_run_ready(char *arg)
{
if (access(arg, X_OK) == -1)
return (0);
else
return (1);
}
/**
* _stralloc - concatenate count strings
* @count: string count
* Return: concatenated strings
*/
char *_stralloc(int count, ...)
{
va_list valist;
char *tmp_arg;
char *tmp_ptr;
char *tmp_ret;
int sLen;
int aLen;
va_start(valist, count);
tmp_arg = va_arg(valist, char *), count--;
aLen = _strlen(tmp_arg);
tmp_ret = malloc(sizeof(char) * aLen + 1);
if (tmp_ret == NULL)
perror("big segfult: "), exit(EXIT_FAILURE);
_strcpy(tmp_ret, tmp_arg);
while (count != 0)
{
tmp_arg = va_arg(valist, char *), count--;
sLen = _strlen(tmp_ret), aLen = _strlen(tmp_arg);
tmp_ptr = malloc(sizeof(char) * (sLen + aLen) + 1);
if (tmp_ptr == NULL)
perror("gros gros segfult: "), exit(EXIT_FAILURE);
if (tmp_ret != NULL)
_strcpy(tmp_ptr, tmp_ret), free(tmp_ret);
_strcat(tmp_ptr, tmp_arg), tmp_ret = tmp_ptr;
}
va_end(valist);
return (tmp_ret);
}
/**
* _find_env_get_value - get value of key
* @key: key is key
* Return: pointer to first letter env value
*/
char *_find_env_get_value(char *key)
{
int i = 0;
char *tmp;
while (_strstr(environ[i], key) == NULL && environ[i] != NULL)
i++;
if (environ[i] == NULL)
return (NULL);
tmp = _strstr(environ[i], "="), tmp++;
return (tmp);
}
/**
* _find_x_path - find path of program
* @env_paths: path token array
* @program: program string
* Return: absolute path of program
*/
char *_find_x_path(char **env_paths, char *program)
{
int i = 1;
char *tmp;
tmp = _stralloc(3, env_paths[0], "/", program);
while (access(tmp, X_OK) == -1 && env_paths[i] != NULL)
{
free(tmp);
tmp = _stralloc(3, env_paths[i], "/", program);
i++;
}
if (env_paths[i] == NULL)
return (NULL);
else
return (tmp);
}