-
+ E0C8C657FA184C8C6F20705B9C9FAE293A0C3BF380E60CDC65BA2449CD28D3F0964E42998601F868444C0F19752BB58934BB75DFFD7F0DCB31170CA7C6538A84
vtools/src/system.h
(0 . 0)(1 . 156)
4964 #ifndef SYSTEM_H
4965 #define SYSTEM_H
4966
4967 #define _GNU_SOURCE
4968
4969 #include <stddef.h>
4970 /* freebsd */
4971 #include <stdint.h>
4972 #include <unistd.h>
4973 /* linux */
4974 #include <inttypes.h>
4975 #include <sys/types.h>
4976
4977 #define STAT_BLOCKSIZE(s) ((s).st_blksize)
4978
4979 #define EXIT_TROUBLE 2
4980
4981 #include <errno.h>
4982
4983 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
4984 #define MAX(a, b) ((a) >= (b) ? (a) : /**/(b))
4985
4986 /* Return 1 if an array of |n| objects, each of size |s|, cannot exist
4987 reliably due to size or |ptrdiff_t| arithmetic overflow. |s| must
4988 be positive and N must be nonnegative. This is a macro, not a
4989 function, so that it works correctly even when |SIZE_MAX < n|.
4990
4991 True if |n * s| would overflow in a |size_t| calculation, or would
4992 generate a value larger than |PTRDIFF_MAX|. This expands to a
4993 constant expression if |n| and |s| are both constants. By gnulib
4994 convention, |SIZE_MAX| represents overflow in size calculations, so
4995 the conservative |size_t|-based dividend to use here is |SIZE_MAX -
4996 1|. */
4997
4998 #define xalloc_oversized(n, s) \
4999 ((size_t) (PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX - 1) / (s) < (n))
5000
5001 #include <stdbool.h>
5002
5003 /* Type used for fast comparison of several bytes at a time. This used
5004 to be |uintmax_t|, but changing it to |size_t| made plain 'cmp' 90\%
5005 faster (|GCC 4.8.1|, |x86|). */
5006
5007 #ifndef word
5008 # define word size_t
5009 #endif
5010
5011 #include <signal.h> /* SA_RESTART */
5012
5013 #include <limits.h>
5014 #ifndef SSIZE_MAX
5015 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
5016 #endif
5017
5018 /* The signed integer type of a line number. Since files are read
5019 into main memory, |ptrdiff_t| should be wide enough. */
5020
5021 typedef ptrdiff_t lin;
5022 #define LIN_MAX PTRDIFF_MAX
5023
5024 /* The signed integer type for printing line numbers, and its printf
5025 length modifier. This is not simply |ptrdiff_t|, to cater to older
5026 and/or nonstandard C libraries where |"l"| works but |"ll"| and
5027 |"t"| do not, or where |long| is too narrow and |"ll"| works but
5028 |"t"| does not. */
5029
5030 #if LIN_MAX <= LONG_MAX
5031 typedef long int printint;
5032 # define pI "l"
5033 #elif LIN_MAX <= LLONG_MAX
5034 typedef long long int printint;
5035 # define pI "ll"
5036 #else
5037 typedef ptrdiff_t printint;
5038 # define pI "t"
5039 #endif
5040
5041 /* Limit so that |2 * context + 1| does not overflow. */
5042
5043 #define CONTEXT_MAX ((LIN_MAX - 1) / 2)
5044
5045
5046 #define file_name_cmp strcmp
5047
5048 /* Do struct stat |*s|, |*t| describe the same special file? */
5049 #ifndef same_special_file
5050 # if HAVE_STRUCT_STAT_ST_RDEV && defined S_ISBLK && defined S_ISCHR
5051 # define same_special_file(s, t) \
5052 (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
5053 || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
5054 && (s)->st_rdev == (t)->st_rdev)
5055 # else
5056 # define same_special_file(s, t) 0
5057 # endif
5058 #endif
5059
5060 /* Do |struct stat *s, *t| describe the same file? Answer -1 if
5061 unknown. */
5062 #ifndef same_file
5063 # define same_file(s, t) \
5064 ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
5065 || same_special_file (s, t))
5066 #endif
5067
5068 /* Do |struct stat *s, *t| have the same file attributes?
5069
5070 POSIX says that two files are identical if |st_ino| and |st_dev|
5071 are the same, but many file systems incorrectly assign the same
5072 (device, inode) pair to two distinct files, including:
5073
5074 - GNU/Linux NFS servers that export all local file systems as a
5075 single NFS file system, if a local device number |(st_dev)| exceeds
5076 255, or if a local inode number |(st_ino)| exceeds 16777215.
5077
5078 - Network Appliance NFS servers in snapshot directories; see
5079 Network Appliance bug \#195.
5080
5081 - ClearCase MVFS; see bug id ATRia04618.
5082
5083 Check whether two files that purport to be the same have the same
5084 attributes, to work around instances of this common bug. Do not
5085 inspect all attributes, only attributes useful in checking for this
5086 bug.
5087
5088 It's possible for two distinct files on a buggy file system to have
5089 the same attributes, but it's not worth slowing down all
5090 implementations (or complicating the configuration) to cater to
5091 these rare cases in buggy implementations. */
5092
5093 #ifndef same_file_attributes
5094 # define same_file_attributes(s, t) \
5095 ((s)->st_mode == (t)->st_mode \
5096 && (s)->st_nlink == (t)->st_nlink \
5097 && (s)->st_uid == (t)->st_uid \
5098 && (s)->st_gid == (t)->st_gid \
5099 && (s)->st_size == (t)->st_size \
5100 && (s)->st_mtime == (t)->st_mtime \
5101 && (s)->st_ctime == (t)->st_ctime)
5102 #endif
5103
5104 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
5105
5106 #define BB_LITTLE_ENDIAN 1
5107
5108 #ifdef __APPLE__
5109 #include <libkern/OSByteOrder.h>
5110 #define SWAP_BE64(x) OSSwapInt64(x)
5111 #elif __FreeBSD__
5112 #include <sys/endian.h>
5113 #define SWAP_BE64(x) bswap64(x)
5114 #elif __linux__
5115 #include <byteswap.h>
5116 #define SWAP_BE64(x) bswap_64(x)
5117 #endif
5118
5119 #endif