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