-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
116 lines (96 loc) · 2.59 KB
/
redirection.c
File metadata and controls
116 lines (96 loc) · 2.59 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
#include "headers/redirection.h"
#include "headers/utils.h"
// Global variables to store original file descriptors
static int original_stdin = -1;
static int original_stdout = -1;
static int original_stderr = -1;
// Set up input redirection
int setup_input_redirection(char *filename) {
if (!filename) return 0;
// Save original stdin
if (original_stdin == -1) {
original_stdin = dup(STDIN_FILENO);
}
int fd = open(filename, O_RDONLY);
if (fd == -1) {
print_error_with_errno("Cannot open input file");
return -1;
}
if (dup2(fd, STDIN_FILENO) == -1) {
print_error_with_errno("Cannot redirect stdin");
close(fd);
return -1;
}
close(fd);
return 0;
}
// Set up output redirection
int setup_output_redirection(char *filename, int append) {
if (!filename) return 0;
// Save original stdout
if (original_stdout == -1) {
original_stdout = dup(STDOUT_FILENO);
}
int flags = O_WRONLY | O_CREAT;
if (append) {
flags |= O_APPEND;
} else {
flags |= O_TRUNC;
}
int fd = open(filename, flags, 0644);
if (fd == -1) {
print_error_with_errno("Cannot open output file");
return -1;
}
if (dup2(fd, STDOUT_FILENO) == -1) {
print_error_with_errno("Cannot redirect stdout");
close(fd);
return -1;
}
close(fd);
return 0;
}
// Set up error redirection
int setup_error_redirection(char *filename) {
if (!filename) return 0;
// Save original stderr
if (original_stderr == -1) {
original_stderr = dup(STDERR_FILENO);
}
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
print_error_with_errno("Cannot open error file");
return -1;
}
if (dup2(fd, STDERR_FILENO) == -1) {
print_error_with_errno("Cannot redirect stderr");
close(fd);
return -1;
}
close(fd);
return 0;
}
// Restore original stdin
void restore_stdin(void) {
if (original_stdin != -1) {
dup2(original_stdin, STDIN_FILENO);
close(original_stdin);
original_stdin = -1;
}
}
// Restore original stdout
void restore_stdout(void) {
if (original_stdout != -1) {
dup2(original_stdout, STDOUT_FILENO);
close(original_stdout);
original_stdout = -1;
}
}
// Restore original stderr
void restore_stderr(void) {
if (original_stderr != -1) {
dup2(original_stderr, STDERR_FILENO);
close(original_stderr);
original_stderr = -1;
}
}