raw
vtools_genesis          1 #ifndef SYSTEM_H
vtools_genesis 2 #define SYSTEM_H
vtools_genesis 3
vtools_genesis 4 #define _GNU_SOURCE
vtools_genesis 5
vtools_genesis 6 #include <stddef.h>
vtools_genesis 7 /* freebsd */
vtools_genesis 8 #include <stdint.h>
vtools_genesis 9 #include <unistd.h>
vtools_genesis 10 /* linux */
vtools_genesis 11 #include <inttypes.h>
vtools_genesis 12 #include <sys/types.h>
vtools_genesis 13
vtools_genesis 14 #define STAT_BLOCKSIZE(s) ((s).st_blksize)
vtools_genesis 15
vtools_genesis 16 #define EXIT_TROUBLE 2
vtools_genesis 17
vtools_genesis 18 #include <errno.h>
vtools_genesis 19
vtools_genesis 20 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
vtools_genesis 21 #define MAX(a, b) ((a) >= (b) ? (a) : /**/(b))
vtools_genesis 22
vtools_genesis 23 /* Return 1 if an array of |n| objects, each of size |s|, cannot exist
vtools_genesis 24 reliably due to size or |ptrdiff_t| arithmetic overflow. |s| must
vtools_genesis 25 be positive and N must be nonnegative. This is a macro, not a
vtools_genesis 26 function, so that it works correctly even when |SIZE_MAX < n|.
vtools_genesis 27
vtools_genesis 28 True if |n * s| would overflow in a |size_t| calculation, or would
vtools_genesis 29 generate a value larger than |PTRDIFF_MAX|. This expands to a
vtools_genesis 30 constant expression if |n| and |s| are both constants. By gnulib
vtools_genesis 31 convention, |SIZE_MAX| represents overflow in size calculations, so
vtools_genesis 32 the conservative |size_t|-based dividend to use here is |SIZE_MAX -
vtools_genesis 33 1|. */
vtools_genesis 34
vtools_genesis 35 #define xalloc_oversized(n, s) \
vtools_genesis 36 ((size_t) (PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX - 1) / (s) < (n))
vtools_genesis 37
vtools_genesis 38 #include <stdbool.h>
vtools_genesis 39
vtools_genesis 40 /* Type used for fast comparison of several bytes at a time. This used
vtools_genesis 41 to be |uintmax_t|, but changing it to |size_t| made plain 'cmp' 90\%
vtools_genesis 42 faster (|GCC 4.8.1|, |x86|). */
vtools_genesis 43
vtools_genesis 44 #ifndef word
vtools_genesis 45 # define word size_t
vtools_genesis 46 #endif
vtools_genesis 47
vtools_genesis 48 #include <signal.h> /* SA_RESTART */
vtools_genesis 49
vtools_genesis 50 #include <limits.h>
vtools_genesis 51 #ifndef SSIZE_MAX
vtools_genesis 52 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
vtools_genesis 53 #endif
vtools_genesis 54
vtools_genesis 55 /* The signed integer type of a line number. Since files are read
vtools_genesis 56 into main memory, |ptrdiff_t| should be wide enough. */
vtools_genesis 57
vtools_genesis 58 typedef ptrdiff_t lin;
vtools_genesis 59 #define LIN_MAX PTRDIFF_MAX
vtools_genesis 60
vtools_genesis 61 /* The signed integer type for printing line numbers, and its printf
vtools_genesis 62 length modifier. This is not simply |ptrdiff_t|, to cater to older
vtools_genesis 63 and/or nonstandard C libraries where |"l"| works but |"ll"| and
vtools_genesis 64 |"t"| do not, or where |long| is too narrow and |"ll"| works but
vtools_genesis 65 |"t"| does not. */
vtools_genesis 66
vtools_genesis 67 #if LIN_MAX <= LONG_MAX
vtools_genesis 68 typedef long int printint;
vtools_genesis 69 # define pI "l"
vtools_genesis 70 #elif LIN_MAX <= LLONG_MAX
vtools_genesis 71 typedef long long int printint;
vtools_genesis 72 # define pI "ll"
vtools_genesis 73 #else
vtools_genesis 74 typedef ptrdiff_t printint;
vtools_genesis 75 # define pI "t"
vtools_genesis 76 #endif
vtools_genesis 77
vtools_genesis 78 /* Limit so that |2 * context + 1| does not overflow. */
vtools_genesis 79
vtools_genesis 80 #define CONTEXT_MAX ((LIN_MAX - 1) / 2)
vtools_genesis 81
vtools_genesis 82
vtools_genesis 83 #define file_name_cmp strcmp
vtools_genesis 84
vtools_genesis 85 /* Do struct stat |*s|, |*t| describe the same special file? */
vtools_genesis 86 #ifndef same_special_file
vtools_genesis 87 # if HAVE_STRUCT_STAT_ST_RDEV && defined S_ISBLK && defined S_ISCHR
vtools_genesis 88 # define same_special_file(s, t) \
vtools_genesis 89 (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
vtools_genesis 90 || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
vtools_genesis 91 && (s)->st_rdev == (t)->st_rdev)
vtools_genesis 92 # else
vtools_genesis 93 # define same_special_file(s, t) 0
vtools_genesis 94 # endif
vtools_genesis 95 #endif
vtools_genesis 96
vtools_genesis 97 /* Do |struct stat *s, *t| describe the same file? Answer -1 if
vtools_genesis 98 unknown. */
vtools_genesis 99 #ifndef same_file
vtools_genesis 100 # define same_file(s, t) \
vtools_genesis 101 ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
vtools_genesis 102 || same_special_file (s, t))
vtools_genesis 103 #endif
vtools_genesis 104
vtools_genesis 105 /* Do |struct stat *s, *t| have the same file attributes?
vtools_genesis 106
vtools_genesis 107 POSIX says that two files are identical if |st_ino| and |st_dev|
vtools_genesis 108 are the same, but many file systems incorrectly assign the same
vtools_genesis 109 (device, inode) pair to two distinct files, including:
vtools_genesis 110
vtools_genesis 111 - GNU/Linux NFS servers that export all local file systems as a
vtools_genesis 112 single NFS file system, if a local device number |(st_dev)| exceeds
vtools_genesis 113 255, or if a local inode number |(st_ino)| exceeds 16777215.
vtools_genesis 114
vtools_genesis 115 - Network Appliance NFS servers in snapshot directories; see
vtools_genesis 116 Network Appliance bug \#195.
vtools_genesis 117
vtools_genesis 118 - ClearCase MVFS; see bug id ATRia04618.
vtools_genesis 119
vtools_genesis 120 Check whether two files that purport to be the same have the same
vtools_genesis 121 attributes, to work around instances of this common bug. Do not
vtools_genesis 122 inspect all attributes, only attributes useful in checking for this
vtools_genesis 123 bug.
vtools_genesis 124
vtools_genesis 125 It's possible for two distinct files on a buggy file system to have
vtools_genesis 126 the same attributes, but it's not worth slowing down all
vtools_genesis 127 implementations (or complicating the configuration) to cater to
vtools_genesis 128 these rare cases in buggy implementations. */
vtools_genesis 129
vtools_genesis 130 #ifndef same_file_attributes
vtools_genesis 131 # define same_file_attributes(s, t) \
vtools_genesis 132 ((s)->st_mode == (t)->st_mode \
vtools_genesis 133 && (s)->st_nlink == (t)->st_nlink \
vtools_genesis 134 && (s)->st_uid == (t)->st_uid \
vtools_genesis 135 && (s)->st_gid == (t)->st_gid \
vtools_genesis 136 && (s)->st_size == (t)->st_size \
vtools_genesis 137 && (s)->st_mtime == (t)->st_mtime \
vtools_genesis 138 && (s)->st_ctime == (t)->st_ctime)
vtools_genesis 139 #endif
vtools_genesis 140
vtools_genesis 141 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
vtools_genesis 142
vtools_genesis 143 #define BB_LITTLE_ENDIAN 1
vtools_genesis 144
vtools_genesis 145 #ifdef __APPLE__
vtools_genesis 146 #include <libkern/OSByteOrder.h>
vtools_genesis 147 #define SWAP_BE64(x) OSSwapInt64(x)
vtools_genesis 148 #elif __FreeBSD__
vtools_genesis 149 #include <sys/endian.h>
vtools_genesis 150 #define SWAP_BE64(x) bswap64(x)
vtools_genesis 151 #elif __linux__
vtools_genesis 152 #include <byteswap.h>
vtools_genesis 153 #define SWAP_BE64(x) bswap_64(x)
vtools_genesis 154 #endif
vtools_genesis 155
vtools_genesis 156 #endif