bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

select() for read a regular file


From: tovis
Subject: select() for read a regular file
Date: Thu, 13 Apr 2006 16:45:45 +0200 (CEST)
User-agent: SquirrelMail/1.4.4

Does the select() working for reading a regular file?
At last I have built a small example code and I get the result that file
is always could be read, event if nothing to read (file siez is zero).

int main(int argc,char *argv[])
{
  fd_set  rd_set;
  struct timeval tout;
  int  df,sw,ret,siz;
  char buff[0x100];
  if ( (df = open(argv[1],(O_RDONLY))) < 0 )
  {
    fprintf(stderr,"ERROR: open - %s\n",strerror(errno));
    return ( 1 );
  }
  sw = 0;
  while ( !sw )
  {
    tout.tv_sec = 1L;
    tout.tv_usec = 0L;
    FD_ZERO =(&rd_set);
    fd_SET(df,&rd_set);
    FD_SET(STDIN_FILENO,&rd_set);
    ret = select(FD_SETSIZE,&rd_set,NULL,NULL,&tout);
    if ( ret > 0 )
    {
      if ( FD_ISSET(df,&rd_set) )
      {
        memset(buff,0,sizeof(buff);
        if ( (siz = read(df,buff,sizeof(buff))) > 0 )
          fprintf(stdout,"RD[%03i]: %.70s\n",siz,buff);
        else if ( siz == 0 )
          fprintf(stderr,"WARNING: nothing to read? - s\n",strerror(errno));
        else
          fprintf(stderr,"ERROR: read - %s\n",strerror(errno));
      }
      if ( FD_ISSET(STDIN_FILENO,&rd_set) )
      {
        memset(buff,0,sizeof(buff));
        fgets(buff,(sizeof(buff) - 1),stdin);
        if ( strstr(buff,"exit" || strstr(buff,"quit") )
          sw = 1;
        fprintf(stdout,"CONSOLE: %.70s\n",buff);
      }
    }
    else if ( ret == 0 )
      fprintf(stderr,"INFO: timeout occured!\n");
    else
      fprintf(stderr,"ERROR: select - %s\n",strerror(errno));
  }
  if ( close(df) )
    fprintf(stderr,"ERROR: close - %s\n",strerror(errno));
  return ( 0 );
}

Could some one figure out whats wrong with this code?






reply via email to

[Prev in Thread] Current Thread [Next in Thread]