-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
69 lines (61 loc) · 1.24 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
#include "headers.h"
int cd(char *prev_dir, char *root)
{
int fg = 0, check = 0, flag = 0;
char *path, *new_path, *token;
char dir[maxl], cpy[maxl];
getcwd(dir, maxl);
token = strtok(NULL, " \t");
path = token;
if (token == NULL)
{
strcpy(cpy, root);
fg = 1;
}
else
{
if (!strcmp(path, "-"))
flag = 1;
else if (!strcmp(path, "~"))
path = root;
else if (token == NULL)
path = root;
else
{
if (path[0] == '~' && path[1] == '/')
{
strcpy(cpy, root);
strcat(cpy, &path[1]);
fg = 1;
}
}
token = strtok(NULL, " \t");
}
if (token == NULL)
{
if (!fg)
new_path = path;
else
new_path = cpy;
}
else
{
perror("Excessive arguments for cd\n");
return 0;
}
if (flag == 0)
check = chdir(new_path);
else
{
printf("%s\n", prev_dir);
check = chdir(prev_dir);
}
if (check == 0)
{
if (strcmp(new_path, dir) != 0)
strcpy(prev_dir, dir);
}
else
perror("Error\n");
return 0;
}