-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
47 lines (38 loc) · 1.1 KB
/
redirection.c
File metadata and controls
47 lines (38 loc) · 1.1 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
#include "redirection.h"
#include "parse.h"
#include "string_functions.h"
int check_redir(char *cmd)
{
const char *result = strchr(cmd, '>');
if (result == NULL)
return 0;
return 1;
}
void redirect(char *cmd, char *fileName)
{
if (fork() == 0)
{
int file_descriptor_1 = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (file_descriptor_1 == -1)
{
printf("Error: File Descripter can't be opened.");
return;
}
int file_descriptor_2 = dup2(file_descriptor_1, STDOUT_FILENO);
close(file_descriptor_1);
parse_string_3(cmd);
close(file_descriptor_2);
exit(0);
}
else
wait(NULL);
}
void call_redirection(char *cmd)
{
// If there is redirection(say cmd_a>b), separate command 'cmd_a' and file 'b',
// go to redirect, change STDOUT to file b and then call parse_string_3(cmd_a) from there.
char **token = tokenize(cmd, "Redirection");
//Remove leading or tailing whitespaces from b.
token[1] = white_space_left_right(token[1]);
redirect(token[0], token[1]);
}