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