virtio: Fix and activate PCI MSI-X support.
[dragonfly.git] / contrib / diffutils / lib / cmpbuf.c
bloba47cb70311df50cdd53a15aa528b86c1abed7cdc
1 /* Buffer primitives for comparison operations.
3 Copyright (C) 1993, 1995, 1998, 2001-2002, 2006, 2009-2013 Free Software
4 Foundation, Inc.
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.
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.
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/>. */
19 #include <config.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <sys/types.h>
28 #include "cmpbuf.h"
29 #include "intprops.h"
31 #ifndef SSIZE_MAX
32 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
33 #endif
35 #undef MIN
36 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
38 /* Read NBYTES bytes from descriptor FD into BUF.
39 NBYTES must not be SIZE_MAX.
40 Return the number of characters successfully read.
41 On error, return SIZE_MAX, setting errno.
42 The number returned is always NBYTES unless end-of-file or error. */
44 size_t
45 block_read (int fd, char *buf, size_t nbytes)
47 char *bp = buf;
48 char const *buflim = buf + nbytes;
49 size_t readlim = MIN (SSIZE_MAX, SIZE_MAX);
53 size_t bytes_remaining = buflim - bp;
54 size_t bytes_to_read = MIN (bytes_remaining, readlim);
55 ssize_t nread = read (fd, bp, bytes_to_read);
56 if (nread <= 0)
58 if (nread == 0)
59 break;
61 /* Accommodate Tru64 5.1, which can't read more than INT_MAX
62 bytes at a time. They call that a 64-bit OS? */
63 if (errno == EINVAL && INT_MAX < bytes_to_read)
65 readlim = INT_MAX;
66 continue;
69 /* This is needed for programs that have signal handlers on
70 older hosts without SA_RESTART. It also accommodates
71 ancient AIX hosts that set errno to EINTR after uncaught
72 SIGCONT. See <news:1r77ojINN85n@ftp.UU.NET>
73 (1993-04-22). */
74 if (! SA_RESTART && errno == EINTR)
75 continue;
77 return SIZE_MAX;
79 bp += nread;
81 while (bp < buflim);
83 return bp - buf;
86 /* Least common multiple of two buffer sizes A and B. However, if
87 either A or B is zero, or if the multiple is greater than LCM_MAX,
88 return a reasonable buffer size. */
90 size_t
91 buffer_lcm (size_t a, size_t b, size_t lcm_max)
93 size_t lcm, m, n, q, r;
95 /* Yield reasonable values if buffer sizes are zero. */
96 if (!a)
97 return b ? b : 8 * 1024;
98 if (!b)
99 return a;
101 /* n = gcd (a, b) */
102 for (m = a, n = b; (r = m % n) != 0; m = n, n = r)
103 continue;
105 /* Yield a if there is an overflow. */
106 q = a / n;
107 lcm = q * b;
108 return lcm <= lcm_max && lcm / b == q ? lcm : a;