]>
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 | * | |
33ded5fc | 86 | * inputs - string to parse, array to put in (size >= maxpara) |
db137867 AC |
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 | ||
db137867 AC |
95 | if(string == NULL || string[0] == '\0') |
96 | return x; | |
97 | ||
3202e249 | 98 | while(*xbuf == ' ') /* skip leading spaces */ |
db137867 AC |
99 | xbuf++; |
100 | if(*xbuf == '\0') /* ignore all-space args */ | |
101 | return x; | |
102 | ||
103 | do | |
104 | { | |
105 | if(*xbuf == ':') /* Last parameter */ | |
106 | { | |
107 | xbuf++; | |
108 | parv[x++] = xbuf; | |
db137867 AC |
109 | return x; |
110 | } | |
111 | else | |
112 | { | |
113 | parv[x++] = xbuf; | |
db137867 AC |
114 | if((p = strchr(xbuf, ' ')) != NULL) |
115 | { | |
116 | *p++ = '\0'; | |
117 | xbuf = p; | |
118 | } | |
119 | else | |
120 | return x; | |
121 | } | |
3202e249 | 122 | while(*xbuf == ' ') |
db137867 AC |
123 | xbuf++; |
124 | if(*xbuf == '\0') | |
125 | return x; | |
126 | } | |
3202e249 | 127 | while(x < maxpara - 1); |
db137867 AC |
128 | |
129 | if(*p == ':') | |
130 | p++; | |
131 | ||
132 | parv[x++] = p; | |
db137867 AC |
133 | return x; |
134 | } | |
135 | ||
508e25a8 EM |
136 | #ifndef HAVE_STRCASECMP |
137 | #ifndef _WIN32 | |
8b813d30 | 138 | /* Fallback taken from FreeBSD. --Elizafox */ |
508e25a8 EM |
139 | int |
140 | rb_strcasecmp(const char *s1, const char *s2) | |
141 | { | |
5a775221 | 142 | const unsigned char *us1 = (const unsigned char *)s1; |
8b813d30 EM |
143 | const unsigned char *us2 = (const unsigned char *)s2; |
144 | ||
145 | while (tolower(*us1) == tolower(*us2++)) | |
508e25a8 | 146 | { |
8b813d30 EM |
147 | if (*us1++ == '\0') |
148 | return 0; | |
508e25a8 EM |
149 | } |
150 | ||
8b813d30 | 151 | return (tolower(*us1) - tolower(*--us2)); |
508e25a8 EM |
152 | } |
153 | #else /* _WIN32 */ | |
154 | int | |
155 | rb_strcasecmp(const char *s1, const char *s2) | |
156 | { | |
157 | return stricmp(s1, s2); | |
158 | } | |
159 | #endif /* _WIN32 */ | |
160 | #else /* HAVE_STRCASECMP */ | |
161 | int | |
162 | rb_strcasecmp(const char *s1, const char *s2) | |
163 | { | |
164 | return strcasecmp(s1, s2); | |
165 | } | |
166 | #endif | |
167 | ||
168 | #ifndef HAVE_STRNCASECMP | |
169 | #ifndef _WIN32 | |
8b813d30 | 170 | /* Fallback taken from FreeBSD. --Elizafox */ |
508e25a8 | 171 | int |
c9b6f583 | 172 | rb_strncasecmp(const char *s1, const char *s2, size_t n) |
508e25a8 | 173 | { |
8b813d30 | 174 | if (n != 0) |
508e25a8 | 175 | { |
8b813d30 EM |
176 | const unsigned char *us1 = (const unsigned char *)s1; |
177 | const unsigned char *us2 = (const unsigned char *)s2; | |
508e25a8 | 178 | |
8b813d30 EM |
179 | do |
180 | { | |
181 | if (tolower(*us1) != tolower(*us2++)) | |
182 | return (tolower(*us1) - tolower(*--us2)); | |
183 | if (*us1++ == '\0') | |
184 | break; | |
185 | } while (--n != 0); | |
186 | } | |
187 | return 0; | |
508e25a8 EM |
188 | } |
189 | #else /* _WIN32 */ | |
190 | int | |
191 | rb_strncasecmp(const char *s1, const char *s2, size_t n) | |
192 | { | |
193 | return strnicmp(s1, s2, n); | |
194 | } | |
195 | #endif /* _WIN32 */ | |
196 | #else /* HAVE_STRNCASECMP */ | |
197 | int | |
198 | rb_strncasecmp(const char *s1, const char *s2, size_t n) | |
199 | { | |
200 | return strncasecmp(s1, s2, n); | |
201 | } | |
202 | #endif | |
203 | ||
204 | #ifndef HAVE_STRCASESTR | |
8b813d30 | 205 | /* Fallback taken from FreeBSD. --Elizafox */ |
508e25a8 | 206 | char * |
ea3168ff | 207 | rb_strcasestr(const char *s, const char *find) |
508e25a8 | 208 | { |
8b813d30 EM |
209 | char c, sc; |
210 | size_t len; | |
508e25a8 | 211 | |
8b813d30 EM |
212 | if ((c = *find++) != 0) { |
213 | c = tolower((unsigned char)c); | |
214 | len = strlen(find); | |
215 | do { | |
216 | do { | |
217 | if ((sc = *s++) == 0) | |
218 | return (NULL); | |
219 | } while ((char)tolower((unsigned char)sc) != c); | |
220 | } while (rb_strncasecmp(s, find, len) != 0); | |
221 | s--; | |
508e25a8 | 222 | } |
8b813d30 | 223 | return ((char *)s); |
508e25a8 EM |
224 | } |
225 | #else | |
226 | char * | |
227 | rb_strcasestr(const char *s, const char *find) | |
228 | { | |
229 | return strcasestr(s, find); | |
230 | } | |
231 | #endif | |
232 | ||
db137867 | 233 | #ifndef HAVE_STRLCAT |
3202e249 | 234 | size_t |
db137867 AC |
235 | rb_strlcat(char *dest, const char *src, size_t count) |
236 | { | |
3202e249 VY |
237 | size_t dsize = strlen(dest); |
238 | size_t len = strlen(src); | |
239 | size_t res = dsize + len; | |
240 | ||
241 | dest += dsize; | |
242 | count -= dsize; | |
243 | if(len >= count) | |
244 | len = count - 1; | |
245 | memcpy(dest, src, len); | |
246 | dest[len] = 0; | |
247 | return res; | |
db137867 AC |
248 | } |
249 | #else | |
250 | size_t | |
251 | rb_strlcat(char *dest, const char *src, size_t count) | |
252 | { | |
253 | return strlcat(dest, src, count); | |
254 | } | |
255 | #endif | |
3202e249 | 256 | |
db137867 | 257 | #ifndef HAVE_STRLCPY |
3202e249 | 258 | size_t |
db137867 AC |
259 | rb_strlcpy(char *dest, const char *src, size_t size) |
260 | { | |
3202e249 VY |
261 | size_t ret = strlen(src); |
262 | ||
263 | if(size) | |
264 | { | |
265 | size_t len = (ret >= size) ? size - 1 : ret; | |
266 | memcpy(dest, src, len); | |
267 | dest[len] = '\0'; | |
268 | } | |
269 | return ret; | |
db137867 AC |
270 | } |
271 | #else | |
272 | size_t | |
273 | rb_strlcpy(char *dest, const char *src, size_t size) | |
274 | { | |
275 | return strlcpy(dest, src, size); | |
276 | } | |
277 | #endif | |
278 | ||
279 | ||
280 | #ifndef HAVE_STRNLEN | |
3202e249 | 281 | size_t |
db137867 | 282 | rb_strnlen(const char *s, size_t count) |
3202e249 | 283 | { |
db137867 | 284 | const char *sc; |
3202e249 | 285 | for(sc = s; count-- && *sc != '\0'; ++sc) |
5ef68b13 | 286 | ; |
db137867 | 287 | return sc - s; |
3202e249 | 288 | } |
db137867 AC |
289 | #else |
290 | size_t | |
291 | rb_strnlen(const char *s, size_t count) | |
292 | { | |
293 | return strnlen(s, count); | |
294 | } | |
295 | #endif | |
296 | ||
5203cba5 VI |
297 | /* |
298 | * rb_snprintf_append() | |
299 | * appends snprintf formatted string to the end of the buffer but not | |
300 | * exceeding len | |
301 | */ | |
302 | int | |
303 | rb_snprintf_append(char *str, size_t len, const char *format, ...) | |
304 | { | |
5203cba5 | 305 | if(len == 0) |
8573c340 | 306 | return -1; |
5203cba5 | 307 | |
8573c340 | 308 | int orig_len = strlen(str); |
5203cba5 | 309 | |
8573c340 | 310 | if((int)len < orig_len) |
5203cba5 VI |
311 | { |
312 | str[len - 1] = '\0'; | |
8573c340 | 313 | return len - 1; |
5203cba5 VI |
314 | } |
315 | ||
316 | va_list ap; | |
317 | va_start(ap, format); | |
8573c340 | 318 | int append_len = vsnprintf(str + orig_len, len - orig_len, format, ap); |
5203cba5 VI |
319 | va_end(ap); |
320 | ||
8573c340 SA |
321 | if (append_len < 0) |
322 | return append_len; | |
323 | ||
324 | return (orig_len + append_len); | |
5203cba5 VI |
325 | } |
326 | ||
e5c434a2 SA |
327 | /* |
328 | * rb_snprintf_try_append() | |
329 | * appends snprintf formatted string to the end of the buffer but not | |
330 | * exceeding len | |
331 | * returns -1 if there isn't enough space for the whole string to fit | |
332 | */ | |
333 | int | |
334 | rb_snprintf_try_append(char *str, size_t len, const char *format, ...) | |
335 | { | |
336 | if(len == 0) | |
337 | return -1; | |
338 | ||
339 | int orig_len = strlen(str); | |
340 | ||
341 | if((int)len < orig_len) { | |
342 | str[len - 1] = '\0'; | |
343 | return -1; | |
344 | } | |
345 | ||
346 | va_list ap; | |
347 | va_start(ap, format); | |
348 | int append_len = vsnprintf(str + orig_len, len - orig_len, format, ap); | |
349 | va_end(ap); | |
350 | ||
351 | if (append_len < 0) | |
352 | return append_len; | |
353 | ||
354 | if (orig_len + append_len > (int)(len - 1)) { | |
355 | str[orig_len] = '\0'; | |
356 | return -1; | |
357 | } | |
358 | ||
359 | return (orig_len + append_len); | |
360 | } | |
361 | ||
3202e249 VY |
362 | /* rb_basename |
363 | * | |
364 | * input - | |
365 | * output - | |
55abcbb2 | 366 | * side effects - |
3202e249 VY |
367 | */ |
368 | char * | |
369 | rb_basename(const char *path) | |
370 | { | |
371 | const char *s; | |
372 | ||
373 | if(!(s = strrchr(path, '/'))) | |
374 | s = path; | |
375 | else | |
376 | s++; | |
377 | return rb_strdup(s); | |
378 | } | |
379 | ||
380 | /* | |
381 | * rb_dirname | |
382 | */ | |
383 | ||
384 | char * | |
385 | rb_dirname (const char *path) | |
386 | { | |
387 | char *s; | |
388 | ||
389 | s = strrchr(path, '/'); | |
390 | if(s == NULL) | |
391 | { | |
392 | return rb_strdup("."); | |
55abcbb2 | 393 | } |
3202e249 VY |
394 | |
395 | /* remove extra slashes */ | |
396 | while(s > path && *s == '/') | |
397 | --s; | |
398 | ||
55abcbb2 | 399 | return rb_strndup(path, ((uintptr_t)s - (uintptr_t)path) + 2); |
3202e249 | 400 | } |
5225f83d | 401 | |
4b1cce65 SA |
402 | |
403 | ||
404 | int rb_fsnprint(char *buf, size_t len, const rb_strf_t *strings) | |
405 | { | |
406 | size_t used = 0; | |
407 | size_t remaining = len; | |
408 | ||
409 | while (strings != NULL) { | |
410 | int ret = 0; | |
411 | ||
412 | if (strings->length != 0) { | |
413 | remaining = strings->length; | |
414 | if (remaining > len - used) | |
415 | remaining = len - used; | |
416 | } | |
417 | ||
418 | if (remaining == 0) | |
419 | break; | |
420 | ||
421 | if (strings->format != NULL) { | |
422 | if (strings->format_args != NULL) { | |
423 | ret = vsnprintf(buf + used, remaining, | |
424 | strings->format, *strings->format_args); | |
425 | } else { | |
426 | ret = rb_strlcpy(buf + used, | |
427 | strings->format, remaining); | |
428 | } | |
429 | } else if (strings->func != NULL) { | |
430 | ret = strings->func(buf + used, remaining, | |
431 | strings->func_args); | |
432 | } | |
433 | ||
434 | if (ret < 0) { | |
435 | return ret; | |
436 | } else if ((size_t)ret > remaining - 1) { | |
437 | used += remaining - 1; | |
438 | } else { | |
439 | used += ret; | |
440 | } | |
441 | ||
442 | if (used >= len - 1) { | |
443 | used = len - 1; | |
444 | break; | |
445 | } | |
446 | ||
447 | remaining -= ret; | |
448 | strings = strings->next; | |
449 | } | |
450 | ||
451 | return used; | |
452 | } | |
453 | ||
454 | int rb_fsnprintf(char *buf, size_t len, const rb_strf_t *strings, const char *format, ...) | |
455 | { | |
456 | va_list args; | |
457 | rb_strf_t prepend_string = { .format = format, .format_args = &args, .next = strings }; | |
458 | int ret; | |
459 | ||
460 | va_start(args, format); | |
461 | ret = rb_fsnprint(buf, len, &prepend_string); | |
462 | va_end(args); | |
463 | ||
464 | return ret; | |
465 | } | |
466 |