Hi
im trying to read data from pipe using poll, here is code:
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#define MAX_BUFF_SIZE 128
int main(void){
char buff[MAX_BUFF_SIZE];
char *buf;
int fd,ret,len;
struct pollfd fdinfo[1];
if((fd = open("/home/s/pipe", O_RDONLY)) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
fdinfo[0].fd = fd;
fdinfo[0].events = POLLIN|POLLPRI ;
while(1){
ret = poll(fdinfo,1,-1);
if (ret < 0) printf("\n\r error");
if ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) ){
len = read(fdinfo[0].fd, buff, MAX_BUFF_SIZE);
if (len > 0){
buff[len] = 0;
buf = (char *) malloc(len);
memcpy(buf, buff, len);
printf("\n read len %d\n %s",len, buf);
free(buf);
}
}
}
return 0;
}
It does not seems to work fine - after each properly data i got a lot of garbage in output, but i dont know where it comes frome. So im asking for help.
thx in advance