xref: /freebsd/contrib/diff/lib/cmpbuf.c (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
118fd37a7SXin LI /* Buffer primitives for comparison operations.
218fd37a7SXin LI 
318fd37a7SXin LI    Copyright (C) 1993, 1995, 1998, 2001, 2002 Free Software Foundation, Inc.
418fd37a7SXin LI 
518fd37a7SXin LI    This program is free software; you can redistribute it and/or modify
618fd37a7SXin LI    it under the terms of the GNU General Public License as published by
718fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
818fd37a7SXin LI    any later version.
918fd37a7SXin LI 
1018fd37a7SXin LI    This program is distributed in the hope that it will be useful,
1118fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
1218fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1318fd37a7SXin LI    GNU General Public License for more details.
1418fd37a7SXin LI 
1518fd37a7SXin LI    You should have received a copy of the GNU General Public License
1618fd37a7SXin LI    along with this program; see the file COPYING.
1718fd37a7SXin LI    If not, write to the Free Software Foundation,
1818fd37a7SXin LI    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
1918fd37a7SXin LI 
2018fd37a7SXin LI #if HAVE_CONFIG_H
2118fd37a7SXin LI # include <config.h>
2218fd37a7SXin LI #endif
2318fd37a7SXin LI 
2418fd37a7SXin LI #include <errno.h>
2518fd37a7SXin LI #include <limits.h>
2618fd37a7SXin LI 
2718fd37a7SXin LI #include <signal.h>
2818fd37a7SXin LI #ifndef SA_RESTART
2918fd37a7SXin LI # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
3018fd37a7SXin LI #  define SA_RESTART SA_INTERRUPT
3118fd37a7SXin LI # else
3218fd37a7SXin LI #  define SA_RESTART 0
3318fd37a7SXin LI # endif
3418fd37a7SXin LI #endif
3518fd37a7SXin LI 
3618fd37a7SXin LI #if HAVE_UNISTD_H
3718fd37a7SXin LI # include <unistd.h>
3818fd37a7SXin LI #endif
3918fd37a7SXin LI 
4018fd37a7SXin LI #if HAVE_INTTYPES_H
4118fd37a7SXin LI # include <inttypes.h>
4218fd37a7SXin LI #endif
4318fd37a7SXin LI 
4418fd37a7SXin LI #include <sys/types.h>
4518fd37a7SXin LI #include "cmpbuf.h"
4618fd37a7SXin LI 
4718fd37a7SXin LI /* Determine whether an integer type is signed, and its bounds.
4818fd37a7SXin LI    This code assumes two's (or one's!) complement with no holes.  */
4918fd37a7SXin LI 
5018fd37a7SXin LI /* The extra casts work around common compiler bugs,
5118fd37a7SXin LI    e.g. Cray C 5.0.3.0 when t == time_t.  */
5218fd37a7SXin LI #ifndef TYPE_SIGNED
5318fd37a7SXin LI # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
5418fd37a7SXin LI #endif
5518fd37a7SXin LI #ifndef TYPE_MINIMUM
5618fd37a7SXin LI # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
5718fd37a7SXin LI 			       ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
5818fd37a7SXin LI 			       : (t) 0))
5918fd37a7SXin LI #endif
6018fd37a7SXin LI #ifndef TYPE_MAXIMUM
6118fd37a7SXin LI # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
6218fd37a7SXin LI #endif
6318fd37a7SXin LI 
6418fd37a7SXin LI #ifndef PTRDIFF_MAX
6518fd37a7SXin LI # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
6618fd37a7SXin LI #endif
6718fd37a7SXin LI #ifndef SIZE_MAX
6818fd37a7SXin LI # define SIZE_MAX TYPE_MAXIMUM (size_t)
6918fd37a7SXin LI #endif
7018fd37a7SXin LI #ifndef SSIZE_MAX
7118fd37a7SXin LI # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
7218fd37a7SXin LI #endif
7318fd37a7SXin LI 
7418fd37a7SXin LI #undef MIN
7518fd37a7SXin LI #define MIN(a, b) ((a) <= (b) ? (a) : (b))
7618fd37a7SXin LI 
7718fd37a7SXin LI /* Read NBYTES bytes from descriptor FD into BUF.
7818fd37a7SXin LI    NBYTES must not be SIZE_MAX.
7918fd37a7SXin LI    Return the number of characters successfully read.
8018fd37a7SXin LI    On error, return SIZE_MAX, setting errno.
8118fd37a7SXin LI    The number returned is always NBYTES unless end-of-file or error.  */
8218fd37a7SXin LI 
8318fd37a7SXin LI size_t
block_read(int fd,char * buf,size_t nbytes)8418fd37a7SXin LI block_read (int fd, char *buf, size_t nbytes)
8518fd37a7SXin LI {
8618fd37a7SXin LI   char *bp = buf;
8718fd37a7SXin LI   char const *buflim = buf + nbytes;
8818fd37a7SXin LI   size_t readlim = SSIZE_MAX;
8918fd37a7SXin LI 
9018fd37a7SXin LI   do
9118fd37a7SXin LI     {
9218fd37a7SXin LI       size_t bytes_to_read = MIN (buflim - bp, readlim);
9318fd37a7SXin LI       ssize_t nread = read (fd, bp, bytes_to_read);
9418fd37a7SXin LI       if (nread <= 0)
9518fd37a7SXin LI 	{
9618fd37a7SXin LI 	  if (nread == 0)
9718fd37a7SXin LI 	    break;
9818fd37a7SXin LI 
9918fd37a7SXin LI 	  /* Accommodate Tru64 5.1, which can't read more than INT_MAX
10018fd37a7SXin LI 	     bytes at a time.  They call that a 64-bit OS?  */
10118fd37a7SXin LI 	  if (errno == EINVAL && INT_MAX < bytes_to_read)
10218fd37a7SXin LI 	    {
10318fd37a7SXin LI 	      readlim = INT_MAX;
10418fd37a7SXin LI 	      continue;
10518fd37a7SXin LI 	    }
10618fd37a7SXin LI 
10718fd37a7SXin LI 	  /* This is needed for programs that have signal handlers on
10818fd37a7SXin LI 	     older hosts without SA_RESTART.  It also accommodates
10918fd37a7SXin LI 	     ancient AIX hosts that set errno to EINTR after uncaught
11018fd37a7SXin LI 	     SIGCONT.  See <news:1r77ojINN85n@ftp.UU.NET>
11118fd37a7SXin LI 	     (1993-04-22).  */
11218fd37a7SXin LI 	  if (! SA_RESTART && errno == EINTR)
11318fd37a7SXin LI 	    continue;
11418fd37a7SXin LI 
11518fd37a7SXin LI 	  return SIZE_MAX;
11618fd37a7SXin LI 	}
11718fd37a7SXin LI       bp += nread;
11818fd37a7SXin LI     }
11918fd37a7SXin LI   while (bp < buflim);
12018fd37a7SXin LI 
12118fd37a7SXin LI   return bp - buf;
12218fd37a7SXin LI }
12318fd37a7SXin LI 
12418fd37a7SXin LI /* Least common multiple of two buffer sizes A and B.  However, if
12518fd37a7SXin LI    either A or B is zero, or if the multiple is greater than LCM_MAX,
12618fd37a7SXin LI    return a reasonable buffer size.  */
12718fd37a7SXin LI 
12818fd37a7SXin LI size_t
buffer_lcm(size_t a,size_t b,size_t lcm_max)12918fd37a7SXin LI buffer_lcm (size_t a, size_t b, size_t lcm_max)
13018fd37a7SXin LI {
13118fd37a7SXin LI   size_t lcm, m, n, q, r;
13218fd37a7SXin LI 
13318fd37a7SXin LI   /* Yield reasonable values if buffer sizes are zero.  */
13418fd37a7SXin LI   if (!a)
13518fd37a7SXin LI     return b ? b : 8 * 1024;
13618fd37a7SXin LI   if (!b)
13718fd37a7SXin LI     return a;
13818fd37a7SXin LI 
13918fd37a7SXin LI   /* n = gcd (a, b) */
14018fd37a7SXin LI   for (m = a, n = b;  (r = m % n) != 0;  m = n, n = r)
14118fd37a7SXin LI     continue;
14218fd37a7SXin LI 
14318fd37a7SXin LI   /* Yield a if there is an overflow.  */
14418fd37a7SXin LI   q = a / n;
14518fd37a7SXin LI   lcm = q * b;
14618fd37a7SXin LI   return lcm <= lcm_max && lcm / b == q ? lcm : a;
14718fd37a7SXin LI }
148