-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection_function.c
More file actions
100 lines (90 loc) · 4.02 KB
/
redirection_function.c
File metadata and controls
100 lines (90 loc) · 4.02 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
int handle_redirection(char** args, int arg_count) {
// Look for redirection operators in the arguments
for (int i = 0; i < arg_count; i++) {
if (strcmp(args[i], ">") == 0 || strcmp(args[i], ">>") == 0 || strcmp(args[i], "<") == 0) {
// Found a redirection operator
if (i + 1 >= arg_count) {
// No file specified after redirection operator
printf("Syntax error: No file specified after redirection operator\n");
last_exit_code = 1;
return -1; // Indicate error
}
char* cmd = args[0]; // Command is first argument
char* file = args[i + 1]; // File is after redirection operator
char* mode = (strcmp(args[i], ">") == 0) ? "w" :
(strcmp(args[i], ">>") == 0) ? "a" : "r";
// We need to handle the redirection by executing the command with proper file handling
if (strcmp(args[i], "<") == 0) {
// Input redirection - not implemented in this simple version
printf("Input redirection not fully implemented\n");
return -1;
}
// Create new argument list without redirection part
char** new_args = malloc((i + 1) * sizeof(char*));
if (new_args == NULL) {
perror("malloc");
return -1;
}
// Copy command and arguments before redirection
for (int j = 0; j < i; j++) {
new_args[j] = strdup(args[j]);
if (new_args[j] == NULL) {
perror("strdup");
for (int k = 0; k < j; k++) {
free(new_args[k]);
}
free(new_args);
return -1;
}
}
new_args[i] = NULL;
// Special handling for echo command with redirection
if (strcmp(cmd, "echo") == 0 && strcmp(args[i], ">") == 0) {
FILE* f = fopen(file, "w");
if (f == NULL) {
perror("fopen");
free_args(new_args);
last_exit_code = 1;
return -1;
}
// Write the echo content to the file
for (int j = 1; j < i; j++) { // Start from 1 to skip "echo"
fputs(new_args[j], f);
if (j < i - 1) { // Add space between arguments
fputc(' ', f);
}
}
fputc('\n', f); // Add newline at the end
fclose(f);
free_args(new_args);
return 0; // Success
} else if (strcmp(cmd, "echo") == 0 && strcmp(args[i], ">>") == 0) {
FILE* f = fopen(file, "a");
if (f == NULL) {
perror("fopen");
free_args(new_args);
last_exit_code = 1;
return -1;
}
// Append the echo content to the file
for (int j = 1; j < i; j++) { // Start from 1 to skip "echo"
fputs(new_args[j], f);
if (j < i - 1) { // Add space between arguments
fputc(' ', f);
}
}
fputc('\n', f); // Add newline at the end
fclose(f);
free_args(new_args);
return 0; // Success
} else {
// For other commands, we would need to use pipes and redirection
// which is more complex. For this implementation, we'll handle only echo.
free_args(new_args);
return -1; // Not a supported redirection case
}
}
}
// No redirection operators found
return -1; // Indicate no redirection to handle
}