1
0
mirror of https://git.disroot.org/80486DX2-66/polonium.git synced 2024-12-25 23:00:07 +05:30
polonium/include/common.h

110 lines
3.2 KiB
C
Raw Permalink Normal View History

2024-07-07 03:18:23 +05:30
#ifndef _COMMON_H
#define _COMMON_H
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
2024-07-07 03:18:23 +05:30
/* enums */
enum configurations_bitshift {
C_BITSHIFT_CONFIRM = 0,
C_BITSHIFT_CONTENTS,
C_BITSHIFT_LINE_ENDINGS,
C_BITSHIFT_PRINTABLE,
C_BITSHIFT_CUSTOM_SEED,
C_BITSHIFT_VERBOSE
2024-07-07 03:18:23 +05:30
};
enum configurations {
C_CONFIRM = 1 << C_BITSHIFT_CONFIRM,
C_CONTENTS = 1 << C_BITSHIFT_CONTENTS,
C_LINE_ENDINGS = 1 << C_BITSHIFT_LINE_ENDINGS,
C_PRINTABLE = 1 << C_BITSHIFT_PRINTABLE,
C_CUSTOM_SEED = 1 << C_BITSHIFT_CUSTOM_SEED,
C_VERBOSE = 1 << C_BITSHIFT_VERBOSE
2024-07-07 03:18:23 +05:30
};
enum file_validation_status {
VALID_FILE,
INVALID_FILE,
FILE_IO_ERROR
};
2024-07-07 03:18:23 +05:30
/* macros: procedures */
#define FPRINTF_MACRO(stream, ...) do { \
fflush(stdout); \
fprintf(stream, __VA_ARGS__); \
} while (0)
2024-09-22 14:52:54 +05:30
2024-11-18 21:14:23 +05:30
#define ERROR_MSG(a, b) \
fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, (a), (b))
2024-07-07 03:18:23 +05:30
#define PERROR_MACRO(s) do { \
int errnum = errno; \
char* err_msg = strerror(errnum); /* XXX: Thread race possible */ \
2024-07-07 03:18:23 +05:30
fflush(stdout); \
2024-11-18 21:14:23 +05:30
ERROR_MSG((s), err_msg); \
2024-07-07 03:18:23 +05:30
} while (0)
2024-09-22 14:52:54 +05:30
#define FATAL_ERROR_PRINT(...) do { \
2024-07-07 03:18:23 +05:30
FPRINTF_MACRO(stderr, __VA_ARGS__); \
2024-09-22 14:52:54 +05:30
putc('\n', stderr); \
} while (0)
2024-07-07 03:18:23 +05:30
#define FATAL_ERROR(...) do { \
FATAL_ERROR_PRINT(__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
2024-09-22 14:52:54 +05:30
2024-07-07 03:18:23 +05:30
#define FATAL_ERROR_RET(...) do { \
FATAL_ERROR_PRINT(__VA_ARGS__); \
return EXIT_FAILURE; \
} while (0)
/* macros: definitions */
#define DEFAULT_CONFIG (C_CONFIRM)
2024-07-07 03:18:23 +05:30
#ifndef SIZE_T_C
# if defined(UINT64_MAX) && SIZE_MAX == UINT64_MAX
# define SIZE_T_C(x) UINT64_C(x)
# elif defined(UINT32_MAX) && SIZE_MAX == UINT32_MAX
# define SIZE_T_C(x) UINT32_C(x)
# else
# define SIZE_T_C(x) ((size_t) x)
# endif
#endif
#ifndef SIZE_MAX
# define SIZE_MAX (~SIZE_T_C(0))
#endif
#define STR_YN_YES "yes"
#define STR_YN_NO "no"
#ifdef HAVE_NO_FSEEKO_FTELLO
# define FSEEK_MACRO fseek
# define FTELL_MACRO ftell
typedef long file_offset_t;
# define FILE_OFFSET_C(x) ((x)##L)
#else
# define FSEEK_MACRO fseeko
# define FTELL_MACRO ftello
typedef off_t file_offset_t;
# define FILE_OFFSET_C(x) ((off_t) x)
#endif
2024-10-12 13:42:12 +05:30
#define MAX_PATH_LENGTH_PLUS_1 256
/* macros: lambdas */
#define STRINGIZE(x) #x
#define INT2STR(x) STRINGIZE(x)
#define YES_NO(x) ((x) ? STR_YN_YES : STR_YN_NO)
#define READ_CONFIG(x) (config & (x))
2024-09-22 14:52:54 +05:30
#define SET_CONFIG(x) config |= (x)
#define CLEAR_CONFIG(x) config &= ~(x)
2024-07-07 03:18:23 +05:30
#endif /* _COMMON_H */