| 1 |
/* |
| 2 |
* This header was copied from util-linux at fall 2011. |
| 3 |
*/ |
| 4 |
|
| 5 |
/* |
| 6 |
* Fundamental C definitions. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef PROCPS_NG_C_H |
| 10 |
#define PROCPS_NG_C_H |
| 11 |
|
| 12 |
#include <limits.h> |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdarg.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <errno.h> |
| 19 |
#ifdef HAVE_ERROR_H |
| 20 |
#include <error.h> |
| 21 |
#else |
| 22 |
#include <stdarg.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
/* |
| 26 |
* Compiler specific stuff |
| 27 |
*/ |
| 28 |
#ifndef __GNUC_PREREQ |
| 29 |
# if defined __GNUC__ && defined __GNUC_MINOR__ |
| 30 |
# define __GNUC_PREREQ(maj, min) \ |
| 31 |
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
| 32 |
# else |
| 33 |
# define __GNUC_PREREQ(maj, min) 0 |
| 34 |
# endif |
| 35 |
#endif |
| 36 |
|
| 37 |
/* |
| 38 |
* Function attributes |
| 39 |
*/ |
| 40 |
#ifndef __ul_alloc_size |
| 41 |
# if __GNUC_PREREQ (4, 3) |
| 42 |
# define __ul_alloc_size(s) __attribute__((alloc_size(s))) |
| 43 |
# else |
| 44 |
# define __ul_alloc_size(s) |
| 45 |
# endif |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifndef __ul_calloc_size |
| 49 |
# if __GNUC_PREREQ (4, 3) |
| 50 |
# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s))) |
| 51 |
# else |
| 52 |
# define __ul_calloc_size(n, s) |
| 53 |
# endif |
| 54 |
#endif |
| 55 |
|
| 56 |
/* |
| 57 |
* Misc |
| 58 |
*/ |
| 59 |
#ifndef PATH_MAX |
| 60 |
# define PATH_MAX 4096 |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef TRUE |
| 64 |
# define TRUE 1 |
| 65 |
#endif |
| 66 |
|
| 67 |
#ifndef FALSE |
| 68 |
# define FALSE 0 |
| 69 |
#endif |
| 70 |
|
| 71 |
/* |
| 72 |
* Program name. |
| 73 |
*/ |
| 74 |
#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME |
| 75 |
# ifdef HAVE___PROGNAME |
| 76 |
extern char *__progname; |
| 77 |
# define program_invocation_short_name __progname |
| 78 |
# else |
| 79 |
# ifdef HAVE_GETEXECNAME |
| 80 |
# define program_invocation_short_name \ |
| 81 |
prog_inv_sh_nm_from_file(getexecname(), 0) |
| 82 |
# else |
| 83 |
# define program_invocation_short_name \ |
| 84 |
prog_inv_sh_nm_from_file(__FILE__, 1) |
| 85 |
# endif |
| 86 |
static char prog_inv_sh_nm_buf[256]; |
| 87 |
static inline char *prog_inv_sh_nm_from_file(char *f, char stripext) |
| 88 |
{ |
| 89 |
char *t; |
| 90 |
|
| 91 |
if ((t = strrchr(f, '/')) != NULL) |
| 92 |
t++; |
| 93 |
else |
| 94 |
t = f; |
| 95 |
|
| 96 |
strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1); |
| 97 |
prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0'; |
| 98 |
|
| 99 |
if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL) |
| 100 |
*t = '\0'; |
| 101 |
|
| 102 |
return prog_inv_sh_nm_buf; |
| 103 |
} |
| 104 |
# endif |
| 105 |
#endif |
| 106 |
|
| 107 |
/* |
| 108 |
* Error printing. |
| 109 |
*/ |
| 110 |
#ifndef HAVE_ERROR_H |
| 111 |
/* Emulate the error() function from glibc */ |
| 112 |
__attribute__((__format__(__printf__, 3, 4))) |
| 113 |
static void error(int status, int errnum, const char *format, ...) |
| 114 |
{ |
| 115 |
va_list argp; |
| 116 |
fprintf(stderr, "%s: ", program_invocation_short_name); |
| 117 |
va_start(argp, format); |
| 118 |
vfprintf(stderr, format, argp); |
| 119 |
va_end(argp); |
| 120 |
if (errnum != 0) |
| 121 |
fprintf(stderr, ": %s", strerror(errnum)); |
| 122 |
fprintf(stderr, "\n"); |
| 123 |
if (status != 0) |
| 124 |
exit(status); |
| 125 |
} |
| 126 |
|
| 127 |
/* Emulate the error_at_line() function from glibc */ |
| 128 |
__attribute__((__format__(__printf__, 5, 6))) |
| 129 |
static void error_at_line(int status, int errnum, const char *filename, |
| 130 |
unsigned int linenum, const char *format, ...) |
| 131 |
{ |
| 132 |
va_list argp; |
| 133 |
fprintf(stderr, "%s:%s:%u: ", program_invocation_short_name, |
| 134 |
filename, linenum); |
| 135 |
va_start(argp, format); |
| 136 |
vfprintf(stderr, format, argp); |
| 137 |
va_end(argp); |
| 138 |
if (errnum != 0) |
| 139 |
fprintf(stderr, ": error code %d", errnum); |
| 140 |
fprintf(stderr, "\n"); |
| 141 |
if (status != 0) |
| 142 |
exit(status); |
| 143 |
} |
| 144 |
#endif |
| 145 |
#define xwarn(...) error(0, errno, __VA_ARGS__) |
| 146 |
#define xwarnx(...) error(0, 0, __VA_ARGS__) |
| 147 |
#define xerr(STATUS, ...) error(STATUS, errno, __VA_ARGS__) |
| 148 |
#define xerrx(STATUS, ...) error(STATUS, 0, __VA_ARGS__) |
| 149 |
|
| 150 |
/* |
| 151 |
* Constant strings for usage() functions. |
| 152 |
*/ |
| 153 |
#define USAGE_HEADER _("\nUsage:\n") |
| 154 |
#define USAGE_OPTIONS _("\nOptions:\n") |
| 155 |
#define USAGE_SEPARATOR _("\n") |
| 156 |
#define USAGE_HELP _(" -h, --help display this help and exit\n") |
| 157 |
#define USAGE_VERSION _(" -V, --version output version information and exit\n") |
| 158 |
#define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man |
| 159 |
|
| 160 |
#define PROCPS_NG_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING |
| 161 |
|
| 162 |
#endif /* PROCPS_NG_C_H */ |