2 * The UCW Library -- Miscellaneous Functions
4 * (c) 1997--2006 Martin Mares <mj@ucw.cz>
5 * (c) 2005 Tomas Valla <tom@ucw.cz>
6 * (c) 2006 Robert Spalek <robert@ucw.cz>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
15 #include "lib/config.h"
18 /* Tell libc we're going to use all extensions available */
24 /* Macros for handling structurues, offsets and alignment */
26 #define CHECK_PTR_TYPE(x, type) ((x)-(type)(x) + (type)(x))
27 #define PTR_TO(s, i) &((s*)0)->i
28 #define OFFSETOF(s, i) ((unsigned int) PTR_TO(s, 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 ALIGN_PTR(p, s) ((addr_int_t)(p) % (s) ? (typeof(p))((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)) : (p))
32 #define UNALIGNED_PART(ptr, type) (((addr_int_t) (ptr)) % sizeof(type))
34 /* Some other macros */
36 #define MIN(a,b) (((a)<(b))?(a):(b))
37 #define MAX(a,b) (((a)>(b))?(a):(b))
38 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
39 #define ABS(x) ((x) < 0 ? -(x) : (x))
40 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
41 #define STRINGIFY(x) #x
42 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
43 #define GLUE(x,y) x##y
44 #define GLUE_(x,y) x##_##y
46 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
47 #define REV_COMPARE(x,y) COMPARE(y,x)
48 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
49 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
51 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits)))) /* Bitwise rotation of an uns to the left */
58 #define NONRET __attribute__((noreturn))
59 #define UNUSED __attribute__((unused))
60 #define CONSTRUCTOR __attribute__((constructor))
61 #define PACKED __attribute__((packed))
62 #define CONST __attribute__((const))
63 #define PURE __attribute__((pure))
64 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
65 #define likely(x) __builtin_expect((x),1)
66 #define unlikely(x) __builtin_expect((x),0)
69 #define LIKE_MALLOC __attribute__((malloc))
70 #define SENTINEL_CHECK __attribute__((sentinel))
73 #define SENTINEL_CHECK
77 #error This program requires the GNU C compiler.
82 #define L_DEBUG 'D' /* Debugging messages */
83 #define L_INFO 'I' /* Informational msgs, warnings and errors */
86 #define L_INFO_R 'i' /* Errors caused by external events */
89 #define L_FATAL '!' /* die() */
91 extern char *log_title; /* NULL - print no title, default is log_progname */
92 extern char *log_filename; /* Expanded name of the current log file */
93 extern volatile int log_switch_nest; /* log_switch() nesting counter, increment to disable automatic switches */
94 extern int log_pid; /* 0 if shouldn't be logged */
95 extern int log_precise_timings; /* Include microsecond timestamps in log messages */
96 extern void (*log_die_hook)(void);
98 extern void (*log_switch_hook)(struct tm *tm);
100 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
102 void vlog_msg(unsigned int cat, const char *msg, va_list args);
103 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
104 void log_init(byte *argv0);
105 void log_file(byte *name);
107 int log_switch(void);
109 void assert_failed(char *assertion, char *file, int line) NONRET;
110 void assert_failed_noinfo(void) NONRET;
113 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
115 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
118 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
121 #define DBG(x,y...) log(L_DEBUG, x,##y)
123 #define DBG(x,y...) do { } while(0)
126 static inline void log_switch_disable(void) { log_switch_nest++; }
127 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
129 /* Memory allocation */
131 #define xmalloc sh_xmalloc
132 #define xrealloc sh_xrealloc
133 #define xfree sh_xfree
137 * The standard dmalloc macros tend to produce lots of namespace
138 * conflicts and we use only xmalloc and xfree, so we can define
139 * the stubs ourselves.
141 #define DMALLOC_DISABLE
143 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
144 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
145 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
148 * Unfortunately, several libraries we might want to link to define
149 * their own xmalloc and we don't want to interfere with them, hence
152 void *xmalloc(unsigned) LIKE_MALLOC;
153 void *xrealloc(void *, unsigned);
157 void *xmalloc_zero(unsigned) LIKE_MALLOC;
158 byte *xstrdup(byte *) LIKE_MALLOC;
160 /* Content-Type pattern matching and filters */
162 int match_ct_patt(byte *, byte *);
166 int sepsplit(byte *str, byte sep, byte **rec, uns max);
167 int wordsplit(byte *, byte **, uns);
169 /* pat(i)match.c: Matching of shell patterns */
171 int match_pattern(byte *, byte *);
172 int match_pattern_nocase(byte *, byte *);
176 void md5_to_hex(byte *, byte *);
177 void hex_to_md5(byte *, byte *);
180 #define MD5_HEX_SIZE 33
189 uns next_table_prime(uns x);
190 uns prev_table_prime(uns x);
196 void init_timer(void);
198 void get_last_timeval(struct timeval *tv);
202 typedef struct regex regex;
204 regex *rx_compile(byte *r, int icase);
205 void rx_free(regex *r);
206 int rx_match(regex *r, byte *s);
207 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
215 void *mmap_file(byte *name, unsigned *len, int writeable);
216 void munmap_file(void *start, unsigned len);
220 void setproctitle_init(int argc, char **argv);
221 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
222 char *getproctitle(void);
226 void randomkey(byte *buf, uns size);
230 #define EXIT_STATUS_MSG_SIZE 32
231 int format_exit_status(byte *msg, int stat);
235 int run_command(byte *cmd, ...);
236 void NONRET exec_command(byte *cmd, ...);
237 void echo_command(byte *buf, int size, byte *cmd, ...);
238 int run_command_v(byte *cmd, va_list args);
239 void NONRET exec_command_v(byte *cmd, va_list args);
240 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
244 int careful_read(int fd, void *buf, int len);
245 int careful_write(int fd, void *buf, int len);
249 void sync_dir(byte *name);
253 typedef int (*sh_sighandler_t)(int);
254 /* obtains signum, returns nonzero if abort() should be called */
255 extern sh_sighandler_t signal_handler[];
258 void handle_signal(int signum, struct sigaction *oldact);
259 void unhandle_signal(int signum, struct sigaction *oldact);
263 byte *str_unesc(byte *dest, byte *src);