1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2016 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file irc_string.c |
23 |
* \brief IRC string functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "config.h" |
28 |
#include "stdinc.h" |
29 |
#include "irc_string.h" |
30 |
|
31 |
|
32 |
int |
33 |
has_wildcards(const char *str) |
34 |
{ |
35 |
const unsigned char *p = (const unsigned char *)str; |
36 |
|
37 |
for (; *p; ++p) |
38 |
{ |
39 |
if (*p == '\\') |
40 |
{ |
41 |
if (*++p == '\0') |
42 |
return 0; |
43 |
} |
44 |
else if (IsMWildChar(*p)) |
45 |
return 1; |
46 |
} |
47 |
|
48 |
return 0; |
49 |
} |
50 |
|
51 |
const char * |
52 |
stripws(char *txt) |
53 |
{ |
54 |
while (*txt == '\t' || *txt == ' ') |
55 |
++txt; |
56 |
|
57 |
char *tmp = txt + strlen(txt) - 1; |
58 |
while (tmp >= txt && (*tmp == '\t' || *tmp == ' ')) |
59 |
--tmp; |
60 |
|
61 |
*(tmp + 1) = '\0'; |
62 |
|
63 |
return txt; |
64 |
} |
65 |
|
66 |
/* |
67 |
* strtok_r - walk through a string of tokens, using a set of separators |
68 |
* argv 9/90 |
69 |
* |
70 |
*/ |
71 |
#ifndef HAVE_STRTOK_R |
72 |
char * |
73 |
strtok_r(char *str, const char *fs, char **save) |
74 |
{ |
75 |
char *pos = *save; /* keep last position across calls */ |
76 |
char *tmp = NULL; |
77 |
|
78 |
if (str) |
79 |
pos = str; /* new string scan */ |
80 |
|
81 |
while (pos && *pos && strchr(fs, *pos)) |
82 |
++pos; /* skip leading separators */ |
83 |
|
84 |
if (!pos || !*pos) |
85 |
return pos = *save = NULL; /* string contains only sep's */ |
86 |
|
87 |
tmp = pos; /* now, keep position of the token */ |
88 |
|
89 |
while (*pos && strchr(fs, *pos) == NULL) |
90 |
++pos; /* skip content of the token */ |
91 |
|
92 |
if (*pos) |
93 |
*pos++ = '\0'; /* remove first sep after the token */ |
94 |
else |
95 |
pos = NULL; /* end of string */ |
96 |
|
97 |
*save = pos; |
98 |
return tmp; |
99 |
} |
100 |
#endif /* !HAVE_STRTOK_R */ |
101 |
|
102 |
/** Fill a vector of tokens from a delimited input list. |
103 |
* Empty tokens (when \a token occurs at the start or end of \a list, |
104 |
* or when \a token occurs adjacent to itself) are ignored. When |
105 |
* \a size tokens have been written to \a vector, the rest of the |
106 |
* string is ignored. |
107 |
* \param names Input buffer. |
108 |
* \param token Delimiter used to split \a list. |
109 |
* \param vector Output vector. |
110 |
* \param size Maximum number of elements to put in \a vector. |
111 |
* \return Number of elements written to \a vector. |
112 |
*/ |
113 |
unsigned int |
114 |
token_vector(char *names, char token, char *vector[], unsigned int size) |
115 |
{ |
116 |
unsigned int count = 0; |
117 |
char *start = names; |
118 |
|
119 |
assert(names); |
120 |
assert(vector); |
121 |
assert(size > 1); |
122 |
|
123 |
vector[count++] = start; |
124 |
|
125 |
for (char *end = strchr(start, token); end; |
126 |
end = strchr(start, token)) |
127 |
{ |
128 |
*end++ = '\0'; |
129 |
start = end; |
130 |
|
131 |
if (*start) |
132 |
{ |
133 |
vector[count++] = start; |
134 |
|
135 |
if (count < size) |
136 |
continue; |
137 |
} |
138 |
|
139 |
break; |
140 |
} |
141 |
|
142 |
return count; |
143 |
} |
144 |
|
145 |
/* libio_basename() |
146 |
* |
147 |
* input - i.e. "/usr/local/ircd/modules/m_whois.so" |
148 |
* output - i.e. "m_whois.so" |
149 |
* side effects - this will be overwritten on subsequent calls |
150 |
*/ |
151 |
const char * |
152 |
libio_basename(const char *path) |
153 |
{ |
154 |
const char *s; |
155 |
|
156 |
if ((s = strrchr(path, '/')) == NULL) |
157 |
s = path; |
158 |
else |
159 |
s++; |
160 |
|
161 |
return s; |
162 |
} |
163 |
|
164 |
/* |
165 |
* strlcat and strlcpy were ripped from openssh 2.5.1p2 |
166 |
* They had the following Copyright info: |
167 |
* |
168 |
* |
169 |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
170 |
* All rights reserved. |
171 |
* |
172 |
* Redistribution and use in source and binary forms, with or without |
173 |
* modification, are permitted provided that the following conditions |
174 |
* are met: |
175 |
* 1. Redistributions of source code must retain the above copyright |
176 |
* notice, this list of conditions and the following disclaimer. |
177 |
* 2. Redistributions in binary form must reproduce the above copyright |
178 |
* notice, this list of conditions and the following disclaimer in the |
179 |
* documentation and/or other materials provided with the distribution. |
180 |
* 3. The name of the author may not be used to endorse or promote products |
181 |
* derived from this software without specific prior written permission. |
182 |
* |
183 |
* THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
184 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
185 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
186 |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
187 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
188 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
189 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
190 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
191 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
192 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
193 |
*/ |
194 |
|
195 |
#ifndef HAVE_STRLCAT |
196 |
size_t |
197 |
strlcat(char *dst, const char *src, size_t siz) |
198 |
{ |
199 |
char *d = dst; |
200 |
const char *s = src; |
201 |
size_t n = siz, dlen; |
202 |
|
203 |
while (n-- != 0 && *d != '\0') |
204 |
d++; |
205 |
|
206 |
dlen = d - dst; |
207 |
n = siz - dlen; |
208 |
|
209 |
if (n == 0) |
210 |
return dlen + strlen(s); |
211 |
|
212 |
while (*s != '\0') |
213 |
{ |
214 |
if (n != 1) |
215 |
{ |
216 |
*d++ = *s; |
217 |
n--; |
218 |
} |
219 |
|
220 |
s++; |
221 |
} |
222 |
|
223 |
*d = '\0'; |
224 |
return dlen + (s - src); /* count does not include NUL */ |
225 |
} |
226 |
#endif |
227 |
|
228 |
#ifndef HAVE_STRLCPY |
229 |
size_t |
230 |
strlcpy(char *dst, const char *src, size_t siz) |
231 |
{ |
232 |
char *d = dst; |
233 |
const char *s = src; |
234 |
size_t n = siz; |
235 |
|
236 |
/* Copy as many bytes as will fit */ |
237 |
if (n != 0 && --n != 0) |
238 |
{ |
239 |
do |
240 |
{ |
241 |
if ((*d++ = *s++) == 0) |
242 |
break; |
243 |
} while (--n != 0); |
244 |
} |
245 |
|
246 |
/* Not enough room in dst, add NUL and traverse rest of src */ |
247 |
if (n == 0) |
248 |
{ |
249 |
if (siz != 0) |
250 |
*d = '\0'; /* NUL-terminate dst */ |
251 |
while (*s++) |
252 |
; |
253 |
} |
254 |
|
255 |
return s - src - 1; /* count does not include NUL */ |
256 |
} |
257 |
#endif |