-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd_execution.c
72 lines (65 loc) · 1.35 KB
/
cd_execution.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
#include "headers.h"
void cd_execution(char **arg, int num)
{
char workingdir[1024];
getcwd(workingdir, MAX_LENGTH);
if (num > 2)
{
printf("Error: cd: too many arguments\n");
return;
}
else if (num == 1)
{
if (chdir(invokedir) < 0)
{
perror("Invalid path");
}
}
else if (strcmp(arg[1], "~") == 0)
{
if (chdir(invokedir) < 0)
{
perror("Invalid path");
}
}
else if (strcmp(arg[1], ".") == 0)
{
return;
}
else if (strcmp(arg[1], "-") == 0)
{
if (chdir(prevdir) < 0)
{
perror("Invalid path");
}
else
{
printf("%s\n", prevdir);
}
}
else if (arg[1][0] == '~')
{
int k = 1;
char newpath[MAX_LENGTH];
strcpy(newpath, invokedir);
newpath[strlen(invokedir)] = '/';
for (k = 1; k < strlen(arg[1]); k++)
{
newpath[strlen(invokedir) + k] = arg[1][k];
}
newpath[strlen(invokedir) + k] = '\0';
if (chdir(newpath) < 0)
{
perror("Invalid path");
}
}
else
{
if (chdir(arg[1]) < 0)
{
perror("Invalid path");
}
}
strcpy(prevdir, workingdir);
getcwd(workingdir, MAX_LENGTH);
}