-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappend_command.c
More file actions
executable file
·37 lines (35 loc) · 848 Bytes
/
append_command.c
File metadata and controls
executable file
·37 lines (35 loc) · 848 Bytes
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
#include "shell.h"
/**
* append_command - Concatenates an input with paths in global variable PATH
* @dir_path: directory string to be append with the command
* @command: command to be concatenated with the directory
* Return: Buffer to concatenated path
*/
char *append_command(char *dir_path, char *command)
{
int a, b = 0, len1, len2;
char *command_path = NULL;
if (dir_path == NULL || command == NULL)
return (NULL);
len1 = _strlen(dir_path);
len2 = _strlen(command);
command_path = malloc(len1 + len2 + 2);
if (command_path == NULL)
return (NULL);
for (a = 0; dir_path[a] != '\0'; a++)
{
command_path[a] = dir_path[a];
}
if (dir_path[a - 1] != '/')
{
command_path[a] = '/';
a++;
}
while (command[b] != '\0')
{
command_path[a + b] = command[b];
b++;
}
command_path[a + b] = '\0';
return (command_path);
}