/* Buffer primitives for comparison operations.12Copyright (C) 1993, 1995, 1998, 2001, 2002 Free Software Foundation, Inc.34This program is free software; you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation; either version 2, or (at your option)7any later version.89This program is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program; see the file COPYING.16If not, write to the Free Software Foundation,1759 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */1819#if HAVE_CONFIG_H20# include <config.h>21#endif2223#include <errno.h>24#include <limits.h>2526#include <signal.h>27#ifndef SA_RESTART28# ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */29# define SA_RESTART SA_INTERRUPT30# else31# define SA_RESTART 032# endif33#endif3435#if HAVE_UNISTD_H36# include <unistd.h>37#endif3839#if HAVE_INTTYPES_H40# include <inttypes.h>41#endif4243#include <sys/types.h>44#include "cmpbuf.h"4546/* Determine whether an integer type is signed, and its bounds.47This code assumes two's (or one's!) complement with no holes. */4849/* The extra casts work around common compiler bugs,50e.g. Cray C 5.0.3.0 when t == time_t. */51#ifndef TYPE_SIGNED52# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))53#endif54#ifndef TYPE_MINIMUM55# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \56? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \57: (t) 0))58#endif59#ifndef TYPE_MAXIMUM60# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))61#endif6263#ifndef PTRDIFF_MAX64# define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)65#endif66#ifndef SIZE_MAX67# define SIZE_MAX TYPE_MAXIMUM (size_t)68#endif69#ifndef SSIZE_MAX70# define SSIZE_MAX TYPE_MAXIMUM (ssize_t)71#endif7273#undef MIN74#define MIN(a, b) ((a) <= (b) ? (a) : (b))7576/* Read NBYTES bytes from descriptor FD into BUF.77NBYTES must not be SIZE_MAX.78Return the number of characters successfully read.79On error, return SIZE_MAX, setting errno.80The number returned is always NBYTES unless end-of-file or error. */8182size_t83block_read (int fd, char *buf, size_t nbytes)84{85char *bp = buf;86char const *buflim = buf + nbytes;87size_t readlim = SSIZE_MAX;8889do90{91size_t bytes_to_read = MIN (buflim - bp, readlim);92ssize_t nread = read (fd, bp, bytes_to_read);93if (nread <= 0)94{95if (nread == 0)96break;9798/* Accommodate Tru64 5.1, which can't read more than INT_MAX99bytes at a time. They call that a 64-bit OS? */100if (errno == EINVAL && INT_MAX < bytes_to_read)101{102readlim = INT_MAX;103continue;104}105106/* This is needed for programs that have signal handlers on107older hosts without SA_RESTART. It also accommodates108ancient AIX hosts that set errno to EINTR after uncaught109SIGCONT. See <news:[email protected]>110(1993-04-22). */111if (! SA_RESTART && errno == EINTR)112continue;113114return SIZE_MAX;115}116bp += nread;117}118while (bp < buflim);119120return bp - buf;121}122123/* Least common multiple of two buffer sizes A and B. However, if124either A or B is zero, or if the multiple is greater than LCM_MAX,125return a reasonable buffer size. */126127size_t128buffer_lcm (size_t a, size_t b, size_t lcm_max)129{130size_t lcm, m, n, q, r;131132/* Yield reasonable values if buffer sizes are zero. */133if (!a)134return b ? b : 8 * 1024;135if (!b)136return a;137138/* n = gcd (a, b) */139for (m = a, n = b; (r = m % n) != 0; m = n, n = r)140continue;141142/* Yield a if there is an overflow. */143q = a / n;144lcm = q * b;145return lcm <= lcm_max && lcm / b == q ? lcm : a;146}147148149