-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
50 lines (49 loc) · 1.08 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
#include "headers.h"
void cd(char *command)
{
char buf1[MAX_SIZE];
strcpy(buf1,command);
char tempdir[MAX_SIZE];
char *temp = strtok(buf1," ");
char *arg = strtok(NULL," ");
// calculate arguments, if > 1 throw an error
int arguments = 0;
while(temp != NULL)
{
temp = strtok(NULL," ");
arguments++;
}
if(arguments > 1)
{
printf("cd:Error,Too many arguments provided.\n");
return;
}
// if arg is NULL or ~, then go to home directory
if(arg == NULL)
{
getcwd(prevdir,MAX_SIZE);
chdir(home);
return;
}
if(strcmp(arg,"~") == 0)
{
getcwd(prevdir, MAX_SIZE);
chdir(home);
return;
}
// if arg is -, go to prev directory
if(strcmp(arg,"-") == 0)
{
strcpy(tempdir,prevdir);
getcwd(prevdir,MAX_SIZE);
chdir(tempdir);
return;
}
getcwd(prevdir, MAX_SIZE);
// if cant change directory, throw error and exit
if(chdir(arg) < 0)
{
perror("Cannot change directory");
return;
}
}