-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
102 lines (100 loc) · 2.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "headers.h"
void cd_func(char *path[], int length)
{
char str[1000],new[1000];
for(int m=0;m<1000;m++)
{
user[m] = '\0';
str[m] = '\0';
new[m] = '\0';
}
int flag = 0;
if(length == 1)
{
if(chdir(presentpath_a_out) == -1)
{
perror("chdir");
}
strcpy(user,"~");
}
else
{
strcpy(new,path[1]);
if(path[1][0] == '~')
{
strcpy(str,presentpath_a_out);
for(int i = 1; i < strlen(new); i++)
{
flag = 1;
break;
}
if(flag == 1)
{
strcat(str,&path[1][1]);
strcpy(cd_present_path,str);
if(chdir(cd_present_path) == -1)
{
perror("chdir");
strcpy(user,"~");
}
else
{
strcpy(user,"~");
strcat(user,&path[1][1]);
}
}
else
{
if(chdir(presentpath_a_out) == -1)
{
perror("chdir");
strcpy(user,"~");
}
}
}
else if(strcmp(path[0] ,"cd") == 0 && (path[1] == '\0'))
{
if(chdir(presentpath_a_out) == -1)
{
perror("chdir");
strcpy(user,"~");
}
}
else
{
int check = 0,i;
if(chdir(path[1]) == -1)
{
perror("chdir");
}
getcwd(cd_present_path,sizeof(cd_present_path));
if(strcmp(cd_present_path,presentpath_a_out) == 0)
{
strcpy(user,"~");
}
else
{
for(i = 0; i <= strlen(presentpath_a_out); i++)
{
if(cd_present_path[i] != presentpath_a_out[i])
{
check = 1;
break;
}
}
if(check == 1)
{
if(i < strlen(presentpath_a_out))
{
strcpy(user,cd_present_path);
}
else if(check == 1)
{
strcpy(user,"~");
strcat(user,&cd_present_path[i]);
}
}
}
}
}
}