Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //open
- #include <fcntl.h>
- //read, write
- #include <unistd.h>
- //signal
- #include <signal.h>
- #define INPIPE "shotPipeOut"
- #define OUTPIPE "shotPipeIn"
- typedef struct _coo{
- int x;
- int y;
- } coo;
- void safeClose(){
- unlink(INPIPE);
- unlink(OUTPIPE);
- exit(1);
- }
- int main(){
- signal(SIGINT, safeClose); //se mi fermano con ctrl-c rimuovo le pipe
- //Creazione mirino, posizione iniziale (0,0)
- coo mirino;
- mirino.x = 0;
- mirino.y = 0;
- //creo le due pipe
- mkfifo(INPIPE, 0666);
- mkfifo(OUTPIPE, 0666);
- int fd0, fd1;
- //apro le due pipe
- if(((fd0 = open(INPIPE, O_RDWR)) < 0) || ((fd1 = open(OUTPIPE, O_RDWR)) < 0)){
- perror("Errore apertura pipe");
- exit(1);
- }
- int i;
- char leggi[10];
- char risposta[10];
- int j = 0;
- while(j++ < 10){
- i = 0;
- //accumulo la stringa
- while((i < 10) && read(fd0, &leggi[i], 1) && (leggi[i++] != '#'));
- coo bersaglio;
- sscanf(leggi, "%d,%d", &(bersaglio.x), &(bersaglio.y));
- coo spostamento;
- spostamento.x = bersaglio.x - mirino.x;
- spostamento.y = bersaglio.y - mirino.y;
- mirino.x = bersaglio.x;
- mirino.y = bersaglio.y;
- sprintf(risposta, "%d,%d#", spostamento.x, spostamento.y);
- printf("%s\n", risposta);
- write(fd1, risposta, strlen(risposta));
- }
- unlink(INPIPE);
- unlink(OUTPIPE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement