diffutils/lib/cmpbuf.c
Paul Eggert 2daf7bfa36 Modernize bootstrap procedure to match current gnulib.
* bootstrap: Sync with coreutils bootstrap, except check that
the directory build-aux exists before trying to copy to it.
* bootstrap.conf: New file.
(gnulib_modules): Add config-h, dup2, extensions, fcntl, fdl,
stat-macros, unistd.
* configure.ac: Invoke gl_EARLY and gl_INIT rather than
GNULIB_AUTOCONF_SNIPPET.
(AC_CONFIG_HEADER): Rename config.h to lib/config.h.
(AC_CHECK_HEADERS_ONCE): Don't check for fcntl.h, locale.h,
sys/file.h, unistd.h.  We now use the fcntl and unistd modules,
and locale.h can be assumed for any C89 compiler.
(DIFFUTILS_PREREQUISITES): Remove.  No longer needed now that
we use the stdint module.
(AC_CHECK_FUNCS_ONCE): Remove dup2, which is no longer needed
now that we use the dup2 module.
(AM_GNU_GETTEXT): Use need-formatstring-macros, and ...
(AM_GNU_GETTEXT_VERSION): specify version 0.15 instead of 0.14.5,
to be consistent with coreutils.
* lib/Makefile.am (noinst_LIBRARIES):
(lib_SOURCES, libdiffutils_a_LIBADD):
(libdiffutils_a_DEPENDENCIES, BUILT_SOURCES, EXTRA_DIST):
(MOSTLYCLEANFILES): Remove; now computed automatically.
(noinst_HEADERS, libdiffutils_a_SOURCES): Just append
our special files now.
* lib/cmpbuf.c: Include config.h unconditionally, since we
no longer define HAVE_CONFIG_H.
* lib/prepargs.c: Likewise.
* src/Makefile.am (LDADD): Use $(LIBINTL), not @LIBINTL@.
(diff_LDADD): Use $(LIB_CLOCK_GETTIME), not @LIB_CLOCK_GETTIME@.
* src/dir.c (dir_read): Use _D_EXACT_NAMLEN, not NAMELEN.
* src/system.h (volatile): Remove, since we assume C89 or better.
Include stat-macros.h.
(S_IRWXU, S_IRWXG, S_IRWXO, S_IRUSR, S_IWUSR):
Remove, since we now use stat-macros.h.
(SEEK_SET, SEEK_CUR): Remove, since we assume C89 or better.
Include unistd.h unconditionally, since we use unistd.
Likewise for fcntl.h.
(dup2): Remove, since we now use dup2.
(O_RDONLY, O_RWDR, O_BINARY): Remove, since we now use
fcntl.
Include dirent.h unconditionally.
(NAMLEN): Remove, replacing with....
(_D_EXACT_NAMLEN): New macro.
Include inttypes.h unconditionally.
(PTRDIFF_MAX, SIZE_MAX, UINTMAX_MAX, strtoumax): Remove, since
we now use inttypes.
Include locale.h unconditionally.
(setlocale): Remove, since we now assume locale.h.
2006-09-05 22:57:29 +00:00

148 lines
3.8 KiB
C

/* Buffer primitives for comparison operations.
Copyright (C) 1993, 1995, 1998, 2001, 2002, 2006 Free Software
Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <config.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#ifndef SA_RESTART
# ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
# define SA_RESTART SA_INTERRUPT
# else
# define SA_RESTART 0
# endif
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <sys/types.h>
#include "cmpbuf.h"
/* Determine whether an integer type is signed, and its bounds.
This code assumes two's (or one's!) complement with no holes. */
/* The extra casts work around common compiler bugs,
e.g. Cray C 5.0.3.0 when t == time_t. */
#ifndef TYPE_SIGNED
# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
#endif
#ifndef TYPE_MINIMUM
# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
: (t) 0))
#endif
#ifndef TYPE_MAXIMUM
# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
#endif
#ifndef PTRDIFF_MAX
# define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
#endif
#ifndef SIZE_MAX
# define SIZE_MAX TYPE_MAXIMUM (size_t)
#endif
#ifndef SSIZE_MAX
# define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
#endif
#undef MIN
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
/* Read NBYTES bytes from descriptor FD into BUF.
NBYTES must not be SIZE_MAX.
Return the number of characters successfully read.
On error, return SIZE_MAX, setting errno.
The number returned is always NBYTES unless end-of-file or error. */
size_t
block_read (int fd, char *buf, size_t nbytes)
{
char *bp = buf;
char const *buflim = buf + nbytes;
size_t readlim = MIN (SSIZE_MAX, SIZE_MAX);
do
{
size_t bytes_remaining = buflim - bp;
size_t bytes_to_read = MIN (bytes_remaining, readlim);
ssize_t nread = read (fd, bp, bytes_to_read);
if (nread <= 0)
{
if (nread == 0)
break;
/* Accommodate Tru64 5.1, which can't read more than INT_MAX
bytes at a time. They call that a 64-bit OS? */
if (errno == EINVAL && INT_MAX < bytes_to_read)
{
readlim = INT_MAX;
continue;
}
/* This is needed for programs that have signal handlers on
older hosts without SA_RESTART. It also accommodates
ancient AIX hosts that set errno to EINTR after uncaught
SIGCONT. See <news:1r77ojINN85n@ftp.UU.NET>
(1993-04-22). */
if (! SA_RESTART && errno == EINTR)
continue;
return SIZE_MAX;
}
bp += nread;
}
while (bp < buflim);
return bp - buf;
}
/* Least common multiple of two buffer sizes A and B. However, if
either A or B is zero, or if the multiple is greater than LCM_MAX,
return a reasonable buffer size. */
size_t
buffer_lcm (size_t a, size_t b, size_t lcm_max)
{
size_t lcm, m, n, q, r;
/* Yield reasonable values if buffer sizes are zero. */
if (!a)
return b ? b : 8 * 1024;
if (!b)
return a;
/* n = gcd (a, b) */
for (m = a, n = b; (r = m % n) != 0; m = n, n = r)
continue;
/* Yield a if there is an overflow. */
q = a / n;
lcm = q * b;
return lcm <= lcm_max && lcm / b == q ? lcm : a;
}