-
Notifications
You must be signed in to change notification settings - Fork 0
/
micalculadora.c
133 lines (100 loc) · 2.27 KB
/
micalculadora.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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
/* Declarations */
int Pipe_Dad[2], Pipe_Son[2];
int Pid;
int Fd_In, Fd_Out;
char Character, Buffer[500], Buffer_2[500];
int B_to_Read_from_IN, B_to_Read_from_SON, i = 0, j, Flag_Comment = -1;
/*PIPES*/
pipe(Pipe_Dad); pipe(Pipe_Son);
/*FORKS*/
Pid = fork();
if(Pid == 0){ /*SON*/
close(Pipe_Dad[1]);
close(Pipe_Son[0]);
dup2(Pipe_Dad[0],0);
dup2(Pipe_Son[1],1);
execlp("bc","bc",NULL);
perror("execlp failed");
}else{ /*DAD*/
close(Pipe_Dad[0]);
close(Pipe_Son[1]);
/*OPEN FILE AND START TO READ*/
Fd_In = open(argv[1], O_RDONLY);
Fd_Out = open(argv[2], O_CREAT|O_RDWR,0777);
do{
B_to_Read_from_IN = read(Fd_In, &Character,1);
if(B_to_Read_from_IN == 0){ /*FINISH WHEN '\0'*/
}else if(Character != '\n'){ /*FULL BUFFER UNTIL '\n' or '/*'*/
if(Character == '/'){
Flag_Comment = Flag_Comment * (-1);
}
if(Flag_Comment != 1 && Character != '/'){
Buffer[i] = Character;
i++;
}
}else{
Buffer[i] = '\n';
i++;
for(j = 0; j <= i; j++){
switch(Buffer[j]){ /*TRANSLATE BUFFER*/
case 'a':
Buffer[j] = ' ';
Buffer[j+1] = '+';
Buffer[j+2] = ' ';
j = j + 2;
break;
case 's':
Buffer[j] = ' ';
Buffer[j+1] = '-';
Buffer[j+2] = ' ';
j = j + 2;
break;
case 'm':
Buffer[j] = ' ';
Buffer[j+1] = '*';
Buffer[j+2] = ' ';
j = j + 2;
break;
case 'd':
Buffer[j] = ' ';
Buffer[j+1] = '/';
Buffer[j+2] = ' ';
j = j + 2;
break;
}
}
;
/*WRITE TO SON*/
write(Pipe_Dad[1],&Buffer,i);
for(j=0;j<=32;j++){
Buffer_2[j] = ' ';
}
/*READ FROM SON*/
read(Pipe_Son[0],&Buffer_2, 32);
j = 0;
/*MAKE UP ' = '*/
Buffer[i-1] = ' ';
Buffer[i] = '=';
Buffer[i+1] = ' ';
i = i + 2;
do{
i++;
Buffer[i] = Buffer_2[j];
j++;
}while(Buffer_2[j] != '\n' && Buffer_2[j] != '\0');
Buffer[i+1] = '\n';
/*WRITE TO OUTPUT FILE*/
write(Fd_Out,&Buffer,i+j);
i = 0;
}
}while(B_to_Read_from_IN != 0);
}
return 0;
}