-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedirectionsHandler.c
155 lines (142 loc) · 4.79 KB
/
RedirectionsHandler.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*****************************************************************************************************\
File Name : RedirectionsHandler.c
Author : Ilay Gilman
Purpose : Holds the implementation for handle_redirections function
\*****************************************************************************************************/
/* Include Section ***********************************************************************************/
/* Standard I/O library - Includes, among else the declaration for printf */
#include <stdio.h>
/* Standard library - various functions for manipulating arrays of characters */
#include <string.h>
/* defines miscellaneous symbolic constants and types, and declares miscellaneous functions */
#include <unistd.h>
/* files modes */
#include <fcntl.h>
#include "VectorParser.h"
/* Definition Section ********************************************************************************/
/* redirect stdout */
#define STDOUT_RED (">")
/* redirect stdout */
#define DSTDOUT_RED (">>")
/* redirect stdin */
#define STDIN_RED ("<")
/* Decleration Section *******************************************************************************/
/*****************************************************************************************************\
* Function Name : open_file
* Function Porpuse : open file & return its fd
* Function Params : char *path, int mode, int *ret_fd
* Return Values : int
* Remarks : available modes -
0 - create & trunc file
1 - append
2 - read
* Authors : Ilay Gilman
\*****************************************************************************************************/
int open_file(char *path, int mode, int *ret_fd);
/* Function Section **********************************************************************************/
/****************************************************************************************************\
* Documentation is in the declertion section
\****************************************************************************************************/
int open_file(char *path, int mode, int *ret_fd)
{
int fd;
/* trunc */
if (mode == 0)
{
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC ,0644);
if (fd == -1)
{
return 7;
}
*ret_fd = fd;
}
/* append */
else if (mode == 1)
{
fd = open(path, O_WRONLY | O_CREAT | O_APPEND ,0644);
if (fd == -1)
{
return 7;
}
*ret_fd = fd;
}
/* check if existed */
else if (mode == 2)
{
fd = open (path, O_RDONLY);
if (fd == -1)
{
return 9;
}
*ret_fd = fd;
}
else
{
return 8;
}
return 0;
}
/****************************************************************************************************\
* Documentation is in the header file
\****************************************************************************************************/
int handle_redirections(char *cmd_argv[], int cmd_argv_len)
{
//DBG - printf("[-]handle_redrections[-]\n");
char *signs[] = {STDOUT_RED, DSTDOUT_RED, STDIN_RED};
int stdout_red_sign_count = count_appearence(cmd_argv, cmd_argv_len, STDOUT_RED);
int dstdout_red_sign_count = count_appearence(cmd_argv, cmd_argv_len, DSTDOUT_RED);
int stdint_red_sign_count = count_appearence(cmd_argv, cmd_argv_len, STDIN_RED);
int redirections_boundaries = stdout_red_sign_count + dstdout_red_sign_count + stdint_red_sign_count;
if (redirections_boundaries == 0)
{
//DBG - printf("handle redirecitons got nthn\n");
return 0;
}
int red_indexes[redirections_boundaries + 1];
get_indexes(cmd_argv, cmd_argv_len, signs, 3, red_indexes);
/* no argument next to redirection */
if (red_indexes[redirections_boundaries] == (cmd_argv_len))
{
return 6;
}
for (int i = 1; i < redirections_boundaries + 1; i++)
{
int file_status = 0;
int file_name_index = red_indexes[i];
char *file_name = cmd_argv[file_name_index];
if (0 == strcmp(*(cmd_argv+file_name_index-1), STDOUT_RED))
{
int file_fd;
file_status = open_file(file_name, 0, &file_fd);
if (file_status != 0)
{
return file_status;
}
dup2(file_fd, 1);
close(file_fd);
}
if (0 == strcmp(*(cmd_argv+file_name_index-1), DSTDOUT_RED))
{
int file_fd;
file_status = open_file(file_name, 1, &file_fd);
if (file_status != 0)
{
return file_status;
}
dup2(file_fd, 1);
close(file_fd);
}
if (0 == strcmp(*(cmd_argv+file_name_index-1), STDIN_RED))
{
int file_fd;
file_status = open_file(file_name, 2, &file_fd);
if (file_status != 0)
{
return file_status;
}
dup2(file_fd, 0);
close(file_fd);
}
}
return 0;
}