]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Got tired by repeating the same `gather pieces from incomplete reads' subroutine.
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 /*
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.
14  */
15
16 #ifndef _SHERLOCK_LIB_H
17 #define _SHERLOCK_LIB_H
18
19 #include "lib/config.h"
20 #include <stdarg.h>
21
22 /* Tell libc we're going to use all extensions available */
23
24 #define _GNU_SOURCE
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 #define UNALIGNED_PART(ptr, type) (((long) (ptr)) % sizeof(type))
32
33 /* Some other macros */
34
35 #define MIN(a,b) (((a)<(b))?(a):(b))
36 #define MAX(a,b) (((a)>(b))?(a):(b))
37 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
38 #define ABS(x) ((x) < 0 ? -(x) : (x))
39 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
40 #define STRINGIFY(x) #x
41 #define GLUE(x,y) x##y
42 #define GLUE_(x,y) x##_##y
43 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
44 #define REV_COMPARE(x,y) COMPARE(y,x)
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 extern char *log_title;                 /* NULL - print no title, default is log_progname */
58
59 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
60 #define log log_msg
61 void vlog_msg(unsigned int cat, const char *msg, va_list args);
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 void assert_failed(char *assertion, char *file, int line) NONRET;
69 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
70 #else
71 void assert_failed(void) NONRET;
72 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
73 #endif
74
75 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
76
77 #ifdef LOCAL_DEBUG
78 #define DBG(x,y...) log(L_DEBUG, x,##y)
79 #else
80 #define DBG(x,y...) do { } while(0)
81 #endif
82
83 /* Memory allocation */
84
85 #ifdef DEBUG_DMALLOC
86 /*
87  * The standard dmalloc macros tend to produce lots of namespace
88  * conflicts and we use only xmalloc and xfree, so we can define
89  * the stubs ourselves.
90  */
91 #define DMALLOC_DISABLE
92 #include <dmalloc.h>
93 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
94 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
95 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
96 #else
97 /*
98  * Unfortunately, several libraries we might want to link to define
99  * their own xmalloc and we don't want to interfere with them, hence
100  * the renaming.
101  */
102 #define xmalloc sh_xmalloc
103 void *xmalloc(unsigned);
104 void *xrealloc(void *, unsigned);
105 #define xfree(x) free(x)
106 #endif
107
108 void *xmalloc_zero(unsigned);
109 byte *stralloc(byte *);
110
111 /* Content-Type pattern matching and filters */
112
113 int match_ct_patt(byte *, byte *);
114
115 /* log2.c */
116
117 int fls(u32);
118
119 /* wordsplit.c */
120
121 int wordsplit(byte *, byte **, uns);
122
123 /* pat(i)match.c: Matching of shell patterns */
124
125 int match_pattern(byte *, byte *);
126 int match_pattern_nocase(byte *, byte *);
127
128 /* md5hex.c */
129
130 void md5_to_hex(byte *, byte *);
131 void hex_to_md5(byte *, byte *);
132
133 #define MD5_SIZE 16
134 #define MD5_HEX_SIZE 33
135
136 /* prime.c */
137
138 int isprime(uns);
139 uns nextprime(uns);
140
141 /* timer.c */
142
143 void init_timer(void);
144 uns get_timer(void);
145
146 /* regex.c */
147
148 typedef struct regex regex;
149
150 regex *rx_compile(byte *r, int icase);
151 void rx_free(regex *r);
152 int rx_match(regex *r, byte *s);
153 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
154
155 /* random.c */
156
157 uns random_max(uns);
158
159 /* mmap.c */
160
161 void *mmap_file(byte *name, unsigned *len, int writeable);
162 void munmap_file(void *start, unsigned len);
163
164 /* partmap.c */
165
166 struct partmap;
167 struct partmap *partmap_open(byte *name, int writeable);
168 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
169 void partmap_close(struct partmap *p);
170 sh_off_t partmap_size(struct partmap *p);
171
172 /* proctitle.c */
173
174 void setproctitle_init(int argc, char **argv);
175 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
176
177 /* randomkey.c */
178
179 void randomkey(byte *buf, uns size);
180
181 /* exitstatus.c */
182
183 #define EXIT_STATUS_MSG_SIZE 32
184 int format_exit_status(byte *msg, int stat);
185
186 /* runcmd.c */
187
188 int run_command(byte *cmd, ...);
189 void NONRET exec_command(byte *cmd, ...);
190 void echo_command(byte *buf, int size, byte *cmd, ...);
191 int run_command_v(byte *cmd, va_list args);
192 void NONRET exec_command_v(byte *cmd, va_list args);
193 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
194
195 /* carefulio.c */
196
197 int careful_read(int fd, void *buf, int len);
198 int careful_write(int fd, void *buf, int len);
199
200 #endif