-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.cpp
70 lines (63 loc) · 1.38 KB
/
cd.cpp
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
65
66
67
68
69
70
#include<iostream>
#include<string>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include "headers.h"
using namespace std;
/*
Handled combination for cd
cd direct to home
cd -- direct to home
cd ~ direct to home
cd # direct to home
cd ~# give error
cd - display path and & cd to previous path
*/
/*******************cd implementatio***********************/
void cdimpl(vector<string> vs){
string s;
if(vs.size() <= 2){
if(vs.size() > 1){
s=vs[1].c_str();
}
if(s.length()==1 && s[0] == '-'){
string curpath;
if(pwdimpl() != NULL){
curpath=pwdimpl();
}
cout<<prevpath<<endl;
if(chdir(prevpath.c_str()) < 0){
perror("chdir");
exit(EXIT_FAILURE);
}
prevpath=curpath;
}
else if(vs.size() == 1 || s[0] == '#' || (s.length()==1 && s[0] == '~') ||
(s.length()==2 && s[0] == '-' && s[1]=='-') ){
if(pwdimpl() != NULL){
prevpath=pwdimpl();
}
if(chdir(getenv("HOME")) < 0){
perror("chdir");
exit(EXIT_FAILURE);
}
}
else if(s.length()==2 && s[0] == '~' && s[1]=='#'){
cout<<"chdir :";
cout<<"No such option found"<<endl;
}
else{
if(pwdimpl() != NULL){
prevpath=pwdimpl();
}
if(chdir(s.c_str()) < 0){
perror("chdir");
exit(EXIT_FAILURE);
}
}
}
else{
cout<<"No Such Command Found"<<endl;
}
}