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