-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIL.c
105 lines (81 loc) · 1.47 KB
/
PIL.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
#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>
int main(int arg, char* args[]){
//1. Abrir el fichero de ordenes
int cmd = open(args[1],O_RDONLY);
//2.Interpretarlo e iniciar bucle
int val_r = 0;
char buf;
char words[3][8];
int i = 0;
int j = 0;
int n;
int m;
//Inicializacion words
for(n=0;n<3;n++){
for(m=0;m<8;m++){
words[i][n] = ' ';
}
}
//Fork and execve
int pid = 0;
char *argv[] = {NULL,NULL};
char *env[] = {NULL};
//Exit and wait
int *status;
do {
val_r = read(cmd,&buf,1);
//EXCEPTION
if (val_r == -1){
printf("ERROR lectura\n");
close(cmd);
exit(0);
}
if ((buf == '\n' || buf == '\0') && val_r != 0){
i=0;
j=0;
/* for(n=0;n<3;n++){
printf("%s \n",words[n]);
}
*/
//3.Llamar a Fork
pid = fork();
if(pid != 0){
printf("Soy papa! \n");
wait(status);
printf("%d! \n",*status);
if(*status != -1){
printf("SON WORKED! \n");
}else{
printf("SON FAILED! \n");
}
}else{
printf("SON PROCESS\n");
printf("-----------\n");
printf("Execute %s\n",words[0]);
//3.1 Execve del comando deseado
argv[0] = words[0];
execve(words[0],argv,env);
printf("ALGO HA FALLADO!\n");
exit(-1);
}
}else if(buf == ' '){
i++;
j = 0;
}else{
words[i][j] = buf;
j++;
}
} while(val_r > 0);
//4.Finalizar
return 0;
}