-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection_flag_check.c
101 lines (85 loc) · 2.08 KB
/
redirection_flag_check.c
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
#include "headers.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
void red_flag_check(char *input)
{
//printf("hihihihiihhih\n");
int std_in_saved = dup(0);
int std_out_saved = dup(1);
char input_copy[1024];
strcpy(input_copy, input);
char *ret;
char *input_file;
char *input_file_name = NULL;
char *output_file = NULL;
char *output_append_file = NULL;
//Finding the name of the input file.
input_file = strstr(input_copy, "<");
if (input_file != NULL)
{
//printf("hihihihiihhih\n");
input_file_name = strtok(input_file + 1, " \t\r\n\v\f");
int fd_input = open(input_file_name, O_RDONLY);
//printf("The input file %s\n", input_file_name);
if (fd_input < 0) {
perror("Failed to open input file.");
exit(1);
}
if (dup2(fd_input, STDIN_FILENO) < 0) {
dup2(std_in_saved, STDIN_FILENO);
}
close(fd_input);
}
else
{
input_file_name = NULL;
dup2(std_in_saved, STDIN_FILENO);
}
//Finding the name of output file.
strcpy(input_copy, input);
ret = strstr(input_copy, ">>");
if (ret == NULL)
{
output_append_file = NULL;
ret = strstr(input_copy, ">");
if (ret != NULL)
{
output_file = strtok(ret + 1, " \t\r\n\v\f");
int fd_out = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
//printf("The output file %s\n", output_file);
//printf("hi red_flag_check \n");
if (fd_out < 0) {
perror("Failed to create/open output file.");
exit(1);
}
if (dup2(fd_out, STDOUT_FILENO) < 0) {
dup2(std_out_saved, STDOUT_FILENO);
}
close(fd_out);
}
else
{
output_file = NULL;
dup2(std_out_saved, STDOUT_FILENO);
}
}
else
{
output_append_file = strtok(ret + 2, " \t\r\n\v\f");
int fd_out = open(output_append_file, O_APPEND | O_WRONLY | O_CREAT, 0644);
//printf("The output append file %s\n", output_append_file);
output_file = NULL;
if (fd_out < 0) {
perror("Failed to create/open output file.");
exit(1);
}
if (dup2(fd_out, STDOUT_FILENO) < 0) {
dup2(std_out_saved, STDOUT_FILENO);
}
close(fd_out);
}
//printf("hi red_flag_check 79\n");
}