1 /* Buffer primitives for comparison operations.
3 Copyright (C) 1993, 1995, 1998, 2001, 2002 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.
17 If not, write to the Free Software Foundation,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
30 # define SA_RESTART SA_INTERRUPT
41 # include <inttypes.h>
44 #include <sys/types.h>
47 /* Determine whether an integer type is signed, and its bounds.
48 This code assumes two's (or one's!) complement with no holes. */
50 /* The extra casts work around common compiler bugs,
51 e.g. Cray C 5.0.3.0 when t == time_t. */
53 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
56 # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
57 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
61 # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
65 # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
68 # define SIZE_MAX TYPE_MAXIMUM (size_t)
71 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
75 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
77 /* Read NBYTES bytes from descriptor FD into BUF.
78 NBYTES must not be SIZE_MAX.
79 Return the number of characters successfully read.
80 On error, return SIZE_MAX, setting errno.
81 The number returned is always NBYTES unless end-of-file or error. */
84 block_read (int fd
, char *buf
, size_t nbytes
)
87 char const *buflim
= buf
+ nbytes
;
88 size_t readlim
= SSIZE_MAX
;
92 size_t bytes_to_read
= MIN (buflim
- bp
, readlim
);
93 ssize_t nread
= read (fd
, bp
, bytes_to_read
);
99 /* Accommodate Tru64 5.1, which can't read more than INT_MAX
100 bytes at a time. They call that a 64-bit OS? */
101 if (errno
== EINVAL
&& INT_MAX
< bytes_to_read
)
107 /* This is needed for programs that have signal handlers on
108 older hosts without SA_RESTART. It also accommodates
109 ancient AIX hosts that set errno to EINTR after uncaught
110 SIGCONT. See <news:1r77ojINN85n@ftp.UU.NET>
112 if (! SA_RESTART
&& errno
== EINTR
)
124 /* Least common multiple of two buffer sizes A and B. However, if
125 either A or B is zero, or if the multiple is greater than LCM_MAX,
126 return a reasonable buffer size. */
129 buffer_lcm (size_t a
, size_t b
, size_t lcm_max
)
131 size_t lcm
, m
, n
, q
, r
;
133 /* Yield reasonable values if buffer sizes are zero. */
135 return b
? b
: 8 * 1024;
140 for (m
= a
, n
= b
; (r
= m
% n
) != 0; m
= n
, n
= r
)
143 /* Yield a if there is an overflow. */
146 return lcm
<= lcm_max
&& lcm
/ b
== q
? lcm
: a
;