Added daemonizing, syslogging, setuid and chrooting to htparser.
[ashd.git] / lib / utils.h
CommitLineData
f0bbedf7
FT
1#ifndef _UTILS_H
2#define _UTILS_H
3
90f6e953
FT
4#include <stdarg.h>
5
b16800fa
FT
6#define max(a, b) (((b) > (a))?(b):(a))
7#define min(a, b) (((b) < (a))?(b):(a))
8
f0bbedf7
FT
9#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
10#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
11#define szmalloc(size) memset(smalloc(size), 0, size)
90f6e953 12#define sstrdup(str) ({const char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
f0bbedf7
FT
13#define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
14
15#define bufinit(buf) memset(&(buf), 0, sizeof(buf))
5fc1bf9f 16#define buffree(buf) do { if((buf).b != NULL) {free((buf).b);} bufinit(buf); } while(0)
f0bbedf7
FT
17#define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b))))
18#define bufadd(buf, new) \
19do { \
20 _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \
21 (buf).b[(buf).d++] = (new); \
22} while(0)
23#define bufcat(buf, new, size) \
24do { \
25 size_t __bufcat_size__; \
26 __bufcat_size__ = (size); \
27 _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \
28 memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
29 (buf).d += __bufcat_size__; \
30} while(0)
90f6e953 31#define bufcatstr(buf, str) \
f0bbedf7
FT
32do { \
33 char *__buf__; \
34 __buf__ = (str); \
35 bufcat((buf), __buf__, strlen(__buf__)); \
36} while(0)
90f6e953
FT
37#define bufcatstr2(buf, str) \
38do { \
39 char *__buf__; \
40 __buf__ = (str); \
41 bufcat((buf), __buf__, strlen(__buf__) + 1); \
42} while(0)
43#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
f0bbedf7
FT
44
45struct buffer {
46 void *b;
47 size_t s, d;
48};
49
50#define typedbuf(type) struct {type *b; size_t s, d;}
51
52struct charbuf {
53 char *b;
54 size_t s, d;
55};
56
57struct charvbuf {
58 char **b;
59 size_t s, d;
60};
61
62void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
90f6e953
FT
63char *decstr(char **p, size_t *len);
64char *vsprintf2(char *format, va_list al);
65char *sprintf2(char *format, ...);
66char *sprintf3(char *format, ...);
9aee4cbc 67off_t atoo(char *n);
f2b26d44
FT
68char **tokenize(char *src);
69void freeca(char **ca);
d1bd343c 70int calen(char **a);
d422fdfd
FT
71void bvprintf(struct charbuf *buf, char *format, va_list al);
72void bprintf(struct charbuf *buf, char *format, ...);
5fc1bf9f 73void replstr(char **p, char *n);
f0bbedf7
FT
74
75#endif