AmendHub

Download:

jcs

/

wallops

/

amendments

/

45

util: Add SLIST macros from OpenBSD


jcs made amendment 45 about 1 year ago
--- util.h Fri Nov 11 16:57:46 2022 +++ util.h Mon Jan 9 15:46:52 2023 @@ -79,6 +79,66 @@ typedef unsigned long u_int32_t; #define BYTE_ORDER BIG_ENDIAN +/* singly-linked lists */ +#define SLIST_HEAD(name, type) struct name { struct type *slh_first; } +#define SLIST_HEAD_INITIALIZER(head) { NULL } +#define SLIST_ENTRY(type) struct { struct type *sle_next; } +#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_END(head) NULL +#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) +#define SLIST_INIT(head) do { SLIST_FIRST((head)) = NULL; } while (0) +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST(head); \ + (var) != SLIST_END(head); \ + (var) = SLIST_NEXT(var, field)) + +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST(head); \ + (var) && ((tvar) = SLIST_NEXT(var, field), 1); \ + (var) = (tvar)) + +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + (elm)->field.sle_next = (slistelm)->field.sle_next; \ + (slistelm)->field.sle_next = (elm); \ +} while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + (elm)->field.sle_next = (head)->slh_first; \ + (head)->slh_first = (elm); \ +} while (0) + +#define SLIST_APPEND(head, elm, type, field) do { \ + if (SLIST_EMPTY(head)) { \ + SLIST_INSERT_HEAD(head, elm, field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + while (curelm->field.sle_next != NULL) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = (elm); \ + } \ +} while (0) + +#define SLIST_REMOVE_AFTER(elm, field) do { \ + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE_HEAD(head, field) do { \ + (head)->slh_first = (head)->slh_first->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + if ((head)->slh_first == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + while (curelm->field.sle_next != (elm)) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \ + } \ + (elm)->field.sle_next = NULL; \ +} while (0) + typedef struct { short push[2], rts; void *addr; @@ -169,5 +229,8 @@ char * strndup(const char *str, size_t maxlen); char * strsep(char **stringp, const char *delim); int snprintf(char *s, size_t size, const char *fmt, ...); int vsnprintf(char *s, size_t size, const char *fmt, void *p); + +short strcasecmp(const char *s1, const char *s2); +short strncasecmp(const char *s1, const char *s2, size_t n); #endif