2 * The UCW Library -- Miscellaneous Functions
4 * (c) 1997--2005 Martin Mares <mj@ucw.cz>
5 * (c) 2005 Tomas Valla <tom@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "lib/config.h"
17 /* Tell libc we're going to use all extensions available */
21 /* Ugly structure handling macros */
23 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
24 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
25 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
26 #define ALIGN_PTR(p, s) ((addr_int_t)(p) % (s) ? (typeof(p))((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)) : (p))
27 #define UNALIGNED_PART(ptr, type) (((addr_int_t) (ptr)) % sizeof(type))
29 /* Some other macros */
31 #define MIN(a,b) (((a)<(b))?(a):(b))
32 #define MAX(a,b) (((a)>(b))?(a):(b))
33 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
34 #define ABS(x) ((x) < 0 ? -(x) : (x))
35 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
36 #define STRINGIFY(x) #x
37 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
38 #define GLUE(x,y) x##y
39 #define GLUE_(x,y) x##_##y
41 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
42 #define REV_COMPARE(x,y) COMPARE(y,x)
43 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
44 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
46 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits)))) /* Bitwise rotation of an uns to the left */
53 #define NONRET __attribute__((noreturn))
54 #define UNUSED __attribute__((unused))
55 #define CONSTRUCTOR __attribute__((constructor))
56 #define PACKED __attribute__((packed))
57 #define CONST __attribute__((const))
58 #define PURE __attribute__((const))
59 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
60 #define likely(x) __builtin_expect((x),1)
61 #define unlikely(x) __builtin_expect((x),0)
64 #error This program requires the GNU C compiler.
69 #define L_DEBUG 'D' /* Debugging messages */
70 #define L_INFO 'I' /* Informational msgs, warnings and errors */
73 #define L_INFO_R 'i' /* Errors caused by external events */
76 #define L_FATAL '!' /* die() */
78 extern char *log_title; /* NULL - print no title, default is log_progname */
79 extern char *log_filename; /* Expanded name of the current log file */
80 extern int log_switch_nest; /* log_switch() nesting counter, increment to disable automatic switches */
81 extern int log_pid; /* 0 if shouldn't be logged */
82 extern void (*log_die_hook)(void);
84 extern void (*log_switch_hook)(struct tm *tm);
86 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
88 void vlog_msg(unsigned int cat, const char *msg, va_list args);
89 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
90 void log_init(byte *argv0);
91 void log_file(byte *name);
95 void assert_failed(char *assertion, char *file, int line) NONRET;
96 void assert_failed_noinfo(void) NONRET;
99 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
101 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
104 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
107 #define DBG(x,y...) log(L_DEBUG, x,##y)
109 #define DBG(x,y...) do { } while(0)
112 static inline void log_switch_disable(void) { log_switch_nest++; }
113 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
115 /* Memory allocation */
117 #define xmalloc sh_xmalloc
118 #define xrealloc sh_xrealloc
119 #define xfree sh_xfree
123 * The standard dmalloc macros tend to produce lots of namespace
124 * conflicts and we use only xmalloc and xfree, so we can define
125 * the stubs ourselves.
127 #define DMALLOC_DISABLE
129 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
130 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
131 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
134 * Unfortunately, several libraries we might want to link to define
135 * their own xmalloc and we don't want to interfere with them, hence
138 void *xmalloc(unsigned);
139 void *xrealloc(void *, unsigned);
140 #define sh_xfree(x) free(x)
143 void *xmalloc_zero(unsigned);
144 byte *xstrdup(byte *);
146 /* Content-Type pattern matching and filters */
148 int match_ct_patt(byte *, byte *);
152 int sepsplit(byte *str, byte sep, byte **rec, uns max);
153 int wordsplit(byte *, byte **, uns);
155 /* pat(i)match.c: Matching of shell patterns */
157 int match_pattern(byte *, byte *);
158 int match_pattern_nocase(byte *, byte *);
162 void md5_to_hex(byte *, byte *);
163 void hex_to_md5(byte *, byte *);
166 #define MD5_HEX_SIZE 33
175 uns next_table_prime(uns x);
176 uns prev_table_prime(uns x);
182 void init_timer(void);
184 void get_last_timeval(struct timeval *tv);
188 typedef struct regex regex;
190 regex *rx_compile(byte *r, int icase);
191 void rx_free(regex *r);
192 int rx_match(regex *r, byte *s);
193 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
201 void *mmap_file(byte *name, unsigned *len, int writeable);
202 void munmap_file(void *start, unsigned len);
207 struct partmap *partmap_open(byte *name, int writeable);
208 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
209 void partmap_close(struct partmap *p);
210 sh_off_t partmap_size(struct partmap *p);
214 void setproctitle_init(int argc, char **argv);
215 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
219 void randomkey(byte *buf, uns size);
223 #define EXIT_STATUS_MSG_SIZE 32
224 int format_exit_status(byte *msg, int stat);
228 int run_command(byte *cmd, ...);
229 void NONRET exec_command(byte *cmd, ...);
230 void echo_command(byte *buf, int size, byte *cmd, ...);
231 int run_command_v(byte *cmd, va_list args);
232 void NONRET exec_command_v(byte *cmd, va_list args);
233 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
237 int careful_read(int fd, void *buf, int len);
238 int careful_write(int fd, void *buf, int len);
242 void sync_dir(byte *name);
246 typedef int (*sh_sighandler_t)(int);
247 /* obtains signum, returns nonzero if abort() should be called */
248 extern sh_sighandler_t signal_handler[];
251 void handle_signal(int signum, struct sigaction *oldact);
252 void unhandle_signal(int signum, struct sigaction *oldact);