-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcd.c
55 lines (54 loc) · 1.43 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
#include "cd.h"
#include "header.h"
#include "helper.h"
void execute_cd(int arg_count, char **argument)
{
if (arg_count == 1)
cd("~");
else if (arg_count == 2)
{
// char *path = remove_char_front_and_back(argument[0], '\042'); // double quotes
// path = remove_char_front_and_back(path, '\047'); // single quotes
// printf("path is : %s\n", path);
cd(argument[1]);
}
else
{
fprintf(stderr, ERROR "TOO MANY ARGUMENTS FOR cd, GIVE ONLY ONE ARGUMENT\n" RESET);
}
}
// handles the request considering only one input is provided
void cd(char *path)
{
char *final_dir = (char *)malloc(name_len * sizeof(char));
if (strcmp(path, "") == 0)
strcpy(final_dir, cwd_path);
else if (path[0] == '~')
{
strcpy(final_dir, get_home_dir_path(path));
}
else if (strcmp(path, "-") == 0)
{
if (strcmp(last_dir_visited, "") == 0)
{
fprintf(stderr, ERROR "PWD DIDN'T CHANGED\n" RESET);
free(final_dir);
return;
}
else
{
printf("%s\n", last_dir_visited);
strcpy(final_dir, last_dir_visited);
}
}
else
strcpy(final_dir, path);
int flag = chdir(final_dir);
if (flag != 0)
{
perror(ERROR "DIRECTORY CAN'T BE CHANGED ");
print_RESET();
}
getcwd(last_dir_visited, name_len);
free(final_dir);
}