Blame


1 60cf07c8 2002-12-26 alex /*
2 60cf07c8 2002-12-26 alex * ngIRCd -- The Next Generation IRC Daemon
3 60cf07c8 2002-12-26 alex * Copyright (c)2001,2002 by 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 fbdf85b5 2002-12-26 alex * <http://samba.anu.edu.au/rsync/doxygen/head/lib_2compat_8c.html>
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 6c5f4beb 2005-01-18 alex static char UNUSED id[] = "$Id: strlcpy.c,v 1.3 2005/01/18 09:05:37 alex 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 fbdf85b5 2002-12-26 alex 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 fbdf85b5 2002-12-26 alex
43 fbdf85b5 2002-12-26 alex if( len1 + len2 >= size ) len2 = size - ( len1 + 1 );
44 fbdf85b5 2002-12-26 alex if( len2 > 0 )
45 fbdf85b5 2002-12-26 alex {
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 fbdf85b5 2002-12-26 alex 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 6c5f4beb 2005-01-18 alex if( size <= 0 ) return 0;
67 fbdf85b5 2002-12-26 alex if( len >= size ) len = size - 1;
68 fbdf85b5 2002-12-26 alex memcpy( dst, src, len );
69 fbdf85b5 2002-12-26 alex dst[len] = 0;
70 6c5f4beb 2005-01-18 alex return ret;
71 fbdf85b5 2002-12-26 alex } /* strlcpy */
72 fbdf85b5 2002-12-26 alex
73 fbdf85b5 2002-12-26 alex #endif
74 fbdf85b5 2002-12-26 alex
75 fbdf85b5 2002-12-26 alex
76 60cf07c8 2002-12-26 alex /* -eof- */