-
Notifications
You must be signed in to change notification settings - Fork 1
/
smart_cat.c
53 lines (49 loc) · 867 Bytes
/
smart_cat.c
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
#include "shed.h"
/**
* smart_cat - append the command to its directory path
*
* @path: array of pointers to tokenized path directories
* @name: the command
* Return: buffer containing concatenated string of the command and its path
*/
char *smart_cat(char **path, char *name)
{
char *buff = NULL;
int i = 0, ii = 0, v = 0;
if (!(path) || !(name))
return (NULL);
if (name[0] == '/')
return (NULL);
while (path[v] != NULL)
{
buff = malloc(sizeof(char) * (_strlen(path[v]) + _strlen(name) + 2));
if (!(buff))
return (NULL);
i = 0;
ii = 0;
while (path[v][ii])
{
buff[i] = path[v][ii];
i++;
ii++;
}
buff[i] = '/';
i++;
ii = 0;
while (name[ii])
{
buff[i] = name[ii];
i++;
ii++;
}
buff[i] = '\0';
if (access(buff, F_OK) == 0)
{
return (buff);
}
else
free(buff);
v++;
}
return (NULL);
}