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