-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
38 lines (32 loc) · 764 Bytes
/
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
#include "shell.h"
int cd(char *argv1[], int argc1, char* home)
{
char err[1005] = {'\0'};
if(argc1 == 1|| !strcmp(argv1[1],"~"))
{
chdir(home);
return 0;
}
if(argc1>2)
{
printf("cd: too many arguements given. Usage: cd [directory]\n");
return 1;
}
int cd_check;
if(argv1[1][0] == '~')
{
char* string_with_home = (char*) malloc(255);
strcpy(string_with_home, home);
strcat(string_with_home, argv1[1]+1); // In case there is a ~/... path
cd_check = chdir(string_with_home);
}
else
cd_check = chdir(argv1[1]);
if(cd_check!=0)
{
strcpy(err, "bash: cd: ");
strcat(err, argv1[1]);
perror(err);
}
return 0;
}