Blame


1 60cf07c8 2002-12-26 alex /*
2 60cf07c8 2002-12-26 alex * ngIRCd -- The Next Generation IRC Daemon
3 3da942e2 2005-02-27 alex * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
4 60cf07c8 2002-12-26 alex *
5 60cf07c8 2002-12-26 alex * This program is free software; you can redistribute it and/or modify
6 60cf07c8 2002-12-26 alex * it under the terms of the GNU General Public License as published by
7 60cf07c8 2002-12-26 alex * the Free Software Foundation; either version 2 of the License, or
8 60cf07c8 2002-12-26 alex * (at your option) any later version.
9 60cf07c8 2002-12-26 alex * Please read the file COPYING, README and AUTHORS for more information.
10 60cf07c8 2002-12-26 alex *
11 fbdf85b5 2002-12-26 alex * strlcpy() and strlcat() replacement functions.
12 fbdf85b5 2002-12-26 alex * See <http://www.openbsd.org/papers/strlcpy-paper.ps> for details.
13 fbdf85b5 2002-12-26 alex *
14 fbdf85b5 2002-12-26 alex * Code partially borrowed from compat.c of rsync, written by Andrew
15 fbdf85b5 2002-12-26 alex * Tridgell (1998) and Martin Pool (2002):
16 3da942e2 2005-02-27 alex * <http://cvs.samba.org/cgi-bin/cvsweb/rsync/lib/compat.c>
17 60cf07c8 2002-12-26 alex */
18 60cf07c8 2002-12-26 alex
19 60cf07c8 2002-12-26 alex
20 60cf07c8 2002-12-26 alex #include "portab.h"
21 60cf07c8 2002-12-26 alex
22 8adff592 2005-03-19 fw static char UNUSED id[] = "$Id: strlcpy.c,v 1.5 2005/03/19 18:43:50 fw Exp $";
23 60cf07c8 2002-12-26 alex
24 60cf07c8 2002-12-26 alex #include "imp.h"
25 fbdf85b5 2002-12-26 alex #include <string.h>
26 fbdf85b5 2002-12-26 alex #include <sys/types.h>
27 60cf07c8 2002-12-26 alex
28 60cf07c8 2002-12-26 alex #include "exp.h"
29 60cf07c8 2002-12-26 alex
30 60cf07c8 2002-12-26 alex
31 fbdf85b5 2002-12-26 alex #ifndef HAVE_STRLCAT
32 fbdf85b5 2002-12-26 alex
33 fbdf85b5 2002-12-26 alex GLOBAL size_t
34 8adff592 2005-03-19 fw strlcat( char *dst, const char *src, size_t size )
35 fbdf85b5 2002-12-26 alex {
36 fbdf85b5 2002-12-26 alex /* Like strncat() but does not 0 fill the buffer and
37 fbdf85b5 2002-12-26 alex * always null terminates. */
38 fbdf85b5 2002-12-26 alex
39 fbdf85b5 2002-12-26 alex size_t len1 = strlen( dst );
40 fbdf85b5 2002-12-26 alex size_t len2 = strlen( src );
41 fbdf85b5 2002-12-26 alex size_t ret = len1 + len2;
42 3da942e2 2005-02-27 alex
43 3da942e2 2005-02-27 alex if( size && ( len1 < size - 1 )) {
44 3da942e2 2005-02-27 alex if( len2 >= size - len1 )
45 3da942e2 2005-02-27 alex len2 = size - len1 - 1;
46 fbdf85b5 2002-12-26 alex memcpy( dst + len1, src, len2 );
47 fbdf85b5 2002-12-26 alex dst[len1 + len2] = 0;
48 fbdf85b5 2002-12-26 alex }
49 fbdf85b5 2002-12-26 alex return ret;
50 fbdf85b5 2002-12-26 alex } /* strlcat */
51 fbdf85b5 2002-12-26 alex
52 fbdf85b5 2002-12-26 alex #endif
53 fbdf85b5 2002-12-26 alex
54 fbdf85b5 2002-12-26 alex
55 fbdf85b5 2002-12-26 alex #ifndef HAVE_STRLCPY
56 fbdf85b5 2002-12-26 alex
57 fbdf85b5 2002-12-26 alex GLOBAL size_t
58 8adff592 2005-03-19 fw strlcpy( char *dst, const char *src, size_t size )
59 fbdf85b5 2002-12-26 alex {
60 fbdf85b5 2002-12-26 alex /* Like strncpy but does not 0 fill the buffer and
61 fbdf85b5 2002-12-26 alex * always null terminates. */
62 fbdf85b5 2002-12-26 alex
63 fbdf85b5 2002-12-26 alex size_t len = strlen( src );
64 6c5f4beb 2005-01-18 alex size_t ret = len;
65 fbdf85b5 2002-12-26 alex
66 3da942e2 2005-02-27 alex if( size > 0 ) {
67 3da942e2 2005-02-27 alex if( len >= size ) len = size - 1;
68 3da942e2 2005-02-27 alex memcpy( dst, src, len );
69 3da942e2 2005-02-27 alex dst[len] = 0;
70 3da942e2 2005-02-27 alex }
71 6c5f4beb 2005-01-18 alex return ret;
72 fbdf85b5 2002-12-26 alex } /* strlcpy */
73 fbdf85b5 2002-12-26 alex
74 fbdf85b5 2002-12-26 alex #endif
75 fbdf85b5 2002-12-26 alex
76 fbdf85b5 2002-12-26 alex
77 60cf07c8 2002-12-26 alex /* -eof- */