-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcd.c
68 lines (67 loc) · 926 Bytes
/
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
61
62
63
64
65
66
67
68
#include "cd.h"
void checkCd(char **input,char home[])
{
char buf[500];
char *gdir;
char *dir;
char *to;
//printf("ok\n");
gdir = getcwd(buf, sizeof(buf));
//printf("%s\n",gdir);
dir = strcat(gdir, "/");
if(input[1]==NULL)
{
if(chdir(home)<0)
{
printf("No such directory.\n");
}
else
{
chdir(home);
}
}
else if(input[1][0]=='/')
{
if(chdir(input[1])<0)
{
printf("No such directory.\n");
}
else
{
chdir(input[1]);
}
}
else if(input[1][0]=='~')
{
int j;
char temp[1000];
int l = strlen(input[1]);
strcpy(temp,home);
int len_temp = strlen(temp);
for(j=1;j<l;j++)
{
temp[len_temp+j-1]= input[1][j];
}
if(chdir(temp)<0)
{
printf("No such directory.\n");
}
else
{
chdir(temp);
}
}
else
{
to = strcat(dir, input[1]);
if(chdir(to)<0)
{
printf("No such directory.\n");
}
else
{
chdir(to);
}
}
//printf("%s\n",to);
}