static int read_count;
static char *read_ptr;
static char read_buf[MAXLINE];
static size_t my_read(int fd,char*ptr)
{
if(read_count<=0)
{
again:
if(read_count=read(fd,readbuf,sizeof(read_buf)))<0)
{
if(errno==EINTER)
goto again;
return(-1);
}
else if(read_count==0)
return (0);
read_ptr=read_buf;
}
read_count--;
*ptr=*read_ptr++;
return(1);
}
size_t readline(int fd,void *vptr, size_t maxlen)
{
size_t n,rc;
char c,*ptr;
ptr=vptr;
for(n=1;n<maxlen;n++)
{
if((rc=my_read(fd,&c))==1)
{
*ptr++=c;
if(c=='\n') break;
}
else if(rc==0)
{
*ptr=0;
return(n-1);
}
}
else
return (-1);
}
*ptr=0;
return(n);
}
size_t readlinebuf(void **vptrptr)
{
if(read_cnt)
*vptrptr=read_ptr;
return read_cnt;
}
带有缓冲功能的read函数
最新推荐文章于 2024-05-31 10:06:01 发布