File: util.h

package info (click to toggle)
fped 0.1%2B201210-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 948 kB
  • ctags: 1,663
  • sloc: ansic: 12,650; yacc: 1,112; sh: 974; lex: 204; makefile: 140; awk: 75
file content (70 lines) | stat: -rw-r--r-- 1,684 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * util.h - Common utility functions
 *
 * Written 2006, 2009, 2010 by Werner Almesberger
 * Copyright 2006, 2009, 2010 Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


#ifndef UTIL_H
#define UTIL_H

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>


#define alloc_size(s)					\
    ({	void *alloc_size_tmp = malloc(s);		\
	if (!alloc_size_tmp)				\
		abort();				\
	alloc_size_tmp; })

#define alloc_type(t) ((t *) alloc_size(sizeof(t)))

#define	zalloc_size(s)					\
    ({	void *zalloc_size_tmp = alloc_size(s);		\
	memset(zalloc_size_tmp, 0, (s));		\
	zalloc_size_tmp; })

#define zalloc_type(t)					\
    ({	t *zalloc_type_tmp = alloc_type(t);		\
	memset(zalloc_type_tmp, 0, sizeof(t));		\
	zalloc_type_tmp; })

#define stralloc(s)					\
    ({	char *stralloc_tmp = strdup(s);			\
	if (!stralloc_tmp)				\
		abort();				\
	stralloc_tmp; })

#define strnalloc(s, n)					\
    ({	char *strnalloc_tmp = alloc_size((n)+1);	\
	if (!strnalloc_tmp)				\
		abort();				\
	strncpy(strnalloc_tmp, (s), (n));		\
	strnalloc_tmp[n] = 0;				\
	strnalloc_tmp; })

#define SWAP(a, b) \
    ({	typeof(a) SWAP_tmp = (a);			\
	(a) = (b);					\
	(b) = SWAP_tmp; })


char *stralloc_vprintf(const char *fmt, va_list ap);
char *stralloc_printf(const char *fmt, ...)
    __attribute__((format(printf, 1, 2)));

int is_id_char(char c, int first);
int is_id(const char *s);

const char *unique(const char *s);
void unique_cleanup(void);

#endif /* !UTIL_H */