2 * Sherlock Library -- Miscellaneous Functions
4 * (c) 1997--2001 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)))
42 #define L_DEBUG 'D' /* Debugging messages */
43 #define L_INFO 'I' /* Informational msgs, warnings and errors */
46 #define L_INFO_R 'i' /* Errors caused by external events */
49 #define L_FATAL '!' /* die() */
51 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
52 void die(byte *, ...) NONRET;
53 void log_init(byte *);
54 void log_file(byte *);
58 void assert_failed(char *assertion, char *file, int line) NONRET;
59 #define ASSERT(x) do { if (!(x)) assert_failed(#x, __FILE__, __LINE__); } while(0)
61 void assert_failed(void) NONRET;
62 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
66 #define DBG(x,y...) log(L_DEBUG, x,##y)
68 #define DBG(x,y...) do { } while(0)
71 /* Memory allocation */
75 * The standard dmalloc macros tend to produce lots of namespace
76 * conflicts and we use only xmalloc and xfree, so we can define
77 * the stubs ourselves.
79 #define DMALLOC_DISABLE
81 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
82 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
83 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
86 * Unfortunately, several libraries we might want to link to define
87 * their own xmalloc and we don't want to interfere with them, hence
90 #define xmalloc sh_xmalloc
91 void *xmalloc(unsigned);
92 void *xrealloc(void *, unsigned);
93 #define xfree(x) free(x)
96 void *xmalloc_zero(unsigned);
97 byte *stralloc(byte *);
99 /* Content-Type pattern matching and filters */
101 int match_ct_patt(byte *, byte *);
109 int wordsplit(byte *, byte **, uns);
111 /* pat(i)match.c: Matching of shell patterns */
113 int match_pattern(byte *, byte *);
114 int match_pattern_nocase(byte *, byte *);
118 void md5_to_hex(byte *, byte *);
119 void hex_to_md5(byte *, byte *);
122 #define MD5_HEX_SIZE 33
131 void init_timer(void);
136 typedef struct regex regex;
138 regex *rx_compile(byte *r, int icase);
139 void rx_free(regex *r);
140 int rx_match(regex *r, byte *s);
141 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
149 void *mmap_file(byte *name, unsigned *len, int writeable);
150 void munmap_file(void *start, unsigned len);
154 void setproctitle_init(int argc, char **argv);
155 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
159 void randomkey(byte *buf, uns size);