7814
Ta
tiny-c-agent / files
tiny-c-agent / src / util.h
2.8 KB
1// Small shared helpers: allocation that aborts on failure, a growable byte 2// buffer, and formatted error storage. Used across the whole program. 3 4#ifndef TINY_UTIL_H 5#define TINY_UTIL_H 6 7#include <stddef.h> 8#include <stdio.h> 9#include <stdarg.h> 10#include <stdbool.h> 11 12void *xmalloc(size_t n); 13void *xcalloc(size_t n, size_t size); 14void *xrealloc(void *p, size_t n); 15char *xstrdup(const char *s); 16 17typedef struct { 18 char *data; 19 size_t len; 20 size_t cap; 21} tiny_buf; 22 23// The TUI assembles styled output with the same buffer. 24typedef tiny_buf tui_buf; 25 26void buf_init(tiny_buf *b); 27void buf_free(tiny_buf *b); 28void buf_clear(tiny_buf *b); 29void buf_reserve(tiny_buf *b, size_t extra); 30void buf_add(tiny_buf *b, const void *p, size_t n); 31void buf_adds(tiny_buf *b, const char *s); 32void buf_addc(tiny_buf *b, char c); 33void buf_addf(tiny_buf *b, const char *fmt, ...); 34// Hands the buffer's string to the caller and resets the buffer. Never NULL. 35char *buf_steal(tiny_buf *b); 36 37// Stores a formatted, malloc'd message into *err, freeing the previous one. 38void tiny_set_error(char **err, const char *fmt, ...); 39 40// tiny_set_error plus a -1 return, for the common error path. 41int tiny_fail(char **err, const char *fmt, ...); 42 43// Creates every component of path, like mkdir -p. Returns 0, or -1 with errno. 44int tiny_mkdirs(const char *path); 45 46// Builds "a/b", avoiding a doubled separator when a already ends in one. 47char *tiny_path_join(const char *a, const char *b); 48 49// An owned, growable list of owned strings. Free with strv_free. 50typedef struct { 51 char **v; 52 size_t n; 53 size_t cap; 54} tiny_strv; 55 56void strv_push(tiny_strv *l, char *s); // takes ownership of `s` 57void strv_free(tiny_strv *l); 58 59// Splits `s` on newlines. The trailing empty part is included, matching a 60// plain split. 61tiny_strv tiny_split_lines(const char *s); 62 63// A malloc'd copy of `s` with trailing newlines and carriage returns removed. 64char *tiny_trim_end(const char *s); 65 66// A malloc'd copy of `s` with leading and trailing ASCII whitespace removed. 67char *tiny_trim(const char *s); 68 69// True when `s` is empty or all ASCII whitespace. 70bool tiny_is_blank(const char *s); 71 72// Reads a whole file. Returns NULL when it cannot be opened or read; errno is 73// left set as the failing call set it. 74char *tiny_read_file(const char *path); 75 76// Reads all of `f` into one malloc'd string. 77char *tiny_read_stream(FILE *f); 78 79// Writes all of `buf` to `fd`, retrying on EINTR. Returns 0, or -1 with errno. 80int tiny_write_all(int fd, const char *buf, size_t len); 81 82// Sets *dst to a copy of `src` (or ""), freeing the previous value. 83void tiny_set_str(char **dst, const char *src); 84 85// Builds an endpoint URL from a base and a path, avoiding a doubled separator. 86// Returns NULL when `base` is NULL or empty. 87char *tiny_url_endpoint(const char *base, const char *path); 88 89#endif