123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #pragma once
- #include <stdlib.h>
- #include <string.h>
- #include <sys/param.h>
- #include <unistd.h>
- #define CLAMP(l, u, value) MAX(MIN(l, u), MIN(MAX(l, u), value))
- #define buffered_snprintf(b, s, ...) \
- (snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? b : NULL)
- #define safe_snprintf(b, s, ...) \
- (snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? strndup(b, sizeof(b)) : NULL)
- static inline const char *strtail(const char *haystack, const char *needle)
- {
- char *pos = strstr(haystack, needle);
- if (pos && (strlen(pos) == strlen(needle)))
- return pos;
- return NULL;
- }
- inline void cleanup_close(int *fd_ptr)
- {
- if (fd_ptr == NULL || *fd_ptr < 0)
- return;
- (void)close(*fd_ptr);
- }
- #define autoclose_fd __attribute__((cleanup(cleanup_close)))
- inline void cleanup_free(void *ptr)
- {
-
- void *target = *(void **)ptr;
- free(target);
- }
- #define autofree __attribute__((cleanup(cleanup_free)))
|