-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_path.c
46 lines (40 loc) · 914 Bytes
/
struct_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
36
37
38
39
40
41
42
43
44
45
46
#include "header.h"
char *getLink(char *path)
{
int size;
char buff[PATH_MAX + 1];
size = readlink(path, buff, PATH_MAX);
if (size < 0)
return nc_malloc(sizeof(char));
buff[size] = '\0';
if (size == PATH_MAX)
printf("Path of link might have been truncated\n");
return nc_strjoin("-> ", buff);
}
static void move(char *dst, char *src)
{
int n;
n = -1;
while (src[++n])
dst[n] = src[n];
}
char *joinPath(char *path, char *name)
{
char *new;
int size1;
int size2;
int total;
if (!name)
return NULL;
if (!path)
return strdup(name);
size1 = nc_strlen(path);
size2 = nc_strlen(name);
total = size1 + size2;
new = malloc(sizeof(char) * (total + 2));
move(new, path);
new[size1] = '/';
move(&new[size1+1],name);
new[total + 1] = '\0';
return new;
}