-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
52 lines (43 loc) · 971 Bytes
/
Copy pathcd.c
File metadata and controls
52 lines (43 loc) · 971 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
42
43
44
45
46
47
48
49
50
51
52
#include "shell.h" // HOME_DIR
#include <unistd.h> // chdir
#include <stdio.h> // perror
#include <string.h> // strlen
void exec_cd(char *dir_path)
{
// no arguments; go to home_dir
if (dir_path == NULL)
{
if (chdir(HOME_DIR) < 0)
{
perror("");
return;
}
return;
}
// remove quotes : why do they not work with chdir ?
if (dir_path[0] == '"' || dir_path[0] == '\'')
{
if (strlen(dir_path) > 0)
{
dir_path[strlen(dir_path) - 1] = '\0';
dir_path = &dir_path[1];
}
}
// telda expansion
if (dir_path[0] == '~')
{
// first change the curr_dir to home
if (chdir(HOME_DIR) < 0)
{
perror("");
return;
}
// now replace ~ by . and proceed normally
dir_path[0] = '.';
}
if (chdir(dir_path) < 0)
{
perror("");
return;
}
}