2 * (c) 2010 Cyrill Gorcunov, gorcunov@gmail.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 * Some bits are stolen from perf tool and Linux kernel :)
33 #define NORETURN __attribute__((__noreturn__))
37 #define __attribute__(x)
41 #define __stringify_1(x) #x
42 #define __stringify(x) __stringify_1(x)
44 extern void die(const char *err
, ...) NORETURN
__attribute__((format (printf
, 1, 2)));
45 extern void die_perror(const char *s
) NORETURN
;
46 extern int error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
47 extern void warning(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
48 extern void debug(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
49 extern void set_die_routine(void (*routine
)(const char *err
, va_list params
) NORETURN
);
51 #define BUG() DIE_IF(1)
55 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
56 __stringify(cnd) "\n"); \
59 extern size_t strlcat(char *dest
, const char *src
, size_t count
);
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
66 static inline void *xzalloc(unsigned long size
)
68 void *p
= malloc(size
);
74 static inline void *xalloc(unsigned long size
)
76 void *p
= malloc(size
);
81 static inline void *zalloc(unsigned long size
)
83 void *p
= malloc(size
);
89 static inline void *xstrdup(char *str
)
91 char *p
= strdup(str
);
96 static inline void *memdup(void *src
, size_t size
)
100 memcpy(p
, src
, size
);
104 #define x_isspace(x) isspace((unsigned char)(x))
106 /* skip leading spaces */
107 static inline char *skip_spaces(const char *p
)
110 while (*p
&& x_isspace(*p
))
115 /* skip trailing spaces */
116 static inline char *skip_spaces_rev(char *p
)
119 size_t i
= strlen(p
);
121 while (i
&& x_isspace(p
[i
]))
129 static inline char *skip_word(const char *p
)
132 while (*p
&& !x_isspace(*p
))
138 * trims spaces around the word and returns
139 * pointer to the start of word, the p will
140 * be set the first character after the word
142 static inline char *trim_word(char **str
)
149 start
= skip_spaces(*str
);
150 end
= skip_word(start
);