Mercurial > hg > octave-shane > gnulib-hg
annotate lib/full-write.c @ 17255:d81be792518a
update from texinfo
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Tue, 01 Jan 2013 15:51:49 -0800 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
1 /* An interface to read and write that retries (if necessary) until complete. |
3403
9ab12ba48e48
Correct credits, as cccp.c no longer
Jim Meyering <jim@meyering.net>
parents:
2460
diff
changeset
|
2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
3 Copyright (C) 1993-1994, 1997-2006, 2009-2013 Free Software Foundation, Inc. |
179 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7592
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
179 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7592
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7592
diff
changeset
|
8 (at your option) any later version. |
179 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7592
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
179 | 17 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7015
diff
changeset
|
18 #include <config.h> |
179 | 19 |
4144
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
20 /* Specification. */ |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
21 #ifdef FULL_READ |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
22 # include "full-read.h" |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
23 #else |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
24 # include "full-write.h" |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
25 #endif |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4142
diff
changeset
|
26 |
4142
1e52316e571b
Undefine and define-away `const' after inclusion
Jim Meyering <jim@meyering.net>
parents:
4140
diff
changeset
|
27 #include <errno.h> |
1e52316e571b
Undefine and define-away `const' after inclusion
Jim Meyering <jim@meyering.net>
parents:
4140
diff
changeset
|
28 |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
29 #ifdef FULL_READ |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
30 # include "safe-read.h" |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
31 # define safe_rw safe_read |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
32 # define full_rw full_read |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
33 # undef const |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
34 # define const /* empty */ |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
35 #else |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
36 # include "safe-write.h" |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
37 # define safe_rw safe_write |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
38 # define full_rw full_write |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
39 #endif |
3403
9ab12ba48e48
Correct credits, as cccp.c no longer
Jim Meyering <jim@meyering.net>
parents:
2460
diff
changeset
|
40 |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
41 #ifdef FULL_READ |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
42 /* Set errno to zero upon EOF. */ |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
43 # define ZERO_BYTE_TRANSFER_ERRNO 0 |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
44 #else |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
45 /* Some buggy drivers return 0 when one tries to write beyond |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
46 a device's end. (Example: Linux 1.2.13 on /dev/fd0.) |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
47 Set errno to ENOSPC so they get a sensible diagnostic. */ |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
48 # define ZERO_BYTE_TRANSFER_ERRNO ENOSPC |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
49 #endif |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
3403
diff
changeset
|
50 |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
51 /* Write(read) COUNT bytes at BUF to(from) descriptor FD, retrying if |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
52 interrupted or if a partial write(read) occurs. Return the number |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
53 of bytes transferred. |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
54 When writing, set errno if fewer than COUNT bytes are written. |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
55 When reading, if fewer than COUNT bytes are read, you must examine |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
56 errno to distinguish failure from EOF (errno == 0). */ |
3403
9ab12ba48e48
Correct credits, as cccp.c no longer
Jim Meyering <jim@meyering.net>
parents:
2460
diff
changeset
|
57 size_t |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
58 full_rw (int fd, const void *buf, size_t count) |
179 | 59 { |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
60 size_t total = 0; |
7592
318625b71404
2006-10-29 Bruno Haible <bruno@clisp.org>
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
61 const char *ptr = (const char *) buf; |
181 | 62 |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
63 while (count > 0) |
179 | 64 { |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
65 size_t n_rw = safe_rw (fd, ptr, count); |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
66 if (n_rw == (size_t) -1) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
67 break; |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
68 if (n_rw == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
69 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
70 errno = ZERO_BYTE_TRANSFER_ERRNO; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
71 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
72 } |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
73 total += n_rw; |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
74 ptr += n_rw; |
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
75 count -= n_rw; |
179 | 76 } |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
3403
diff
changeset
|
77 |
4140
40c9aa461079
Rework so that it may serve to define full_read, too.
Jim Meyering <jim@meyering.net>
parents:
4002
diff
changeset
|
78 return total; |
179 | 79 } |