- #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;
- }
需要注意:readline函數是每次調用一次系統函數讀取一個字節,這樣導致效率很低。