]>
jfr.im git - solanum.git/blob - librb/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
36 #include <librb_config.h>
45 static rb_bh
*dnode_heap
;
47 rb_init_rb_dlink_nodes(size_t dh_size
)
50 dnode_heap
= rb_bh_create(sizeof(rb_dlink_node
), dh_size
, "librb_dnode_heap");
51 if(dnode_heap
== NULL
)
59 * output - pointer to new rb_dlink_node
63 rb_make_rb_dlink_node(void)
65 return (rb_bh_alloc(dnode_heap
));
71 * inputs - pointer to rb_dlink_node
73 * side effects - free given rb_dlink_node
76 rb_free_rb_dlink_node(rb_dlink_node
*ptr
)
79 rb_bh_free(dnode_heap
, ptr
);
82 /* rb_string_to_array()
83 * Changes a given buffer into an array of parameters.
84 * Taken from ircd-ratbox.
86 * inputs - string to parse, array to put in (size >= maxpara)
87 * outputs - number of parameters
90 rb_string_to_array(char *string
, char **parv
, int maxpara
)
92 char *p
, *xbuf
= string
;
95 if(string
== NULL
|| string
[0] == '\0')
98 while(*xbuf
== ' ') /* skip leading spaces */
100 if(*xbuf
== '\0') /* ignore all-space args */
105 if(*xbuf
== ':') /* Last parameter */
114 if((p
= strchr(xbuf
, ' ')) != NULL
)
127 while(x
< maxpara
- 1);
136 #ifndef HAVE_STRCASECMP
137 /* Fallback taken from FreeBSD. --Elizafox */
139 rb_strcasecmp(const char *s1
, const char *s2
)
141 const unsigned char *us1
= (const unsigned char *)s1
;
142 const unsigned char *us2
= (const unsigned char *)s2
;
144 while (tolower(*us1
) == tolower(*us2
++))
150 return (tolower(*us1
) - tolower(*--us2
));
154 rb_strcasecmp(const char *s1
, const char *s2
)
156 return strcasecmp(s1
, s2
);
160 #ifndef HAVE_STRNCASECMP
161 /* Fallback taken from FreeBSD. --Elizafox */
163 rb_strncasecmp(const char *s1
, const char *s2
, size_t n
)
167 const unsigned char *us1
= (const unsigned char *)s1
;
168 const unsigned char *us2
= (const unsigned char *)s2
;
172 if (tolower(*us1
) != tolower(*us2
++))
173 return (tolower(*us1
) - tolower(*--us2
));
182 rb_strncasecmp(const char *s1
, const char *s2
, size_t n
)
184 return strncasecmp(s1
, s2
, n
);
188 #ifndef HAVE_STRCASESTR
189 /* Fallback taken from FreeBSD. --Elizafox */
191 rb_strcasestr(const char *s
, const char *find
)
196 if ((c
= *find
++) != 0) {
197 c
= tolower((unsigned char)c
);
201 if ((sc
= *s
++) == 0)
203 } while ((char)tolower((unsigned char)sc
) != c
);
204 } while (rb_strncasecmp(s
, find
, len
) != 0);
211 rb_strcasestr(const char *s
, const char *find
)
213 return strcasestr(s
, find
);
219 rb_strlcat(char *dest
, const char *src
, size_t count
)
221 size_t dsize
= strlen(dest
);
222 size_t len
= strlen(src
);
223 size_t res
= dsize
+ len
;
229 memcpy(dest
, src
, len
);
235 rb_strlcat(char *dest
, const char *src
, size_t count
)
237 return strlcat(dest
, src
, count
);
243 rb_strlcpy(char *dest
, const char *src
, size_t size
)
245 size_t ret
= strlen(src
);
249 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
250 memcpy(dest
, src
, len
);
257 rb_strlcpy(char *dest
, const char *src
, size_t size
)
259 return strlcpy(dest
, src
, size
);
266 rb_strnlen(const char *s
, size_t count
)
269 for(sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
275 rb_strnlen(const char *s
, size_t count
)
277 return strnlen(s
, count
);
282 * rb_snprintf_append()
283 * appends snprintf formatted string to the end of the buffer but not
287 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
292 int orig_len
= strlen(str
);
294 if((int)len
< orig_len
)
301 va_start(ap
, format
);
302 int append_len
= vsnprintf(str
+ orig_len
, len
- orig_len
, format
, ap
);
308 return (orig_len
+ append_len
);
312 * rb_snprintf_try_append()
313 * appends snprintf formatted string to the end of the buffer but not
315 * returns -1 if there isn't enough space for the whole string to fit
318 rb_snprintf_try_append(char *str
, size_t len
, const char *format
, ...)
323 int orig_len
= strlen(str
);
325 if((int)len
< orig_len
) {
331 va_start(ap
, format
);
332 int append_len
= vsnprintf(str
+ orig_len
, len
- orig_len
, format
, ap
);
338 if (orig_len
+ append_len
> (int)(len
- 1)) {
339 str
[orig_len
] = '\0';
343 return (orig_len
+ append_len
);
353 rb_basename(const char *path
)
357 if(!(s
= strrchr(path
, '/')))
369 rb_dirname (const char *path
)
373 s
= strrchr(path
, '/');
376 return rb_strdup(".");
379 /* remove extra slashes */
380 while(s
> path
&& *s
== '/')
383 return rb_strndup(path
, ((uintptr_t)s
- (uintptr_t)path
) + 2);
388 int rb_fsnprint(char *buf
, size_t len
, const rb_strf_t
*strings
)
391 size_t remaining
= len
;
393 while (strings
!= NULL
) {
396 if (strings
->length
!= 0) {
397 remaining
= strings
->length
;
398 if (remaining
> len
- used
)
399 remaining
= len
- used
;
405 if (strings
->format
!= NULL
) {
406 if (strings
->format_args
!= NULL
) {
407 ret
= vsnprintf(buf
+ used
, remaining
,
408 strings
->format
, *strings
->format_args
);
410 ret
= rb_strlcpy(buf
+ used
,
411 strings
->format
, remaining
);
413 } else if (strings
->func
!= NULL
) {
414 ret
= strings
->func(buf
+ used
, remaining
,
420 } else if ((size_t)ret
> remaining
- 1) {
421 used
+= remaining
- 1;
426 if (used
>= len
- 1) {
432 strings
= strings
->next
;
438 int rb_fsnprintf(char *buf
, size_t len
, const rb_strf_t
*strings
, const char *format
, ...)
441 rb_strf_t prepend_string
= { .format
= format
, .format_args
= &args
, .next
= strings
};
444 va_start(args
, format
);
445 ret
= rb_fsnprint(buf
, len
, &prepend_string
);