-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
53 lines (40 loc) · 982 Bytes
/
Copy pathutils.c
File metadata and controls
53 lines (40 loc) · 982 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#define UTIL_TAB " "
void tabs_print(int depth) {
for (int i = 0; i < depth; i++)
printf(UTIL_TAB);
}
char *tabs_to_string(int depth) {
int tablen = sizeof(UTIL_TAB) - 1;
char *result = malloc(sizeof(char) * (tablen * depth + 1));
for (int i = 0; i < depth; i++)
for (int j = 0; j < tablen; j++)
result[(i * tablen) + j] = UTIL_TAB[j];
result[depth * tablen] = '\0';
return result;
}
void error_check_malloc(void *p) {
if (p == NULL) {
perror("malloc failed");
exit(-1);
}
}
char *string_concat(const char *s1, const char *s2) {
if (s1 != NULL && s2 != NULL) {
char *result = malloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1));
error_check_malloc(result);
strcpy(result, s1);
strcat(result, s2);
return result;
}
if (s1 == NULL && s2 == NULL)
return strdup("");
if (s1 == NULL)
return strdup(s2);
if (s2 == NULL)
return strdup(s1);
return NULL;
}