#include #include #include #include #include #include #include "common_utils/utils.h" char *append(char *buf, const char *appended, size_t *pcap) { char c; char *buf0 = buf; size_t cap = *pcap; if (buf0 == NULL) return NULL; if (appended == NULL || appended[0] == 0) return buf0; if (*pcap < strlen(appended)+1) { return NULL; } while (cap > 1 && 0 != (c = *appended++)) { *buf++ = c; cap--; } assert(cap > 0); *pcap = cap; *buf = 0; return buf; } bool append_realloc(char **head, size_t *cap, const char *appended) { if (!head) return NULL; if (!*head) return NULL; if (!cap) return NULL; if (!appended) return NULL; size_t cursize = strlen(*head); size_t needed = strlen(appended) + 1; size_t remains = *cap - cursize; if (remains < needed) { size_t need_extra = needed - remains; size_t newsize = *cap + need_extra; char *new = realloc(*head, newsize); if (!new) return false; *head = new; *cap = newsize; } strcpy(*head + cursize, appended); return true; } bool fd_is_valid(int fd) { return fcntl(fd, F_GETFD) != -1 || errno != EBADF; } bool parse_boolean_arg(const char *str) { if (0 == strcasecmp(str, "on")) return true; if (0 == strcmp(str, "1")) return true; if (0 == strcasecmp(str, "yes")) return true; if (0 == strcasecmp(str, "enable")) return true; if (0 == strcasecmp(str, "en")) return true; if (0 == strcasecmp(str, "y")) return true; if (0 == strcasecmp(str, "a")) return true; return false; } bool parse_boolean_arg_false(const char *str) { if (0 == strcasecmp(str, "off")) return true; if (0 == strcmp(str, "0")) return true; if (0 == strcasecmp(str, "no")) return true; if (0 == strcasecmp(str, "disable")) return true; if (0 == strcasecmp(str, "dis")) return true; if (0 == strcasecmp(str, "n")) return true; return false; }