]>
Commit | Line | Data |
---|---|---|
b57f37fb WP |
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 | * | |
b57f37fb WP |
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 | */ | |
34 | #define _GNU_SOURCE 1 | |
35 | #include <libratbox_config.h> | |
36 | #include <ratbox_lib.h> | |
37 | #include <rb_tools.h> | |
38 | ||
39 | ||
40 | /* | |
41 | * init_rb_dlink_nodes | |
42 | * | |
43 | */ | |
44 | static rb_bh *dnode_heap; | |
45 | void | |
46 | rb_init_rb_dlink_nodes(size_t dh_size) | |
47 | { | |
48 | ||
49 | dnode_heap = rb_bh_create(sizeof(rb_dlink_node), dh_size, "librb_dnode_heap"); | |
50 | if(dnode_heap == NULL) | |
51 | rb_outofmemory(); | |
52 | } | |
53 | ||
54 | /* | |
55 | * make_rb_dlink_node | |
56 | * | |
57 | * inputs - NONE | |
58 | * output - pointer to new rb_dlink_node | |
59 | * side effects - NONE | |
60 | */ | |
61 | rb_dlink_node * | |
62 | rb_make_rb_dlink_node(void) | |
63 | { | |
94b4fbf9 | 64 | return (rb_bh_alloc(dnode_heap)); |
b57f37fb WP |
65 | } |
66 | ||
67 | /* | |
68 | * free_rb_dlink_node | |
69 | * | |
70 | * inputs - pointer to rb_dlink_node | |
71 | * output - NONE | |
72 | * side effects - free given rb_dlink_node | |
73 | */ | |
74 | void | |
94b4fbf9 | 75 | rb_free_rb_dlink_node(rb_dlink_node *ptr) |
b57f37fb WP |
76 | { |
77 | assert(ptr != NULL); | |
78 | rb_bh_free(dnode_heap, ptr); | |
79 | } | |
80 | ||
81 | /* rb_string_to_array() | |
82 | * Changes a given buffer into an array of parameters. | |
83 | * Taken from ircd-ratbox. | |
84 | * | |
85 | * inputs - string to parse, array to put in | |
86 | * outputs - number of parameters | |
87 | */ | |
88 | int | |
89 | rb_string_to_array(char *string, char **parv, int maxpara) | |
90 | { | |
91 | char *p, *xbuf = string; | |
92 | int x = 0; | |
93 | ||
94 | parv[x] = NULL; | |
95 | ||
96 | if(string == NULL || string[0] == '\0') | |
97 | return x; | |
98 | ||
94b4fbf9 | 99 | while(*xbuf == ' ') /* skip leading spaces */ |
b57f37fb WP |
100 | xbuf++; |
101 | if(*xbuf == '\0') /* ignore all-space args */ | |
102 | return x; | |
103 | ||
104 | do | |
105 | { | |
106 | if(*xbuf == ':') /* Last parameter */ | |
107 | { | |
108 | xbuf++; | |
109 | parv[x++] = xbuf; | |
110 | parv[x] = NULL; | |
111 | return x; | |
112 | } | |
113 | else | |
114 | { | |
115 | parv[x++] = xbuf; | |
116 | parv[x] = NULL; | |
117 | if((p = strchr(xbuf, ' ')) != NULL) | |
118 | { | |
119 | *p++ = '\0'; | |
120 | xbuf = p; | |
121 | } | |
122 | else | |
123 | return x; | |
124 | } | |
94b4fbf9 | 125 | while(*xbuf == ' ') |
b57f37fb WP |
126 | xbuf++; |
127 | if(*xbuf == '\0') | |
128 | return x; | |
129 | } | |
94b4fbf9 | 130 | while(x < maxpara - 1); |
b57f37fb WP |
131 | |
132 | if(*p == ':') | |
133 | p++; | |
134 | ||
135 | parv[x++] = p; | |
136 | parv[x] = NULL; | |
137 | return x; | |
138 | } | |
139 | ||
140 | #ifndef HAVE_STRLCAT | |
94b4fbf9 | 141 | size_t |
b57f37fb WP |
142 | rb_strlcat(char *dest, const char *src, size_t count) |
143 | { | |
94b4fbf9 VY |
144 | size_t dsize = strlen(dest); |
145 | size_t len = strlen(src); | |
146 | size_t res = dsize + len; | |
147 | ||
148 | dest += dsize; | |
149 | count -= dsize; | |
150 | if(len >= count) | |
151 | len = count - 1; | |
152 | memcpy(dest, src, len); | |
153 | dest[len] = 0; | |
154 | return res; | |
b57f37fb WP |
155 | } |
156 | #else | |
157 | size_t | |
158 | rb_strlcat(char *dest, const char *src, size_t count) | |
159 | { | |
160 | return strlcat(dest, src, count); | |
161 | } | |
162 | #endif | |
94b4fbf9 | 163 | |
b57f37fb | 164 | #ifndef HAVE_STRLCPY |
94b4fbf9 | 165 | size_t |
b57f37fb WP |
166 | rb_strlcpy(char *dest, const char *src, size_t size) |
167 | { | |
94b4fbf9 VY |
168 | size_t ret = strlen(src); |
169 | ||
170 | if(size) | |
171 | { | |
172 | size_t len = (ret >= size) ? size - 1 : ret; | |
173 | memcpy(dest, src, len); | |
174 | dest[len] = '\0'; | |
175 | } | |
176 | return ret; | |
b57f37fb WP |
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 | |
94b4fbf9 | 188 | size_t |
b57f37fb | 189 | rb_strnlen(const char *s, size_t count) |
94b4fbf9 | 190 | { |
b57f37fb | 191 | const char *sc; |
94b4fbf9 | 192 | for(sc = s; count-- && *sc != '\0'; ++sc) |
b57f37fb WP |
193 | ;; |
194 | return sc - s; | |
94b4fbf9 | 195 | } |
b57f37fb WP |
196 | #else |
197 | size_t | |
198 | rb_strnlen(const char *s, size_t count) | |
199 | { | |
200 | return strnlen(s, count); | |
201 | } | |
202 | #endif | |
203 | ||
94b4fbf9 VY |
204 | /* rb_basename |
205 | * | |
206 | * input - | |
207 | * output - | |
208 | * side effects - | |
209 | */ | |
210 | char * | |
211 | rb_basename(const char *path) | |
212 | { | |
213 | const char *s; | |
214 | ||
215 | if(!(s = strrchr(path, '/'))) | |
216 | s = path; | |
217 | else | |
218 | s++; | |
219 | return rb_strdup(s); | |
220 | } | |
221 | ||
222 | /* | |
223 | * rb_dirname | |
224 | */ | |
225 | ||
226 | char * | |
227 | rb_dirname (const char *path) | |
228 | { | |
229 | char *s; | |
230 | ||
231 | s = strrchr(path, '/'); | |
232 | if(s == NULL) | |
233 | { | |
234 | return rb_strdup("."); | |
235 | } | |
236 | ||
237 | /* remove extra slashes */ | |
238 | while(s > path && *s == '/') | |
239 | --s; | |
240 | ||
241 | return rb_strndup(path, ((uintptr_t)s - (uintptr_t)path) + 2); | |
242 | } |