1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #pragma once
- #include <stdio.h>
- #include <string.h>
- #include <sys/param.h>
- #include <unistd.h>
- #define CLAMP(lbound, ubound, value) MIN(MIN(lbound, ubound), MAX(MAX(lbound, ubound), 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)))
|