title: Reading from pipe using poll
body: <p>im trying to read data from pipe using poll, here is code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;poll.h&gt;
  #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 &lt; 0) printf("\n\r error");
  
          if ( ((fdinfo[0].revents&amp;POLLIN) == POLLIN) || ((fdinfo[0].revents&amp;POLLPRI) == POLLPRI) ){
              len = read(fdinfo[0].fd, buff, MAX_BUFF_SIZE);
              if (len &gt; 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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>thx in advance</p>