annotate lib/safe-write.c @ 4002:9fcf64c770ed

Orthogonal approach to read()/write() that handles EINTR and counts > 2^31 correctly.
author Bruno Haible <bruno@clisp.org>
date Fri, 22 Nov 2002 16:25:53 +0000
parents
children 6a1238f585b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* An interface to write() that retries after interrupts.
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 1993, 1994, 1998, 2002 Free Software Foundation, Inc.
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #if HAVE_CONFIG_H
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 # include <config.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #endif
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include "safe-write.h"
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 /* Get ssize_t. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <sys/types.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #if HAVE_UNISTD_H
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # include <unistd.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #endif
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <errno.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #ifndef errno
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 extern int errno;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #endif
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include <limits.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* We don't pass an nbytes count > SSIZE_MAX to write() - POSIX says the
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 effect would be implementation-defined. Also we don't pass an nbytes
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 count > INT_MAX but <= SSIZE_MAX to write() - this triggers a bug in
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 Tru64 5.1. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #define MAX_BYTES_TO_READ INT_MAX
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Write up to COUNT bytes at BUF to descriptor FD, retrying if interrupted.
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 Return the actual number of bytes written, zero for EOF, or (size_t) -1
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 for an error. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 size_t
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 safe_write (int fd, const void *buf, size_t count)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 size_t total_written = 0;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 if (count > 0)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 const char *ptr = (const char *) buf;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 do
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 size_t nbytes_to_write = count;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ssize_t result;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Limit the number of bytes to write in one round, to avoid running
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 into unspecified behaviour. But keep the file pointer block
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 aligned when doing so. */
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 if (nbytes_to_write > MAX_BYTES_TO_READ)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 nbytes_to_write = MAX_BYTES_TO_READ & ~8191;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 result = write (fd, ptr, nbytes_to_write);
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 if (result < 0)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 {
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #ifdef EINTR
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (errno == EINTR)
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 continue;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 return result;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 }
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 total_written += result;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 ptr += result;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 count -= result;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 }
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 while (count > 0);
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 }
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 return total_written;
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 }