]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Add sh_time_t type.
[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, byte *msg, ...);
58 void die(byte *, ...) NONRET;
59 void log_init(byte *);
60 void log_file(byte *);
61
62 #ifdef DEBUG
63 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
64 #else
65 #define ASSERT(x) do { } while(0)
66 #endif
67
68 #ifdef LOCAL_DEBUG
69 #define DBG(x,y...) log(L_DEBUG, x,##y)
70 #else
71 #define DBG(x,y...) do { } while(0)
72 #endif
73
74 /* Memory allocation */
75
76 #ifdef DMALLOC
77 /*
78  * The standard dmalloc macros tend to produce lots of namespace
79  * conflicts and we use only xmalloc and xfree, so we can define
80  * the stubs ourselves.
81  */
82 #define DMALLOC_DISABLE
83 #include <dmalloc.h>
84 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
85 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
86 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
87 #else
88 /*
89  * Unfortunately, several libraries we might want to link to define
90  * their own xmalloc and we don't want to interfere with them, hence
91  * the renaming.
92  */
93 #define xmalloc sh_xmalloc
94 void *xmalloc(unsigned);
95 void *xrealloc(void *, unsigned);
96 #define xfree(x) free(x)
97 #endif
98
99 byte *stralloc(byte *);
100
101 /* Objects */
102
103 struct fastbuf;
104
105 struct odes {                           /* Object description */
106   struct oattr *attrs;
107   struct mempool *pool, *local_pool;
108 };
109
110 struct oattr {                          /* Object attribute */
111   struct oattr *next, *same, *last_same;
112   byte attr;
113   byte val[1];
114 };
115
116 void obj_dump(struct odes *);
117 struct odes *obj_new(struct mempool *);
118 void obj_free(struct odes *);
119 int obj_read(struct fastbuf *, struct odes *);
120 void obj_write(struct fastbuf *, struct odes *);
121 struct oattr *obj_find_attr(struct odes *, uns);
122 struct oattr *obj_find_attr_last(struct odes *, uns);
123 uns obj_del_attr(struct odes *, struct oattr *);
124 byte *obj_find_aval(struct odes *, uns);
125 struct oattr *obj_set_attr(struct odes *, uns, byte *);
126 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
127 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
128
129 /* Content-Type pattern matching and filters */
130
131 int match_ct_patt(byte *, byte *);
132
133 /* log2.c */
134
135 int log2(u32);
136
137 /* wordsplit.c */
138
139 int wordsplit(byte *, byte **, uns);
140
141 /* pat(i)match.c: Matching of shell patterns */
142
143 int match_pattern(byte *, byte *);
144 int match_pattern_nocase(byte *, byte *);
145
146 /* md5hex.c */
147
148 void md5_to_hex(byte *, byte *);
149 void hex_to_md5(byte *, byte *);
150
151 #define MD5_SIZE 16
152 #define MD5_HEX_SIZE 33
153
154 /* prime.c */
155
156 int isprime(uns);
157 uns nextprime(uns);
158
159 /* timer.c */
160
161 void init_timer(void);
162 uns get_timer(void);
163
164 /* regex.c */
165
166 typedef struct regex regex;
167
168 regex *rx_compile(byte *r);
169 void rx_free(regex *r);
170 int rx_match(regex *r, byte *s);
171 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
172
173 /* random.c */
174
175 uns random_max(uns);
176
177 /* mmap.c */
178
179 void *mmap_file(byte *name, unsigned *len);
180
181 #endif