src/system.h - coreutils-8.23
Functions defined
Macros defined
Source code
#include <alloca.h>
#if 2 < __GLIBC__ || ( 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>
#define MODE_RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#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>
#include <limits.h>
#include "pathmax.h"
#ifndef PATH_MAX
# define PATH_MAX 8192
#endif
#include "configmake.h"
#include <sys/time.h>
#include <time.h>
#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 # 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
#include <string.h>
#include <errno.h>
#ifndef ENODATA
# define ENODATA (-1)
#endif
#include <stdbool.h>
#include <stdlib.h>
#include "version.h"
enum
{
EXIT_TIMEDOUT = 124, EXIT_CANCELED = 125, EXIT_CANNOT_INVOKE = 126, EXIT_ENOENT = 127 };
#include "exitfail.h"
static inline void
initialize_exit_failure (int status)
{
if (status != EXIT_FAILURE)
exit_failure = status;
}
#include <fcntl.h>
#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
# define D_INO(dp) NOT_AN_INODE_NUMBER
#endif
#include <inttypes.h>
#ifndef initialize_main
# define initialize_main(ac, av)
#endif
#include "stat-macros.h"
#include "timespec.h"
#include <ctype.h>
#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
static inline unsigned char to_uchar (char ch) { return ch; }
#include <locale.h>
#include "gettext.h"
#if ! ENABLE_NLS
# undef textdomain
# define textdomain(Domainname) # undef bindtextdomain
# define bindtextdomain(Domainname, Dirname) #endif
#define _(msgid) gettext (msgid)
#define N_(msgid) msgid
static inline unsigned long int
select_plural (uintmax_t n)
{
enum { PLURAL_REDUCER = 1000000 };
return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
}
#define STREQ(a, b) (strcmp (a, b) == 0)
#define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
#define STRPREFIX(a, b) (strncmp(a, b, strlen (b)) == 0)
#define STRNCMP_LIT(s, literal) \
strncmp (s, "" literal "", sizeof (literal) - 1)
#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_SETGROUPS
# if HAVE_GETGRGID_NOMEMBERS
# define getgrgid(gid) getgrgid_nomembers(gid)
# endif
# if HAVE_GETGRNAM_NOMEMBERS
# define getgrnam(nam) getgrnam_nomembers(nam)
# endif
# if HAVE_GETGRENT_NOMEMBERS
# define getgrent() getgrent_nomembers()
# endif
#endif
#if !HAVE_DECL_GETUID
uid_t getuid ();
#endif
#include "xalloc.h"
#include "verify.h"
#define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
x2nrealloc (P, PN, sizeof *(P)))
#define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
x2realloc (P, PN))
#include "unlocked-io.h"
#include "same-inode.h"
#include "dirname.h"
#include "openat.h"
static inline bool
dot_or_dotdot (char const *file_name)
{
if (file_name[0] == '.')
{
char sep = file_name[(file_name[1] == '.') + 1];
return (! sep || ISSLASH (sep));
}
else
return false;
}
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;
}
}
static inline bool
is_empty_dir (int fd_cwd, char const *dir)
{
DIR *dirp;
struct dirent const *dp;
int saved_errno;
int fd = openat (fd_cwd, dir,
(O_RDONLY | O_DIRECTORY
| O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
if (fd < 0)
return false;
dirp = fdopendir (fd);
if (dirp == NULL)
{
close (fd);
return false;
}
errno = 0;
dp = readdir_ignoring_dot_and_dotdot (dirp);
saved_errno = errno;
closedir (dirp);
if (dp != NULL)
return false;
return saved_errno == 0 ? true : false;
}
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 GETOPT_SELINUX_CONTEXT_OPTION_DECL \
"context", optional_argument, NULL, 'Z'
#define case_GETOPT_HELP_CHAR \
case GETOPT_HELP_CHAR: \
usage (EXIT_SUCCESS); \
break;
#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 "closein.h"
#include "closeout.h"
#define emit_bug_reporting_address unused__emit_bug_reporting_address
#include "version-etc.h"
#undef emit_bug_reporting_address
#include "propername.h"
#define proper_name(x) (x)
#include "progname.h"
#define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
case GETOPT_VERSION_CHAR: \
version_etc (stdout, Program_name, PACKAGE_NAME, 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
#ifdef lint
# define IF_LINT(Code) Code
#else
# define IF_LINT(Code) #endif
#ifndef __attribute__
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
# define __attribute__(x) # endif
#endif
#ifndef ATTRIBUTE_NORETURN
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
#endif
#undef ATTRIBUTE_WARN_UNUSED_RESULT
#if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
# define ATTRIBUTE_WARN_UNUSED_RESULT #else
# define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
#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
#if ! HAVE_SYNC
# define sync() #endif
static inline size_t _GL_ATTRIBUTE_CONST
gcd (size_t u, size_t v)
{
do
{
size_t t = u % v;
u = v;
v = t;
}
while (v);
return u;
}
static inline size_t _GL_ATTRIBUTE_CONST
lcm (size_t u, size_t v)
{
return u * (v / gcd (u, v));
}
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);
}
static inline bool _GL_ATTRIBUTE_PURE
is_nul (void const *buf, size_t bufsize)
{
typedef uintptr_t word;
void const *vp;
char const *cbuf = buf;
word const *wp = buf;
while (*wp++ == 0)
continue;
vp = wp - 1;
char const *cp = vp;
while (*cp++ == 0)
continue;
return cbuf + bufsize < cp;
}
#define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
( \
(void) (&(Accum) == (Type *) NULL), \
(void) verify_true (! TYPE_SIGNED (Type)), \
(void) verify_true (sizeof (Accum) == sizeof (Type)), \
(((Type) -1 / 10 < (Accum) \
|| (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
? false : (((Accum) = (Accum) * 10 + (Digit_val)), true)) \
)
static inline void
emit_mandatory_arg_note (void)
{
fputs (_("\n\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
}
static inline void
emit_size_note (void)
{
fputs (_("\n\
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).\n\
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).\n\
"), stdout);
}
static inline void
emit_blocksize_note (char const *program)
{
printf (_("\n\
Display values are in units of the first available SIZE from --block-size,\n\
and the %s_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.\n\
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).\n\
"), program);
}
static inline void
emit_ancillary_info (void)
{
printf (_("\n%s online help: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
const char *lc_messages = setlocale (LC_MESSAGES, NULL);
if (lc_messages && STRNCMP_LIT (lc_messages, "en_"))
{
printf (_("Report %s translation bugs to "
"<http://translationproject.org/team/>\n"),
last_component (program_name));
}
printf (_("For complete documentation, run: "
"info coreutils '%s invocation'\n"), last_component (program_name));
}
static inline void
emit_try_help (void)
{
fprintf (stderr, _("Try '%s --help' for more information.\n"), program_name);
}
#include "inttostr.h"
static inline char *
timetostr (time_t t, char *buf)
{
return (TYPE_SIGNED (time_t)
? imaxtostr (t, buf)
: umaxtostr (t, buf));
}
static inline char *
bad_cast (char const *s)
{
return (char *) s;
}
static inline bool
usable_st_size (struct stat const *sb)
{
return (S_ISREG (sb->st_mode) || S_ISLNK (sb->st_mode)
|| S_TYPEISSHM (sb) || S_TYPEISTMO (sb));
}
void usage (int status) ATTRIBUTE_NORETURN;
#define devmsg(...) \
do \
{ \
if (dev_debug) \
fprintf (stderr, __VA_ARGS__); \
} \
while (0)
#define emit_cycle_warning(file_name) \
do \
{ \
error (0, 0, _("\
WARNING: Circular directory structure.\n\
This almost certainly means that you have a corrupted file system.\n\
NOTIFY YOUR SYSTEM MANAGER.\n\
The following directory is part of the cycle:\n %s\n"), \
quote (file_name)); \
} \
while (0)
static inline char *
stzncpy (char *restrict dest, char const *restrict src, size_t len)
{
char const *src_end = src + len;
while (src < src_end && *src)
*dest++ = *src++;
*dest = 0;
return dest;
}
#ifndef ARRAY_CARDINALITY
# define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
#endif
static inline char * se_const (char const * sctx) { return (char *) sctx; }