]>
Commit | Line | Data |
---|---|---|
db137867 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * tools.c: Various functions needed here and there. | |
4 | * | |
5 | * Copyright (C) 1996-2002 Hybrid Development Team | |
6 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
21 | * USA | |
22 | * | |
23 | * $Id: tools.c 25040 2008-01-23 16:11:34Z androsyn $ | |
24 | * | |
25 | * Here is the original header: | |
26 | * | |
27 | * Useful stuff, ripped from places .. | |
28 | * adrian chadd <adrian@creative.net.au> | |
29 | * | |
30 | * The TOOLS_C define builds versions of the functions in tools.h | |
31 | * so that they end up in the resulting object files. If its not | |
32 | * defined, tools.h will build inlined versions of the functions | |
33 | * on supported compilers | |
34 | */ | |
35 | #define _GNU_SOURCE 1 | |
36 | #include <libratbox_config.h> | |
37 | #include <ratbox_lib.h> | |
38 | #include <rb_tools.h> | |
39 | ||
40 | ||
41 | /* | |
42 | * init_rb_dlink_nodes | |
43 | * | |
44 | */ | |
45 | static rb_bh *dnode_heap; | |
46 | void | |
47 | rb_init_rb_dlink_nodes(size_t dh_size) | |
48 | { | |
49 | ||
50 | dnode_heap = rb_bh_create(sizeof(rb_dlink_node), dh_size, "librb_dnode_heap"); | |
51 | if(dnode_heap == NULL) | |
52 | rb_outofmemory(); | |
53 | } | |
54 | ||
55 | /* | |
56 | * make_rb_dlink_node | |
57 | * | |
58 | * inputs - NONE | |
59 | * output - pointer to new rb_dlink_node | |
60 | * side effects - NONE | |
61 | */ | |
62 | rb_dlink_node * | |
63 | rb_make_rb_dlink_node(void) | |
64 | { | |
65 | return(rb_bh_alloc(dnode_heap)); | |
66 | } | |
67 | ||
68 | /* | |
69 | * free_rb_dlink_node | |
70 | * | |
71 | * inputs - pointer to rb_dlink_node | |
72 | * output - NONE | |
73 | * side effects - free given rb_dlink_node | |
74 | */ | |
75 | void | |
76 | rb_free_rb_dlink_node(rb_dlink_node * ptr) | |
77 | { | |
78 | assert(ptr != NULL); | |
79 | rb_bh_free(dnode_heap, ptr); | |
80 | } | |
81 | ||
82 | /* rb_string_to_array() | |
83 | * Changes a given buffer into an array of parameters. | |
84 | * Taken from ircd-ratbox. | |
85 | * | |
86 | * inputs - string to parse, array to put in | |
87 | * outputs - number of parameters | |
88 | */ | |
89 | int | |
90 | rb_string_to_array(char *string, char **parv, int maxpara) | |
91 | { | |
92 | char *p, *xbuf = string; | |
93 | int x = 0; | |
94 | ||
95 | parv[x] = NULL; | |
96 | ||
97 | if(string == NULL || string[0] == '\0') | |
98 | return x; | |
99 | ||
100 | while (*xbuf == ' ') /* skip leading spaces */ | |
101 | xbuf++; | |
102 | if(*xbuf == '\0') /* ignore all-space args */ | |
103 | return x; | |
104 | ||
105 | do | |
106 | { | |
107 | if(*xbuf == ':') /* Last parameter */ | |
108 | { | |
109 | xbuf++; | |
110 | parv[x++] = xbuf; | |
111 | parv[x] = NULL; | |
112 | return x; | |
113 | } | |
114 | else | |
115 | { | |
116 | parv[x++] = xbuf; | |
117 | parv[x] = NULL; | |
118 | if((p = strchr(xbuf, ' ')) != NULL) | |
119 | { | |
120 | *p++ = '\0'; | |
121 | xbuf = p; | |
122 | } | |
123 | else | |
124 | return x; | |
125 | } | |
126 | while (*xbuf == ' ') | |
127 | xbuf++; | |
128 | if(*xbuf == '\0') | |
129 | return x; | |
130 | } | |
131 | while (x < maxpara - 1); | |
132 | ||
133 | if(*p == ':') | |
134 | p++; | |
135 | ||
136 | parv[x++] = p; | |
137 | parv[x] = NULL; | |
138 | return x; | |
139 | } | |
140 | ||
141 | #ifndef HAVE_STRLCAT | |
142 | size_t | |
143 | rb_strlcat(char *dest, const char *src, size_t count) | |
144 | { | |
145 | size_t dsize = strlen(dest); | |
146 | size_t len = strlen(src); | |
147 | size_t res = dsize + len; | |
148 | ||
149 | dest += dsize; | |
150 | count -= dsize; | |
151 | if (len >= count) | |
152 | len = count-1; | |
153 | memcpy(dest, src, len); | |
154 | dest[len] = 0; | |
155 | return res; | |
156 | } | |
157 | #else | |
158 | size_t | |
159 | rb_strlcat(char *dest, const char *src, size_t count) | |
160 | { | |
161 | return strlcat(dest, src, count); | |
162 | } | |
163 | #endif | |
164 | ||
165 | #ifndef HAVE_STRLCPY | |
166 | size_t | |
167 | rb_strlcpy(char *dest, const char *src, size_t size) | |
168 | { | |
169 | size_t ret = strlen(src); | |
170 | ||
171 | if (size) { | |
172 | size_t len = (ret >= size) ? size-1 : ret; | |
173 | memcpy(dest, src, len); | |
174 | dest[len] = '\0'; | |
175 | } | |
176 | return ret; | |
177 | } | |
178 | #else | |
179 | size_t | |
180 | rb_strlcpy(char *dest, const char *src, size_t size) | |
181 | { | |
182 | return strlcpy(dest, src, size); | |
183 | } | |
184 | #endif | |
185 | ||
186 | ||
187 | #ifndef HAVE_STRNLEN | |
188 | size_t | |
189 | rb_strnlen(const char *s, size_t count) | |
190 | { | |
191 | const char *sc; | |
192 | for (sc = s; count-- && *sc != '\0'; ++sc) | |
193 | ;; | |
194 | return sc - s; | |
195 | } | |
196 | #else | |
197 | size_t | |
198 | rb_strnlen(const char *s, size_t count) | |
199 | { | |
200 | return strnlen(s, count); | |
201 | } | |
202 | #endif | |
203 |