2008-04-01 16:52:26 +00:00
|
|
|
/*
|
|
|
|
* ircd-ratbox: A slightly useful ircd.
|
|
|
|
* tools.h: Header for the various tool functions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
|
|
|
|
* Copyright (C) 1996-2002 Hybrid Development Team
|
|
|
|
* Copyright (C) 2002-2005 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RB_LIB_H
|
2008-12-02 23:49:39 +00:00
|
|
|
# error "Do not use tools.h directly"
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __TOOLS_H__
|
|
|
|
#define __TOOLS_H__
|
|
|
|
|
|
|
|
size_t rb_strlcpy(char *dst, const char *src, size_t siz);
|
|
|
|
size_t rb_strlcat(char *dst, const char *src, size_t siz);
|
|
|
|
size_t rb_strnlen(const char *s, size_t count);
|
2016-02-10 01:25:32 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
int rb_snprintf_append(char *str, size_t len, const char *format, ...)
|
|
|
|
__attribute__ ((format(printf, 3, 4)));
|
|
|
|
#else
|
|
|
|
int rb_snprintf_append(char *str, const size_t size, const char *, ...);
|
|
|
|
#endif
|
|
|
|
|
2008-12-02 23:49:39 +00:00
|
|
|
char *rb_basename(const char *);
|
|
|
|
char *rb_dirname(const char *);
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
int rb_string_to_array(char *string, char **parv, int maxpara);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* double-linked-list stuff
|
|
|
|
*/
|
|
|
|
typedef struct _rb_dlink_node rb_dlink_node;
|
|
|
|
typedef struct _rb_dlink_list rb_dlink_list;
|
|
|
|
|
|
|
|
struct _rb_dlink_node
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
rb_dlink_node *prev;
|
|
|
|
rb_dlink_node *next;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _rb_dlink_list
|
|
|
|
{
|
|
|
|
rb_dlink_node *head;
|
|
|
|
rb_dlink_node *tail;
|
|
|
|
unsigned long length;
|
|
|
|
};
|
|
|
|
|
|
|
|
rb_dlink_node *rb_make_rb_dlink_node(void);
|
2008-12-02 23:49:39 +00:00
|
|
|
void rb_free_rb_dlink_node(rb_dlink_node *lp);
|
2008-04-01 16:52:26 +00:00
|
|
|
void rb_init_rb_dlink_nodes(size_t dh_size);
|
|
|
|
|
|
|
|
/* This macros are basically swiped from the linux kernel
|
|
|
|
* they are simple yet effective
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-03-03 04:25:47 +00:00
|
|
|
* Walks forward of a list.
|
2008-04-01 16:52:26 +00:00
|
|
|
* pos is your node
|
|
|
|
* head is your list head
|
|
|
|
*/
|
|
|
|
#define RB_DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next)
|
|
|
|
|
|
|
|
/*
|
2014-03-03 04:25:47 +00:00
|
|
|
* Walks forward of a list safely while removing nodes
|
2008-04-01 16:52:26 +00:00
|
|
|
* pos is your node
|
|
|
|
* n is another list head for temporary storage
|
|
|
|
* head is your list head
|
|
|
|
*/
|
|
|
|
#define RB_DLINK_FOREACH_SAFE(pos, n, head) for (pos = (head), n = pos ? pos->next : NULL; pos != NULL; pos = n, n = pos ? pos->next : NULL)
|
|
|
|
|
|
|
|
#define RB_DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev)
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns the list length */
|
|
|
|
#define rb_dlink_list_length(list) (list)->length
|
|
|
|
|
|
|
|
#define rb_dlinkAddAlloc(data, list) rb_dlinkAdd(data, rb_make_rb_dlink_node(), list)
|
|
|
|
#define rb_dlinkAddTailAlloc(data, list) rb_dlinkAddTail(data, rb_make_rb_dlink_node(), list)
|
|
|
|
#define rb_dlinkDestroy(node, list) do { rb_dlinkDelete(node, list); rb_free_rb_dlink_node(node); } while(0)
|
|
|
|
|
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
/*
|
2008-04-01 16:52:26 +00:00
|
|
|
* dlink_ routines are stolen from squid, except for rb_dlinkAddBefore,
|
|
|
|
* which is mine.
|
|
|
|
* -- adrian
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkMoveNode(rb_dlink_node *m, rb_dlink_list *oldlist, rb_dlink_list *newlist)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
/* Assumption: If m->next == NULL, then list->tail == m
|
|
|
|
* and: If m->prev == NULL, then list->head == m
|
|
|
|
*/
|
|
|
|
assert(m != NULL);
|
|
|
|
assert(oldlist != NULL);
|
|
|
|
assert(newlist != NULL);
|
|
|
|
|
|
|
|
if(m->next)
|
|
|
|
m->next->prev = m->prev;
|
|
|
|
else
|
|
|
|
oldlist->tail = m->prev;
|
|
|
|
|
|
|
|
if(m->prev)
|
|
|
|
m->prev->next = m->next;
|
|
|
|
else
|
|
|
|
oldlist->head = m->next;
|
|
|
|
|
|
|
|
m->prev = NULL;
|
|
|
|
m->next = newlist->head;
|
|
|
|
if(newlist->head != NULL)
|
|
|
|
newlist->head->prev = m;
|
|
|
|
else if(newlist->tail == NULL)
|
|
|
|
newlist->tail = m;
|
|
|
|
newlist->head = m;
|
|
|
|
|
|
|
|
oldlist->length--;
|
|
|
|
newlist->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkAdd(void *data, rb_dlink_node *m, rb_dlink_list *list)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(m != NULL);
|
|
|
|
assert(list != NULL);
|
|
|
|
|
|
|
|
m->data = data;
|
|
|
|
m->prev = NULL;
|
|
|
|
m->next = list->head;
|
|
|
|
|
|
|
|
/* Assumption: If list->tail != NULL, list->head != NULL */
|
|
|
|
if(list->head != NULL)
|
|
|
|
list->head->prev = m;
|
|
|
|
else if(list->tail == NULL)
|
|
|
|
list->tail = m;
|
|
|
|
|
|
|
|
list->head = m;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkAddBefore(rb_dlink_node *b, void *data, rb_dlink_node *m, rb_dlink_list *list)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
assert(b != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(m != NULL);
|
|
|
|
assert(list != NULL);
|
|
|
|
|
|
|
|
/* Shortcut - if its the first one, call rb_dlinkAdd only */
|
|
|
|
if(b == list->head)
|
|
|
|
{
|
|
|
|
rb_dlinkAdd(data, m, list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m->data = data;
|
|
|
|
b->prev->next = m;
|
|
|
|
m->prev = b->prev;
|
|
|
|
b->prev = m;
|
|
|
|
m->next = b;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
rb_dlinkMoveTail(rb_dlink_node *m, rb_dlink_list *list)
|
|
|
|
{
|
|
|
|
if(list->tail == m)
|
|
|
|
return;
|
2008-12-02 23:49:39 +00:00
|
|
|
|
2014-03-03 04:25:47 +00:00
|
|
|
/* From here assume that m->next != NULL as that can only
|
2008-04-01 16:52:26 +00:00
|
|
|
* be at the tail and assume that the node is on the list
|
|
|
|
*/
|
2008-12-02 23:49:39 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
m->next->prev = m->prev;
|
|
|
|
|
|
|
|
if(m->prev != NULL)
|
|
|
|
m->prev->next = m->next;
|
|
|
|
else
|
|
|
|
list->head = m->next;
|
|
|
|
|
|
|
|
list->tail->next = m;
|
|
|
|
m->prev = list->tail;
|
|
|
|
m->next = NULL;
|
|
|
|
list->tail = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkAddTail(void *data, rb_dlink_node *m, rb_dlink_list *list)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
assert(m != NULL);
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
m->data = data;
|
|
|
|
m->next = NULL;
|
|
|
|
m->prev = list->tail;
|
|
|
|
|
|
|
|
/* Assumption: If list->tail != NULL, list->head != NULL */
|
|
|
|
if(list->tail != NULL)
|
|
|
|
list->tail->next = m;
|
|
|
|
else if(list->head == NULL)
|
|
|
|
list->head = m;
|
|
|
|
|
|
|
|
list->tail = m;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execution profiles show that this function is called the most
|
|
|
|
* often of all non-spontaneous functions. So it had better be
|
|
|
|
* efficient. */
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkDelete(rb_dlink_node *m, rb_dlink_list *list)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
assert(m != NULL);
|
|
|
|
assert(list != NULL);
|
|
|
|
/* Assumption: If m->next == NULL, then list->tail == m
|
|
|
|
* and: If m->prev == NULL, then list->head == m
|
|
|
|
*/
|
|
|
|
if(m->next)
|
|
|
|
m->next->prev = m->prev;
|
|
|
|
else
|
|
|
|
list->tail = m->prev;
|
|
|
|
|
|
|
|
if(m->prev)
|
|
|
|
m->prev->next = m->next;
|
|
|
|
else
|
|
|
|
list->head = m->next;
|
|
|
|
|
|
|
|
m->next = m->prev = NULL;
|
|
|
|
list->length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline rb_dlink_node *
|
|
|
|
rb_dlinkFindDelete(void *data, rb_dlink_list *list)
|
|
|
|
{
|
|
|
|
rb_dlink_node *m;
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
RB_DLINK_FOREACH(m, list->head)
|
|
|
|
{
|
|
|
|
if(m->data != data)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(m->next)
|
|
|
|
m->next->prev = m->prev;
|
|
|
|
else
|
|
|
|
list->tail = m->prev;
|
|
|
|
|
|
|
|
if(m->prev)
|
|
|
|
m->prev->next = m->next;
|
|
|
|
else
|
|
|
|
list->head = m->next;
|
|
|
|
|
|
|
|
m->next = m->prev = NULL;
|
|
|
|
list->length--;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
rb_dlinkFindDestroy(void *data, rb_dlink_list *list)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
ptr = rb_dlinkFindDelete(data, list);
|
|
|
|
|
|
|
|
if(ptr != NULL)
|
|
|
|
{
|
|
|
|
rb_free_rb_dlink_node(ptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rb_dlinkFind
|
2014-03-03 04:25:47 +00:00
|
|
|
* inputs - list to search
|
2008-04-01 16:52:26 +00:00
|
|
|
* - data
|
|
|
|
* output - pointer to link or NULL if not found
|
|
|
|
* side effects - Look for ptr in the linked listed pointed to by link.
|
|
|
|
*/
|
|
|
|
static inline rb_dlink_node *
|
|
|
|
rb_dlinkFind(void *data, rb_dlink_list *list)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
2008-12-02 23:49:39 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
RB_DLINK_FOREACH(ptr, list->head)
|
|
|
|
{
|
|
|
|
if(ptr->data == data)
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2008-12-02 23:49:39 +00:00
|
|
|
rb_dlinkMoveList(rb_dlink_list *from, rb_dlink_list *to)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
assert(from != NULL);
|
|
|
|
assert(to != NULL);
|
|
|
|
|
|
|
|
/* There are three cases */
|
|
|
|
/* case one, nothing in from list */
|
|
|
|
if(from->head == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* case two, nothing in to list */
|
|
|
|
if(to->head == NULL)
|
|
|
|
{
|
|
|
|
to->head = from->head;
|
|
|
|
to->tail = from->tail;
|
|
|
|
from->head = from->tail = NULL;
|
|
|
|
to->length = from->length;
|
|
|
|
from->length = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* third case play with the links */
|
|
|
|
from->tail->next = to->head;
|
|
|
|
to->head->prev = from->tail;
|
|
|
|
to->head = from->head;
|
|
|
|
from->head = from->tail = NULL;
|
|
|
|
to->length += from->length;
|
|
|
|
from->length = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-11 21:56:33 +00:00
|
|
|
typedef struct _rb_zstring
|
|
|
|
{
|
|
|
|
uint16_t len; /* big enough */
|
|
|
|
uint16_t alloclen;
|
|
|
|
uint8_t *data;
|
|
|
|
} rb_zstring_t;
|
|
|
|
|
|
|
|
size_t rb_zstring_serialized(rb_zstring_t *zs, void **buf, size_t *buflen);
|
|
|
|
size_t rb_zstring_deserialize(rb_zstring_t *zs, void *buf);
|
|
|
|
void rb_zstring_free(rb_zstring_t *zs);
|
|
|
|
rb_zstring_t *rb_zstring_alloc(void);
|
|
|
|
rb_zstring_t *rb_zstring_from_c_len(const char *buf, size_t len);
|
|
|
|
rb_zstring_t *rb_zstring_from_c(const char *buf);
|
|
|
|
size_t rb_zstring_len(rb_zstring_t *zs);
|
|
|
|
void rb_zstring_append_from_zstring(rb_zstring_t *dst_zs, rb_zstring_t *src_zs);
|
|
|
|
void rb_zstring_append_from_c(rb_zstring_t *zs, const char *buf, size_t len);
|
|
|
|
char *rb_zstring_to_c(rb_zstring_t *zs, char *buf, size_t len);
|
|
|
|
char *rb_zstring_to_c_alloc(rb_zstring_t *zs);
|
|
|
|
size_t rb_zstring_to_ptr(rb_zstring_t *zs, void **ptr);
|
2016-03-22 02:07:53 +00:00
|
|
|
const char *rb_path_to_self(void);
|
2015-12-11 21:56:33 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif /* __TOOLS_H__ */
|