]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
fls() is gone.
[libucw.git] / lib / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2005 Martin Mares <mj@ucw.cz>
5  *      (c) 2005 Tomas Valla <tom@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_LIB_H
12 #define _UCW_LIB_H
13
14 #include "lib/config.h"
15 #include <stdarg.h>
16
17 /* Tell libc we're going to use all extensions available */
18
19 #define _GNU_SOURCE
20
21 /* Ugly structure handling macros */
22
23 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
24 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
25 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
26 #define ALIGN_PTR(p, s) ((addr_int_t)(p) % (s) ? (typeof(p))((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)) : (p))
27 #define UNALIGNED_PART(ptr, type) (((addr_int_t) (ptr)) % sizeof(type))
28
29 /* Some other macros */
30
31 #define MIN(a,b) (((a)<(b))?(a):(b))
32 #define MAX(a,b) (((a)>(b))?(a):(b))
33 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
34 #define ABS(x) ((x) < 0 ? -(x) : (x))
35 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
36 #define STRINGIFY(x) #x
37 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
38 #define GLUE(x,y) x##y
39 #define GLUE_(x,y) x##_##y
40
41 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
42 #define REV_COMPARE(x,y) COMPARE(y,x)
43 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
44 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
45
46 /* GCC Extensions */
47
48 #ifdef __GNUC__
49
50 #undef inline
51 #define NONRET __attribute__((noreturn))
52 #define UNUSED __attribute__((unused))
53 #define CONSTRUCTOR __attribute__((constructor))
54 #define PACKED __attribute__((packed))
55 #define CONST __attribute__((const))
56 #define PURE __attribute__((const))
57 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
58 #define likely(x) __builtin_expect((x),1)
59 #define unlikely(x) __builtin_expect((x),0)
60
61 #else
62 #error This program requires the GNU C compiler.
63 #endif
64
65 /* Logging */
66
67 #define L_DEBUG         'D'             /* Debugging messages */
68 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
69 #define L_WARN          'W'
70 #define L_ERROR         'E'
71 #define L_INFO_R        'i'             /* Errors caused by external events */
72 #define L_WARN_R        'w'
73 #define L_ERROR_R       'e'
74 #define L_FATAL         '!'             /* die() */
75
76 extern char *log_title;                 /* NULL - print no title, default is log_progname */
77 extern char *log_filename;              /* Expanded name of the current log file */
78 extern int log_switch_nest;             /* log_switch() nesting counter, increment to disable automatic switches */
79 extern int log_pid;                     /* 0 if shouldn't be logged */
80 extern void (*log_die_hook)(void);
81 struct tm;
82 extern void (*log_switch_hook)(struct tm *tm);
83
84 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
85 #define log log_msg
86 void vlog_msg(unsigned int cat, const char *msg, va_list args);
87 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
88 void log_init(byte *argv0);
89 void log_file(byte *name);
90 void log_fork(void);
91 int log_switch(void);
92
93 void assert_failed(char *assertion, char *file, int line) NONRET;
94 void assert_failed_noinfo(void) NONRET;
95
96 #ifdef DEBUG_ASSERTS
97 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
98 #else
99 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
100 #endif
101
102 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
103
104 #ifdef LOCAL_DEBUG
105 #define DBG(x,y...) log(L_DEBUG, x,##y)
106 #else
107 #define DBG(x,y...) do { } while(0)
108 #endif
109
110 static inline void log_switch_disable(void) { log_switch_nest++; }
111 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
112
113 /* Memory allocation */
114
115 #define xmalloc sh_xmalloc
116 #define xrealloc sh_xrealloc
117 #define xfree sh_xfree
118
119 #ifdef DEBUG_DMALLOC
120 /*
121  * The standard dmalloc macros tend to produce lots of namespace
122  * conflicts and we use only xmalloc and xfree, so we can define
123  * the stubs ourselves.
124  */
125 #define DMALLOC_DISABLE
126 #include <dmalloc.h>
127 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
128 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
129 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
130 #else
131 /*
132  * Unfortunately, several libraries we might want to link to define
133  * their own xmalloc and we don't want to interfere with them, hence
134  * the renaming.
135  */
136 void *xmalloc(unsigned);
137 void *xrealloc(void *, unsigned);
138 #define sh_xfree(x) free(x)
139 #endif
140
141 void *xmalloc_zero(unsigned);
142 byte *xstrdup(byte *);
143
144 /* Content-Type pattern matching and filters */
145
146 int match_ct_patt(byte *, byte *);
147
148 /* wordsplit.c */
149
150 int sepsplit(byte *str, byte sep, byte **rec, uns max);
151 int wordsplit(byte *, byte **, uns);
152
153 /* pat(i)match.c: Matching of shell patterns */
154
155 int match_pattern(byte *, byte *);
156 int match_pattern_nocase(byte *, byte *);
157
158 /* md5hex.c */
159
160 void md5_to_hex(byte *, byte *);
161 void hex_to_md5(byte *, byte *);
162
163 #define MD5_SIZE 16
164 #define MD5_HEX_SIZE 33
165
166 /* prime.c */
167
168 int isprime(uns);
169 uns nextprime(uns);
170
171 /* timer.c */
172
173 struct timeval;
174
175 void init_timer(void);
176 uns get_timer(void);
177 void get_last_timeval(struct timeval *tv);
178
179 /* regex.c */
180
181 typedef struct regex regex;
182
183 regex *rx_compile(byte *r, int icase);
184 void rx_free(regex *r);
185 int rx_match(regex *r, byte *s);
186 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
187
188 /* random.c */
189
190 uns random_max(uns);
191
192 /* mmap.c */
193
194 void *mmap_file(byte *name, unsigned *len, int writeable);
195 void munmap_file(void *start, unsigned len);
196
197 /* partmap.c */
198
199 struct partmap;
200 struct partmap *partmap_open(byte *name, int writeable);
201 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
202 void partmap_close(struct partmap *p);
203 sh_off_t partmap_size(struct partmap *p);
204
205 /* proctitle.c */
206
207 void setproctitle_init(int argc, char **argv);
208 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
209
210 /* randomkey.c */
211
212 void randomkey(byte *buf, uns size);
213
214 /* exitstatus.c */
215
216 #define EXIT_STATUS_MSG_SIZE 32
217 int format_exit_status(byte *msg, int stat);
218
219 /* runcmd.c */
220
221 int run_command(byte *cmd, ...);
222 void NONRET exec_command(byte *cmd, ...);
223 void echo_command(byte *buf, int size, byte *cmd, ...);
224 int run_command_v(byte *cmd, va_list args);
225 void NONRET exec_command_v(byte *cmd, va_list args);
226 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
227
228 /* carefulio.c */
229
230 int careful_read(int fd, void *buf, int len);
231 int careful_write(int fd, void *buf, int len);
232
233 /* sync.c */
234
235 void sync_dir(byte *name);
236
237 /* sighandler.c */
238
239 typedef int (*sh_sighandler_t)(int);
240   /* obtains signum, returns nonzero if abort() should be called */
241 extern sh_sighandler_t signal_handler[];
242
243 struct sigaction;
244 void handle_signal(int signum, struct sigaction *oldact);
245 void unhandle_signal(int signum, struct sigaction *oldact);
246
247 #endif