Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1213446
  • 博文数量: 261
  • 博客积分: 4196
  • 博客等级: 上校
  • 技术积分: 3410
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 17:05
文章分类

全部博文(261)

文章存档

2018年(1)

2017年(22)

2016年(2)

2015年(8)

2014年(27)

2013年(40)

2012年(161)

分类: LINUX

2012-03-12 17:09:18

#include <unistd.h>
#include <errno.h>

ssize_t readn(int fd, void *buf, size_t count)
{
  char *strtmp;
  ssize_t reval, realcount=0;
  strtmp = (char *)buf;
  
  while (count>0)
  {
    reval = read(fd, strtmp, count);
    if (reval<0)
      if (errno == EINTR)
        continue;
      else return -1;
    else if (reval>0)
    {
      count -= reval;
      strtmp += reval;
      realcount += reval;
      continue;
    }
    else break;
  }
  
  return realcount;
}

ssize_t writen(int fd, const void *buf, size_t count)
{
  char *strtmp;
  ssize_t reval, realcount=count;
  strtmp = (char *)buf;
  
  while(count>0)
  {
    reval = write(fd, strtmp, count);
    if (reval < 0)
      if (errno == EINTR)
        continue;
      else return -1;
      
    count -= reval;
    strtmp += reval;
  }
  
  return realcount;
}
ssize_t readline(int fd, void *buf, int size)
{
  char *strtmp;
  ssize_t reval, realcount=0;
  strtmp = (char *)buf;
 
  while(size>1)
  {
    reval = read(fd, strtmp, 1);
    if (reval<0)
      if (errno == EINTR)
        continue;
      else return -1;
    else if (reval == 0)
      break;
    else
    {
      realcount++;
      size--;
      if (*strtmp++ =='\n')
        break;
    }
  }
  *strtmp='\0';
  return realcount;
}

PS:这些函数貌似都是写好,封装好的。系统内部没有!

阅读(1206) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

HYYLINUX2012-03-30 09:01:02

rongpmcu: 我觉得有一个前提,fd是阻塞的,呵呵.....
这个嘛!我也不知道,只是在别人程序里看到这几个函数,就在网上搜出来了,我也木有用过啊……(*^__^*)

rongpmcu2012-03-29 23:23:00

我觉得有一个前提,fd是阻塞的,呵呵