-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat_path.c
35 lines (30 loc) · 895 Bytes
/
concat_path.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/**
* concat_path - Concatenates two strings with a "/" in between
* Description:
* 1 - Calculate the full length
* 2 - Allocates memory according to length
* 3 - Concatenate strings with / and store in a pointer
* @base: The base path string.
* @addendum: The string we want to add
* Return: A pointer to the newly concatenated string, or NULL on failure.
*/
char *concat_path(const char *base, const char *addendum)
{
/* Declare and initialize variables */
size_t full_length = 0;
char *result = NULL;
/* Calculate length and allocate memory */
full_length = strlen(base) + strlen("/") + strlen(addendum) + 1;
result = malloc(full_length * sizeof(char));
if (result == NULL)
{
printf("Error (malloc)");
return (NULL);
}
/* Build the string */
sprintf(result, "%s/%s", base, addendum);
return (result);
}