-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwd.c
46 lines (41 loc) · 1.07 KB
/
pwd.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* getAbsolute(void) {
size_t pathsize = MAXLEN;
char* path = (char *) malloc(sizeof(char)*pathsize);
if (getcwd(path, pathsize) == NULL) {
printf("Error Number: %d\n", errno);
perror("absolute: ");
return NULL;
} else {
return path;
}
}
char* getRelative(char* home, char* path) {
size_t relsize = 1000;
char* rel = (char *)malloc(sizeof(char)*1000);
size_t homesize = strlen(home);
size_t pathsize = strlen(path);
if (homesize <= pathsize) {
long index = 0;
for(long i=0;i<homesize;i++){
if (home[i] != path[i]) {
index = i;
break;
}
}
if (index == 0) {
rel[0] = '~';
index = 1;
for(long i=homesize;i<pathsize;i++) {
rel[index] = path[i];
index++;
}
rel[index] = '\0';
} else {
return getAbsolute();
}
} else {
return getAbsolute();
}
return rel;
}