]>
jfr.im git - solanum.git/blob - src/irc_string.c
2 * ircd-ratbox: A slightly useful ircd.
3 * irc_string.c: IRC string functions.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: irc_string.c 678 2006-02-03 20:25:01Z jilles $
28 #include "sprintf_irc.h"
29 #include "irc_string.h"
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...)
56 * Thu Nov 24 18:22:48 1986
64 strcpy(buf
, ctime(&value
));
65 if((p
= strchr(buf
, '\n')) != NULL
)
72 * clean_string - clean up a string possibly containing garbage
74 * *sigh* Before the kiddies find this new and exciting way of
75 * annoying opers, lets clean up what is sent to local opers
79 clean_string(char *dest
, const unsigned char *src
, size_t len
)
85 if(dest
== NULL
|| src
== NULL
)
88 len
-= 3; /* allow for worst case, '^A\0' */
90 while(*src
&& (len
> 0))
92 if(*src
& 0x80) /* if high bit is set */
97 else if(!IsPrint(*src
)) /* if NOT printable */
101 *d
++ = 0x40 + *src
; /* turn it into a printable */
113 * strip_tabs(dst, src, length)
115 * Copies src to dst, while converting all \t (tabs) into spaces.
117 * NOTE: jdc: I have a gut feeling there's a faster way to do this.
120 strip_tabs(char *dest
, const unsigned char *src
, size_t len
)
123 /* Sanity check; we don't want anything nasty... */
127 if(dest
== NULL
|| src
== NULL
)
130 while(*src
&& (len
> 0))
134 *d
++ = ' '; /* Translate the tab into a space */
138 *d
++ = *src
; /* Copy src to dst */
143 *d
= '\0'; /* Null terminate, thanks and goodbye */
148 * strtoken - walk through a string of tokens, using a set of separators
153 strtoken(char **save
, char *str
, const char *fs
)
155 char *pos
= *save
; /* keep last position across calls */
159 pos
= str
; /* new string scan */
161 while(pos
&& *pos
&& strchr(fs
, *pos
) != NULL
)
162 ++pos
; /* skip leading separators */
165 return (pos
= *save
= NULL
); /* string contains only sep's */
167 tmp
= pos
; /* now, keep position of the token */
169 while(*pos
&& strchr(fs
, *pos
) == NULL
)
170 ++pos
; /* skip content of the token */
173 *pos
++ = '\0'; /* remove first sep after the token */
175 pos
= NULL
; /* end of string */
182 * Copyright (c) 1996-1999 by Internet Software Consortium.
184 * Permission to use, copy, modify, and distribute this software for any
185 * purpose with or without fee is hereby granted, provided that the above
186 * copyright notice and this permission notice appear in all copies.
188 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
189 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
190 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
191 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
192 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
193 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
194 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
198 #define SPRINTF(x) ((size_t)rb_sprintf x)
201 * WARNING: Don't even consider trying to compile this on a system where
202 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
205 static const char *inet_ntop4(const u_char
* src
, char *dst
, unsigned int size
);
207 static const char *inet_ntop6(const u_char
* src
, char *dst
, unsigned int size
);
211 * inet_ntop4(src, dst, size)
212 * format an IPv4 address
216 * (1) uses no statics
217 * (2) takes a u_char* not an in_addr as input
222 inet_ntop4(const unsigned char *src
, char *dst
, unsigned int size
)
226 return strcpy(dst
, inetntoa((const char *) src
));
230 * inet_ntop6(src, dst, size)
231 * convert IPv6 binary address into presentation (printable) format
237 inet_ntop6(const unsigned char *src
, char *dst
, unsigned int size
)
240 * Note that int32_t and int16_t need only be "at least" large enough
241 * to contain a value of the specified size. On some systems, like
242 * Crays, there is no such thing as an integer variable with 16 bits.
243 * Keep this in mind if you think this function should have been coded
244 * to use pointer overlays. All the world's not a VAX.
246 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp
;
252 u_int words
[IN6ADDRSZ
/ INT16SZ
];
257 * Copy the input (bytewise) array into a wordwise array.
258 * Find the longest run of 0x00's in src[] for :: shorthanding.
260 memset(words
, '\0', sizeof words
);
261 for(i
= 0; i
< IN6ADDRSZ
; i
+= 2)
262 words
[i
/ 2] = (src
[i
] << 8) | src
[i
+ 1];
265 for(i
= 0; i
< (IN6ADDRSZ
/ INT16SZ
); i
++)
270 cur
.base
= i
, cur
.len
= 1;
278 if(best
.base
== -1 || cur
.len
> best
.len
)
286 if(best
.base
== -1 || cur
.len
> best
.len
)
289 if(best
.base
!= -1 && best
.len
< 2)
296 for(i
= 0; i
< (IN6ADDRSZ
/ INT16SZ
); i
++)
298 /* Are we inside the best run of 0x00's? */
299 if(best
.base
!= -1 && i
>= best
.base
&& i
< (best
.base
+ best
.len
))
309 /* Are we following an initial run of 0x00s or any real hex? */
312 /* Is this address an encapsulated IPv4? */
313 if(i
== 6 && best
.base
== 0 &&
314 (best
.len
== 6 || (best
.len
== 5 && words
[5] == 0xffff)))
316 if(!inet_ntop4(src
+ 12, tp
, sizeof tmp
- (tp
- tmp
)))
321 tp
+= SPRINTF((tp
, "%x", words
[i
]));
323 /* Was it a trailing run of 0x00's? */
324 if(best
.base
!= -1 && (best
.base
+ best
.len
) == (IN6ADDRSZ
/ INT16SZ
))
329 * Check for overflow, copy, and we're done.
332 if((unsigned int) (tp
- tmp
) > size
)
336 return strcpy(dst
, tmp
);
341 strip_colour(char *string
)
345 char *last_non_space
= NULL
;
346 /* c is source, c2 is target */
356 if(c
[1] == ',' && isdigit(c
[2]))
382 *last_non_space
= '\0';