]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
One more deadly testcase.
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 /*
11  *  This file should be included as the very first include in all
12  *  source files, especially before all OS includes since it sets
13  *  up libc feature macros.
14  */
15
16 #ifndef _SHERLOCK_LIB_H
17 #define _SHERLOCK_LIB_H
18
19 #include "lib/config.h"
20
21 /* Tell libc we're going to use all extensions available */
22
23 #define _GNU_SOURCE
24
25 /* Ugly structure handling macros */
26
27 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
28 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
29 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
30 #define UNALIGNED_PART(ptr, type) (((long) (ptr)) % sizeof(type))
31
32 /* Some other macros */
33
34 #define MIN(a,b) (((a)<(b))?(a):(b))
35 #define MAX(a,b) (((a)>(b))?(a):(b))
36 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
37 #define ABS(x) ((x) < 0 ? -(x) : (x))
38 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
39 #define STRINGIFY(x) #x
40 #define GLUE(x,y) x##y
41 #define GLUE_(x,y) x##_##y
42 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
43 #define REV_COMPARE(x,y) COMPARE(y,x)
44
45 /* Logging */
46
47 #define L_DEBUG         'D'             /* Debugging messages */
48 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
49 #define L_WARN          'W'
50 #define L_ERROR         'E'
51 #define L_INFO_R        'i'             /* Errors caused by external events */
52 #define L_WARN_R        'w'
53 #define L_ERROR_R       'e'
54 #define L_FATAL         '!'             /* die() */
55
56 extern char *log_title;                 /* NULL - print no title, default is log_progname */
57
58 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
59 #define log log_msg
60 void die(byte *, ...) NONRET;
61 void log_init(byte *);
62 void log_file(byte *);
63 void log_fork(void);
64
65 #ifdef DEBUG
66 void assert_failed(char *assertion, char *file, int line) NONRET;
67 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
68 #else
69 void assert_failed(void) NONRET;
70 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
71 #endif
72
73 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
74
75 #ifdef LOCAL_DEBUG
76 #define DBG(x,y...) log(L_DEBUG, x,##y)
77 #else
78 #define DBG(x,y...) do { } while(0)
79 #endif
80
81 /* Memory allocation */
82
83 #ifdef DEBUG_DMALLOC
84 /*
85  * The standard dmalloc macros tend to produce lots of namespace
86  * conflicts and we use only xmalloc and xfree, so we can define
87  * the stubs ourselves.
88  */
89 #define DMALLOC_DISABLE
90 #include <dmalloc.h>
91 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
92 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
93 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
94 #else
95 /*
96  * Unfortunately, several libraries we might want to link to define
97  * their own xmalloc and we don't want to interfere with them, hence
98  * the renaming.
99  */
100 #define xmalloc sh_xmalloc
101 void *xmalloc(unsigned);
102 void *xrealloc(void *, unsigned);
103 #define xfree(x) free(x)
104 #endif
105
106 void *xmalloc_zero(unsigned);
107 byte *stralloc(byte *);
108
109 /* Content-Type pattern matching and filters */
110
111 int match_ct_patt(byte *, byte *);
112
113 /* log2.c */
114
115 int fls(u32);
116
117 /* wordsplit.c */
118
119 int wordsplit(byte *, byte **, uns);
120
121 /* pat(i)match.c: Matching of shell patterns */
122
123 int match_pattern(byte *, byte *);
124 int match_pattern_nocase(byte *, byte *);
125
126 /* md5hex.c */
127
128 void md5_to_hex(byte *, byte *);
129 void hex_to_md5(byte *, byte *);
130
131 #define MD5_SIZE 16
132 #define MD5_HEX_SIZE 33
133
134 /* prime.c */
135
136 int isprime(uns);
137 uns nextprime(uns);
138
139 /* timer.c */
140
141 void init_timer(void);
142 uns get_timer(void);
143
144 /* regex.c */
145
146 typedef struct regex regex;
147
148 regex *rx_compile(byte *r, int icase);
149 void rx_free(regex *r);
150 int rx_match(regex *r, byte *s);
151 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
152
153 /* random.c */
154
155 uns random_max(uns);
156
157 /* mmap.c */
158
159 void *mmap_file(byte *name, unsigned *len, int writeable);
160 void munmap_file(void *start, unsigned len);
161
162 /* partmap.c */
163
164 struct partmap;
165 struct partmap *partmap_open(byte *name, int writeable);
166 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
167 void partmap_close(struct partmap *p);
168 sh_off_t partmap_size(struct partmap *p);
169
170 /* proctitle.c */
171
172 void setproctitle_init(int argc, char **argv);
173 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
174
175 /* randomkey.c */
176
177 void randomkey(byte *buf, uns size);
178
179 #endif