summaryrefslogtreecommitdiff
path: root/local/c.h
blob: 2471209c9316d594ccc5e13a47d0c9743e18653d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * This header was copied from util-linux at fall 2011.
 */

/*
 * Fundamental C definitions.
 */

#ifndef PROCPS_NG_C_H
#define PROCPS_NG_C_H

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_ERROR_H
#include <error.h>
#else
#include <stdarg.h>
#endif

/*
 * Compiler specific stuff
 */
#ifndef __GNUC_PREREQ
# if defined __GNUC__ && defined __GNUC_MINOR__
#  define __GNUC_PREREQ(maj, min) \
	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
# else
#  define __GNUC_PREREQ(maj, min) 0
# endif
#endif

/*
 * Function attributes
 */
#ifndef __ul_alloc_size
# if __GNUC_PREREQ (4, 3)
#  define __ul_alloc_size(s) __attribute__((alloc_size(s)))
# else
#  define __ul_alloc_size(s)
# endif
#endif

#ifndef __ul_calloc_size
# if __GNUC_PREREQ (4, 3)
#  define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s)))
# else
#  define __ul_calloc_size(n, s)
# endif
#endif

/*
 * Misc
 */
#ifndef PATH_MAX
# define PATH_MAX 4096
#endif

#ifndef TRUE
# define TRUE 1
#endif

#ifndef FALSE
# define FALSE 0
#endif

/*
 * Program name.
 */
#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
# ifdef HAVE___PROGNAME
extern char *__progname;
#  define program_invocation_short_name __progname
# else
#  ifdef HAVE_GETEXECNAME
#   define program_invocation_short_name \
		prog_inv_sh_nm_from_file(getexecname(), 0)
#  else
#   define program_invocation_short_name \
		prog_inv_sh_nm_from_file(__FILE__, 1)
#  endif
static char prog_inv_sh_nm_buf[256];
static inline char *prog_inv_sh_nm_from_file(char *f, char stripext)
{
	char *t;

	if ((t = strrchr(f, '/')) != NULL)
		t++;
	else
		t = f;

	strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
	prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';

	if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
		*t = '\0';

	return prog_inv_sh_nm_buf;
}
# endif
#endif

/*
 * Error printing.
 */
#ifndef HAVE_ERROR_H
/* Emulate the error() function from glibc */
__attribute__((__format__(__printf__, 3, 4)))
static void error(int status, int errnum, const char *format, ...)
{
        va_list argp;
        fprintf(stderr, "%s: ", program_invocation_short_name);
        va_start(argp, format);
        vfprintf(stderr, format, argp);
        va_end(argp);
        if (errnum != 0)
                fprintf(stderr, ": %s", strerror(errnum));
        fprintf(stderr, "\n");
        if (status != 0)
                exit(status);
}

/* Emulate the error_at_line() function from glibc */
__attribute__((__format__(__printf__, 5, 6)))
static void error_at_line(int status, int errnum, const char *filename,
                          unsigned int linenum, const char *format, ...)
{
        va_list argp;
        fprintf(stderr, "%s:%s:%u: ", program_invocation_short_name,
                filename, linenum);
        va_start(argp, format);
        vfprintf(stderr, format, argp);
        va_end(argp);
        if (errnum != 0)
                fprintf(stderr, ": error code %d", errnum);
        fprintf(stderr, "\n");
        if (status != 0)
                exit(status);
}
#endif
#define xwarn(...) error(0, errno, __VA_ARGS__)
#define xwarnx(...) error(0, 0, __VA_ARGS__)
#define xerr(STATUS, ...) error(STATUS, errno, __VA_ARGS__)
#define xerrx(STATUS, ...) error(STATUS, 0, __VA_ARGS__)

/*
 * Constant strings for usage() functions.
 */
#define USAGE_HEADER     _("\nUsage:\n")
#define USAGE_OPTIONS    _("\nOptions:\n")
#define USAGE_SEPARATOR  _("\n")
#define USAGE_HELP       _(" -h, --help     display this help and exit\n")
#define USAGE_VERSION    _(" -V, --version  output version information and exit\n")
#define USAGE_MAN_TAIL(_man)   _("\nFor more details see %s.\n"), _man

#define PROCPS_NG_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING

#endif /* PROCPS_NG_C_H */