-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_cd.c
More file actions
41 lines (32 loc) · 748 Bytes
/
command_cd.c
File metadata and controls
41 lines (32 loc) · 748 Bytes
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
#include "shell.h"
#include "utils.h"
#include "command_cd.h"
bool cd(char* path){
char* temp_prev = getcwd(NULL, 0);
int cd_err = -2;
if(path[0] == '-'){
cd_err = chdir(prev_path);
}
else if(*path == 0){
cd_err = chdir(root_dir);
}
else{
if(!strcmp(&path[0], "~")){
cd_err = chdir(root_dir);
path += 2;
if(strlen(path) > 0){
cd_err = chdir(path);
}
}
else{
cd_err = chdir(path);
}
}
if(cd_err < 0){
perror("There was an error while executing cd");
return false;
}
absolute_path = getcwd(NULL, 0);
strcpy(prev_path, temp_prev);
return true;
}