]>
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 | * | |
db137867 AC |
23 | * |
24 | * Here is the original header: | |
25 | * | |
26 | * Useful stuff, ripped from places .. | |
27 | * adrian chadd <adrian@creative.net.au> | |
28 | * | |
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 | |
33 | */ | |
c056dba2 | 34 | |
db137867 | 35 | #define _GNU_SOURCE 1 |
fe037171 EM |
36 | #include <librb_config.h> |
37 | #include <rb_lib.h> | |
db137867 AC |
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 | { | |
3202e249 | 65 | return (rb_bh_alloc(dnode_heap)); |
db137867 AC |
66 | } |
67 | ||
68 | /* | |
69 | * free_rb_dlink_node | |
70 | * | |
71 | * inputs - pointer to rb_dlink_node | |
72 | * output - NONE | |
55abcbb2 | 73 | * side effects - free given rb_dlink_node |
db137867 AC |
74 | */ |
75 | void | |
3202e249 | 76 | rb_free_rb_dlink_node(rb_dlink_node *ptr) |
db137867 AC |
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 | ||
3202e249 | 100 | while(*xbuf == ' ') /* skip leading spaces */ |
db137867 AC |
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 | } | |
3202e249 | 126 | while(*xbuf == ' ') |
db137867 AC |
127 | xbuf++; |
128 | if(*xbuf == '\0') | |
129 | return x; | |
130 | } | |
3202e249 | 131 | while(x < maxpara - 1); |
db137867 AC |
132 | |
133 | if(*p == ':') | |
134 | p++; | |
135 | ||
136 | parv[x++] = p; | |
137 | parv[x] = NULL; | |
138 | return x; | |
139 | } | |
140 | ||
141 | #ifndef HAVE_STRLCAT | |
3202e249 | 142 | size_t |
db137867 AC |
143 | rb_strlcat(char *dest, const char *src, size_t count) |
144 | { | |
3202e249 VY |
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; | |
db137867 AC |
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 | |
3202e249 | 164 | |
db137867 | 165 | #ifndef HAVE_STRLCPY |
3202e249 | 166 | size_t |
db137867 AC |
167 | rb_strlcpy(char *dest, const char *src, size_t size) |
168 | { | |
3202e249 VY |
169 | size_t ret = strlen(src); |
170 | ||
171 | if(size) | |
172 | { | |
173 | size_t len = (ret >= size) ? size - 1 : ret; | |
174 | memcpy(dest, src, len); | |
175 | dest[len] = '\0'; | |
176 | } | |
177 | return ret; | |
db137867 AC |
178 | } |
179 | #else | |
180 | size_t | |
181 | rb_strlcpy(char *dest, const char *src, size_t size) | |
182 | { | |
183 | return strlcpy(dest, src, size); | |
184 | } | |
185 | #endif | |
186 | ||
187 | ||
188 | #ifndef HAVE_STRNLEN | |
3202e249 | 189 | size_t |
db137867 | 190 | rb_strnlen(const char *s, size_t count) |
3202e249 | 191 | { |
db137867 | 192 | const char *sc; |
3202e249 | 193 | for(sc = s; count-- && *sc != '\0'; ++sc) |
5ef68b13 | 194 | ; |
db137867 | 195 | return sc - s; |
3202e249 | 196 | } |
db137867 AC |
197 | #else |
198 | size_t | |
199 | rb_strnlen(const char *s, size_t count) | |
200 | { | |
201 | return strnlen(s, count); | |
202 | } | |
203 | #endif | |
204 | ||
5203cba5 VI |
205 | /* |
206 | * rb_snprintf_append() | |
207 | * appends snprintf formatted string to the end of the buffer but not | |
208 | * exceeding len | |
209 | */ | |
210 | int | |
211 | rb_snprintf_append(char *str, size_t len, const char *format, ...) | |
212 | { | |
5203cba5 VI |
213 | if(len == 0) |
214 | return 0; | |
215 | ||
e23126c8 | 216 | size_t x = strlen(str); |
5203cba5 VI |
217 | |
218 | if(len < x) | |
219 | { | |
220 | str[len - 1] = '\0'; | |
221 | return (int)len - 1; | |
222 | } | |
223 | ||
224 | va_list ap; | |
225 | va_start(ap, format); | |
e23126c8 | 226 | int y = (vsnprintf(str + x, len - x, format, ap) + (int)x); |
5203cba5 VI |
227 | va_end(ap); |
228 | ||
e23126c8 | 229 | return (y); |
5203cba5 VI |
230 | } |
231 | ||
3202e249 VY |
232 | /* rb_basename |
233 | * | |
234 | * input - | |
235 | * output - | |
55abcbb2 | 236 | * side effects - |
3202e249 VY |
237 | */ |
238 | char * | |
239 | rb_basename(const char *path) | |
240 | { | |
241 | const char *s; | |
242 | ||
243 | if(!(s = strrchr(path, '/'))) | |
244 | s = path; | |
245 | else | |
246 | s++; | |
247 | return rb_strdup(s); | |
248 | } | |
249 | ||
250 | /* | |
251 | * rb_dirname | |
252 | */ | |
253 | ||
254 | char * | |
255 | rb_dirname (const char *path) | |
256 | { | |
257 | char *s; | |
258 | ||
259 | s = strrchr(path, '/'); | |
260 | if(s == NULL) | |
261 | { | |
262 | return rb_strdup("."); | |
55abcbb2 | 263 | } |
3202e249 VY |
264 | |
265 | /* remove extra slashes */ | |
266 | while(s > path && *s == '/') | |
267 | --s; | |
268 | ||
55abcbb2 | 269 | return rb_strndup(path, ((uintptr_t)s - (uintptr_t)path) + 2); |
3202e249 | 270 | } |
5225f83d AC |
271 | |
272 | size_t rb_zstring_serialized(rb_zstring_t *zs, void **buf, size_t *buflen) | |
273 | { | |
274 | uint8_t *p; | |
275 | size_t alloclen = sizeof(uint16_t) + zs->len; | |
276 | ||
277 | p = rb_malloc(sizeof(alloclen)); | |
278 | memcpy(p, &zs->len, sizeof(uint16_t)); | |
279 | p += sizeof(uint16_t); | |
280 | memcpy(p, zs->data, zs->len); | |
281 | return alloclen; | |
282 | } | |
283 | ||
284 | size_t rb_zstring_deserialize(rb_zstring_t *zs, void *buf) | |
285 | { | |
286 | uint8_t *p = (uint8_t *)buf; | |
287 | ||
288 | memcpy(&zs->len, p, sizeof(uint16_t)); | |
289 | p += sizeof(uint16_t); | |
290 | if(zs->len == 0) | |
291 | { | |
292 | zs->data = NULL; | |
293 | return sizeof(uint16_t); | |
294 | } | |
295 | zs->data = rb_malloc(zs->len); | |
296 | memcpy(zs->data, p, zs->len); | |
297 | return zs->len + sizeof(uint16_t); | |
298 | } | |
299 | ||
300 | void rb_zstring_free(rb_zstring_t *zs) | |
301 | { | |
302 | rb_free(zs->data); | |
303 | rb_free(zs); | |
304 | ||
305 | } | |
306 | ||
307 | rb_zstring_t *rb_zstring_alloc(void) | |
308 | { | |
309 | rb_zstring_t *zs = rb_malloc(sizeof(rb_zstring_t)); | |
310 | return zs; | |
311 | } | |
312 | ||
313 | rb_zstring_t *rb_zstring_from_c_len(const char *buf, size_t len) | |
314 | { | |
315 | rb_zstring_t *zs; | |
316 | ||
317 | if(len > UINT16_MAX-1) | |
318 | return NULL; | |
319 | ||
320 | zs = rb_zstring_alloc(); | |
321 | zs->alloclen = zs->len = (uint16_t)len; | |
322 | zs->alloclen = (uint16_t)len; | |
323 | if(zs->alloclen < 128) | |
324 | zs->alloclen = 128; | |
325 | zs->data = rb_malloc(zs->alloclen); | |
326 | memcpy(zs->data, buf, zs->len); | |
327 | return(zs); | |
328 | } | |
329 | ||
330 | rb_zstring_t *rb_zstring_from_c(const char *buf) | |
331 | { | |
332 | return rb_zstring_from_c_len(buf, strlen(buf)); | |
333 | } | |
334 | ||
335 | size_t rb_zstring_len(rb_zstring_t *zs) | |
336 | { | |
337 | return zs->len; | |
338 | } | |
339 | ||
340 | void rb_zstring_append_from_zstring(rb_zstring_t *dst_zs, rb_zstring_t *src_zs) | |
341 | { | |
342 | void *ep; | |
343 | size_t nlen = dst_zs->len + src_zs->len; | |
344 | ||
345 | if(nlen > dst_zs->alloclen) | |
346 | { | |
347 | dst_zs->alloclen += src_zs->len + 64; | |
348 | dst_zs->data = rb_realloc(dst_zs->data, dst_zs->alloclen); | |
349 | } | |
350 | ||
351 | ep = dst_zs->data + dst_zs->len; | |
352 | memcpy(ep, src_zs->data, src_zs->len); | |
353 | } | |
354 | ||
355 | void rb_zstring_append_from_c(rb_zstring_t *zs, const char *buf, size_t len) | |
356 | { | |
357 | void *ep; | |
358 | size_t nlen = zs->len + len; | |
359 | ||
360 | if(nlen > zs->alloclen) | |
361 | { | |
362 | zs->alloclen += len + 64; | |
363 | zs->data = rb_realloc(zs->data, zs->alloclen); | |
364 | } | |
365 | ep = zs->data + zs->len; | |
366 | zs->len += len; | |
367 | memcpy(ep, buf, len); | |
368 | } | |
369 | ||
370 | char *rb_zstring_to_c(rb_zstring_t *zs, char *buf, size_t len) | |
371 | { | |
372 | size_t cpylen; | |
373 | if(len < zs->len) | |
374 | cpylen = len - 1; | |
375 | else | |
376 | cpylen = zs->len; | |
377 | buf[cpylen] = '\0'; | |
378 | memcpy(buf, zs->data, cpylen); | |
379 | return buf; | |
380 | } | |
381 | ||
382 | ||
383 | char *rb_zstring_to_c_alloc(rb_zstring_t *zs) | |
384 | { | |
385 | char *p; | |
386 | p = rb_malloc(zs->len+1); | |
387 | memcpy(p, zs->data, zs->len); | |
388 | return p; | |
389 | } | |
390 | ||
391 | size_t rb_zstring_to_ptr(rb_zstring_t *zs, void **ptr) | |
392 | { | |
393 | *ptr = (void *)zs->data; | |
394 | return zs->len; | |
395 | } |