#ifndef UTILITY_H
#define UTILITY_H
#ifdef __cplusplus
extern "C" {
#endif
// standard C includes
#include <stdint.h>
#include <time.h>
// bring TLS support to antique compilers
#ifndef thread_local
#ifdef _MSC_VER
#define thread_local __declspec(thread) // the thread_local keyword wasn't defined before C++11 and C23
#else // a saner compiler
#define thread_local _Thread_local // the thread_local keyword wasn't defined before C++11 and C23
#endif // _MSC_VER
#endif // !thread_local
// Microsoft-specific shims
#ifdef _MSC_VER
#if defined(_M_AMD64)
#define __x86_64__ 1
#elif defined(_M_ARM64) || defined(_M_ARM64EC)
#define __aarch64__ 1
#endif // native platform identification
static inline char *sane_strerror (int errnum) { thread_local static char errorstr[256]; strerror_s (errorstr, sizeof (errorstr), errnum); return (errorstr); } // non-complaining strerror() implementation for MSVC. Note that strerror() is unsafe IF AND ONLY IF the locale is changed during program execution AND a string pointer returned by strerror is saved and reused by the program. Guess the odds it will happen here...
#define strerror(n) sane_strerror ((n)) // STFU Microsoft
static inline char *sane_getenv (const char *varname) { extern char **environ; size_t i, len = strlen (varname); for (i = 0; environ[i] != NULL; i++) if ((strncmp (environ[i], varname, len) == 0) && ((environ[i])[len] == '=')) return (&(environ[i])[len + 1]); return (NULL); } // non-complaining getenv() implementation for MSVC. Note that getenv_s() access _environ too as per Microsoft docs, so this is perfectly legit. Nuff said.
#define getenv(v) sane_getenv ((v)) // STFU Microsoft
static struct tm *sane_localtime (const time_t *clock) { thread_local static struct tm localtime_tm; if (localtime_s (&localtime_tm, clock) != 0) return (NULL); return (&localtime_tm); } // non-complaining localtime() implementation for MSVC. Uses TLS.
#define localtime(t) sane_localtime ((t)) // STFU Microsoft
static inline char *sane_asctime (const struct tm *tm_time) { thread_local static char asctimestr[64]; if (asctime_s (asctimestr, sizeof (asctimestr), tm_time) != 0) return (NULL); return (asctimestr); } // non-complaining asctime() implementation for MSVC. Uses TLS.
#define asctime(t) sane_asctime ((t)) // STFU Microsoft
#define S_IFIFO 0x1000
#define S_IFLNK 0xa000
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define strdup(s) _strdup ((s))
#define strcasecmp(s1,s2) _stricmp ((s1), (s2))
#define strtok_r(s,delim,ctx) strtok_s ((s), (delim), (ctx))
#define mkdir(p,m) _mkdir ((p))
#define utimbuf __utimbuf32
#define utime(p,t) _utime32 ((p), (t))
#define access(p,m) _access ((p), (m))
#define MAXPATHLEN 1024
#else // !_MSC_VER
#define strcpy_s(s1,s1size,s2) strcpy ((s1), (s2))
#define strcat_s(s1,s1size,s2) strcat ((s1), (s2))
#define sprintf_s(s1,s1size,...) sprintf ((s1), __VA_ARGS__)
#define fopen_s(fp,pathname,mode) *(fp) = fopen ((pathname), (mode))
#endif // _MSC_VER
// handy macros that generate a version number in the format "YYYYMMDD" corresponding to the build date. Usage: printf ("version " VERSION_FMT_YYYYMMDD "\n", VERSION_ARG_YYYYMMDD);
#ifndef VERSION_ARG_YYYYMMDD
#define BUILDDATE_YEAR (&__DATE__[7]) // compiler will optimize this into a const string, e.g. "2021"
#define BUILDDATE_MONTH ( \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jan ") ? "01" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Feb ") ? "02" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Mar ") ? "03" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Apr ") ? "04" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "May ") ? "05" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jun ") ? "06" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jul ") ? "07" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Aug ") ? "08" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Sep ") ? "09" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Oct ") ? "10" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Nov ") ? "11" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Dec ") ? "12" : \
"XX" \
) // compiler will optimize this into a const string, e.g. "11"
#define BUILDDATE_DAY ( \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 1 ") ? "01" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 2 ") ? "02" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 3 ") ? "03" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 4 ") ? "04" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 5 ") ? "05" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 6 ") ? "06" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 7 ") ? "07" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 8 ") ? "08" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 9 ") ? "09" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 10 ") ? "10" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 11 ") ? "11" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 12 ") ? "12" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 13 ") ? "13" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 14 ") ? "14" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 15 ") ? "15" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 16 ") ? "16" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 17 ") ? "17" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 18 ") ? "18" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 19 ") ? "19" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 20 ") ? "20" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 21 ") ? "21" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 22 ") ? "22" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 23 ") ? "23" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 24 ") ? "24" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 25 ") ? "25" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 26 ") ? "26" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 27 ") ? "27" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 28 ") ? "28" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 29 ") ? "29" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 30 ") ? "30" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 31 ") ? "31" : \
"XX" \
) // compiler will optimize this into a const string, e.g. "14"
#define VERSION_FMT_YYYYMMDD "%s%s%s"
#define VERSION_ARG_YYYYMMDD BUILDDATE_YEAR, BUILDDATE_MONTH, BUILDDATE_DAY
#endif // !VERSION_ARG_YYYYMMDD
// macro to bring __FILE_NAME__ support to moronic compilers
#ifndef __FILE_NAME__ // Clang 9+ has the macro, GCC 12+ added it too in 2021, MSVC obviously won't do it. Heh.
#define __FILE_NAME__ ( \
(sizeof (__FILE__) > 2) && ((__FILE__[sizeof (__FILE__) - 2] == '/') || (__FILE__[sizeof (__FILE__) - 2] == '\\')) ? &__FILE__[sizeof (__FILE__) - 1] : \
(sizeof (__FILE__) > 3) && ((__FILE__[sizeof (__FILE__) - 3] == '/') || (__FILE__[sizeof (__FILE__) - 3] == '\\')) ? &__FILE__[sizeof (__FILE__) - 2] : \
(sizeof (__FILE__) > 4) && ((__FILE__[sizeof (__FILE__) - 4] == '/') || (__FILE__[sizeof (__FILE__) - 4] == '\\')) ? &__FILE__[sizeof (__FILE__) - 3] : \
(sizeof (__FILE__) > 5) && ((__FILE__[sizeof (__FILE__) - 5] == '/') || (__FILE__[sizeof (__FILE__) - 5] == '\\')) ? &__FILE__[sizeof (__FILE__) - 4] : \
(sizeof (__FILE__) > 6) && ((__FILE__[sizeof (__FILE__) - 6] == '/') || (__FILE__[sizeof (__FILE__) - 6] == '\\')) ? &__FILE__[sizeof (__FILE__) - 5] : \
(sizeof (__FILE__) > 7) && ((__FILE__[sizeof (__FILE__) - 7] == '/') || (__FILE__[sizeof (__FILE__) - 7] == '\\')) ? &__FILE__[sizeof (__FILE__) - 6] : \
(sizeof (__FILE__) > 8) && ((__FILE__[sizeof (__FILE__) - 8] == '/') || (__FILE__[sizeof (__FILE__) - 8] == '\\')) ? &__FILE__[sizeof (__FILE__) - 7] : \
(sizeof (__FILE__) > 9) && ((__FILE__[sizeof (__FILE__) - 9] == '/') || (__FILE__[sizeof (__FILE__) - 9] == '\\')) ? &__FILE__[sizeof (__FILE__) - 8] : \
(sizeof (__FILE__) > 10) && ((__FILE__[sizeof (__FILE__) - 10] == '/') || (__FILE__[sizeof (__FILE__) - 10] == '\\')) ? &__FILE__[sizeof (__FILE__) - 9] : \
(sizeof (__FILE__) > 11) && ((__FILE__[sizeof (__FILE__) - 11] == '/') || (__FILE__[sizeof (__FILE__) - 11] == '\\')) ? &__FILE__[sizeof (__FILE__) - 10] : \
(sizeof (__FILE__) > 12) && ((__FILE__[sizeof (__FILE__) - 12] == '/') || (__FILE__[sizeof (__FILE__) - 12] == '\\')) ? &__FILE__[sizeof (__FILE__) - 11] : \
(sizeof (__FILE__) > 13) && ((__FILE__[sizeof (__FILE__) - 13] == '/') || (__FILE__[sizeof (__FILE__) - 13] == '\\')) ? &__FILE__[sizeof (__FILE__) - 12] : \
(sizeof (__FILE__) > 14) && ((__FILE__[sizeof (__FILE__) - 14] == '/') || (__FILE__[sizeof (__FILE__) - 14] == '\\')) ? &__FILE__[sizeof (__FILE__) - 13] : \
(sizeof (__FILE__) > 15) && ((__FILE__[sizeof (__FILE__) - 15] == '/') || (__FILE__[sizeof (__FILE__) - 15] == '\\')) ? &__FILE__[sizeof (__FILE__) - 14] : \
(sizeof (__FILE__) > 16) && ((__FILE__[sizeof (__FILE__) - 16] == '/') || (__FILE__[sizeof (__FILE__) - 16] == '\\')) ? &__FILE__[sizeof (__FILE__) - 15] : \
(sizeof (__FILE__) > 17) && ((__FILE__[sizeof (__FILE__) - 17] == '/') || (__FILE__[sizeof (__FILE__) - 17] == '\\')) ? &__FILE__[sizeof (__FILE__) - 16] : \
(sizeof (__FILE__) > 18) && ((__FILE__[sizeof (__FILE__) - 18] == '/') || (__FILE__[sizeof (__FILE__) - 18] == '\\')) ? &__FILE__[sizeof (__FILE__) - 17] : \
(sizeof (__FILE__) > 19) && ((__FILE__[sizeof (__FILE__) - 19] == '/') || (__FILE__[sizeof (__FILE__) - 19] == '\\')) ? &__FILE__[sizeof (__FILE__) - 18] : \
(sizeof (__FILE__) > 20) && ((__FILE__[sizeof (__FILE__) - 20] == '/') || (__FILE__[sizeof (__FILE__) - 20] == '\\')) ? &__FILE__[sizeof (__FILE__) - 19] : \
(sizeof (__FILE__) > 21) && ((__FILE__[sizeof (__FILE__) - 21] == '/') || (__FILE__[sizeof (__FILE__) - 21] == '\\')) ? &__FILE__[sizeof (__FILE__) - 20] : \
(sizeof (__FILE__) > 22) && ((__FILE__[sizeof (__FILE__) - 22] == '/') || (__FILE__[sizeof (__FILE__) - 22] == '\\')) ? &__FILE__[sizeof (__FILE__) - 21] : \
(sizeof (__FILE__) > 23) && ((__FILE__[sizeof (__FILE__) - 23] == '/') || (__FILE__[sizeof (__FILE__) - 23] == '\\')) ? &__FILE__[sizeof (__FILE__) - 22] : \
(sizeof (__FILE__) > 24) && ((__FILE__[sizeof (__FILE__) - 24] == '/') || (__FILE__[sizeof (__FILE__) - 24] == '\\')) ? &__FILE__[sizeof (__FILE__) - 23] : \
(sizeof (__FILE__) > 25) && ((__FILE__[sizeof (__FILE__) - 25] == '/') || (__FILE__[sizeof (__FILE__) - 25] == '\\')) ? &__FILE__[sizeof (__FILE__) - 24] : \
(sizeof (__FILE__) > 26) && ((__FILE__[sizeof (__FILE__) - 26] == '/') || (__FILE__[sizeof (__FILE__) - 26] == '\\')) ? &__FILE__[sizeof (__FILE__) - 25] : \
(sizeof (__FILE__) > 27) && ((__FILE__[sizeof (__FILE__) - 27] == '/') || (__FILE__[sizeof (__FILE__) - 27] == '\\')) ? &__FILE__[sizeof (__FILE__) - 26] : \
(sizeof (__FILE__) > 28) && ((__FILE__[sizeof (__FILE__) - 28] == '/') || (__FILE__[sizeof (__FILE__) - 28] == '\\')) ? &__FILE__[sizeof (__FILE__) - 27] : \
(sizeof (__FILE__) > 29) && ((__FILE__[sizeof (__FILE__) - 29] == '/') || (__FILE__[sizeof (__FILE__) - 29] == '\\')) ? &__FILE__[sizeof (__FILE__) - 28] : \
(sizeof (__FILE__) > 30) && ((__FILE__[sizeof (__FILE__) - 30] == '/') || (__FILE__[sizeof (__FILE__) - 30] == '\\')) ? &__FILE__[sizeof (__FILE__) - 29] : \
(sizeof (__FILE__) > 31) && ((__FILE__[sizeof (__FILE__) - 31] == '/') || (__FILE__[sizeof (__FILE__) - 31] == '\\')) ? &__FILE__[sizeof (__FILE__) - 30] : \
(sizeof (__FILE__) > 32) && ((__FILE__[sizeof (__FILE__) - 32] == '/') || (__FILE__[sizeof (__FILE__) - 32] == '\\')) ? &__FILE__[sizeof (__FILE__) - 31] : \
(sizeof (__FILE__) > 33) && ((__FILE__[sizeof (__FILE__) - 33] == '/') || (__FILE__[sizeof (__FILE__) - 33] == '\\')) ? &__FILE__[sizeof (__FILE__) - 32] : \
__FILE__) // this *COMPILE-TIME* macro complements the __FILE__ macro defined by the C standard by returning just the filename portion of the full path. Supports filenames up to 32 chars. Expand as necessary.
#endif // !__FILE_NAME__
// logging macros
#define LOG(type,lvl,...) do { if ((lvl) <= verbose_level) { fprintf (stderr, "ifstool: %s: ", (type)); if (verbose_level > 1) fprintf (stderr, "%s:%d:%s(): ", __FILE_NAME__, __LINE__, __func__); fprintf (stderr, __VA_ARGS__); fputc ('\n', stderr); } } while (0)
#define LOG_ERROR(...) LOG ("error", 0, __VA_ARGS__)
#define LOG_WARNING(...) LOG ("warning", 1, __VA_ARGS__)
#define LOG_INFO(...) LOG ("info", 2, __VA_ARGS__)
#define LOG_DEBUG(...) LOG ("debug", 3, __VA_ARGS__)
// macro to gently exit with an error message
#define DIE_WITH_EXITCODE(exitcode,...) do { LOG_ERROR (__VA_ARGS__); exit ((exitcode)); } while (0)
// macro to exit less brutally than with abort() if something doesn't go the way we'd like to
#define ASSERT(is_it_true,...) do { if (!(is_it_true)) { LOG ("fatal error", 0, "consistency check failed:"); LOG ("fatal error", 0, #is_it_true); LOG ("fatal error", 0, __VA_ARGS__); exit (1); } } while (0)
#define ASSERT_WITH_ERRNO(is_it_true) ASSERT ((is_it_true), "%s", strerror (errno))
#ifdef __cplusplus
}
#endif
#endif // UTILITY_H