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