Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/safe-read.c @ 7292:17785d5bede0
Fix docstrings
author | Sergey Poznyakoff <gray@gnu.org.ua> |
---|---|
date | Sun, 10 Sep 2006 11:52:44 +0000 |
parents | fd0ccce602e4 |
children | 8a1a9361108c |
rev | line source |
---|---|
4056
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
1 /* An interface to read and write that retries after interrupts. |
5159 | 2 |
6275 | 3 Copyright (C) 1993, 1994, 1998, 2002, 2003, 2004, 2005 Free Software |
5159 | 4 Foundation, Inc. |
182 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
642
diff
changeset
|
17 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
182 | 19 |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
20 #ifdef HAVE_CONFIG_H |
642
02a593f698b9
Indent nested CPP-directives.
Jim Meyering <jim@meyering.net>
parents:
371
diff
changeset
|
21 # include <config.h> |
182 | 22 #endif |
23 | |
4144
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
24 /* Specification. */ |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
25 #ifdef SAFE_WRITE |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
26 # include "safe-write.h" |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
27 #else |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
28 # include "safe-read.h" |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
29 #endif |
96e52fc820a5
Include specification first.
Bruno Haible <bruno@clisp.org>
parents:
4056
diff
changeset
|
30 |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
2807
diff
changeset
|
31 /* Get ssize_t. */ |
182 | 32 #include <sys/types.h> |
6275 | 33 #include <unistd.h> |
182 | 34 |
35 #include <errno.h> | |
36 | |
4050 | 37 #ifdef EINTR |
38 # define IS_EINTR(x) ((x) == EINTR) | |
39 #else | |
40 # define IS_EINTR(x) 0 | |
41 #endif | |
42 | |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
2807
diff
changeset
|
43 #include <limits.h> |
1320
abeaa95922bd
(noinst_HEADERS): Add safe-read.h.
Jim Meyering <jim@meyering.net>
parents:
728
diff
changeset
|
44 |
4056
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
45 #ifdef SAFE_WRITE |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
46 # define safe_rw safe_write |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
47 # define rw write |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
48 #else |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
49 # define safe_rw safe_read |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
50 # define rw read |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
51 # undef const |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
52 # define const /* empty */ |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
53 #endif |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
54 |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
55 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
56 interrupted. Return the actual number of bytes read(written), zero for EOF, |
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
57 or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */ |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
2807
diff
changeset
|
58 size_t |
4056
9294b143dc83
Rework so that it may serve to define safe_write, too.
Jim Meyering <jim@meyering.net>
parents:
4054
diff
changeset
|
59 safe_rw (int fd, void const *buf, size_t count) |
4002
9fcf64c770ed
Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
2807
diff
changeset
|
60 { |
5159 | 61 /* Work around a bug in Tru64 5.1. Attempting to read more than |
62 INT_MAX bytes fails with errno == EINVAL. See | |
63 <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>. | |
64 When decreasing COUNT, keep it block-aligned. */ | |
65 enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 }; | |
66 | |
67 for (;;) | |
68 { | |
69 ssize_t result = rw (fd, buf, count); | |
179 | 70 |
5159 | 71 if (0 <= result) |
72 return result; | |
73 else if (IS_EINTR (errno)) | |
74 continue; | |
75 else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count) | |
76 count = BUGGY_READ_MAXIMUM; | |
77 else | |
78 return result; | |
371 | 79 } |
179 | 80 } |