原文参见:
http://blog.roodo.com/rocksaying/archives/2159383.html
- // The prototype of POSIX of read() is:
- // ssize_t read(int fd, void *buf, size_t nbyte);
- ssize_t saferead(int fd, void *buf, size_t nbyte) {
- size_t nbr; /* number of bytes readed */
- ssize_t rc; /* return code of read() */
- void* bp;
- bp = buf;
- nbr = nbyte;
- while( (rc=read(fd, bp, nbr)) < nbr ) {
- if( rc > 0 ) {
- bp += rc;
- nbr -= rc;
- }
- else if( errno != EINTR )
- abort();
- }
- }
-
- // The prototype of POSIX of write() is:
- // ssize_t write(int fd, void *buf, size_t nbyte) {
- ssize_t safewrite(int fd, void *buf, size_t nbyte) {
- size_t nbw; /* number of bytes written */
- ssize_t rc; /* return code of write() */
- void* bp;
- bp = buf;
- nbw = nbyte;
- while( (rc=write(fd, bp, nbw)) < nbw ) {
- if( rc > 0 ) {
- bp += rc;
- nbw -= rc;
- }
- else if( errno != EINTR )
- abort();
- }
- }
阅读(925) | 评论(0) | 转发(0) |