Linux c 源码(readn:从fd中读取n字节数据)
2015-09-08 11:15
501 查看
/* Read "n" bytes from a descriptor. */ readn(int fd, void *vptr, size_t n) { size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft = n; while (nleft > 0) { if ( (nread = read(fd, ptr, nleft)) < 0) { if (errno == EINTR) nread = 0; /* and call read() again */ else return(-1); } else if (nread == 0) break; /* EOF */ nleft -= nread; ptr += nread; } return(n - nleft); /* return >= 0 */ } /* end readn */ ssize_t Readn(int fd, void *ptr, size_t nbytes) { ssize_t n; if ( (n = readn(fd, ptr, nbytes)) < 0) err_sys("readn error"); return(n); }
使用的时候使用函数Readn,Readn是个包裹函数,编程的时候就不需要对函数进行错误处理了。
相关文章推荐
- Linux初学者的简易脚本---- 匹配合法ip
- linux信号量机制(semaphore)
- linux(centos)搭建SVN服务器
- 使用sam-ba烧写linux4sam_4.7系统到SAMA5D3 Xplained开发板Send Boot File
- Linux部署Resin4.0.44
- Linux部署Resin4.0.44
- linux 笔记
- Linux(centOS)Java环境配置
- centos非root用户(普通用户)安装rpm包
- 关于linux中nfs服务器的搭建
- linux c libcurl的简单使用
- Java读linux系统文件文件名乱码
- Linux字符串转换成时间并且得n天差后的时间
- 【转】Linux - udev 網絡介面卡的順序
- llinux 的一些命令和错误
- linux日志读写接口函数
- VM里linux安装VMTools
- Linux下execl函数学习
- 关于在linux平台或cygwin平台运行opencv出现Illege instruction(core dump)问题的缘由
- Linux 索引节点(inode)详解