-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_price_getter.c
103 lines (84 loc) · 2.23 KB
/
c_price_getter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
extern double convert(double a,double b);
#define FIFO_NAME "/tmp/fifo_crypto"
void signal_handler(int signal){
printf("\nThe SIGINT signal was received\n");
unlink(FIFO_NAME);
exit(signal);
}
int main(){
//asm("include converter.asm");
signal(SIGINT, signal_handler);
int fd;
char buf[1024];
char coin[1024];
ssize_t num_bytes;
umask(0);
// Verificar si la FIFO existe
if (access(FIFO_NAME, F_OK) == -1) {
// Si no existe, crear la FIFO
if(mkfifo(FIFO_NAME, 0666) == -1){
perror("While creating FIFO");
exit(1);
}
}
printf("\nWhich crypto's price do you want? Write 'ETH' for \
Ethereum or 'BTC' for Bitcoin\n");
if(fgets(buf, sizeof(buf), stdin) == NULL)
printf("\nEither an error occured or you entered an empty value\n");
int i=0;
while (1) {
if(strcmp(&buf[i], "\n")== 0){
buf[i] = '\0';
break;
}
i++;
}
strcpy(coin, buf);
fd = open(FIFO_NAME, O_WRONLY);
if (fd == -1){
perror("\nWhile opening the FIFO for reading\n");
exit(1);
}
if(write(fd, &buf, sizeof(buf)) == -1)
perror("\nWhile trying to write to the FIFO\n");
sleep(1);
close(fd);
fd = open(FIFO_NAME, O_RDONLY);
i = 0;
while (1) {
num_bytes = read(fd, buf, sizeof(buf));
if (num_bytes == -1) {
perror("read");
exit(EXIT_FAILURE);
}
if(num_bytes>0){
printf("Received message: Price of %s is %s USD.\n", coin, buf);
buf[num_bytes] = '\0';
}
//Final de la lectura
if (num_bytes == 0) {
break;
}
i++;
}
double USD_ARS = 392;
double USD_EUR = 0.90;
double AUX = strtod(buf, NULL);
double A_PESO;
double A_EURO;
A_PESO = convert(AUX,USD_ARS);
AUX = strtoul(buf, NULL, 10);
A_EURO = convert(AUX,USD_EUR);
printf("Price of %s is %f ARS.\n", coin, A_PESO);
printf("Price of %s is %f EUR.\n", coin, A_EURO);
close(fd);
unlink(FIFO_NAME);
return 0;
}