| 1 |
/*
|
| 2 |
* sb_write.c
|
| 3 |
*
|
| 4 |
* IO functions.
|
| 5 |
*
|
| 6 |
* Copyright 1999-2006 Gentoo Foundation
|
| 7 |
*
|
| 8 |
*
|
| 9 |
* This program is free software; you can redistribute it and/or modify it
|
| 10 |
* under the terms of the GNU General Public License as published by the
|
| 11 |
* Free Software Foundation version 2 of the License.
|
| 12 |
*
|
| 13 |
* This program is distributed in the hope that it will be useful, but
|
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 16 |
* General Public License for more details.
|
| 17 |
*
|
| 18 |
* You should have received a copy of the GNU General Public License along
|
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
| 20 |
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
| 21 |
*
|
| 22 |
* $Header$
|
| 23 |
*/
|
| 24 |
|
| 25 |
|
| 26 |
#include <errno.h>
|
| 27 |
#include <stdio.h>
|
| 28 |
#include <sys/stat.h>
|
| 29 |
#include <unistd.h>
|
| 30 |
#include <fcntl.h>
|
| 31 |
|
| 32 |
#include "sbutil.h"
|
| 33 |
|
| 34 |
|
| 35 |
/* General purpose function to _reliably_ read from a file.
|
| 36 |
*
|
| 37 |
* Returns total read bytes or -1 on error.
|
| 38 |
*/
|
| 39 |
|
| 40 |
size_t sb_read(int fd, void *buf, size_t count)
|
| 41 |
{
|
| 42 |
ssize_t n;
|
| 43 |
size_t accum = 0;
|
| 44 |
|
| 45 |
do {
|
| 46 |
n = read(fd, buf + accum, count - accum);
|
| 47 |
|
| 48 |
if (n > 0) {
|
| 49 |
accum += n;
|
| 50 |
continue;
|
| 51 |
}
|
| 52 |
|
| 53 |
if (n < 0) {
|
| 54 |
if (EINTR == errno) {
|
| 55 |
/* Reset errno to not trigger DBG_MSG */
|
| 56 |
errno = 0;
|
| 57 |
continue;
|
| 58 |
}
|
| 59 |
|
| 60 |
DBG_MSG("Failed to read from fd=%i!\n", fd);
|
| 61 |
return -1;
|
| 62 |
}
|
| 63 |
|
| 64 |
/* Found EOF */
|
| 65 |
break;
|
| 66 |
} while (accum < count);
|
| 67 |
|
| 68 |
return accum;
|
| 69 |
}
|
| 70 |
|