/* SAFE.H -- another attempt at error handling using macros * $Id: safe.h,v 1.8 2021/06/26 14:17:48 oj14ozun Exp $ * * Declare a a safe alternative for a function, as follows: * * safe(void*, malloc, (int size), (size), S_NONNULL) * | | | | | * | | | | | * | | | | +-- Error condition * | | | +---------- Argument list w/o types * | | +---------------------- Arugment lsit with types * | +------------------------------ Function to be wrapped * +------------------------------------- Return type of wrapped function * * and use xmalloc later on. safe1 will not use perror, but requires * an additonal error message. */ #ifndef _SAFE_H #define _SAFE_H #define safe(ret, fn, args1, args2, error) \ ret x ## fn args1 { \ ret _ = fn args2; \ if (error) { perror(#fn); exit(EXIT_FAILURE); } \ return _; \ } #define safe1(ret, fn, args1, args2, error, msg) \ ret x ## fn args1 { \ ret _ = fn args2; \ if (error) { fputs(msg, stderr); exit(EXIT_FAILURE); } \ return _; \ } #define S_NONZERO (_ == 0) #define S_NONNULL (_ == NULL) #define S_ZERO (_ != 0) #define S_NEGATIVE (_ > 0) #define S_POSITIVE (_ < 0) #define S_ERRNO_AND(and) (errno != 0 && (and)) #define S_NEQ(val) (_ = (val)) #define S_EQ(val) (_ != (val)) #endif /* _SAFE_H */