-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
177 lines (138 loc) · 5.58 KB
/
redirection.c
File metadata and controls
177 lines (138 loc) · 5.58 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
//Darshil Patel
//3-22-23
//CIS 3207: Systems Programming and Operating System
//Project 2 : SHELL similar to tsch
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/types.h>
#include "tools.h"
#define MAX_ARGS 1024
//Handles input/output redirection in a shell program
int process_redirection(char **args, int *input_fd, int *output_fd) {
//printf("process redirection works\n");
//Function loops through the array of command arguments, looking for < and > characters
//which indicate input and output redirection respectively.
for (int i = 0; args[i] != NULL; i++) {
//If < is found, the function attempts to open the specified input file and store the resulting
//file descriptor in input_fd.
if (strcmp(args[i], "<") == 0) {
if (args[i + 1] != NULL) {
*input_fd = open(args[i + 1], O_RDONLY);
if (*input_fd < 0) {
perror("Error 404 in opening input file");
return -1;
}
args[i] = NULL;
args[i + 1] = NULL;
i++;
}
else {
fprintf(stderr, "Error 404 b/c no input file specified\n");
return -1;
}
}
//If > is found, the function attempts to open the specified output file and store the resulting
//file descriptor in output_fd.
else if (strcmp(args[i], ">") == 0) {
if (args[i + 1] != NULL) {
*output_fd = open(args[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (*output_fd < 0) {
perror("Error 404 in opening output file");
return -1;
}
args[i] = NULL;
args[i + 1] = NULL;
i++;
}
else {
fprintf(stderr, "Error 404 b/c no output file specified\n");
return -1;
}
}
}
return 0;
}
char **append_arg(char **args, char *arg) {
int count = 0;
while (args[count] != NULL) { //Count # elements in array
count++;
}
args = realloc(args, sizeof(char *) * (count + 2)); //allocate memory
if (!args) {
fprintf(stderr, "Error 404: couldnt allocate memory for arguments\n");
exit(EXIT_FAILURE);
}
args[count] = arg; //appends the string arg to the end of the args array
args[count + 1] = NULL; //Then, sets the next element to null.
return args;
}
// I have tried to use from Recommended approaches to implement Pipes from Readme
// void execute_pipes(char **args, int num_pipes);
//implements the execution of multiple commands connected by pipes
void execute_pipes(char **args, int num_pipes) {
int i, pipe_fds[2 * num_pipes]; //Array - file descriptors for the pipes.
for (i = 0; i < num_pipes; i++) {
//creates the required number of pipes
if (pipe(pipe_fds + i * 2) < 0) { //and increases the pipe_fds array.
perror("Error 404; coudnt create pipe");
exit(EXIT_FAILURE);
}
}
//Create child processes and connect pipes
int pid, status;
int args_idx = 0;
for (i = 0; i <= num_pipes; i++) {
pid = fork();
if (pid == 0) {
if (i != 0) { //current process is not first, redirect its standard input to read end of previous pipe using dup2()
dup2(pipe_fds[(i - 1) * 2], STDIN_FILENO);
}
if (i != num_pipes) { //current process is not last, redirect its standard output to write end of current pipe using dup2()
dup2(pipe_fds[i * 2 + 1], STDOUT_FILENO);
}
for (int j = 0; j < 2 * num_pipes; j++) {
close(pipe_fds[j]); //Close all pipe file descriptors
}
//Parse the command arguments and build a new cmd_args array.
char **cmd_args = parse(args[args_idx], " ");
for (int j = args_idx + 1; args[j] != NULL && strcmp(args[j], "|") != 0; j++) {
cmd_args = append_arg(cmd_args, args[j]);
}
//Look for the executable using search_executable function.
char *executable_path = search_executable(cmd_args[0]);
if (executable_path) {
extern char **environ;
//Execute the command using execve() if the executable was found, otherwise print an error message and exit.
if (execve(executable_path, cmd_args, environ) == -1) {
fprintf(stderr, "Error 404; couldnt execute '%s': %s\n", cmd_args[0], strerror(errno));
exit(EXIT_FAILURE);
}
free(executable_path);
}
else {
fprintf(stderr, "Error 404; not found command: %s\n", cmd_args[0]);
exit(EXIT_FAILURE);
}
}
else if (pid < 0) {
perror("Error 404: couldnt fork");
exit(EXIT_FAILURE);
}
while (args[args_idx] != NULL && strcmp(args[args_idx], "|") != 0) {
args_idx++;
} //Update the args_idx variable to point to the next command after the current pipe
args_idx++;
}
for (i = 0; i < 2 * num_pipes; i++) {
close(pipe_fds[i]);
}
for (i = 0; i <= num_pipes; i++) {
wait(&status);
}
}