Blame src/CharReader.cc

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#include <fcntl.h>
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#include "trio.h"
Packit 8f70b4
#include "CharReader.h"
Packit 8f70b4
Packit 8f70b4
int CharReader::Do()
Packit 8f70b4
{
Packit 8f70b4
   int m=STALL;
Packit 8f70b4
   if(ch!=NOCHAR)
Packit 8f70b4
      return m;
Packit 8f70b4
   if(!Ready(fd,POLLIN))
Packit 8f70b4
   {
Packit 8f70b4
      Block(fd,POLLIN);
Packit 8f70b4
      return m;
Packit 8f70b4
   }
Packit 8f70b4
Packit 8f70b4
   int oldfl=fcntl(fd,F_GETFL);
Packit 8f70b4
   if(!(oldfl&O_NONBLOCK))
Packit 8f70b4
      fcntl(fd,F_SETFL,oldfl|O_NONBLOCK);
Packit 8f70b4
Packit 8f70b4
   unsigned char c;
Packit 8f70b4
   int res=read(fd,&c,1);
Packit 8f70b4
   if(res==-1 && errno==EAGAIN)
Packit 8f70b4
      Block(fd,POLLIN);
Packit 8f70b4
   else if(res==-1 && errno==EINTR)
Packit 8f70b4
      m=MOVED;
Packit 8f70b4
   else if(res>0)
Packit 8f70b4
   {
Packit 8f70b4
      ch=c;
Packit 8f70b4
      m=MOVED;
Packit 8f70b4
   }
Packit 8f70b4
   else	 // eof or error.
Packit 8f70b4
   {
Packit 8f70b4
      ch=EOFCHAR;
Packit 8f70b4
      m=MOVED;
Packit 8f70b4
   }
Packit 8f70b4
Packit 8f70b4
   if(!(oldfl&O_NONBLOCK))
Packit 8f70b4
      fcntl(fd,F_SETFL,oldfl);
Packit 8f70b4
Packit 8f70b4
   if(res==-1 && ch==EOFCHAR)
Packit 8f70b4
      fprintf(stderr,"read(%d): %s\n",fd,strerror(errno));
Packit 8f70b4
Packit 8f70b4
   return m;
Packit 8f70b4
}