-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
39 lines (38 loc) · 845 Bytes
/
Copy pathcd.c
File metadata and controls
39 lines (38 loc) · 845 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
#include "headers.h"
void change_dir(char * command)
{
char * save_command;
char command_copy[1000];
strcpy(command_copy, command);
char * parameter = strtok_r(command_copy, " ", &save_command);
parameter = strtok_r(NULL, " ", &save_command);
if(parameter == NULL || strcmp(parameter,"~") == 0)
{
int i;
for(i=0;i<strlen(home);i++)
path[i] = home[i];
path[i] = '\0';
int returned_value = chdir(home);
}
else if(strcmp(parameter,".") == 0)
return;
else if(strcmp(parameter,"..") == 0)
{
int i;
for(i=strlen(path)-1;path[i]!= '/';i--){;}
path[i] = '\0';
chdir(path);
}
else if(chdir(parameter) == 0)
{
//update path variable
path[strlen(path)] = '/';
int i = strlen(path);
for(int j=0;j < strlen(parameter);i++, j++)
path[i] = parameter[j];
path[i] = '\0';
}
else
perror("Error");
return;
}