-
Notifications
You must be signed in to change notification settings - Fork 3
/
cd.c
59 lines (50 loc) · 1.51 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
#include "main.h"
void cd(long long int numArgs, char *commandArgument) {
// Throw an error if no. of Arguments to the cd command > 2
if (numArgs > 2) {
printf(stderr, "Error : too many arguments have been passed!");
return;
}
// Means we have to move to the home directory as only cd is passed
if (numArgs == 1) {
// Get the current directory
getCurrentDirectory();
strcpy(pseudoHome, currentDir);
if(chdir(pseudoHome) < 0){
perror("cd ");
return;
}
strcpy(lastCD, currentDir);
}
// Means we have 2 arguments to the command.
else {
// Case - Move to the pseudoHome
if(strcmp(commandArgument, "~") == 0) {
if(chdir(pseudoHome) < 0) {
perror("cd ");
return;
}
strcpy(lastCD, currentDir);
getCurrentDirectory();
}
// Case - When we have to store the last cd as well.
else if (strcmp(commandArgument, "-") == 0) {
printf("%s\n", lastCD);
if(chdir(lastCD) < 0) {
perror("cd ");
return;
}
strcpy(lastCD, currentDir);
}
// Else move to the directory specified
else {
if (chdir(commandArgument) < 0) {
perror("cd ");
return;
}
strcpy(lastCD, currentDir);
getCurrentDirectory();
}
}
return;
}