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