-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargus.c
247 lines (208 loc) · 5.44 KB
/
argus.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define SIZE 1024
#define INPUT_BUFFER_SIZE (64)
#define QUIT_STRING "q"
int main(int argc, char * argv[]){
int fd;
int fd2;
int r;
int fd3;
char *buf=NULL;
char b;
char input[100];
char buffer[1024];
if(argc == 1){
write(1,"Bem vindo\n",10);
write(1,"Ira introduzir os pedidos por aqui (q para sair)\n",49);
char *command = (char*) malloc(1024) ;
int i;
if(fork()==0){
fd2 = open("FIFO2",O_RDONLY);
char * resp = (char*) malloc(sizeof(1024));
while(1){
while((r = read(fd2,resp,1024)) > 0) {
write(1,resp,r);
}
}
}
do{
i=0;
while((read(0,&b,1)>0) && b!='\n'){
input[i]=b;
i++;
}
input[i]='\0';
command = NULL;
command = strtok(input, " ");
if(strcmp(command,"tempo-inactividade")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"i");
command=strtok(NULL,"\n");
strcpy(buf+1, command);
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"tempo-execucao")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"m");
command=strtok(NULL,"\n");
strcpy(buf+1, command);
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"executar")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"e");
command=strtok(NULL,"\n");
strcpy(buf+1, command);
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"listar")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"l");
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"terminar")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"t");
command=strtok(NULL,"\n");
strcpy(buf+1, command);
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"historico")==0){
buf= malloc(1+strlen(input));
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
strcpy(buf,"r");
write(fd,buf,strlen(buf)+1);
}
else if(strcmp(command,"ajuda")==0){
write(1,"AJUDAS\n",7);
write(1,"Comandos:\n",10);
write(1,"tempo-inactividade n (n=tempo maximo de inatividade num pipe anonimo)\n",70);
write(1,"tempo-execucao n (n=tempo maximo de execucao de uma tarefa)\n",60);
write(1,"executar p1 | p2 .. | pn (executar tarefa)\n",43);
write(1,"listar (listar tarefas em execução)\n",36);
write(1,"terminar n (terminar tarefa n)\n",31);
write(1,"historico (registo historico de tarefas terminadas\n",51);
}
else{
write(1,"Comando desconhecido\n",21);
}
}while (strcmp(command, QUIT_STRING) != 0);
if (NULL != command)
{
write(1,"User quit.\n",11);
}
}else{
int r=0;
char * comando = argv[1];
char c = comando[1];
int fifo;
switch(c){
case('e'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"executar tarefas\n",17);
buf = malloc(1+strlen(argv[2]));
strcpy(buf,"e");
strcpy(buf+1,argv[2]);
int bytes_read=0;
write(fd,buf,strlen(buf)+1);
fd2 = open("FIFO2",O_RDONLY);
while((r = read(fd2,buffer,SIZE)) > 0) {
write(1,buffer,r);
}
break;
case('h'):
write(1,"AJUDAS\n",7);
write(1,"Comandos:\n",10);
write(1,"tempo-inactividade n (n=tempo maximo de inatividade num pipe anonimo)\n",70);
write(1,"tempo-execucao n (n=tempo maximo de execucao de uma tarefa)\n",60);
write(1,"executar p1 | p2 .. | pn (executar tarefa)\n",43);
write(1,"listar (listar tarefas em execução)\n",36);
write(1,"terminar n (terminar tarefa n)\n",31);
write(1,"historico (registo historico de tarefas terminadas\n",51);
break;
case('r'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"registos\n",9);
buf = malloc(1);
buf = "r";
write(fd,buf,strlen(buf)+1);
fd2 = open("FIFO2",O_RDONLY);
while((r = read(fd2,buffer,SIZE)) > 0) {
write(1,buffer,r);
}
break;
case('m'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"tempo maximo definido\n",22);
buf = malloc(1+strlen(argv[2]));
strcpy(buf,"m");
strcpy(buf+1,argv[2]);
write(fd,buf,strlen(buf)+1);
break;
case('i'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"tempo de inatividade entre pipes anonimos definido\n",51);
buf = malloc(1+strlen(argv[2]));
strcpy(buf,"i");
strcpy(buf+1,argv[2]);
write(fd,buf,strlen(buf)+1);
break;
case('l'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"Tarefas em execucao\n",20);
buf = malloc(1);
buf = "l";
write(fd,buf,strlen(buf)+1);
fd2 = open("FIFO2",O_RDONLY);
while((r = read(fd2,buffer,SIZE)) > 0) {
write(1,buffer,r);
}
break;
case('t'):
if((fd=open("FIFO",O_WRONLY))<0){
perror("open");
}
write(1,"Terminando Tarefa\n",18);
buf = malloc(1+strlen(argv[2]));
strcpy(buf,"t");
strcat(buf,argv[2]);
write(fd,buf,strlen(buf)+1);
break;
}
}
return 0;
}