]> mj.ucw.cz Git - libucw.git/blob - ucw/io-careful.c
Use #include <ucw/...> instead of "ucw/..." (and similarly for the other libs)
[libucw.git] / ucw / io-careful.c
1 /*
2  *      UCW Library -- Careful Read/Write
3  *
4  *      (c) 2004--2012 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 #include <ucw/io.h>
12
13 #include <unistd.h>
14
15 /*
16  *  Reads and writes on sockets and pipes can return partial results,
17  *  so we implement an iterated read/write call.
18  */
19
20 int
21 careful_read(int fd, void *buf, int len)
22 {
23   byte *pos = buf;
24   while (len)
25     {
26       int l = read(fd, pos, len);
27       if (l < 0)
28         return -1;
29       if (!l)
30         return 0;
31       pos += l;
32       len -= l;
33     }
34   return 1;
35 }
36
37 int
38 careful_write(int fd, const void *buf, int len)
39 {
40   const byte *pos = buf;
41   while (len)
42     {
43       int l = write(fd, pos, len);
44       if (l < 0)
45         return -1;
46       if (!l)
47         return 0;
48       pos += l;
49       len -= l;
50     }
51   return 1;
52 }