2 * Sherlock Library -- Miscellaneous Functions
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
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.
16 #ifndef _SHERLOCK_LIB_H
17 #define _SHERLOCK_LIB_H
19 #include "lib/config.h"
21 /* Tell libc we're going to use all extensions available */
25 /* Ugly structure handling macros */
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))
32 /* Some other macros */
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)
46 #define L_DEBUG 'D' /* Debugging messages */
47 #define L_INFO 'I' /* Informational msgs, warnings and errors */
50 #define L_INFO_R 'i' /* Errors caused by external events */
53 #define L_FATAL '!' /* die() */
55 extern char *log_title; /* NULL - print no title, default is log_progname */
57 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
59 void die(byte *, ...) NONRET;
60 void log_init(byte *);
61 void log_file(byte *);
65 void assert_failed(char *assertion, char *file, int line) NONRET;
66 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
68 void assert_failed(void) NONRET;
69 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
73 #define DBG(x,y...) log(L_DEBUG, x,##y)
75 #define DBG(x,y...) do { } while(0)
78 /* Memory allocation */
82 * The standard dmalloc macros tend to produce lots of namespace
83 * conflicts and we use only xmalloc and xfree, so we can define
84 * the stubs ourselves.
86 #define DMALLOC_DISABLE
88 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
89 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
90 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
93 * Unfortunately, several libraries we might want to link to define
94 * their own xmalloc and we don't want to interfere with them, hence
97 #define xmalloc sh_xmalloc
98 void *xmalloc(unsigned);
99 void *xrealloc(void *, unsigned);
100 #define xfree(x) free(x)
103 void *xmalloc_zero(unsigned);
104 byte *stralloc(byte *);
106 /* Content-Type pattern matching and filters */
108 int match_ct_patt(byte *, byte *);
116 int wordsplit(byte *, byte **, uns);
118 /* pat(i)match.c: Matching of shell patterns */
120 int match_pattern(byte *, byte *);
121 int match_pattern_nocase(byte *, byte *);
125 void md5_to_hex(byte *, byte *);
126 void hex_to_md5(byte *, byte *);
129 #define MD5_HEX_SIZE 33
138 void init_timer(void);
143 typedef struct regex regex;
145 regex *rx_compile(byte *r, int icase);
146 void rx_free(regex *r);
147 int rx_match(regex *r, byte *s);
148 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
156 void *mmap_file(byte *name, unsigned *len, int writeable);
157 void munmap_file(void *start, unsigned len);
162 struct partmap *partmap_open(byte *name, int writeable);
163 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
164 void partmap_close(struct partmap *p);
165 sh_off_t partmap_size(struct partmap *p);
169 void setproctitle_init(int argc, char **argv);
170 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
174 void randomkey(byte *buf, uns size);