-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
153 lines (128 loc) · 3.99 KB
/
utils.h
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
/*
* Copyright (C) 2020 Alexander Larsson <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define TRUE 1
#define FALSE 0
typedef int bool;
#if 1
#define __debug__(x) debuglog x
#else
#define __debug__(x)
#endif
#define UNUSED __attribute__((__unused__))
#define N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
void die_with_error (const char *format,
...);
void die (const char *format,
...);
void report (const char *format,
...);
void debuglog (const char *format,
...);
void enable_debuglog (void);
void die_oom (void);
void * xmalloc (size_t size);
void * xcalloc (size_t size);
void * xrealloc (void *ptr,
size_t size);
void strfreev (char **str_array);
size_t strv_length (char **str_array);
char * xstrdup (const char *str);
int has_prefix (const char *str,
const char *prefix);
char * xasprintf (const char *format,
...);
char * load_file_data (int fd,
size_t *size);
char * load_file_at (int dirfd,
const char *path);
int send_fd (int socket,
int fd);
int recv_fd (int socket);
static inline int
steal_fd (int *fdp)
{
int fd = *fdp;
*fdp = -1;
return fd;
}
static inline void *
steal_pointer (void *pp)
{
void **ptr = (void **) pp;
void *ref;
ref = *ptr;
*ptr = NULL;
return ref;
}
/* type safety */
#define steal_pointer(pp) \
(0 ? (*(pp)) : (steal_pointer) (pp))
static inline void
cleanup_fdp (int *fdp)
{
int errsv;
assert (fdp);
int fd = steal_fd (fdp);
if (fd >= 0)
{
errsv = errno;
if (close (fd) < 0)
assert (errno != EBADF);
errno = errsv;
}
}
static inline void
cleanup_strvp (void *p)
{
void **pp = (void **) p;
strfreev (*pp);
}
static inline void
cleanup_freep (void *p)
{
void **pp = (void **) p;
if (*pp)
free (*pp);
}
#define _CLEANUP(func) __attribute__((cleanup(func)))
#define autofd _CLEANUP(cleanup_fdp)
#define autofree _CLEANUP(cleanup_freep)
#define autofreev _CLEANUP(cleanup_strvp)
#define _AUTOPTR_FUNC_NAME(TypeName) _autoptr_cleanup_##TypeName
#define _AUTO_FUNC_NAME(TypeName) _auto_cleanup_##TypeName
#define _AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
#define DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
typedef TypeName *_AUTOPTR_TYPENAME(TypeName); \
static inline void _AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); }
#define DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
static inline void _AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); }
#define autoptr(TypeName) _CLEANUP(_AUTOPTR_FUNC_NAME(TypeName)) _AUTOPTR_TYPENAME(TypeName)
#define auto(TypeName) _CLEANUP(_AUTO_FUNC_NAME(TypeName)) TypeName