-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.cpp
38 lines (36 loc) · 790 Bytes
/
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
#include"cd.h"
using namespace std;
int cd(char *argv)
{
char * name;
char abs_path[1024];
if(argv == NULL) //no dir given
{
name = new char[strlen(getenv("HOME"))+1];
strcpy(name, getenv("HOME"));
}
else if(argv[0] == '~')
{
string home = string(getenv("HOME"));
name = new char[home.length() + strlen(argv) +1];
home = home + string(&(argv[1]));
strcpy(name, home.c_str());
}
else
name = argv;
realpath(name, abs_path);
if(chdir(name))
{
if(errno == EACCES)
cout<<"cd: "<<name<<": Permission denied"<<endl;
else if(errno == ENOENT)
cout<<"cd: "<<name<<": No such file or directory"<<endl;
else if(errno == ENOTDIR)
cout<<"cd: "<<name<<": Is not a directory"<<endl;
else
perror("");
return 1;
}
setenv("PWD", abs_path, 1);
return 0;
}