| 1 |
/* $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $ */ |
| 2 |
|
| 3 |
/*- |
| 4 |
* Copyright (c) 2007 |
| 5 |
* Thorsten Glaser <tg@mirbsd.de> |
| 6 |
* |
| 7 |
* Provided that these terms and disclaimer and all copyright notices |
| 8 |
* are retained or reproduced in an accompanying document, permission |
| 9 |
* is granted to deal in this work without restriction, including un- |
| 10 |
* limited rights to use, publicly perform, distribute, sell, modify, |
| 11 |
* merge, give away, or sublicence. |
| 12 |
* |
| 13 |
* Advertising materials mentioning features or use of this work must |
| 14 |
* display the following acknowledgement: |
| 15 |
* This product includes material provided by Thorsten Glaser. |
| 16 |
* This acknowledgement does not need to be reprinted if this work is |
| 17 |
* linked into a bigger work whose licence does not allow such clause |
| 18 |
* and the author of this work is given due credit in the bigger work |
| 19 |
* or its accompanying documents, where such information is generally |
| 20 |
* kept, provided that said credits are retained. |
| 21 |
* |
| 22 |
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
| 23 |
* the utmost extent permitted by applicable law, neither express nor |
| 24 |
* implied; without malicious intent or gross negligence. In no event |
| 25 |
* may a licensor, author or contributor be held liable for indirect, |
| 26 |
* direct, other damage, loss, or other issues arising in any way out |
| 27 |
* of dealing in the work, even if advised of the possibility of such |
| 28 |
* damage or existence of a defect, except proven that it results out |
| 29 |
* of said person's immediate fault when using the work as intended. |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifndef NFOHELPR_H |
| 33 |
#define NFOHELPR_H |
| 34 |
|
| 35 |
/* stolen from mksh's sh.h */ |
| 36 |
#ifdef EXTERN |
| 37 |
# define I__(i) = i |
| 38 |
#else |
| 39 |
# define I__(i) |
| 40 |
# define EXTERN extern |
| 41 |
# define EXTERN_DEFINED |
| 42 |
#endif |
| 43 |
|
| 44 |
/* debugging */ |
| 45 |
#define D(level, fmt, ...) do { \ |
| 46 |
if (debug >= (level)) { \ |
| 47 |
fflush(NULL); \ |
| 48 |
fprintf(stderr, (fmt), ##__VA_ARGS__); \ |
| 49 |
fflush(NULL); \ |
| 50 |
} \ |
| 51 |
} while (0) |
| 52 |
|
| 53 |
__BEGIN_DECLS |
| 54 |
EXTERN int debug I__(0); |
| 55 |
|
| 56 |
#ifdef NFOTISER |
| 57 |
#ifdef __GLIBC__ |
| 58 |
/* fgetln.c */ |
| 59 |
char *fgetln(FILE *, size_t *); |
| 60 |
/* strlfun.c */ |
| 61 |
size_t strlcat(char *, const char *, size_t); |
| 62 |
size_t strlcpy(char *, const char *, size_t); |
| 63 |
#endif |
| 64 |
#endif |
| 65 |
__END_DECLS |
| 66 |
|
| 67 |
/* useful stuff */ |
| 68 |
#define xmalloc(size) ({ \ |
| 69 |
void *xmalloc_ptr; \ |
| 70 |
size_t xmalloc_sz = (size); \ |
| 71 |
\ |
| 72 |
if ((xmalloc_ptr = malloc(xmalloc_sz)) == NULL) \ |
| 73 |
err(255, "cannot allocate %zu bytes", \ |
| 74 |
xmalloc_sz); \ |
| 75 |
(xmalloc_ptr); \ |
| 76 |
}) |
| 77 |
|
| 78 |
#define xrealloc(ptr, size) ({ \ |
| 79 |
void *xrealloc_newptr; \ |
| 80 |
size_t xrealloc_sz = (size); \ |
| 81 |
\ |
| 82 |
xrealloc_newptr = realloc((ptr), xrealloc_sz); \ |
| 83 |
if (xrealloc_newptr == NULL) \ |
| 84 |
err(255, "cannot realloc to %zu bytes", \ |
| 85 |
xrealloc_sz); \ |
| 86 |
(xrealloc_newptr); \ |
| 87 |
}) |
| 88 |
|
| 89 |
#define str_save(s) ({ \ |
| 90 |
const char *str_save_s = (s); \ |
| 91 |
\ |
| 92 |
(str_nsave(str_save_s, strlen(str_save_s))); \ |
| 93 |
}) |
| 94 |
|
| 95 |
#define str_nsave(s, n) ({ \ |
| 96 |
char *str_nsave_ptr; \ |
| 97 |
size_t str_nsave_sz = (n); \ |
| 98 |
\ |
| 99 |
str_nsave_ptr = xmalloc(str_nsave_sz + 1); \ |
| 100 |
memcpy(str_nsave_ptr, (s), str_nsave_sz); \ |
| 101 |
str_nsave_ptr[str_nsave_sz] = '\0'; \ |
| 102 |
(str_nsave_ptr); \ |
| 103 |
}) |
| 104 |
|
| 105 |
#define str_add(d, s) ({ \ |
| 106 |
const char *str_add_s = (s); \ |
| 107 |
\ |
| 108 |
(str_nadd((d), str_add_s, strlen(str_add_s))); \ |
| 109 |
}) |
| 110 |
|
| 111 |
#define str_nadd(d, s, n) ({ \ |
| 112 |
char *str_nadd_ptr = (d), *str_nadd_newptr; \ |
| 113 |
size_t str_nadd_sz, str_nadd_addsz = (n); \ |
| 114 |
\ |
| 115 |
str_nadd_sz = str_nadd_ptr ? \ |
| 116 |
strlen(str_nadd_ptr) : 0; \ |
| 117 |
str_nadd_newptr = str_nadd_ptr ? \ |
| 118 |
xrealloc(str_nadd_ptr, \ |
| 119 |
str_nadd_sz + str_nadd_addsz + 1) : \ |
| 120 |
xmalloc(str_nadd_addsz + 1); \ |
| 121 |
str_nadd_ptr = str_nadd_newptr + str_nadd_sz; \ |
| 122 |
memcpy(str_nadd_ptr, (s), str_nadd_addsz); \ |
| 123 |
str_nadd_ptr[str_nadd_addsz] = '\0'; \ |
| 124 |
(str_nadd_newptr); \ |
| 125 |
}) |
| 126 |
|
| 127 |
#endif |