-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
37 lines (27 loc) · 643 Bytes
/
cd.c
File metadata and controls
37 lines (27 loc) · 643 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
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include"utils.h"
#include"pwd.h"
bool validate_cd(Cmd_s *cmd) {
if (cmd->argc > 2) {
fprintf(stdout, "Usage: cd <dir>\n");
return 1;
}
return 0;
}
void cd(Cmd_s *cmd) {
if (validate_cd(cmd)) return;
char *path;
if (cmd->argc == 1) path = home_path;
else if (!strcmp(cmd->argv[1], "~")) path = home_path;
else if (!strcmp(cmd->argv[1], "-")) path = old_path;
else path = cmd->argv[1];
char *temp = (char *) malloc (MAX_INPUT_SIZE*sizeof(char));
getcwd(temp, MAX_INPUT_SIZE);
if (chdir(path)) {
perror("ash: cd");
}
strcpy(old_path, temp);
free(temp);
}