-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.c
More file actions
240 lines (219 loc) · 5.39 KB
/
args.c
File metadata and controls
240 lines (219 loc) · 5.39 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "main.h"
//take in the input
char * get_line()
{
if (r != 0)
{
return line;
}
cur = 0;
pipeline = 0 ;
pcur = 0;
char * buffer = malloc(sizeof(char)*MAX_LENGTH);
int c,position =0;
bufsize = MAX_LENGTH;
if (!buffer)
{
printf("Allocation error\n");
exit(1);
}
while (1)
{
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n')
{
buffer[position] = '\0';
r = 1;
return buffer;
}
else
{
if (c == '|')
pipeline++ ;
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize)
{
bufsize += MAX_LENGTH;
buffer = realloc(buffer, bufsize);
if (!buffer)
{
printf("Reallocation error\n");
exit(1);
}
}
}
}
//evaluate the command and break it to meaningful pieces for piping and redirection.
//evaluate for semicolon separated commands
char * evaluate()
{
char * buffer = malloc(sizeof(char)*MAX_LENGTH);
int c,position = 0;
int x = 0;
bufsize = MAX_LENGTH;
if (!buffer)
{
printf("Allocation error\n");
exit(1);
}
while (1)
{
if (x == 0 && pcur == pipeline && pcur > 0)
{
x = 1;
dup2(pipe_file[pcur-1][0],0);
dup2(Stdout,1);
close(pipe_file[pcur-1][1]);
pend = 1;
}
c = line[cur];
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n' || c == '\0')
{
buffer[position] = '\0';
pend = 1;
r = 0;
cur = 0;
semi_end = 0;
return buffer;
}
else if (c == '<' || c == '>')
{
int c_direct = c;
char f[20];
int file = 0;
redirect = 1;
if (c == '>' && line[cur+1] == '>')
{
cur++;
c_direct = '?'; // random symbol to represent >>
}
do{
cur++;
c = line[cur];
}while(c == ' ');
int i = 0;
while(isalnum(c))
{
f[i++] = c;
cur++;
c = line[cur];
}
f[i] = '\0';
if (c_direct == '<')
{
file = open(f, O_RDONLY | O_CREAT);
if(file < 0)
{
printf("unable to open file %s\n",f);
exit(1);
}
dup2(file,0);
}
else if (c_direct == '?')
{
file = open(f, O_WRONLY | O_CREAT | O_APPEND, 0644);
if(file < 0)
{
printf("unable to open file %s\n",f);
exit(1);
}
dup2(file,1);
}
else
{
file = open(f, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(file < 0)
{
printf("unable to open file %s\n",f);
exit(1);
}
dup2(file,1);
}
redirect_file = file;
}
else if(c == '|')
{
pcur++;
if (pipe(pipe_file[pcur-1]) != 0) {
perror("bad pipe");
exit(1);
}
if (pcur == 1)
{
dup2(pipe_file[0][1],1);
close(pipe_file[0][1]);
}
else
{
dup2(pipe_file[pcur - 2][0],0);
dup2(pipe_file[pcur - 1][1],1);
}
cur++ ;
return buffer ;
}
else if(c==';')
{
cur++;
semi_end = 1;
return buffer ;
}
else
{
buffer[position] = c;
position++;
}
cur++ ;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize)
{
bufsize += MAX_LENGTH;
buffer = realloc(buffer, bufsize);
if (!buffer)
{
printf("Reallocation error\n");
exit(1);
}
}
}
}
//tokenize the input line with given delimiters
char ** get_tokens(char * input)
{
char ** tokens = malloc(sizeof(char *)*NUM);
char * t;
int position =0;
int bufsize = NUM;
tokens[0] = strtok(input,DELIM);
position ++;
t = strtok(NULL,DELIM);
while(t)
{
tokens[position] = t;
position ++;
if (position >= bufsize )
{
bufsize += NUM ;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens)
{
printf("tokens not reallocated properly\n");
exit(1);
}
}
t = strtok(NULL,DELIM);
}
tokens[position] = NULL;
tokens_len = position ;
return tokens;
}