1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-15 00:12:00 +02:00
Files
coreutils/src/system.h
Paul Eggert 05e03fc656 Add a bootstrap procedure, so that the CVS version contains fewer
files and we bootstrap the rest from gnulib, gettext, etc.
* README-cvs: New file.
* bootstrap: New file.
* bootstrap.conf: New file.
* .x-sc_trailing_blank: Remove config-log, .gdb-history.  Add .po.
* configure.ac (AC_PREREQ): Move here from m4/*.m4, for benefit
of gnulib-tool.
(gl_DEFAULT_POSIX2_VERSION, gl_USE_SYSTEM_EXTENSIONS, gl_PERL):
(gl_IGNORE_UNUSED_LIBRARIES): Remove; now done by gnulib.
(gl_EARLY): Add.
(gl_MACROS): Call just after gl_EARLY, just for clarity.
* src/c99-to-c98.diff: Remove patch to ls.c; no longer needed.
* src/kill.c (strtoimax): Remove decl.
* src/ls.c: Include "wcwidth.h" instead of rolling it ourselves.
* src/wc.c: Likewise.
* src/ls.c (sort_files): Rewrite to avoid need for C99-style
declaration, so that we don't need to patch this file.
* src/printf.c (strtoimax, strtoumax): Remove decls.
* src/su.c: Include getpass.h.
(getpass): remove.
* src/system.h: Include mempcpy.h, stpcpy.h, strpbrk.h.
Include inttypes.h unconditionally; remove decls it handles.
* lib/Makefile.am: include gnulib.mk, so that we can remove most of
this file.
(AM_CPPFLAGS): Don't mention -I$(srcdir), since that's now done
for us.
(noinst_LIBRARIES, LDDADD, DEFS): Remove.
(libcoreutils_a_SOURCES): Trim down greatly, just to the files
that aren't in gnulib.
Remove defns gnulib does for us.
* m4/check-decl.m4 (gl_CHECK_DECLS):
Don't include stdio.h, string.h, stdlib.h, unistd.h, sys/time.h,
time.h.  Use AC_CHECK_DECLS_ONCE for free, getenv, geteuid, getlogin,
getuid, lseek, malloc, memchr, realloc.  Don't check for getutent,
memrchr, nanosleep, stpcpy, strndup, strnlen, strstr, strtoul,
strtoull.
(_gl_DECL_HEADERS): Remove; all uses removed.
* m4/jm-macros.m4 (gl_MACROS): Move AC_PREREQ into configure.ac
for the benefit of gnulib-tool.  Call gl_INIT.
Do not call or require macros that gnulib will handle for us.
Don't check for fchmod, hasmntopt, isascii, lchown, listmntent,
mempcpy, realpath, wcrtomb, tzset.
(gl_CHECK_ALL_HEADERS): Don't check for sys/statvfs.h, sys/vfs.h,
sys/mount.h.
(gl_CHECK_ALL_TYPES): Require AC_TYPE_UNSIGNED_LONG_LONG_INT.
Don't check for struct stat.st_blksize.
Don't require AC_STRUCT_ST_DM_MODE, gt_TYPE_SSIZE_T,
gl_AC_TYPE_UINT32_T, gl_AC_TYPE_UINTMAX_T, gl_AC_TYPE_UINTPTR_T,
gl_AC_TYPE_UNSIGNED_LONG_LONG.
* m4/prereq.m4 (gl_PREREQ): Require gl_FUNC_XFTS, gl_ROOT_DEV_INO.
Don't require macros that gnulib does for us.
* m4/stat-prog.m4 (cu_PREREQ_STAT_PROG): Don't check for
sys/sysmacros.h.  Don't check for statvfs.  Use AC_CHECK_HEADERS_ONCE
for netinet/in.h, nfs/nfs_clnt.h, nfs/vfs.h.
Don't require gl_AC_TYPE_LONG_LONG, gt_HEADER_INTTYPES_H.
2006-08-21 07:30:45 +00:00

657 lines
16 KiB
C

/* system-dependent definitions for coreutils
Copyright (C) 1989, 1991-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; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <alloca.h>
/* Include sys/types.h before this file. */
#if 2 <= __GLIBC__ && 2 <= __GLIBC_MINOR__
# if ! defined _SYS_TYPES_H
you must include <sys/types.h> before including this file
# endif
#endif
#include <sys/stat.h>
#if !defined HAVE_MKFIFO
# define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
#endif
#if HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#include <unistd.h>
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
# define STDERR_FILENO 2
#endif
/* limits.h must come before pathmax.h because limits.h on some systems
undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
#include <limits.h>
#include "pathmax.h"
#include "localedir.h"
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
/* Since major is a function on SVR4, we can't use `ifndef major'. */
#if MAJOR_IN_MKDEV
# include <sys/mkdev.h>
# define HAVE_MAJOR
#endif
#if MAJOR_IN_SYSMACROS
# include <sys/sysmacros.h>
# define HAVE_MAJOR
#endif
#ifdef major /* Might be defined in sys/types.h. */
# define HAVE_MAJOR
#endif
#ifndef HAVE_MAJOR
# define major(dev) (((dev) >> 8) & 0xff)
# define minor(dev) ((dev) & 0xff)
# define makedev(maj, min) (((maj) << 8) | (min))
#endif
#undef HAVE_MAJOR
#if ! defined makedev && defined mkdev
# define makedev(maj, min) mkdev (maj, min)
#endif
/* Don't use bcopy! Use memmove if source and destination may overlap,
memcpy otherwise. */
#include <string.h>
#include "mempcpy.h"
#include "memrchr.h"
#include "stpcpy.h"
#include "strpbrk.h"
#include <errno.h>
/* Some systems don't define the following symbols. */
#ifndef ENOSYS
# define ENOSYS (-1)
#endif
#ifndef EISDIR
# define EISDIR (-1)
#endif
#include <stdbool.h>
#include <stdlib.h>
/* The following test is to work around the gross typo in
systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
is defined to 0, not 1. */
#if !EXIT_FAILURE
# undef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
/* Exit statuses for programs like 'env' that exec other programs.
EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs. */
enum
{
EXIT_FAIL = 1,
EXIT_CANNOT_INVOKE = 126,
EXIT_ENOENT = 127
};
#include "exitfail.h"
/* Set exit_failure to STATUS if that's not the default already. */
static inline void
initialize_exit_failure (int status)
{
if (status != EXIT_FAILURE)
exit_failure = status;
}
#include <fcntl.h>
#if !defined SEEK_SET
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
#ifndef F_OK
# define F_OK 0
# define X_OK 1
# define W_OK 2
# define R_OK 4
#endif
#if !defined O_DIRECT && defined O_DIRECTIO
/* Tru64 spells it `O_DIRECTIO'. */
# define O_DIRECT O_DIRECTIO
#endif
#if !defined O_DIRECT
# define O_DIRECT 0
#endif
#if !defined O_DIRECTORY
# define O_DIRECTORY 0
#endif
#if !defined O_DSYNC
# define O_DSYNC 0
#endif
#if !defined O_NDELAY
# define O_NDELAY 0
#endif
#if !defined O_NOATIME
# define O_NOATIME 0
#endif
#if !defined O_NONBLOCK
# define O_NONBLOCK O_NDELAY
#endif
#if !defined O_NOCTTY
# define O_NOCTTY 0
#endif
#if !defined O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
#if !defined O_NOLINKS
# define O_NOLINKS 0
#endif
#if !defined O_RSYNC
# define O_RSYNC 0
#endif
#if !defined O_SYNC
# define O_SYNC 0
#endif
/* For systems that distinguish between text and binary I/O.
O_BINARY is usually declared in fcntl.h */
#if !defined O_BINARY && defined _O_BINARY
/* For MSC-compatible compilers. */
# define O_BINARY _O_BINARY
# define O_TEXT _O_TEXT
#endif
#ifdef __BEOS__
/* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
# undef O_BINARY
# undef O_TEXT
#endif
#ifndef O_BINARY
# define O_BINARY 0
# define O_TEXT 0
#endif
#include <dirent.h>
#ifndef _D_EXACT_NAMLEN
# define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
#endif
enum
{
NOT_AN_INODE_NUMBER = 0
};
#ifdef D_INO_IN_DIRENT
# define D_INO(dp) (dp)->d_ino
#else
/* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
# define D_INO(dp) NOT_AN_INODE_NUMBER
#endif
/* Get or fake the disk device blocksize.
Usually defined by sys/param.h (if at all). */
#if !defined DEV_BSIZE && defined BSIZE
# define DEV_BSIZE BSIZE
#endif
#if !defined DEV_BSIZE && defined BBSIZE /* SGI */
# define DEV_BSIZE BBSIZE
#endif
#ifndef DEV_BSIZE
# define DEV_BSIZE 4096
#endif
/* Extract or fake data from a `struct stat'.
ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
#ifndef HAVE_STRUCT_STAT_ST_BLOCKS
# define ST_BLKSIZE(statbuf) DEV_BSIZE
# if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
# define ST_NBLOCKS(statbuf) \
((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
# else /* !_POSIX_SOURCE && BSIZE */
# define ST_NBLOCKS(statbuf) \
(S_ISREG ((statbuf).st_mode) \
|| S_ISDIR ((statbuf).st_mode) \
? st_blocks ((statbuf).st_size) : 0)
# endif /* !_POSIX_SOURCE && BSIZE */
#else /* HAVE_STRUCT_STAT_ST_BLOCKS */
/* Some systems, like Sequents, return st_blksize of 0 on pipes.
Also, when running `rsh hpux11-system cat any-file', cat would
determine that the output stream had an st_blksize of 2147421096.
So here we arbitrarily limit the `optimal' block size to 4MB.
If anyone knows of a system for which the legitimate value for
st_blksize can exceed 4MB, please report it as a bug in this code. */
# define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
&& (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
? (statbuf).st_blksize : DEV_BSIZE)
# if defined hpux || defined __hpux__ || defined __hpux
/* HP-UX counts st_blocks in 1024-byte units.
This loses when mixing HP-UX and BSD file systems with NFS. */
# define ST_NBLOCKSIZE 1024
# else /* !hpux */
# if defined _AIX && defined _I386
/* AIX PS/2 counts st_blocks in 4K units. */
# define ST_NBLOCKSIZE (4 * 1024)
# else /* not AIX PS/2 */
# if defined _CRAY
# define ST_NBLOCKS(statbuf) \
(S_ISREG ((statbuf).st_mode) \
|| S_ISDIR ((statbuf).st_mode) \
? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
# endif /* _CRAY */
# endif /* not AIX PS/2 */
# endif /* !hpux */
#endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
#ifndef ST_NBLOCKS
# define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
#endif
#ifndef ST_NBLOCKSIZE
# ifdef S_BLKSIZE
# define ST_NBLOCKSIZE S_BLKSIZE
# else
# define ST_NBLOCKSIZE 512
# endif
#endif
/* Redirection and wildcarding when done by the utility itself.
Generally a noop, but used in particular for native VMS. */
#ifndef initialize_main
# define initialize_main(ac, av)
#endif
#include "stat-macros.h"
#include "timespec.h"
#include <inttypes.h>
#include <stdint.h>
#include <ctype.h>
#if ! (defined isblank || HAVE_DECL_ISBLANK)
# define isblank(c) ((c) == ' ' || (c) == '\t')
#endif
/* ISDIGIT differs from isdigit, as follows:
- Its arg may be any int or unsigned int; it need not be an unsigned char
or EOF.
- It's typically faster.
POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
isdigit unless it's important to use the locale's definition
of `digit' even when the host does not conform to POSIX. */
#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
/* Convert a possibly-signed character to an unsigned character. This is
a bit safer than casting to unsigned char, since it catches some type
errors that the cast doesn't. */
static inline unsigned char to_uchar (char ch) { return ch; }
#include <locale.h>
/* Take care of NLS matters. */
#include "gettext.h"
#if ! ENABLE_NLS
# undef textdomain
# define textdomain(Domainname) /* empty */
# undef bindtextdomain
# define bindtextdomain(Domainname, Dirname) /* empty */
#endif
#define _(msgid) gettext (msgid)
#define N_(msgid) msgid
/* Return a value that pluralizes the same way that N does, in all
languages we know of. */
static inline unsigned long int
select_plural (uintmax_t n)
{
/* Reduce by a power of ten, but keep it away from zero. The
gettext manual says 1000000 should be safe. */
enum { PLURAL_REDUCER = 1000000 };
return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
}
#define STREQ(a, b) (strcmp ((a), (b)) == 0)
#if !HAVE_DECL_FREE
void free ();
#endif
#if !HAVE_DECL_MALLOC
char *malloc ();
#endif
#if !HAVE_DECL_MEMCHR
char *memchr ();
#endif
#if !HAVE_DECL_REALLOC
char *realloc ();
#endif
#if !HAVE_DECL_GETENV
char *getenv ();
#endif
#if !HAVE_DECL_LSEEK
off_t lseek ();
#endif
#if !HAVE_DECL_GETLOGIN
char *getlogin ();
#endif
#if !HAVE_DECL_TTYNAME
char *ttyname ();
#endif
#if !HAVE_DECL_GETEUID
uid_t geteuid ();
#endif
#if !HAVE_DECL_GETPWUID
struct passwd *getpwuid ();
#endif
#if !HAVE_DECL_GETGRGID
struct group *getgrgid ();
#endif
#if !HAVE_DECL_GETUID
uid_t getuid ();
#endif
#include "xalloc.h"
#include "verify.h"
/* This is simply a shorthand for the common case in which
the third argument to x2nrealloc would be `sizeof *(P)'.
Ensure that sizeof *(P) is *not* 1. In that case, it'd be
better to use X2REALLOC, although not strictly necessary. */
#define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
x2nrealloc (P, PN, sizeof *(P)))
/* Using x2realloc (when appropriate) usually makes your code more
readable than using x2nrealloc, but it also makes it so your
code will malfunction if sizeof *(P) ever becomes 2 or greater.
So use this macro instead of using x2realloc directly. */
#define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
x2realloc (P, PN))
/* Include automatically-generated macros for unlocked I/O. */
#include "unlocked-io.h"
#include "same-inode.h"
#define DOT_OR_DOTDOT(Basename) \
(Basename[0] == '.' && (Basename[1] == '\0' \
|| (Basename[1] == '.' && Basename[2] == '\0')))
/* A wrapper for readdir so that callers don't see entries for `.' or `..'. */
static inline struct dirent const *
readdir_ignoring_dot_and_dotdot (DIR *dirp)
{
while (1)
{
struct dirent const *dp = readdir (dirp);
if (dp == NULL || ! DOT_OR_DOTDOT (dp->d_name))
return dp;
}
}
#if SETVBUF_REVERSED
# define SETVBUF(Stream, Buffer, Type, Size) \
setvbuf (Stream, Type, Buffer, Size)
#else
# define SETVBUF(Stream, Buffer, Type, Size) \
setvbuf (Stream, Buffer, Type, Size)
#endif
/* Factor out some of the common --help and --version processing code. */
/* These enum values cannot possibly conflict with the option values
ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
enum
{
GETOPT_HELP_CHAR = (CHAR_MIN - 2),
GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
};
#define GETOPT_HELP_OPTION_DECL \
"help", no_argument, NULL, GETOPT_HELP_CHAR
#define GETOPT_VERSION_OPTION_DECL \
"version", no_argument, NULL, GETOPT_VERSION_CHAR
#define case_GETOPT_HELP_CHAR \
case GETOPT_HELP_CHAR: \
usage (EXIT_SUCCESS); \
break;
/* Program_name must be a literal string.
Usually it is just PROGRAM_NAME. */
#define USAGE_BUILTIN_WARNING \
_("\n" \
"NOTE: your shell may have its own version of %s, which usually supersedes\n" \
"the version described here. Please refer to your shell's documentation\n" \
"for details about the options it supports.\n")
#define HELP_OPTION_DESCRIPTION \
_(" --help display this help and exit\n")
#define VERSION_OPTION_DESCRIPTION \
_(" --version output version information and exit\n")
#include "closeout.h"
#include "version-etc.h"
#define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
case GETOPT_VERSION_CHAR: \
version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors, \
(char *) NULL); \
exit (EXIT_SUCCESS); \
break;
#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
#include "intprops.h"
#ifndef SSIZE_MAX
# define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
#endif
#ifndef OFF_T_MIN
# define OFF_T_MIN TYPE_MINIMUM (off_t)
#endif
#ifndef OFF_T_MAX
# define OFF_T_MAX TYPE_MAXIMUM (off_t)
#endif
#ifndef UID_T_MAX
# define UID_T_MAX TYPE_MAXIMUM (uid_t)
#endif
#ifndef GID_T_MAX
# define GID_T_MAX TYPE_MAXIMUM (gid_t)
#endif
#ifndef PID_T_MAX
# define PID_T_MAX TYPE_MAXIMUM (pid_t)
#endif
/* Use this to suppress gcc's `...may be used before initialized' warnings. */
#ifdef lint
# define IF_LINT(Code) Code
#else
# define IF_LINT(Code) /* empty */
#endif
#ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
# define __attribute__(x) /* empty */
# endif
#endif
#ifndef ATTRIBUTE_NORETURN
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
#endif
#ifndef ATTRIBUTE_UNUSED
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
#endif
#if defined strdupa
# define ASSIGN_STRDUPA(DEST, S) \
do { DEST = strdupa (S); } while (0)
#else
# define ASSIGN_STRDUPA(DEST, S) \
do \
{ \
const char *s_ = (S); \
size_t len_ = strlen (s_) + 1; \
char *tmp_dest_ = alloca (len_); \
DEST = memcpy (tmp_dest_, (s_), len_); \
} \
while (0)
#endif
#ifndef EOVERFLOW
# define EOVERFLOW EINVAL
#endif
#if ! HAVE_FSEEKO && ! defined fseeko
# define fseeko(s, o, w) ((o) == (long int) (o) \
? fseek (s, o, w) \
: (errno = EOVERFLOW, -1))
#endif
#if ! HAVE_SYNC
# define sync() /* empty */
#endif
/* Compute the greatest common divisor of U and V using Euclid's
algorithm. U and V must be nonzero. */
static inline size_t
gcd (size_t u, size_t v)
{
do
{
size_t t = u % v;
u = v;
v = t;
}
while (v);
return u;
}
/* Compute the least common multiple of U and V. U and V must be
nonzero. There is no overflow checking, so callers should not
specify outlandish sizes. */
static inline size_t
lcm (size_t u, size_t v)
{
return u * (v / gcd (u, v));
}
/* Return PTR, aligned upward to the next multiple of ALIGNMENT.
ALIGNMENT must be nonzero. The caller must arrange for ((char *)
PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
locations. */
static inline void *
ptr_align (void const *ptr, size_t alignment)
{
char const *p0 = ptr;
char const *p1 = p0 + alignment - 1;
return (void *) (p1 - (size_t) p1 % alignment);
}
/* If 10*Accum + Digit_val is larger than the maximum value for Type,
then don't update Accum and return false to indicate it would
overflow. Otherwise, set Accum to that new value and return true.
Verify at compile-time that Type is Accum's type, and that Type is
unsigned. Accum must be an object, so that we can take its
address. Accum and Digit_val may be evaluated multiple times.
The "Added check" below is not strictly required, but it causes GCC
to return a nonzero exit status instead of merely a warning
diagnostic, and that is more useful. */
#define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
( \
(void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
(void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned. */ \
(void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check. */ \
(((Type) -1 / 10 < (Accum) \
|| (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \
)