-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
113 lines (105 loc) · 1.77 KB
/
redirection.c
File metadata and controls
113 lines (105 loc) · 1.77 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//#include "./functions.h"
int tokenise(char *inp,char tokens[100][1024],char *delim);
void trim(char *inp);
void redirect(char *inp,int flag)
{
if(flag==0)
{
char tokens[100][1024];
int a = tokenise(inp,tokens,"<");
char *argv[1024];
int k = 0;
int i;
for( i=0;i<a;i++)
{
if(tokens[i][0]=='\0')
break;
else
{
trim(tokens[i]);
argv[i] = tokens[i];
}
//printf("%s\n",tokens[i]);
}
argv[i] = NULL;
pid_t child_pid;
child_pid = fork();
if(child_pid > 0)
{
pid_t tpid = waitpid(child_pid,&k,0);
}
else
{
execvp(argv[0], argv);
printf("\033[1;31mUnknown command\033[0m :(\n");
exit(0);
}
}
else
{
int i=0,j=0;
char temp[1024];
char tokens[100][1024],tokens1[100][1024];
char *argv[1024];
while(inp[i]!='\0')
{
if(inp[i]=='<')
{
i++;
continue;
}
else
{
temp[j]=inp[i];
i++;j++;
}
}
temp[j] = '\0';
int fl = 0,k=0;
for(i=0;i<j-1;i++)
{
if(temp[i]=='>' && temp[i+1]=='>')
fl=1;
}
int a= tokenise(temp,tokens,">");
char delim[] = " \t\r\n\v\f";
int b= tokenise(tokens[0],tokens1,delim);
for(i=0;i<b;i++)
{
trim(tokens1[i]);
argv[i] = tokens1[i];
}
argv[i]=NULL;
for(i=1;i<a;i++)
{
trim(tokens[i]);
//argv[i+b-1] = tokens[i];
//printf("%s\n",tokens[i]);
}
//argv[i+b-1] = NULL;
pid_t child_pid;
child_pid = fork();
if(child_pid > 0)
{
pid_t tpid = waitpid(child_pid,&k,0);
}
else
{
int fd;
close(1);
if(fl==1)
{
fd = open(tokens[1],O_WRONLY|O_CREAT|O_APPEND,0644);
}
else
{
fd = open(tokens[1],O_WRONLY|O_CREAT|O_TRUNC,0644);
}
//close(fd);
execvp(argv[0], argv);
close(STDOUT_FILENO);
printf("\033[1;31mUnknown command\033[0m :(\n");
exit(0);
}
}
}