]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
b397c89b47c25152f799b00e21c8ea86b6bf12f2
[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 #ifndef NULL
23 #define NULL ((void *)0)
24 #endif
25
26 /* Ugly structure handling macros */
27
28 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
29 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
30 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
31
32 /* Temporary Files */
33
34 #define TMP_DIR "tmp"
35 #define TMP_DIR_LEN 3
36
37 struct tempfile {
38   int fh;
39   byte name[32];
40 };
41
42 void open_temp(struct tempfile *, byte *);
43 void delete_temp(struct tempfile *);
44 u32 temprand(uns);
45
46 /* Logging */
47
48 #define L_DEBUG         'D'             /* Debugging messages */
49 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
50 #define L_WARN          'W'
51 #define L_ERROR         'E'
52 #define L_INFO_R        'i'             /* Errors caused by external events */
53 #define L_WARN_R        'w'
54 #define L_ERROR_R       'e'
55 #define L_FATAL         '!'             /* die() */
56
57 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
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 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
65 #else
66 #define ASSERT(x) do { } while(0)
67 #endif
68
69 #ifdef LOCAL_DEBUG
70 #define DBG(x,y...) log(L_DEBUG, x,##y)
71 #else
72 #define DBG(x,y...) do { } while(0)
73 #endif
74
75 /* Memory allocation */
76
77 #ifdef DMALLOC
78 /*
79  * The standard dmalloc macros tend to produce lots of namespace
80  * conflicts and we use only xmalloc and xfree, so we can define
81  * the stubs ourselves.
82  */
83 #define DMALLOC_DISABLE
84 #include <dmalloc.h>
85 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
86 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
87 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
88 #else
89 /*
90  * Unfortunately, several libraries we might want to link to define
91  * their own xmalloc and we don't want to interfere with them, hence
92  * the renaming.
93  */
94 #define xmalloc sh_xmalloc
95 void *xmalloc(unsigned);
96 void *xmalloc_zero(unsigned);
97 void *xrealloc(void *, unsigned);
98 #define xfree(x) free(x)
99 #endif
100
101 byte *stralloc(byte *);
102
103 /* Objects */
104
105 struct fastbuf;
106
107 struct odes {                           /* Object description */
108   struct oattr *attrs;
109   struct mempool *pool, *local_pool;
110 };
111
112 struct oattr {                          /* Object attribute */
113   struct oattr *next, *same, *last_same;
114   byte attr;
115   byte val[1];
116 };
117
118 void obj_dump(struct odes *);
119 struct odes *obj_new(struct mempool *);
120 void obj_free(struct odes *);
121 int obj_read(struct fastbuf *, struct odes *);
122 void obj_write(struct fastbuf *, struct odes *);
123 struct oattr *obj_find_attr(struct odes *, uns);
124 struct oattr *obj_find_attr_last(struct odes *, uns);
125 uns obj_del_attr(struct odes *, struct oattr *);
126 byte *obj_find_aval(struct odes *, uns);
127 struct oattr *obj_set_attr(struct odes *, uns, byte *);
128 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
129 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
130
131 /* Content-Type pattern matching and filters */
132
133 int match_ct_patt(byte *, byte *);
134
135 /* log2.c */
136
137 int log2(u32);
138
139 /* wordsplit.c */
140
141 int wordsplit(byte *, byte **, uns);
142
143 /* pat(i)match.c: Matching of shell patterns */
144
145 int match_pattern(byte *, byte *);
146 int match_pattern_nocase(byte *, byte *);
147
148 /* md5hex.c */
149
150 void md5_to_hex(byte *, byte *);
151 void hex_to_md5(byte *, byte *);
152
153 #define MD5_SIZE 16
154 #define MD5_HEX_SIZE 33
155
156 /* prime.c */
157
158 int isprime(uns);
159 uns nextprime(uns);
160
161 /* timer.c */
162
163 void init_timer(void);
164 uns get_timer(void);
165
166 /* regex.c */
167
168 typedef struct regex regex;
169
170 regex *rx_compile(byte *r);
171 void rx_free(regex *r);
172 int rx_match(regex *r, byte *s);
173 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
174
175 /* random.c */
176
177 uns random_max(uns);
178
179 /* mmap.c */
180
181 void *mmap_file(byte *name, unsigned *len);
182
183 #endif