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