1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2015 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 |
/* |
52 |
* strip_tabs(dst, src, length) |
53 |
* |
54 |
* Copies src to dst, while converting all \t (tabs) into spaces. |
55 |
*/ |
56 |
void |
57 |
strip_tabs(char *dest, const char *src, size_t len) |
58 |
{ |
59 |
char *d = dest; |
60 |
|
61 |
/* Sanity check; we don't want anything nasty... */ |
62 |
assert(dest); |
63 |
assert(src); |
64 |
assert(len > 0); |
65 |
|
66 |
for (; --len && *src; ++src) |
67 |
*d++ = *src == '\t' ? ' ' : *src; |
68 |
|
69 |
*d = '\0'; /* NUL terminate, thanks and goodbye */ |
70 |
} |
71 |
|
72 |
/* |
73 |
* strtoken - walk through a string of tokens, using a set of separators |
74 |
* argv 9/90 |
75 |
* |
76 |
*/ |
77 |
#ifndef HAVE_STRTOK_R |
78 |
char * |
79 |
strtoken(char **save, char *str, const char *fs) |
80 |
{ |
81 |
char *pos = *save; /* keep last position across calls */ |
82 |
char *tmp = NULL; |
83 |
|
84 |
if (str) |
85 |
pos = str; /* new string scan */ |
86 |
|
87 |
while (pos && *pos && strchr(fs, *pos)) |
88 |
++pos; /* skip leading separators */ |
89 |
|
90 |
if (!pos || !*pos) |
91 |
return pos = *save = NULL; /* string contains only sep's */ |
92 |
|
93 |
tmp = pos; /* now, keep position of the token */ |
94 |
|
95 |
while (*pos && strchr(fs, *pos) == NULL) |
96 |
++pos; /* skip content of the token */ |
97 |
|
98 |
if (*pos) |
99 |
*pos++ = '\0'; /* remove first sep after the token */ |
100 |
else |
101 |
pos = NULL; /* end of string */ |
102 |
|
103 |
*save = pos; |
104 |
return tmp; |
105 |
} |
106 |
#endif /* !HAVE_STRTOK_R */ |
107 |
|
108 |
/** Fill a vector of tokens from a delimited input list. |
109 |
* Empty tokens (when \a token occurs at the start or end of \a list, |
110 |
* or when \a token occurs adjacent to itself) are ignored. When |
111 |
* \a size tokens have been written to \a vector, the rest of the |
112 |
* string is ignored. |
113 |
* \param names Input buffer. |
114 |
* \param token Delimiter used to split \a list. |
115 |
* \param vector Output vector. |
116 |
* \param size Maximum number of elements to put in \a vector. |
117 |
* \return Number of elements written to \a vector. |
118 |
*/ |
119 |
int |
120 |
token_vector(char *names, char token, char *vector[], int size) |
121 |
{ |
122 |
int count = 0; |
123 |
char *start = names; |
124 |
char *end = NULL; |
125 |
|
126 |
assert(names); |
127 |
assert(vector); |
128 |
assert(size > 1); |
129 |
|
130 |
vector[count++] = start; |
131 |
|
132 |
for (end = strchr(start, token); end; |
133 |
end = strchr(start, token)) |
134 |
{ |
135 |
*end++ = '\0'; |
136 |
start = end; |
137 |
|
138 |
if (*start) |
139 |
{ |
140 |
vector[count++] = start; |
141 |
|
142 |
if (count < size) |
143 |
continue; |
144 |
} |
145 |
|
146 |
break; |
147 |
} |
148 |
|
149 |
return count; |
150 |
} |
151 |
|
152 |
/* libio_basename() |
153 |
* |
154 |
* input - i.e. "/usr/local/ircd/modules/m_whois.so" |
155 |
* output - i.e. "m_whois.so" |
156 |
* side effects - this will be overwritten on subsequent calls |
157 |
*/ |
158 |
const char * |
159 |
libio_basename(const char *path) |
160 |
{ |
161 |
const char *s; |
162 |
|
163 |
if ((s = strrchr(path, '/')) == NULL) |
164 |
s = path; |
165 |
else |
166 |
s++; |
167 |
|
168 |
return s; |
169 |
} |
170 |
|
171 |
/* |
172 |
* strlcat and strlcpy were ripped from openssh 2.5.1p2 |
173 |
* They had the following Copyright info: |
174 |
* |
175 |
* |
176 |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
177 |
* All rights reserved. |
178 |
* |
179 |
* Redistribution and use in source and binary forms, with or without |
180 |
* modification, are permitted provided that the following conditions |
181 |
* are met: |
182 |
* 1. Redistributions of source code must retain the above copyright |
183 |
* notice, this list of conditions and the following disclaimer. |
184 |
* 2. Redistributions in binary form must reproduce the above copyright |
185 |
* notice, this list of conditions and the following disclaimer in the |
186 |
* documentation and/or other materials provided with the distribution. |
187 |
* 3. The name of the author may not be used to endorse or promote products |
188 |
* derived from this software without specific prior written permission. |
189 |
* |
190 |
* THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
191 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
192 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
193 |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
194 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
195 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
196 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
197 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
198 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
199 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
200 |
*/ |
201 |
|
202 |
#ifndef HAVE_STRLCAT |
203 |
size_t |
204 |
strlcat(char *dst, const char *src, size_t siz) |
205 |
{ |
206 |
char *d = dst; |
207 |
const char *s = src; |
208 |
size_t n = siz, dlen; |
209 |
|
210 |
while (n-- != 0 && *d != '\0') |
211 |
d++; |
212 |
|
213 |
dlen = d - dst; |
214 |
n = siz - dlen; |
215 |
|
216 |
if (n == 0) |
217 |
return dlen + strlen(s); |
218 |
|
219 |
while (*s != '\0') |
220 |
{ |
221 |
if (n != 1) |
222 |
{ |
223 |
*d++ = *s; |
224 |
n--; |
225 |
} |
226 |
|
227 |
s++; |
228 |
} |
229 |
|
230 |
*d = '\0'; |
231 |
return dlen + (s - src); /* count does not include NUL */ |
232 |
} |
233 |
#endif |
234 |
|
235 |
#ifndef HAVE_STRLCPY |
236 |
size_t |
237 |
strlcpy(char *dst, const char *src, size_t siz) |
238 |
{ |
239 |
char *d = dst; |
240 |
const char *s = src; |
241 |
size_t n = siz; |
242 |
|
243 |
/* Copy as many bytes as will fit */ |
244 |
if (n != 0 && --n != 0) |
245 |
{ |
246 |
do |
247 |
{ |
248 |
if ((*d++ = *s++) == 0) |
249 |
break; |
250 |
} while (--n != 0); |
251 |
} |
252 |
|
253 |
/* Not enough room in dst, add NUL and traverse rest of src */ |
254 |
if (n == 0) |
255 |
{ |
256 |
if (siz != 0) |
257 |
*d = '\0'; /* NUL-terminate dst */ |
258 |
while (*s++) |
259 |
; |
260 |
} |
261 |
|
262 |
return s - src - 1; /* count does not include NUL */ |
263 |
} |
264 |
#endif |