@@ -107,9 +107,12 @@ char * search_cmd_path(const char * program) {
107
107
void execute_single_cmd (CMD_OPTS_REDIRECT * cmd ) {
108
108
// Assume the fork for this single cmd happened before this function was called
109
109
// print_cmd_struct(cmd);
110
+
111
+ printf ("Command: %s\nProcess ID: %d\n" , cmd -> program , getpid ());
112
+
110
113
if (cmd -> in_fd != 0 )
111
114
if (dup2 (cmd -> in_fd , 0 ) == -1 )
112
- err_exit ("# Error in dup2. Exiting...\n" );
115
+ err_exit ("Error in dup2. Exiting...\n" );
113
116
if (cmd -> out_fd != 0 )
114
117
if (dup2 (cmd -> out_fd , 1 ) == -1 )
115
118
err_exit ("Error in dup2. Exiting...\n" );
@@ -126,6 +129,7 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
126
129
}
127
130
else {
128
131
dup2 (in_redirect_fd , 0 );
132
+ printf ("Input file '%s' opened in fd %d. Fd %d is remapped to %d\n" , cmd -> in_redirect_file , in_redirect_fd , 0 , in_redirect_fd );
129
133
}
130
134
}
131
135
if (cmd -> is_append && cmd -> out_redirect_file != NULL ) {
@@ -137,6 +141,7 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
137
141
}
138
142
else {
139
143
dup2 (out_redirect_fd , 1 );
144
+ printf ("Append file '%s' opened in fd %d. Fd %d is remapped to %d\n" , cmd -> out_redirect_file , out_redirect_fd , 1 , out_redirect_fd );
140
145
}
141
146
}
142
147
else if (!cmd -> is_append && cmd -> out_redirect_file != NULL ) {
@@ -148,12 +153,15 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
148
153
}
149
154
else {
150
155
dup2 (out_redirect_fd , 1 );
156
+ printf ("Output file '%s' opened in fd %d. Fd %d is remapped to %d\n" , cmd -> out_redirect_file , out_redirect_fd , 1 , out_redirect_fd );
151
157
}
152
158
}
153
159
154
160
// 'cmd_path' is the path of directory slashed with program
155
161
char * cmd_path = search_cmd_path (cmd -> program );
156
162
if (cmd_path != NULL ) {
163
+ if (cmd -> out_fd == 1 && cmd -> out_redirect_file == NULL )
164
+ printf ("\n************OUTPUT************\n" );
157
165
execv (cmd_path , cmd -> opts );
158
166
}
159
167
else {
@@ -206,8 +214,8 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
206
214
if (i < n_cmds ) {
207
215
cmds [i - 1 ]-> out_fd = pipe_fd [i - 1 ][1 ];
208
216
cmds [i ]-> in_fd = pipe_fd [i - 1 ][0 ];
217
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , cmds [i - 1 ]-> program , cmds [i ]-> program , pipe_fd [i - 1 ][0 ], pipe_fd [i - 1 ][1 ]);
209
218
}
210
-
211
219
pid_t child_cmd_pid = fork ();
212
220
if (child_cmd_pid < 0 ) {
213
221
err_exit ("Error in forking. Exiting...\n" );
@@ -229,6 +237,10 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
229
237
close (pipe_fd [i - 2 ][0 ]);
230
238
}
231
239
waitpid (child_cmd_pid , & status , 0 );
240
+ if (cmds [i - 1 ]-> out_fd == 1 && cmds [i - 1 ]-> out_redirect_file == NULL )
241
+ printf ("******************************\n" );
242
+ printf ("\nStatus of PID %d: %d\n" , child_cmd_pid , status );
243
+ printf ("______________________________\n\n" );
232
244
}
233
245
}
234
246
return ;
@@ -246,11 +258,15 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
246
258
}
247
259
else {
248
260
int status ;
249
- if (cmds [0 ]-> in_fd != 0 )
250
- close (cmds [0 ]-> in_fd );
251
- if (cmds [0 ]-> out_fd != 1 )
252
- close (cmds [0 ]-> out_fd );
261
+ if (cmds [n_cmds - 1 ]-> in_fd != 0 )
262
+ close (cmds [n_cmds - 1 ]-> in_fd );
263
+ if (cmds [n_cmds - 1 ]-> out_fd != 1 )
264
+ close (cmds [n_cmds - 1 ]-> out_fd );
253
265
waitpid (child_cmd_pid , & status , 0 );
266
+ if (cmds [n_cmds - 1 ]-> out_fd == 1 && cmds [n_cmds - 1 ]-> out_redirect_file == NULL )
267
+ printf ("******************************\n" );
268
+ printf ("\nStatus of PID %d: %d\n" , child_cmd_pid , status );
269
+ printf ("______________________________\n\n" );
254
270
}
255
271
}
256
272
@@ -277,12 +293,18 @@ void execute_double_pipe_cmd(CMD_OPTS_REDIRECT * in_cmd,
277
293
}
278
294
if (child_cmd_pid == 0 ) {
279
295
// create new process for the single command
296
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , in_cmd -> program , out1_cmd -> program , pipe_fd [0 ][0 ], pipe_fd [0 ][1 ]);
297
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , in_cmd -> program , out2_cmd -> program , pipe_fd [1 ][0 ], pipe_fd [1 ][1 ]);
280
298
execute_single_cmd (in_cmd );
281
299
}
282
300
else {
283
301
int status ;
284
302
close (pipe_fd [0 ][1 ]);
285
303
waitpid (child_cmd_pid , & status , 0 );
304
+ if (in_cmd -> out_fd == 1 && in_cmd -> out_redirect_file == NULL )
305
+ printf ("******************************\n" );
306
+ printf ("\nStatus of PID %d: %d\n" , child_cmd_pid , status );
307
+ printf ("______________________________\n\n" );
286
308
}
287
309
288
310
tee (pipe_fd [0 ][0 ], pipe_fd [1 ][1 ], INT_MAX , 0 );
@@ -316,12 +338,19 @@ void execute_triple_pipe_cmd(CMD_OPTS_REDIRECT * in_cmd,
316
338
}
317
339
if (child_cmd_pid == 0 ) {
318
340
// create new process for the single command
341
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , in_cmd -> program , out1_cmd -> program , pipe_fd [0 ][0 ], pipe_fd [0 ][1 ]);
342
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , in_cmd -> program , out2_cmd -> program , pipe_fd [1 ][0 ], pipe_fd [1 ][1 ]);
343
+ printf ("Pipe between '%s' and '%s': Read end - %d and Write end - %d\n" , in_cmd -> program , out3_cmd -> program , pipe_fd [2 ][0 ], pipe_fd [2 ][1 ]);
319
344
execute_single_cmd (in_cmd );
320
345
}
321
346
else {
322
347
int status ;
323
348
close (pipe_fd [0 ][1 ]);
324
349
waitpid (child_cmd_pid , & status , 0 );
350
+ if (in_cmd -> out_fd == 1 && in_cmd -> out_redirect_file == NULL )
351
+ printf ("******************************\n" );
352
+ printf ("\nStatus of PID %d: %d\n" , child_cmd_pid , status );
353
+ printf ("______________________________\n\n" );
325
354
}
326
355
327
356
tee (pipe_fd [0 ][0 ], pipe_fd [1 ][1 ], INT_MAX , 0 );
@@ -835,7 +864,8 @@ int main() {
835
864
int curr_pid = getpid ();
836
865
printf ("Process details:\n" );
837
866
printf ("\tProcess Id: %d\n" , curr_pid );
838
- printf ("\tProcess Group Id: %d %d\n" , getpgid (curr_pid ), tcgetpgrp (STDIN_FILENO ));
867
+ printf ("\tProcess Group Id: %d\n" , getpgid (curr_pid ));
868
+ printf ("\tForeground Process Group Id: %d\n" , tcgetpgrp (STDIN_FILENO ));
839
869
printf ("\n" );
840
870
841
871
0 commit comments