-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.c
70 lines (63 loc) · 1.17 KB
/
orders.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
#include <stdio.h>
//OPEN
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//READ
#include <unistd.h>
//EXIT
#include <stdlib.h>
//WAIT
#include <sys/wait.h>
//STRCAT
#include <string.h>
int main(int arg,char* args[]){
int pid,in,out,status,c,d;
//Array de palabras y su funcion de inicilizado
char words[3][8];
int resetear(char words[3][8]){
for(c=0;c<3;c++){
for(d=0;d<8;d++){
words[c][d] = NULL;
}
}
return 0;
}
int bfr; //bytes for read
char buf;
int i,j =0;
int cmd = open(args[1],O_RDONLY);
resetear(words);
while(bfr = read(cmd,&buf,1) > 0){
if(buf == ' '){
j=0;
i++;
}else if(buf == '\n'){
pid = fork();
if(pid == 0){
in = open(words[1],O_RDONLY);
out = open(words[2],O_WRONLY);
printf("Voy a ejecutar:<%s> | Entrada: <%s> | Salida: <%s>\n",words[0],words[1],words[2]);
close(0);
close(1);
dup(in);
dup(out);
execlp(words[0],words[0],NULL);
exit(-1);
}
wait(&status);
if(status == 0){
printf("%s","Ejecucion correcta\n");
}else{
printf("%s","Ejecucion incorrecta\n");
}
resetear(words);
i=0;
j=0;
}else{
words[i][j] = buf;
j++;
}
}
return 0;
}