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