-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
43 lines (42 loc) · 1.05 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
#include "header.h"
void cd(char *line)
{
char *token = line;
int i=0;
token = strtok(NULL, " \n\t\r");
if(token == NULL)
{
if(chdir(HOME) != 0)
perror("cd");
}
else {
while (token != NULL)
{
if (strcmp(token, "~") == 0) {
if (chdir(HOME) == -1){
perror("cd");
}
} else if (strcmp(token, "..") == 0) {
if (chdir(dirname(CWD)) == -1)
{
perror("cd");
}
} else {
// char *new_dir = (char *)malloc(sizeof(char) * 2000);
// strcpy(new_dir, CWD);
// strcat(new_dir, token);
if (chdir(token) == -1){
perror("cd");
}
}
token = strtok(NULL, " \n\t\r");
i++;
if (i>1) {
perror("only one argument expected");
break;
}
}
}
// printf("\n");
return;
}