]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
0fe45010f2419ad4ba3b03ae74ac030444d2845d
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2001 Martin Mares <mj@ucw.cz>
5  */
6
7 /*
8  *  This file should be included as the very first include in all
9  *  source files, especially before all OS includes since it sets
10  *  up libc feature macros.
11  */
12
13 #ifndef _SHERLOCK_LIB_H
14 #define _SHERLOCK_LIB_H
15
16 #include "lib/config.h"
17
18 /* Tell libc we're going to use all extensions available */
19
20 #define _GNU_SOURCE
21
22 /* Ugly structure handling macros */
23
24 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
25 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
26 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
27
28 /* Some other macros */
29
30 #define MIN(a,b) (((a)<(b))?(a):(b))
31 #define MAX(a,b) (((a)>(b))?(a):(b))
32 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
33 #define ABS(x) ((x) < 0 ? -(x) : (x))
34 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
35
36 /* Temporary Files */
37
38 #define TMP_DIR "tmp"
39 #define TMP_DIR_LEN 3
40
41 struct tempfile {
42   int fh;
43   byte name[32];
44 };
45
46 void open_temp(struct tempfile *, byte *);
47 void delete_temp(struct tempfile *);
48 u32 temprand(uns);
49
50 /* Logging */
51
52 #define L_DEBUG         'D'             /* Debugging messages */
53 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
54 #define L_WARN          'W'
55 #define L_ERROR         'E'
56 #define L_INFO_R        'i'             /* Errors caused by external events */
57 #define L_WARN_R        'w'
58 #define L_ERROR_R       'e'
59 #define L_FATAL         '!'             /* die() */
60
61 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
62 void die(byte *, ...) NONRET;
63 void log_init(byte *);
64 void log_file(byte *);
65 void log_fork(void);
66
67 #ifdef DEBUG
68 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
69 #else
70 #define ASSERT(x) do { } while(0)
71 #endif
72
73 #ifdef LOCAL_DEBUG
74 #define DBG(x,y...) log(L_DEBUG, x,##y)
75 #else
76 #define DBG(x,y...) do { } while(0)
77 #endif
78
79 /* Memory allocation */
80
81 #ifdef DMALLOC
82 /*
83  * The standard dmalloc macros tend to produce lots of namespace
84  * conflicts and we use only xmalloc and xfree, so we can define
85  * the stubs ourselves.
86  */
87 #define DMALLOC_DISABLE
88 #include <dmalloc.h>
89 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
90 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
91 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
92 #else
93 /*
94  * Unfortunately, several libraries we might want to link to define
95  * their own xmalloc and we don't want to interfere with them, hence
96  * the renaming.
97  */
98 #define xmalloc sh_xmalloc
99 void *xmalloc(unsigned);
100 void *xmalloc_zero(unsigned);
101 void *xrealloc(void *, unsigned);
102 #define xfree(x) free(x)
103 #endif
104
105 byte *stralloc(byte *);
106
107 /* Objects */
108
109 struct fastbuf;
110
111 struct odes {                           /* Object description */
112   struct oattr *attrs;
113   struct mempool *pool, *local_pool;
114 };
115
116 struct oattr {                          /* Object attribute */
117   struct oattr *next, *same, *last_same;
118   byte attr;
119   byte val[1];
120 };
121
122 void obj_dump(struct odes *);
123 struct odes *obj_new(struct mempool *);
124 void obj_free(struct odes *);
125 int obj_read(struct fastbuf *, struct odes *);
126 void obj_write(struct fastbuf *, struct odes *);
127 struct oattr *obj_find_attr(struct odes *, uns);
128 struct oattr *obj_find_attr_last(struct odes *, uns);
129 uns obj_del_attr(struct odes *, struct oattr *);
130 byte *obj_find_aval(struct odes *, uns);
131 struct oattr *obj_set_attr(struct odes *, uns, byte *);
132 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
133 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
134
135 /* Content-Type pattern matching and filters */
136
137 int match_ct_patt(byte *, byte *);
138
139 /* log2.c */
140
141 int log2(u32);
142
143 /* wordsplit.c */
144
145 int wordsplit(byte *, byte **, uns);
146
147 /* pat(i)match.c: Matching of shell patterns */
148
149 int match_pattern(byte *, byte *);
150 int match_pattern_nocase(byte *, byte *);
151
152 /* md5hex.c */
153
154 void md5_to_hex(byte *, byte *);
155 void hex_to_md5(byte *, byte *);
156
157 #define MD5_SIZE 16
158 #define MD5_HEX_SIZE 33
159
160 /* prime.c */
161
162 int isprime(uns);
163 uns nextprime(uns);
164
165 /* timer.c */
166
167 void init_timer(void);
168 uns get_timer(void);
169
170 /* regex.c */
171
172 typedef struct regex regex;
173
174 regex *rx_compile(byte *r);
175 void rx_free(regex *r);
176 int rx_match(regex *r, byte *s);
177 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
178
179 /* random.c */
180
181 uns random_max(uns);
182
183 /* mmap.c */
184
185 void *mmap_file(byte *name, unsigned *len, int writeable);
186 void munmap_file(void *start, unsigned len);
187
188 /* proctitle.c */
189
190 void setproctitle_init(int argc, char **argv);
191 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
192
193 #endif