#pragma once
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if defined(__GNUC__) || defined(__clang__)
#define ATTR_FMT_PRINTF(fmt_index, chk_index) \
__attribute__ ((format (printf, fmt_index, chk_index)))
#else
#define ATTR_FMT_PRINTF(fmt_index, chk_index)
#endif
#define SUN_PATH_LEN (sizeof(struct sockaddr_un) - \
offsetof(struct sockaddr_un, sun_path))
bool isbigendian(void);
/* Return the file size of path, UINT64_MAX if an error occured during
evaluation */
uint64_t file_size(const char *path);
/* getdelim wrapper removing the delimiter if present at the end of the line */
ssize_t getdelim_trim(char **lineptr, size_t *n, int delim, FILE *stream);
static inline ssize_t getline_trim(char **lineptr, size_t *n, FILE *stream)
{
return getdelim_trim(lineptr, n, '\n', stream);
}
static inline uint64_t u64min(uint64_t a, uint64_t b)
{
return a < b ? a : b;
}
/* strto* functions with error checking boilerplate taken care off (aborting
when encountering an error) */
float xstrtof(const char *str);
long xstrtol(const char *str);
int xstrtoi(const char *str);
/* Set SA_RESTART for all signals not having term/core as default handler */
void signals_nointerrupt(void);
/* Read nbyte of fd into buf, completely. Returns false if an error occured or
EOF was reached before reading nbyte */
bool read_full(int fd, void *buf, size_t nbyte);
/* Write nbyte of buf to fd, completely. Returns false if an error occured */
bool write_full(int fd, const void *buf, size_t nbyte);
/* -1: recv error other than ECONNRESET
n <= len: n bytes read (n can be 0)
len + 1: len bytes read, but buffer was too small to read everything */
ssize_t recv_full(int sockfd, void *buf, size_t len);
/* true: OK
false: error */
bool send_full(int sockfd, void *buf, size_t len);
/* open() that aborts if an error is encountered */
int xopen(const char *path, int oflag);
/* close() that aborts if an error is encountered */
void xclose(int fd);
/* fclose() that aborts if an error is encountered */
void xfclose(FILE *stream);
/* strdup() that aborts instead of returning NULL */
void *xstrdup(const char *s);
/* malloc() that aborts instead of returning NULL */
void *xmalloc(size_t size);