1*18fd37a7SXin LI /* Buffer primitives for comparison operations.
2*18fd37a7SXin LI
3*18fd37a7SXin LI Copyright (C) 1993, 1995, 1998, 2001, 2002 Free Software Foundation, Inc.
4*18fd37a7SXin LI
5*18fd37a7SXin LI This program is free software; you can redistribute it and/or modify
6*18fd37a7SXin LI it under the terms of the GNU General Public License as published by
7*18fd37a7SXin LI the Free Software Foundation; either version 2, or (at your option)
8*18fd37a7SXin LI any later version.
9*18fd37a7SXin LI
10*18fd37a7SXin LI This program is distributed in the hope that it will be useful,
11*18fd37a7SXin LI but WITHOUT ANY WARRANTY; without even the implied warranty of
12*18fd37a7SXin LI MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*18fd37a7SXin LI GNU General Public License for more details.
14*18fd37a7SXin LI
15*18fd37a7SXin LI You should have received a copy of the GNU General Public License
16*18fd37a7SXin LI along with this program; see the file COPYING.
17*18fd37a7SXin LI If not, write to the Free Software Foundation,
18*18fd37a7SXin LI 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19*18fd37a7SXin LI
20*18fd37a7SXin LI #if HAVE_CONFIG_H
21*18fd37a7SXin LI # include <config.h>
22*18fd37a7SXin LI #endif
23*18fd37a7SXin LI
24*18fd37a7SXin LI #include <errno.h>
25*18fd37a7SXin LI #include <limits.h>
26*18fd37a7SXin LI
27*18fd37a7SXin LI #include <signal.h>
28*18fd37a7SXin LI #ifndef SA_RESTART
29*18fd37a7SXin LI # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
30*18fd37a7SXin LI # define SA_RESTART SA_INTERRUPT
31*18fd37a7SXin LI # else
32*18fd37a7SXin LI # define SA_RESTART 0
33*18fd37a7SXin LI # endif
34*18fd37a7SXin LI #endif
35*18fd37a7SXin LI
36*18fd37a7SXin LI #if HAVE_UNISTD_H
37*18fd37a7SXin LI # include <unistd.h>
38*18fd37a7SXin LI #endif
39*18fd37a7SXin LI
40*18fd37a7SXin LI #if HAVE_INTTYPES_H
41*18fd37a7SXin LI # include <inttypes.h>
42*18fd37a7SXin LI #endif
43*18fd37a7SXin LI
44*18fd37a7SXin LI #include <sys/types.h>
45*18fd37a7SXin LI #include "cmpbuf.h"
46*18fd37a7SXin LI
47*18fd37a7SXin LI /* Determine whether an integer type is signed, and its bounds.
48*18fd37a7SXin LI This code assumes two's (or one's!) complement with no holes. */
49*18fd37a7SXin LI
50*18fd37a7SXin LI /* The extra casts work around common compiler bugs,
51*18fd37a7SXin LI e.g. Cray C 5.0.3.0 when t == time_t. */
52*18fd37a7SXin LI #ifndef TYPE_SIGNED
53*18fd37a7SXin LI # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
54*18fd37a7SXin LI #endif
55*18fd37a7SXin LI #ifndef TYPE_MINIMUM
56*18fd37a7SXin LI # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
57*18fd37a7SXin LI ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
58*18fd37a7SXin LI : (t) 0))
59*18fd37a7SXin LI #endif
60*18fd37a7SXin LI #ifndef TYPE_MAXIMUM
61*18fd37a7SXin LI # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
62*18fd37a7SXin LI #endif
63*18fd37a7SXin LI
64*18fd37a7SXin LI #ifndef PTRDIFF_MAX
65*18fd37a7SXin LI # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
66*18fd37a7SXin LI #endif
67*18fd37a7SXin LI #ifndef SIZE_MAX
68*18fd37a7SXin LI # define SIZE_MAX TYPE_MAXIMUM (size_t)
69*18fd37a7SXin LI #endif
70*18fd37a7SXin LI #ifndef SSIZE_MAX
71*18fd37a7SXin LI # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
72*18fd37a7SXin LI #endif
73*18fd37a7SXin LI
74*18fd37a7SXin LI #undef MIN
75*18fd37a7SXin LI #define MIN(a, b) ((a) <= (b) ? (a) : (b))
76*18fd37a7SXin LI
77*18fd37a7SXin LI /* Read NBYTES bytes from descriptor FD into BUF.
78*18fd37a7SXin LI NBYTES must not be SIZE_MAX.
79*18fd37a7SXin LI Return the number of characters successfully read.
80*18fd37a7SXin LI On error, return SIZE_MAX, setting errno.
81*18fd37a7SXin LI The number returned is always NBYTES unless end-of-file or error. */
82*18fd37a7SXin LI
83*18fd37a7SXin LI size_t
block_read(int fd,char * buf,size_t nbytes)84*18fd37a7SXin LI block_read (int fd, char *buf, size_t nbytes)
85*18fd37a7SXin LI {
86*18fd37a7SXin LI char *bp = buf;
87*18fd37a7SXin LI char const *buflim = buf + nbytes;
88*18fd37a7SXin LI size_t readlim = SSIZE_MAX;
89*18fd37a7SXin LI
90*18fd37a7SXin LI do
91*18fd37a7SXin LI {
92*18fd37a7SXin LI size_t bytes_to_read = MIN (buflim - bp, readlim);
93*18fd37a7SXin LI ssize_t nread = read (fd, bp, bytes_to_read);
94*18fd37a7SXin LI if (nread <= 0)
95*18fd37a7SXin LI {
96*18fd37a7SXin LI if (nread == 0)
97*18fd37a7SXin LI break;
98*18fd37a7SXin LI
99*18fd37a7SXin LI /* Accommodate Tru64 5.1, which can't read more than INT_MAX
100*18fd37a7SXin LI bytes at a time. They call that a 64-bit OS? */
101*18fd37a7SXin LI if (errno == EINVAL && INT_MAX < bytes_to_read)
102*18fd37a7SXin LI {
103*18fd37a7SXin LI readlim = INT_MAX;
104*18fd37a7SXin LI continue;
105*18fd37a7SXin LI }
106*18fd37a7SXin LI
107*18fd37a7SXin LI /* This is needed for programs that have signal handlers on
108*18fd37a7SXin LI older hosts without SA_RESTART. It also accommodates
109*18fd37a7SXin LI ancient AIX hosts that set errno to EINTR after uncaught
110*18fd37a7SXin LI SIGCONT. See <news:1r77ojINN85n@ftp.UU.NET>
111*18fd37a7SXin LI (1993-04-22). */
112*18fd37a7SXin LI if (! SA_RESTART && errno == EINTR)
113*18fd37a7SXin LI continue;
114*18fd37a7SXin LI
115*18fd37a7SXin LI return SIZE_MAX;
116*18fd37a7SXin LI }
117*18fd37a7SXin LI bp += nread;
118*18fd37a7SXin LI }
119*18fd37a7SXin LI while (bp < buflim);
120*18fd37a7SXin LI
121*18fd37a7SXin LI return bp - buf;
122*18fd37a7SXin LI }
123*18fd37a7SXin LI
124*18fd37a7SXin LI /* Least common multiple of two buffer sizes A and B. However, if
125*18fd37a7SXin LI either A or B is zero, or if the multiple is greater than LCM_MAX,
126*18fd37a7SXin LI return a reasonable buffer size. */
127*18fd37a7SXin LI
128*18fd37a7SXin LI size_t
buffer_lcm(size_t a,size_t b,size_t lcm_max)129*18fd37a7SXin LI buffer_lcm (size_t a, size_t b, size_t lcm_max)
130*18fd37a7SXin LI {
131*18fd37a7SXin LI size_t lcm, m, n, q, r;
132*18fd37a7SXin LI
133*18fd37a7SXin LI /* Yield reasonable values if buffer sizes are zero. */
134*18fd37a7SXin LI if (!a)
135*18fd37a7SXin LI return b ? b : 8 * 1024;
136*18fd37a7SXin LI if (!b)
137*18fd37a7SXin LI return a;
138*18fd37a7SXin LI
139*18fd37a7SXin LI /* n = gcd (a, b) */
140*18fd37a7SXin LI for (m = a, n = b; (r = m % n) != 0; m = n, n = r)
141*18fd37a7SXin LI continue;
142*18fd37a7SXin LI
143*18fd37a7SXin LI /* Yield a if there is an overflow. */
144*18fd37a7SXin LI q = a / n;
145*18fd37a7SXin LI lcm = q * b;
146*18fd37a7SXin LI return lcm <= lcm_max && lcm / b == q ? lcm : a;
147*18fd37a7SXin LI }
148