Repository navigation
Expand file tree
/
Copy pathio_redirection.c
More file actions
143 lines (129 loc) · 3.59 KB
/
Copy pathio_redirection.c
File metadata and controls
143 lines (129 loc) · 3.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
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
#include "headers.h"
char input_file[256];
char output_file[256];
char overwrite_file[256];
char* io_redrn(char *command, int inp_flg, int out_flg, int over_flg, char **past_commands, int command_count, char *initial_address, char* current_address)
{
char *tokens[100];
int number_of_tokens = 0;
char *token = strtok(command, " ");
while (token != NULL)
{
tokens[number_of_tokens++] = strdup(token);
token = strtok(NULL, " ");
}
for (int i = 0; i < number_of_tokens; i++)
{
if (!strcmp(tokens[i], "<"))
{
if (i == 0)
{
printf("No Input File provided ! \n");
return NULL;
}
// printf("%s ",tokens[i]);
// input_flag = 1;
strcpy(input_file, tokens[i + 1]);
}
if (!strcmp(tokens[i], ">"))
{
if (i == number_of_tokens - 1)
{
printf("No Output File provided ! \n");
return NULL;
}
// printf("%s ",tokens[i]);
// output_flag = 1;
strcpy(output_file, tokens[i + 1]);
}
if (!strcmp(tokens[i], ">>"))
{
if (i == number_of_tokens - 1)
{
printf("No Output File provided ! \n");
return NULL;
}
// printf("%s ",tokens[i]);
// overwrite_flag = 1;
strcpy(overwrite_file, tokens[i + 1]);
}
}
if (inp_flg)
{
int inp_file = open(input_file, O_RDONLY);
dup2(inp_file, STDIN_FILENO);
close(inp_file);
}
if (out_flg)
{
int out_file = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(out_file, STDOUT_FILENO);
close(out_file);
}
if (over_flg)
{
int over_file = open(overwrite_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
dup2(over_file, STDOUT_FILENO);
close(over_file);
}
char *arguments[100];
int number_args = 1;
arguments[0] = tokens[0];
for (int i = 1; i < number_of_tokens; i++)
{
if (!strcmp(tokens[i], "<"))
{
arguments[number_args]=" ";
}
else if (!strcmp(tokens[i], ">") || !strcmp(tokens[i],">>"))
{
// arguments[number_args]='\0';
break;
}
else
{
arguments[number_args] = tokens[i];
}
number_args = number_args + 1;
}
arguments[number_args] = NULL;
int total_length=0;
for (int a = 0; a < number_args; a++)
{
// printf("%s ", arguments[a]);
total_length+=strlen(arguments[a]);
}
// printf("\n");
char* new_command = (char*)malloc(total_length + 1);
// printf("%s",arguments);
new_command[0] = '\0';
char space[] = " ";
// Iterate through the char** array and concatenate each word
for (int i = 0; i < number_args; i++) {
strcat(new_command, arguments[i]);
strcat(new_command, space);
}
// printf("io->%s\n",new_command);
execute(new_command, past_commands, command_count, initial_address, current_address);
// pid_t pid = fork();
// if (pid == 0)
// {
// if (execvp(arguments[0], arguments) == -1)
// {
// // perror("Execvp failed");
// // break;
// // exit(1);
// }
// }
// else if (pid > 0)
// {
// // int status;
// // waitpid(pid, &status, 0);
// wait(NULL);
// }
// else
// {
// perror("Fork failed");
// }
return new_command;
}