1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2020 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 |
bool |
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 false; |
43 |
} |
44 |
else if (IsMWildChar(*p)) |
45 |
return true; |
46 |
} |
47 |
|
48 |
return false; |
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 |
/* $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $ */ |
165 |
/* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */ |
166 |
/* |
167 |
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
168 |
* |
169 |
* Permission to use, copy, modify, and distribute this software for any |
170 |
* purpose with or without fee is hereby granted, provided that the above |
171 |
* copyright notice and this permission notice appear in all copies. |
172 |
* |
173 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
174 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
175 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
176 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
177 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
178 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
179 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
180 |
*/ |
181 |
|
182 |
#ifndef HAVE_STRLCAT |
183 |
/* |
184 |
* Appends src to string dst of size dsize (unlike strncat, dsize is the |
185 |
* full size of dst, not space left). At most dsize-1 characters |
186 |
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
187 |
* Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
188 |
* If retval >= dsize, truncation occurred. |
189 |
*/ |
190 |
size_t |
191 |
strlcat(char *dst, const char *src, size_t dsize) |
192 |
{ |
193 |
const char *odst = dst; |
194 |
const char *osrc = src; |
195 |
size_t n = dsize; |
196 |
size_t dlen; |
197 |
|
198 |
/* Find the end of dst and adjust bytes left but don't go past end. */ |
199 |
while (n-- != 0 && *dst != '\0') |
200 |
dst++; |
201 |
|
202 |
dlen = dst - odst; |
203 |
n = dsize - dlen; |
204 |
|
205 |
if (n-- == 0) |
206 |
return dlen + strlen(src); |
207 |
|
208 |
while (*src != '\0') |
209 |
{ |
210 |
if (n != 0) |
211 |
{ |
212 |
*dst++ = *src; |
213 |
n--; |
214 |
} |
215 |
|
216 |
src++; |
217 |
} |
218 |
|
219 |
*dst = '\0'; |
220 |
|
221 |
return dlen + (src - osrc); /* count does not include NUL */ |
222 |
} |
223 |
#endif |
224 |
|
225 |
#ifndef HAVE_STRLCPY |
226 |
/* |
227 |
* Copy string src to buffer dst of size dsize. At most dsize-1 |
228 |
* chars will be copied. Always NUL terminates (unless dsize == 0). |
229 |
* Returns strlen(src); if retval >= dsize, truncation occurred. |
230 |
*/ |
231 |
size_t |
232 |
strlcpy(char *dst, const char *src, size_t dsize) |
233 |
{ |
234 |
const char *osrc = src; |
235 |
size_t nleft = dsize; |
236 |
|
237 |
/* Copy as many bytes as will fit. */ |
238 |
if (nleft != 0) |
239 |
{ |
240 |
while (--nleft != 0) |
241 |
{ |
242 |
if ((*dst++ = *src++) == '\0') |
243 |
break; |
244 |
} |
245 |
} |
246 |
|
247 |
/* Not enough room in dst, add NUL and traverse rest of src. */ |
248 |
if (nleft == 0) |
249 |
{ |
250 |
if (dsize != 0) |
251 |
*dst = '\0'; /* NUL-terminate dst */ |
252 |
|
253 |
while (*src++) |
254 |
; |
255 |
} |
256 |
|
257 |
return src - osrc - 1; /* count does not include NUL */ |
258 |
} |
259 |
#endif |