-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
97 lines (80 loc) · 1.65 KB
/
cd.c
File metadata and controls
97 lines (80 loc) · 1.65 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// #include "prompt.h"
#include "headers.h"
// #include "inputgiven.h"
// #include "echo1.h"
// #include "cd.h"
void cd(int rdhere , int wrthere , char *inp)
{
int realin,realout,red;
realin=dup(STDIN_FILENO);
realout=dup(STDOUT_FILENO);
if(rdhere!=STDIN_FILENO)
{
dup2(rdhere,STDIN_FILENO);
}
if(wrthere!=STDOUT_FILENO)
{
dup2(wrthere,STDOUT_FILENO);
}
char path[10240];
memset(path,0,strlen(path));
// char *path;
// path = (char *)malloc(10000);
// printf("inpobt %s\n",inp);
// printf("%ld\n",strlen(inp));
int var = 0;
for(int i=0;i<strlen(inp);i++)
{
if(inp[i] == ' ')
{
var = i + 1;
break;
}
}
// printf("var %d\n",var);
int var2 = var;
char temp[2] = "";
if(var == 0)
{
strcpy(path,home_path);
}
else
{
if(inp[var2] == '~')
{
strcpy(path,home_path);
var++;
}
else if(inp[var2] == '-')
{
strcpy(path,oldpath);
var++;
}
for(int i =var; i<strlen(inp) ;i++)
{
temp[0] = inp[i];
temp[1] = '\0';
// printf("temp %s\n",temp);
strcat(path,temp);
}
}
// printf("path %s\n",path);
getcwd(oldpath, 1024);
int var3=chdir(path);
if(var3 < 0)
{
//handle error
perror("->");
}
if(rdhere!=STDIN_FILENO)
{
dup2(realin,STDIN_FILENO);
}
if(wrthere!=STDOUT_FILENO)
{
dup2(realout,STDOUT_FILENO);
}
red=close(realin);
red=close(realout);
// doubt if only cd is passed not going in if loop
}