11#define MTRIX_MAX_PATH ((size_t)1024U)
14#define MTRIX_MAX_UNIX_PATH ((size_t)108U)
17#define MTRIX_MAX_URL_LEN ((size_t)1024U)
20#define MTRIX_MAX_ARGS ((size_t)2U)
23#define BUILD_URL(url, ...) build_url(url, (const char *[]){__VA_ARGS__, NULL})
46void log_err(
const char *fmt, ...);
49void log_errv(
const char *fmt, va_list argp);
59static const char *
strchrnul(
const char *s,
int c);
62static size_t strlcpy(
char *restrict dst,
const char *restrict src,
size_t n);
68char *
is_prefix(
const char *prefix,
const char *s);
86bool read_all(
int fd,
void *p,
size_t n);
92bool write_all(
int fd,
const void *p,
size_t n);
103bool exec(
const char *
const *argv,
int fin,
int fout,
int ferr);
106bool wait_n(
size_t n,
const pid_t *p);
109void join_lines(
unsigned char *b,
unsigned char *e);
113 const char *p,
size_t size,
size_t n,
struct mtrix_buffer *b);
116bool build_url(
char *url,
const char *
const *v);
144static inline const char *
strchrnul(
const char *s,
int c) {
151 char *restrict dst,
const char *restrict src,
size_t n)
153 const char *
const p = dst;
154 while(n && (*dst++ = *src++))
156 return (
size_t)(dst - p);
Resizable buffer used by several functions.
Definition utils.h:35
char * p
Owning pointer to the dynamically-allocated data.
Definition utils.h:37
size_t n
Size of the buffer pointed to by p.
Definition utils.h:39
Data used for POST requests.
Definition utils.h:127
const char * url
Target URL.
Definition utils.h:129
size_t data_len
Length of data.
Definition utils.h:131
const char * data
POST payload.
Definition utils.h:133
#define MTRIX_MAX_PATH
Maximum path length, arbitrarily chosen.
Definition utils.h:11
void log_err(const char *fmt,...)
Writes the printf-style message to the error output.
Definition utils.c:35
size_t mtrix_buffer_append(const char *p, size_t size, size_t n, struct mtrix_buffer *b)
Copies data to the buffer, reallocating if necessary.
Definition utils.c:184
bool post(post_request r, bool verbose, struct mtrix_buffer *b)
Performs a POST request.
Definition utils.c:255
char * is_prefix(const char *prefix, const char *s)
Checks if a string has a certain prefix.
Definition utils.c:51
bool wait_n(size_t n, const pid_t *p)
Waits for n child processes to exit.
Definition utils.c:149
bool build_url(char *url, const char *const *v)
Joins several URL parts into one, limited to MTRIX_MAX_URL_LEN.
Definition utils.c:208
FILE * open_or_create(const char *path, const char *flags)
Performs an open(2)s with O_CREAT followed by fopen.
Definition utils.c:94
FILE * log_set(FILE *f)
Sets the output stream used by log functions and returns previous value.
Definition utils.c:21
void log_errv(const char *fmt, va_list argp)
Definition utils.c:27
bool copy_arg(const char *name, struct mtrix_buffer dst, const char *src)
Copies a value from an argv-style array.
Definition utils.c:58
const char * CMD_NAME
Command name, used as a second prefix for log messages if non-NULL.
Definition main.c:56
char * join_path(char v[static MTRIX_MAX_PATH], int n,...)
Concatenate n segments, with length checking.
Definition utils.c:74
bool request(const char *url, struct mtrix_buffer *b, bool verbose)
Performs a GET request.
Definition utils.c:240
void join_lines(unsigned char *b, unsigned char *e)
Replaces new-line characters with spaces.
Definition utils.c:171
void log_errno(const char *fmt,...)
Similar to log_err, but also logs strerror(errno).
Definition utils.c:42
static size_t strlcpy(char *restrict dst, const char *restrict src, size_t n)
See libbsd's function.
Definition utils.h:150
static const char * strchrnul(const char *s, int c)
See glibc's function.
Definition utils.h:144
bool exec(const char *const *argv, int fin, int fout, int ferr)
Executes a command with optional input/output/error redirection.
Definition utils.c:136
bool read_all(int fd, void *p, size_t n)
Repeatedly calls read(2) until all data are read.
Definition utils.c:105
const char * PROG_NAME
Program name, used as a prefix for log messages if non-NULL.
Definition main.c:53
bool write_all(int fd, const void *p, size_t n)
Repeatedly calls write(2) until all data are written.
Definition utils.c:117