annotate lib/safe-write.c @ 4052:9bc86ccaeda6

safe-read.c, safe-write.c: Change variable names and comments, but not semantics, to minimize the differences between these two files.
author Jim Meyering <jim@meyering.net>
date Tue, 03 Dec 2002 10:55:51 +0000
parents 6a1238f585b1
children 02d9405a0354
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
1 /* An interface to write that retries after interrupts.
4002
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
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
36 #ifdef EINTR
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
37 # define IS_EINTR(x) ((x) == EINTR)
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
38 #else
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
39 # define IS_EINTR(x) 0
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
40 #endif
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
41
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include <limits.h>
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
4049
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
44 #ifndef CHAR_BIT
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
45 # define CHAR_BIT 8
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
46 #endif
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
47
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
48 /* The extra casts work around common compiler bugs. */
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
49 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
50 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
51 It is necessary at least when t == time_t. */
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
52 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
53 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
54 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
55
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
56 #ifndef INT_MAX
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
57 # define INT_MAX TYPE_MAXIMUM (int)
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
58 #endif
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
59
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Write up to COUNT bytes at BUF to descriptor FD, retrying if interrupted.
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
61 Return the actual number of bytes written, zero for EOF, or SAFE_RW_ERROR
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
62 upon error. */
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 size_t
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 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
65 {
4049
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
66 ssize_t result;
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
68 /* POSIX limits COUNT to SSIZE_MAX, but we limit it further, requiring
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
69 that COUNT <= INT_MAX, to avoid triggering a bug in Tru64 5.1.
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
70 When decreasing COUNT, keep the file pointer block-aligned.
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
71 Note that in any case, write may succeed, yet write fewer than COUNT
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
72 bytes, so the caller must be prepared to handle partial results. */
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
73 if (count > INT_MAX)
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
74 count = INT_MAX & ~8191;
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75
4049
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
76 do
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
77 {
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
78 result = write (fd, buf, count);
4049
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
79 }
4052
9bc86ccaeda6 safe-read.c, safe-write.c: Change variable names and comments, but
Jim Meyering <jim@meyering.net>
parents: 4049
diff changeset
80 while (result < 0 && IS_EINTR (errno));
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
4049
6a1238f585b1 Synchronize safe-write with safe-read.
Bruno Haible <bruno@clisp.org>
parents: 4002
diff changeset
82 return (size_t) result;
4002
9fcf64c770ed Orthogonal approach to read()/write() that handles EINTR and counts > 2^31
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 }