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