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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/* Yash: yet another shell */
/* util.h: miscellaneous utility functions */
/* (C) 2007-2021 magicant */
/* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef YASH_UTIL_H
#define YASH_UTIL_H
#include <stdlib.h>
#define Size_max ((size_t) -1) // = SIZE_MAX
/********** Miscellaneous Functions **********/
static inline int xunsetenv(const char *name)
__attribute__((nonnull));
/* Removes the environment variable of the specified name.
* This function wraps the `unsetenv' function, which has an incompatible
* prototype on some old environments. */
int xunsetenv(const char *name)
{
#if UNSETENV_RETURNS_INT
return unsetenv(name);
#else
unsetenv(name);
return 0;
#endif
}
/********** Memory Functions **********/
static inline size_t add(size_t a, size_t b)
__attribute__((pure));
static inline size_t mul(size_t a, size_t b)
__attribute__((pure));
static inline void *xcalloc(size_t nmemb, size_t size)
__attribute__((malloc,warn_unused_result));
static inline void *xmalloc(size_t size)
__attribute__((malloc,warn_unused_result));
static inline void *xmallocn(size_t count, size_t elemsize)
__attribute__((malloc,warn_unused_result));
static inline void *xmalloce(size_t count1, size_t count2, size_t elemsize)
__attribute__((malloc,warn_unused_result));
static inline void *xmallocs(size_t mainsize, size_t count, size_t elemsize)
__attribute__((malloc,warn_unused_result));
static inline void *xrealloc(void *ptr, size_t size)
__attribute__((malloc,warn_unused_result));
static inline void *xreallocn(void *ptr, size_t count, size_t elemsize)
__attribute__((malloc,warn_unused_result));
static inline void *xrealloce(void *ptr,
size_t count1, size_t count2, size_t elemsize)
__attribute__((malloc,warn_unused_result));
static inline void *xreallocs(void *ptr,
size_t mainsize, size_t count, size_t elemsize)
__attribute__((malloc,warn_unused_result));
extern void alloc_failed(void)
__attribute__((noreturn));
/* Computes `a + b', but aborts the program by ENOMEM if the result overflows.
*/
size_t add(size_t a, size_t b)
{
size_t sum = a + b;
if (sum < a)
alloc_failed();
return sum;
}
/* Computes `a * b', but aborts the program by ENOMEM if the result overflows.
*/
size_t mul(size_t a, size_t b)
{
size_t product = a * b;
if (b != 0 && product / b != a)
alloc_failed();
return product;
}
/* Attempts `calloc' and aborts the program on failure. */
void *xcalloc(size_t nmemb, size_t size)
{
void *result = calloc(nmemb, size);
if (result == NULL && nmemb > 0 && size > 0)
alloc_failed();
return result;
}
/* Attempts `malloc' and aborts the program on failure. */
void *xmalloc(size_t size)
{
void *result = malloc(size);
if (result == NULL && size > 0)
alloc_failed();
return result;
}
/* Like `xmalloc(count * elemsize)', but aborts the program if the size is too
* large. */
void *xmallocn(size_t count, size_t elemsize)
{
return xmalloc(mul(count, elemsize));
}
/* Like `xmalloc((count1 + count2) * elemsize)', but aborts the program if the
* size is too large. */
void *xmalloce(size_t count1, size_t count2, size_t elemsize)
{
return xmallocn(add(count1, count2), elemsize);
}
/* Like `xmalloc(mainsize + count * elemsize)', but aborts the program if the
* size is too large. */
void *xmallocs(size_t mainsize, size_t count, size_t elemsize)
{
return xmalloc(add(mainsize, mul(count, elemsize)));
}
/* Attempts `realloc' and aborts the program on failure. */
void *xrealloc(void *ptr, size_t size)
{
/* The behavior of `realloc(non_null_pointer, 0)' is unreliable. Some
* implementations free the previously allocated region and return NULL.
* Some reallocate an empty region and return a pointer to it. The latter
* may fail to allocate the new region, leaving the previous region intact
* and returning NULL. That means, when `realloc(non_null_pointer, 0)'
* returned NULL, we cannot tell if the previous region has been freed or
* not. */
if (size == 0) {
free(ptr);
return NULL;
}
void *result = realloc(ptr, size);
if (result == NULL)
alloc_failed();
return result;
}
/* Like `xrealloc(ptr, count * elemsize)', but aborts the program if the size is
* too large. */
void *xreallocn(void *ptr, size_t count, size_t elemsize)
{
return xrealloc(ptr, mul(count, elemsize));
}
/* Like `xrealloc(ptr, (count1 + count2) * elemsize)', but aborts the program if
* the size is too large. */
void *xrealloce(void *ptr, size_t count1, size_t count2, size_t elemsize)
{
return xreallocn(ptr, add(count1, count2), elemsize);
}
/* Like `xrealloc(ptr, mainsize + count * elemsize)', but aborts the program if
* the size is too large. */
void *xreallocs(void *ptr, size_t mainsize, size_t count, size_t elemsize)
{
return xrealloc(ptr, add(mainsize, mul(count, elemsize)));
}
/********** String Utilities **********/
#if !HAVE_STRNLEN
extern size_t xstrnlen(const char *s, size_t maxlen)
__attribute__((pure,nonnull));
#endif
#if !HAVE_STRDUP
extern char *xstrdup(const char *s)
__attribute__((malloc,warn_unused_result,nonnull));
#endif
#if !HAVE_WCSNLEN
extern size_t xwcsnlen(const wchar_t *s, size_t maxlen)
__attribute__((pure,nonnull));
#endif
extern wchar_t *xwcsndup(const wchar_t *s, size_t maxlen)
__attribute__((malloc,warn_unused_result,nonnull));
#if !HAVE_WCSDUP
static inline wchar_t *xwcsdup(const wchar_t *s)
__attribute__((malloc,warn_unused_result,nonnull));
#endif
extern _Bool xstrtoi(const char *s, int base, int *resultp)
__attribute__((warn_unused_result,nonnull));
extern _Bool xwcstoi(const wchar_t *s, int base, int *resultp)
__attribute__((warn_unused_result,nonnull));
extern _Bool xwcstol(const wchar_t *s, int base, long *resultp)
__attribute__((warn_unused_result,nonnull));
extern _Bool xwcstoul(const wchar_t *s, int base, unsigned long *resultp)
__attribute__((warn_unused_result,nonnull));
extern char *matchstrprefix(const char *s, const char *prefix)
__attribute__((pure,nonnull));
extern wchar_t *matchwcsprefix(const wchar_t *s, const wchar_t *prefix)
__attribute__((pure,nonnull));
extern void *copyaswcs(const void *p)
__attribute__((malloc,warn_unused_result,nonnull));
#if HAVE_STRNLEN
# ifndef strnlen
extern size_t strnlen(const char *s, size_t maxlen);
# endif
# define xstrnlen strnlen
#endif
#if HAVE_STRDUP
# ifndef strdup
extern char *strdup(const char *s);
# endif
# define xstrdup strdup
#endif
#if HAVE_WCSNLEN
# ifndef wcsnlen
extern size_t wcsnlen(const wchar_t *s, size_t maxlen);
# endif
# define xwcsnlen wcsnlen
#endif
#if HAVE_WCSDUP
# ifndef wcsdup
extern wchar_t *wcsdup(const wchar_t *s);
# endif
# define xwcsdup wcsdup
#else
/* Returns a newly malloced copy of the specified string.
* Aborts the program if failed to allocate memory. */
wchar_t *xwcsdup(const wchar_t *s)
{
return xwcsndup(s, Size_max);
}
#endif
/* These macros are used to cast the argument properly.
* We don't need such macros for wide characters. */
#define xisalnum(c) (isalnum((unsigned char) (c)))
#define xisalpha(c) (isalpha((unsigned char) (c)))
#define xisblank(c) (isblank((unsigned char) (c)))
#define xiscntrl(c) (iscntrl((unsigned char) (c)))
#define xisdigit(c) (isdigit((unsigned char) (c)))
#define xisgraph(c) (isgraph((unsigned char) (c)))
#define xislower(c) (islower((unsigned char) (c)))
#define xisprint(c) (isprint((unsigned char) (c)))
#define xispunct(c) (ispunct((unsigned char) (c)))
#define xisspace(c) (isspace((unsigned char) (c)))
#define xisupper(c) (isupper((unsigned char) (c)))
#define xisxdigit(c) (isxdigit((unsigned char) (c)))
#define xtoupper(c) (toupper((unsigned char) (c)))
#define xtolower(c) (tolower((unsigned char) (c)))
/* Casts scalar to char safely. */
#define TO_CHAR(value) \
((union { char c; unsigned char uc; }) { .uc = (unsigned char) (value), }.c)
/********** Error Utilities **********/
extern const wchar_t *yash_program_invocation_name;
extern const wchar_t *yash_program_invocation_short_name;
extern const wchar_t *current_builtin_name;
extern unsigned yash_error_message_count;
extern void xerror(int errno_, const char *restrict format, ...)
__attribute__((format(printf,2,3)));
extern _Bool xprintf(const char *restrict format, ...)
__attribute__((format(printf,1,2)));
#undef Size_max
#endif /* YASH_UTIL_H */
/* vim: set ts=8 sts=4 sw=4 et tw=80: */
|