]> mj.ucw.cz Git - libucw.git/blob - ucw/carefulio.c
Logging: Improved log-syslog.
[libucw.git] / ucw / carefulio.c
1 /*
2  *      UCW Library -- Careful Read/Write
3  *
4  *      (c) 2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "ucw/lib.h"
11
12 #include <unistd.h>
13
14 /*
15  *  Reads and writes on sockets and pipes can return partial results,
16  *  so we implement an iterated read/write call.
17  */
18
19 int
20 careful_read(int fd, void *buf, int len)
21 {
22   byte *pos = buf;
23   while (len)
24     {
25       int l = read(fd, pos, len);
26       if (l < 0)
27         return -1;
28       if (!l)
29         return 0;
30       pos += l;
31       len -= l;
32     }
33   return 1;
34 }
35
36 int
37 careful_write(int fd, const void *buf, int len)
38 {
39   const byte *pos = buf;
40   while (len)
41     {
42       int l = write(fd, pos, len);
43       if (l < 0)
44         return -1;
45       if (!l)
46         return 0;
47       pos += l;
48       len -= l;
49     }
50   return 1;
51 }