-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_cd.c
More file actions
42 lines (42 loc) · 1.08 KB
/
execute_cd.c
File metadata and controls
42 lines (42 loc) · 1.08 KB
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
#include"header.h"
#include"variables.h"
int execute_cd(char **args,int flag){
char pwd[2000];getcwd(pwd, sizeof(pwd));
if(args[1] == NULL){
char *s = HOME;
int stat = chdir(s);
if(stat != 0)
perror("shell");
else{
strcpy(OLDPWD, pwd);
}
}
else if(string_compare(args[1], "-")){
if(string_compare(OLDPWD, "\0")){
fprintf(stderr, "shell: cd: OLDPWD not set\n");
}
else{
int stat = chdir(OLDPWD);
if(stat != 0)
perror("shell");
else{
printf("%s\n", OLDPWD);
strcpy(OLDPWD, pwd);
}
}
}
else if(args[1][0] == '~'){
int stat = chdir(concat(HOME, &args[1][1]));
if(stat != 0) perror("shell");
else strcpy(OLDPWD, pwd);
}
else{
int stat = chdir(args[1]);
if(stat != 0)
perror("shell"); // Here I 'll place the name of my shell
else{
strcpy(OLDPWD, pwd);
}
}
return 1;
}