-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpathappend.c
43 lines (37 loc) · 824 Bytes
/
pathappend.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
#include "simple_shell.h"
/**
* path_append - Append program or command to the path
* @path: PATH directory
* @cmd: program file or command
* Return: pointer to a PATH newly string or NULL on failure
*/
char *path_append(char *path, char *cmd)
{
int len_path = 0, len_cmd = 0;
char *full_path;
if (path == NULL)
path = "";
if (cmd == NULL)
cmd = "";
full_path = malloc(((_strlen(path) + _strlen(cmd)) * sizeof(char)) + 2);
if (full_path == NULL)
return (NULL);
while (path[len_path])
{
full_path[len_path] = path[len_path];
len_path++;
}
if (path[len_path - 1] != '/')
{
full_path[len_path] = '/';
len_path++;
}
while (cmd[len_cmd])
{
full_path[len_path + len_cmd] = cmd[len_cmd];
len_cmd++;
}
full_path[len_path + len_cmd] = '\0';
free_function(1, cmd);
return (full_path);
}