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 */
23 /* Ugly structure handling macros */
25 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
26 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
27 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
28 #define ALIGN_PTR(p, s) ((addr_int_t)(p) % (s) ? (typeof(p))((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)) : (p))
29 #define UNALIGNED_PART(ptr, type) (((addr_int_t) (ptr)) % sizeof(type))
31 /* Some other macros */
33 #define MIN(a,b) (((a)<(b))?(a):(b))
34 #define MAX(a,b) (((a)>(b))?(a):(b))
35 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
36 #define ABS(x) ((x) < 0 ? -(x) : (x))
37 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
38 #define STRINGIFY(x) #x
39 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
40 #define GLUE(x,y) x##y
41 #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 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
46 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
48 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits)))) /* Bitwise rotation of an uns to the left */
55 #define NONRET __attribute__((noreturn))
56 #define UNUSED __attribute__((unused))
57 #define CONSTRUCTOR __attribute__((constructor))
58 #define PACKED __attribute__((packed))
59 #define CONST __attribute__((const))
60 #define PURE __attribute__((const))
61 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
62 #define likely(x) __builtin_expect((x),1)
63 #define unlikely(x) __builtin_expect((x),0)
66 #error This program requires the GNU C compiler.
71 #define L_DEBUG 'D' /* Debugging messages */
72 #define L_INFO 'I' /* Informational msgs, warnings and errors */
75 #define L_INFO_R 'i' /* Errors caused by external events */
78 #define L_FATAL '!' /* die() */
80 extern char *log_title; /* NULL - print no title, default is log_progname */
81 extern char *log_filename; /* Expanded name of the current log file */
82 extern volatile int log_switch_nest; /* log_switch() nesting counter, increment to disable automatic switches */
83 extern int log_pid; /* 0 if shouldn't be logged */
84 extern void (*log_die_hook)(void);
86 extern void (*log_switch_hook)(struct tm *tm);
88 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
90 void vlog_msg(unsigned int cat, const char *msg, va_list args);
91 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
92 void log_init(byte *argv0);
93 void log_file(byte *name);
97 void assert_failed(char *assertion, char *file, int line) NONRET;
98 void assert_failed_noinfo(void) NONRET;
101 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
103 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
106 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
109 #define DBG(x,y...) log(L_DEBUG, x,##y)
111 #define DBG(x,y...) do { } while(0)
114 static inline void log_switch_disable(void) { log_switch_nest++; }
115 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
117 /* Memory allocation */
119 #define xmalloc sh_xmalloc
120 #define xrealloc sh_xrealloc
121 #define xfree sh_xfree
125 * The standard dmalloc macros tend to produce lots of namespace
126 * conflicts and we use only xmalloc and xfree, so we can define
127 * the stubs ourselves.
129 #define DMALLOC_DISABLE
131 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
132 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
133 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
136 * Unfortunately, several libraries we might want to link to define
137 * their own xmalloc and we don't want to interfere with them, hence
140 void *xmalloc(unsigned);
141 void *xrealloc(void *, unsigned);
142 #define sh_xfree(x) free(x)
145 void *xmalloc_zero(unsigned);
146 byte *xstrdup(byte *);
148 /* Content-Type pattern matching and filters */
150 int match_ct_patt(byte *, byte *);
154 int sepsplit(byte *str, byte sep, byte **rec, uns max);
155 int wordsplit(byte *, byte **, uns);
157 /* pat(i)match.c: Matching of shell patterns */
159 int match_pattern(byte *, byte *);
160 int match_pattern_nocase(byte *, byte *);
164 void md5_to_hex(byte *, byte *);
165 void hex_to_md5(byte *, byte *);
168 #define MD5_HEX_SIZE 33
177 uns next_table_prime(uns x);
178 uns prev_table_prime(uns x);
184 void init_timer(void);
186 void get_last_timeval(struct timeval *tv);
190 typedef struct regex regex;
192 regex *rx_compile(byte *r, int icase);
193 void rx_free(regex *r);
194 int rx_match(regex *r, byte *s);
195 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
203 void *mmap_file(byte *name, unsigned *len, int writeable);
204 void munmap_file(void *start, unsigned len);
208 void setproctitle_init(int argc, char **argv);
209 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
213 void randomkey(byte *buf, uns size);
217 #define EXIT_STATUS_MSG_SIZE 32
218 int format_exit_status(byte *msg, int stat);
222 int run_command(byte *cmd, ...);
223 void NONRET exec_command(byte *cmd, ...);
224 void echo_command(byte *buf, int size, byte *cmd, ...);
225 int run_command_v(byte *cmd, va_list args);
226 void NONRET exec_command_v(byte *cmd, va_list args);
227 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
231 int careful_read(int fd, void *buf, int len);
232 int careful_write(int fd, void *buf, int len);
236 void sync_dir(byte *name);
240 typedef int (*sh_sighandler_t)(int);
241 /* obtains signum, returns nonzero if abort() should be called */
242 extern sh_sighandler_t signal_handler[];
245 void handle_signal(int signum, struct sigaction *oldact);
246 void unhandle_signal(int signum, struct sigaction *oldact);