-
Notifications
You must be signed in to change notification settings - Fork 1
/
cd.c
60 lines (55 loc) · 1.14 KB
/
cd.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Headers.h"
#include "cd.h"
#include "pwd.h"
void change(char*relative,char*path,int start)
{
int len=strlen(path);
if(len>2)
{
for(int i=start; i<len; i++)
{
relative[i] = path[i];
}
chdir(relative);
}
}
void cd(char*path, char*home, char*prev)
{
//printf("%s\n",prev);
char relative[1024]="./";
char root = '/';
char dot = '.';
char tilde ='~';
char dash = '-';
if(path[0]==dash)
{
char temp[1000];
strcpy(temp,prev);
prev =getcwd(prev,100);
chdir(temp);
}
else
{
char *path_new = strtok(path," \t");
//printf("x%sx",path_new);
prev = getcwd(prev,100);
if(path[0]== tilde)
{
chdir(home);
change(relative,path_new,2);
}
else if(path_new[0]==dot && path_new[1]!=dot)
{
change(relative,path_new,0);
}
else if(chdir(path_new)==-1)
{
perror("CHDIR:");
}
}
}
void handle_cd(char *path, char *prev,char *start)
{
//char *start = getenv("PWD");
cd(path,start,prev);
}