-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
42 lines (39 loc) · 983 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
39
40
41
42
#include "header.h"
void cd(char* buff,char* curadd,char* homadd){
const char dl[2]=" ";
char* token;
token = strtok(buff,dl);
token = strtok( 0 ,dl);
if(token==NULL || strcmp(token,"~")==0)
token=homadd;
int n = chdir(token);
if(n<0){
perror("Error opening dir");
return;
}
char str[100005];
getcwd(str,100000);
int lenofh = strlen(homadd);
int lenofc = strlen(str);
if(lenofc>=lenofh){
char s[100005];
for(int i=0;i<lenofh;i++){
s[i]=str[i];
}
s[lenofh]='\0';
int c= strcmp(s,homadd);
if(c==0 && (str[lenofh]=='/' || str[lenofh]=='\0')){
curadd[0]='~';
for(int i=1;i<lenofc-lenofh+1;i++){
curadd[i]=str[i+lenofh-1];
}
curadd[lenofc-lenofh+1]='\0';
}
else{
strcpy(curadd,str);
}
}
else{
strcpy(curadd,str);
}
}