]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
923afca5622d349a258f82b08b359e6c0a3171b6
[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
43 /* Logging */
44
45 #define L_DEBUG         'D'             /* Debugging messages */
46 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
47 #define L_WARN          'W'
48 #define L_ERROR         'E'
49 #define L_INFO_R        'i'             /* Errors caused by external events */
50 #define L_WARN_R        'w'
51 #define L_ERROR_R       'e'
52 #define L_FATAL         '!'             /* die() */
53
54 extern char *log_title;                 /* NULL - print no title, default is log_progname */
55
56 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
57 #define log log_msg
58 void die(byte *, ...) NONRET;
59 void log_init(byte *);
60 void log_file(byte *);
61 void log_fork(void);
62
63 #ifdef DEBUG
64 void assert_failed(char *assertion, char *file, int line) NONRET;
65 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
66 #else
67 void assert_failed(void) NONRET;
68 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
69 #endif
70
71 #ifdef LOCAL_DEBUG
72 #define DBG(x,y...) log(L_DEBUG, x,##y)
73 #else
74 #define DBG(x,y...) do { } while(0)
75 #endif
76
77 /* Memory allocation */
78
79 #ifdef DEBUG_DMALLOC
80 /*
81  * The standard dmalloc macros tend to produce lots of namespace
82  * conflicts and we use only xmalloc and xfree, so we can define
83  * the stubs ourselves.
84  */
85 #define DMALLOC_DISABLE
86 #include <dmalloc.h>
87 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
88 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
89 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
90 #else
91 /*
92  * Unfortunately, several libraries we might want to link to define
93  * their own xmalloc and we don't want to interfere with them, hence
94  * the renaming.
95  */
96 #define xmalloc sh_xmalloc
97 void *xmalloc(unsigned);
98 void *xrealloc(void *, unsigned);
99 #define xfree(x) free(x)
100 #endif
101
102 void *xmalloc_zero(unsigned);
103 byte *stralloc(byte *);
104
105 /* Content-Type pattern matching and filters */
106
107 int match_ct_patt(byte *, byte *);
108
109 /* log2.c */
110
111 int fls(u32);
112
113 /* wordsplit.c */
114
115 int wordsplit(byte *, byte **, uns);
116
117 /* pat(i)match.c: Matching of shell patterns */
118
119 int match_pattern(byte *, byte *);
120 int match_pattern_nocase(byte *, byte *);
121
122 /* md5hex.c */
123
124 void md5_to_hex(byte *, byte *);
125 void hex_to_md5(byte *, byte *);
126
127 #define MD5_SIZE 16
128 #define MD5_HEX_SIZE 33
129
130 /* prime.c */
131
132 int isprime(uns);
133 uns nextprime(uns);
134
135 /* timer.c */
136
137 void init_timer(void);
138 uns get_timer(void);
139
140 /* regex.c */
141
142 typedef struct regex regex;
143
144 regex *rx_compile(byte *r, int icase);
145 void rx_free(regex *r);
146 int rx_match(regex *r, byte *s);
147 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
148
149 /* random.c */
150
151 uns random_max(uns);
152
153 /* mmap.c */
154
155 void *mmap_file(byte *name, unsigned *len, int writeable);
156 void munmap_file(void *start, unsigned len);
157
158 /* partmap.c */
159
160 struct partmap;
161 struct partmap *partmap_open(byte *name, int writeable);
162 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
163 void partmap_close(struct partmap *p);
164 sh_off_t partmap_size(struct partmap *p);
165
166 /* proctitle.c */
167
168 void setproctitle_init(int argc, char **argv);
169 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
170
171 /* randomkey.c */
172
173 void randomkey(byte *buf, uns size);
174
175 #endif