-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_output.c
More file actions
86 lines (83 loc) · 2.35 KB
/
input_output.c
File metadata and controls
86 lines (83 loc) · 2.35 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
#include "headers.h"
void in_out_redirection(char **command, int bg, int cmd_len, int *pipe_fd, int *pipe_fd2, int active_pipe)
{
char *input_file = NULL;
char *output_file = NULL;
int input_fd = -1;
int output_fd = -1;
int pid = fork();
if (pid < 0)
{
perror("fork");
}
else if (pid == 0)
{
for (int i = 0; i < cmd_len; i++)
{
if (strcmp(command[i], "<") == 0)
{
command[i] = NULL;
if (i + 1 < cmd_len)
{
input_file = command[i + 1];
input_fd = open(input_file, O_RDONLY);
if (input_fd == -1)
{
perror("open");
printf("No such input file found: %s\n", input_file);
return;
}
}
}
else if (strcmp(command[i], ">") == 0)
{
command[i] = NULL;
if (i + 1 < cmd_len)
{
output_file = command[i + 1];
output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (output_fd == -1)
{
perror("open");
return;
}
}
}
else if (strcmp(command[i], ">>") == 0)
{
command[i] = NULL;
if (i + 1 < cmd_len)
{
output_file = command[i + 1];
output_fd = open(output_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (output_fd == -1)
{
perror("open");
return;
}
}
}
}
if (input_fd != -1)
{
dup2(input_fd, STDIN_FILENO);
close(input_fd);
}
if (output_fd != -1)
{
dup2(output_fd, STDOUT_FILENO);
close(output_fd);
}
int err = execvp(command[0], command);
if (err == -1)
{
printf("ERROR : '%s' is not a valid command\n", command[0]);
}
}
else
{
fg_present = pid;
waitpid(pid, NULL, 0);
fg_present = 0;
}
}