3 /* Buffer primitives for comparison operations.
5 Copyright (C) 1993, 1995, 1998, 2001, 2002 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING.
19 If not, write to the Free Software Foundation,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
32 # define SA_RESTART SA_INTERRUPT
43 # include <inttypes.h>
46 #include <sys/types.h>
49 /* Determine whether an integer type is signed, and its bounds.
50 This code assumes two's (or one's!) complement with no holes. */
52 /* The extra casts work around common compiler bugs,
53 e.g. Cray C 5.0.3.0 when t == time_t. */
55 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
58 # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
59 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
63 # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
67 # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
70 # define SIZE_MAX TYPE_MAXIMUM (size_t)
73 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
77 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
79 /* Read NBYTES bytes from descriptor FD into BUF.
80 NBYTES must not be SIZE_MAX.
81 Return the number of characters successfully read.
82 On error, return SIZE_MAX, setting errno.
83 The number returned is always NBYTES unless end-of-file or error. */
86 block_read (int fd
, char *buf
, size_t nbytes
)
89 char const *buflim
= buf
+ nbytes
;
90 size_t readlim
= SSIZE_MAX
;
94 size_t bytes_to_read
= MIN (buflim
- bp
, readlim
);
95 ssize_t nread
= read (fd
, bp
, bytes_to_read
);
101 /* Accommodate Tru64 5.1, which can't read more than INT_MAX
102 bytes at a time. They call that a 64-bit OS? */
103 if (errno
== EINVAL
&& INT_MAX
< bytes_to_read
)
109 /* This is needed for programs that have signal handlers on
110 older hosts without SA_RESTART. It also accommodates
111 ancient AIX hosts that set errno to EINTR after uncaught
112 SIGCONT. See <news:1r77ojINN85n@ftp.UU.NET>
114 if (! SA_RESTART
&& errno
== EINTR
)
126 /* Least common multiple of two buffer sizes A and B. However, if
127 either A or B is zero, or if the multiple is greater than LCM_MAX,
128 return a reasonable buffer size. */
131 buffer_lcm (size_t a
, size_t b
, size_t lcm_max
)
133 size_t lcm
, m
, n
, q
, r
;
135 /* Yield reasonable values if buffer sizes are zero. */
137 return b
? b
: 8 * 1024;
142 for (m
= a
, n
= b
; (r
= m
% n
) != 0; m
= n
, n
= r
)
145 /* Yield a if there is an overflow. */
148 return lcm
<= lcm_max
&& lcm
/ b
== q
? lcm
: a
;