Imported Upstream version 1.10
[platform/upstream/gdbm.git] / src / fullio.c
1 /* This file is part of GDBM, the GNU data base manager.
2    Copyright (C) 2011  Free Software Foundation, Inc.
3
4    GDBM is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    GDBM is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
16
17 #include "autoconf.h"
18 #include "gdbmdefs.h"
19
20 /* Read exactly SIZE bytes of data into BUFFER.  Return value is 0 on
21    success, GDBM_FILE_EOF, if not enough data is available, and
22    GDBM_FILE_READ_ERROR, if a read error occurs.  In the latter case
23    errno keeps actual system error code. */
24 int
25 _gdbm_full_read (GDBM_FILE dbf, void *buffer, size_t size)
26 {
27   char *ptr = buffer;
28   while (size)
29     {
30       ssize_t rdbytes = __read (dbf, ptr, size);
31       if (rdbytes == -1)
32         {
33           if (errno == EINTR)
34             continue;
35           return GDBM_FILE_READ_ERROR;
36         }
37       if (rdbytes == 0)
38         return GDBM_FILE_EOF;
39       ptr += rdbytes;
40       size -= rdbytes;
41     }
42   return 0;
43 }
44
45 /* Write exactly SIZE bytes of data from BUFFER tp DBF.  Return 0 on
46    success, and GDBM_FILE_READ_ERROR on error.  In the latter case errno
47    will keep actual system error code. */
48 int
49 _gdbm_full_write (GDBM_FILE dbf, void *buffer, size_t size)
50 {
51   char *ptr = buffer;
52   while (size)
53     {
54       ssize_t wrbytes = __write (dbf, ptr, size);
55       if (wrbytes == -1)
56         {
57           if (errno == EINTR)
58             continue;
59           return GDBM_FILE_WRITE_ERROR;
60         }
61       if (wrbytes == 0)
62         {
63           errno = ENOSPC;
64           return GDBM_FILE_WRITE_ERROR;
65         }
66       ptr += wrbytes;
67       size -= wrbytes;
68     }
69   return 0;
70 }