Skip to content

Commit cb94eec

Browse files
2 parents 7e8334d + 02ab507 commit cb94eec

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

P1/shell.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ char * search_cmd_path(const char * program) {
107107
void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
108108
// Assume the fork for this single cmd happened before this function was called
109109
// print_cmd_struct(cmd);
110+
111+
printf("Command: %s\nProcess ID: %d\n", cmd->program, getpid());
112+
110113
if (cmd->in_fd != 0)
111114
if (dup2(cmd->in_fd, 0) == -1)
112-
err_exit("#Error in dup2. Exiting...\n");
115+
err_exit("Error in dup2. Exiting...\n");
113116
if (cmd->out_fd != 0)
114117
if (dup2(cmd->out_fd, 1) == -1)
115118
err_exit("Error in dup2. Exiting...\n");
@@ -126,6 +129,7 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
126129
}
127130
else {
128131
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);
129133
}
130134
}
131135
if (cmd->is_append && cmd->out_redirect_file != NULL) {
@@ -137,6 +141,7 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
137141
}
138142
else {
139143
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);
140145
}
141146
}
142147
else if (!cmd->is_append && cmd->out_redirect_file != NULL) {
@@ -148,12 +153,15 @@ void execute_single_cmd(CMD_OPTS_REDIRECT * cmd) {
148153
}
149154
else {
150155
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);
151157
}
152158
}
153159

154160
// 'cmd_path' is the path of directory slashed with program
155161
char * cmd_path = search_cmd_path(cmd->program);
156162
if (cmd_path != NULL) {
163+
if (cmd->out_fd == 1 && cmd->out_redirect_file == NULL)
164+
printf("\n************OUTPUT************\n");
157165
execv(cmd_path, cmd->opts);
158166
}
159167
else {
@@ -206,8 +214,8 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
206214
if(i < n_cmds) {
207215
cmds[i-1]->out_fd = pipe_fd[i-1][1];
208216
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]);
209218
}
210-
211219
pid_t child_cmd_pid = fork();
212220
if(child_cmd_pid < 0) {
213221
err_exit("Error in forking. Exiting...\n");
@@ -229,6 +237,10 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
229237
close(pipe_fd[i-2][0]);
230238
}
231239
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");
232244
}
233245
}
234246
return;
@@ -246,11 +258,15 @@ void execute_multiple_pipe_cmd(CMD_OPTS_REDIRECT ** cmds, size_t n_cmds) {
246258
}
247259
else {
248260
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);
253265
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");
254270
}
255271
}
256272

@@ -277,12 +293,18 @@ void execute_double_pipe_cmd(CMD_OPTS_REDIRECT * in_cmd,
277293
}
278294
if (child_cmd_pid == 0) {
279295
// 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]);
280298
execute_single_cmd(in_cmd);
281299
}
282300
else {
283301
int status;
284302
close(pipe_fd[0][1]);
285303
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");
286308
}
287309

288310
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,
316338
}
317339
if (child_cmd_pid == 0) {
318340
// 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]);
319344
execute_single_cmd(in_cmd);
320345
}
321346
else {
322347
int status;
323348
close(pipe_fd[0][1]);
324349
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");
325354
}
326355

327356
tee(pipe_fd[0][0], pipe_fd[1][1], INT_MAX, 0);
@@ -835,7 +864,8 @@ int main() {
835864
int curr_pid = getpid();
836865
printf("Process details:\n");
837866
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));
839869
printf("\n");
840870

841871

0 commit comments

Comments
 (0)