-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrel_path.c
More file actions
45 lines (44 loc) · 752 Bytes
/
rel_path.c
File metadata and controls
45 lines (44 loc) · 752 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <dirent.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include "Shellheader.h"
char* rel_path(char * cwd)
{
int s_cwd = strlen(cwd),s_root = strlen(root);
// printf("%d %d\n",s_cwd,s_root );
if(s_root == s_cwd)
{
return "~";
}
else if(s_root > s_cwd)
{
return cwd;
}
else
{
char *CWD = (char*)malloc(100);
for(int i=0;i<s_root;i++)
{
if(root[i]!=cwd[i])
return cwd;
}
int i;
CWD[0]='~';CWD[1]='/';
for(i=s_root+1;i<s_cwd;i++)
{
CWD[2+i-(s_root+1)] = cwd[i];
}
CWD[2+i-(s_root+1)]='\0';
return CWD;
}
}