-
Notifications
You must be signed in to change notification settings - Fork 0
/
hop.c
64 lines (64 loc) · 1.43 KB
/
hop.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef __HOP_C__
#define __HOP_C__
#include "hop.h"
#include "display.h"
#include<stdio.h>
#include<string.h>
#include<unistd.h>
char prev[4096]=".";
void PrintPath(){
char cur[4096];
getcwd(cur, sizeof(cur));
printf("%s\n",cur);
return;
}
char* prev_dir(){
return prev;
}
void hop(char* newdir,char* home){
// printf("hop\n");
if(strcmp(newdir,"-")==0){
char temp[4096];
getcwd(temp, sizeof(temp));
if(chdir(prev)==-1){
printf("No such directory\n");
return;
}
strcpy(prev,temp);
PrintPath();
return;
}
getcwd(prev, sizeof(prev));
if(newdir[0]=='~' && newdir[1]=='/' && strlen(newdir)>2){
char abs_path[4096];
strcpy(abs_path,home);
replaceSubstring(newdir, "~", "");
strcat(abs_path,newdir);
if(chdir(abs_path)==-1){
printf("No such directory\n");
return;
}
}
else if(strcmp(newdir,"~")==0){
if(chdir(home)==-1){
printf("No such directory\n");
return;
}
}
else if(strcmp(newdir,"hop\n")==0 || strcmp(newdir,"\n")==0){
if(chdir(home)==-1){
printf("No such directory\n");
return;
}
}
else{
if(chdir(newdir)==-1){
printf("No such directory\n");
return;
}
}
// printf("reached here\n");
PrintPath();
return;
}
#endif