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