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