]>
jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/tools.c
2 * ircd-ratbox: A slightly useful ircd.
3 * tools.c: Various functions needed here and there.
5 * Copyright (C) 1996-2002 Hybrid Development Team
6 * Copyright (C) 2002-2005 ircd-ratbox development team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 * Here is the original header:
26 * Useful stuff, ripped from places ..
27 * adrian chadd <adrian@creative.net.au>
29 * The TOOLS_C define builds versions of the functions in tools.h
30 * so that they end up in the resulting object files. If its not
31 * defined, tools.h will build inlined versions of the functions
32 * on supported compilers
35 #include <libratbox_config.h>
36 #include <ratbox_lib.h>
44 static rb_bh
*dnode_heap
;
46 rb_init_rb_dlink_nodes(size_t dh_size
)
49 dnode_heap
= rb_bh_create(sizeof(rb_dlink_node
), dh_size
, "librb_dnode_heap");
50 if(dnode_heap
== NULL
)
58 * output - pointer to new rb_dlink_node
62 rb_make_rb_dlink_node(void)
64 return (rb_bh_alloc(dnode_heap
));
70 * inputs - pointer to rb_dlink_node
72 * side effects - free given rb_dlink_node
75 rb_free_rb_dlink_node(rb_dlink_node
*ptr
)
78 rb_bh_free(dnode_heap
, ptr
);
81 /* rb_string_to_array()
82 * Changes a given buffer into an array of parameters.
83 * Taken from ircd-ratbox.
85 * inputs - string to parse, array to put in
86 * outputs - number of parameters
89 rb_string_to_array(char *string
, char **parv
, int maxpara
)
91 char *p
, *xbuf
= string
;
96 if(string
== NULL
|| string
[0] == '\0')
99 while(*xbuf
== ' ') /* skip leading spaces */
101 if(*xbuf
== '\0') /* ignore all-space args */
106 if(*xbuf
== ':') /* Last parameter */
117 if((p
= strchr(xbuf
, ' ')) != NULL
)
130 while(x
< maxpara
- 1);
142 rb_strlcat(char *dest
, const char *src
, size_t count
)
144 size_t dsize
= strlen(dest
);
145 size_t len
= strlen(src
);
146 size_t res
= dsize
+ len
;
152 memcpy(dest
, src
, len
);
158 rb_strlcat(char *dest
, const char *src
, size_t count
)
160 return strlcat(dest
, src
, count
);
166 rb_strlcpy(char *dest
, const char *src
, size_t size
)
168 size_t ret
= strlen(src
);
172 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
173 memcpy(dest
, src
, len
);
180 rb_strlcpy(char *dest
, const char *src
, size_t size
)
182 return strlcpy(dest
, src
, size
);
189 rb_strnlen(const char *s
, size_t count
)
192 for(sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
198 rb_strnlen(const char *s
, size_t count
)
200 return strnlen(s
, count
);
211 rb_basename(const char *path
)
215 if(!(s
= strrchr(path
, '/')))
227 rb_dirname (const char *path
)
231 s
= strrchr(path
, '/');
234 return rb_strdup(".");
237 /* remove extra slashes */
238 while(s
> path
&& *s
== '/')
241 return rb_strndup(path
, ((uintptr_t)s
- (uintptr_t)path
) + 2);