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 |
/** Fill a vector of tokens from a delimited input list. |
67 |
* Empty tokens (when \a token occurs at the start or end of \a list, |
68 |
* or when \a token occurs adjacent to itself) are ignored. When |
69 |
* \a size tokens have been written to \a vector, the rest of the |
70 |
* string is ignored. |
71 |
* \param names Input buffer. |
72 |
* \param token Delimiter used to split \a list. |
73 |
* \param vector Output vector. |
74 |
* \param size Maximum number of elements to put in \a vector. |
75 |
* \return Number of elements written to \a vector. |
76 |
*/ |
77 |
unsigned int |
78 |
token_vector(char *names, char token, char *vector[], unsigned int size) |
79 |
{ |
80 |
unsigned int count = 0; |
81 |
char *start = names; |
82 |
|
83 |
assert(names); |
84 |
assert(vector); |
85 |
assert(size > 1); |
86 |
|
87 |
vector[count++] = start; |
88 |
|
89 |
for (char *end = strchr(start, token); end; |
90 |
end = strchr(start, token)) |
91 |
{ |
92 |
*end++ = '\0'; |
93 |
start = end; |
94 |
|
95 |
if (*start) |
96 |
{ |
97 |
vector[count++] = start; |
98 |
|
99 |
if (count < size) |
100 |
continue; |
101 |
} |
102 |
|
103 |
break; |
104 |
} |
105 |
|
106 |
return count; |
107 |
} |
108 |
|
109 |
/* libio_basename() |
110 |
* |
111 |
* input - i.e. "/usr/local/ircd/modules/m_whois.so" |
112 |
* output - i.e. "m_whois.so" |
113 |
* side effects - this will be overwritten on subsequent calls |
114 |
*/ |
115 |
const char * |
116 |
libio_basename(const char *path) |
117 |
{ |
118 |
const char *s; |
119 |
|
120 |
if ((s = strrchr(path, '/')) == NULL) |
121 |
s = path; |
122 |
else |
123 |
s++; |
124 |
|
125 |
return s; |
126 |
} |
127 |
|
128 |
/* $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $ */ |
129 |
/* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */ |
130 |
/* |
131 |
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
132 |
* |
133 |
* Permission to use, copy, modify, and distribute this software for any |
134 |
* purpose with or without fee is hereby granted, provided that the above |
135 |
* copyright notice and this permission notice appear in all copies. |
136 |
* |
137 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
138 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
139 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
140 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
141 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
142 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
143 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
144 |
*/ |
145 |
|
146 |
#ifndef HAVE_STRLCAT |
147 |
/* |
148 |
* Appends src to string dst of size dsize (unlike strncat, dsize is the |
149 |
* full size of dst, not space left). At most dsize-1 characters |
150 |
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
151 |
* Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
152 |
* If retval >= dsize, truncation occurred. |
153 |
*/ |
154 |
size_t |
155 |
strlcat(char *dst, const char *src, size_t dsize) |
156 |
{ |
157 |
const char *odst = dst; |
158 |
const char *osrc = src; |
159 |
size_t n = dsize; |
160 |
size_t dlen; |
161 |
|
162 |
/* Find the end of dst and adjust bytes left but don't go past end. */ |
163 |
while (n-- != 0 && *dst != '\0') |
164 |
dst++; |
165 |
|
166 |
dlen = dst - odst; |
167 |
n = dsize - dlen; |
168 |
|
169 |
if (n-- == 0) |
170 |
return dlen + strlen(src); |
171 |
|
172 |
while (*src != '\0') |
173 |
{ |
174 |
if (n != 0) |
175 |
{ |
176 |
*dst++ = *src; |
177 |
n--; |
178 |
} |
179 |
|
180 |
src++; |
181 |
} |
182 |
|
183 |
*dst = '\0'; |
184 |
|
185 |
return dlen + (src - osrc); /* count does not include NUL */ |
186 |
} |
187 |
#endif |
188 |
|
189 |
#ifndef HAVE_STRLCPY |
190 |
/* |
191 |
* Copy string src to buffer dst of size dsize. At most dsize-1 |
192 |
* chars will be copied. Always NUL terminates (unless dsize == 0). |
193 |
* Returns strlen(src); if retval >= dsize, truncation occurred. |
194 |
*/ |
195 |
size_t |
196 |
strlcpy(char *dst, const char *src, size_t dsize) |
197 |
{ |
198 |
const char *osrc = src; |
199 |
size_t nleft = dsize; |
200 |
|
201 |
/* Copy as many bytes as will fit. */ |
202 |
if (nleft != 0) |
203 |
{ |
204 |
while (--nleft != 0) |
205 |
{ |
206 |
if ((*dst++ = *src++) == '\0') |
207 |
break; |
208 |
} |
209 |
} |
210 |
|
211 |
/* Not enough room in dst, add NUL and traverse rest of src. */ |
212 |
if (nleft == 0) |
213 |
{ |
214 |
if (dsize != 0) |
215 |
*dst = '\0'; /* NUL-terminate dst */ |
216 |
|
217 |
while (*src++) |
218 |
; |
219 |
} |
220 |
|
221 |
return src - osrc - 1; /* count does not include NUL */ |
222 |
} |
223 |
#endif |