1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * Following code was copied from glib to GNU Midnight Commander to
22 * provide compatibility with older versions of glib.
32 #include "glibcompat.h"
34 #if GLIB_MAJOR_VERSION < 2
36 /* Functions g_strlcpy and g_strlcat were originally developed by
37 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
38 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
39 * for more information.
44 g_strlcpy (gchar
*dest
,
48 g_return_val_if_fail (dest
!= NULL
, 0);
49 g_return_val_if_fail (src
!= NULL
, 0);
51 return strlcpy (dest
, src
, dest_size
);
55 g_strlcat (gchar
*dest
,
59 g_return_val_if_fail (dest
!= NULL
, 0);
60 g_return_val_if_fail (src
!= NULL
, 0);
62 return strlcat (dest
, src
, dest_size
);
65 #else /* ! HAVE_STRLCPY */
68 * Copy string src to buffer dest (of buffer size dest_size). At most
69 * dest_size-1 characters will be copied. Always NUL terminates
70 * (unless dest_size == 0). This function does NOT allocate memory.
71 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
72 * Returns size of attempted result, strlen(src),
73 * so if retval >= dest_size, truncation occurred.
76 g_strlcpy (gchar
*dest
,
80 register gchar
*d
= dest
;
81 register const gchar
*s
= src
;
82 register gsize n
= dest_size
;
84 g_return_val_if_fail (dest
!= NULL
, 0);
85 g_return_val_if_fail (src
!= NULL
, 0);
87 /* Copy as many bytes as will fit */
88 if (n
!= 0 && --n
!= 0)
91 register gchar c
= *s
++;
99 /* If not enough room in dest, add NUL and traverse rest of src */
108 return s
- src
- 1; /* count does not include NUL */
110 #endif /* ! HAVE_STRLCPY */
112 #endif /* GLIB_MAJOR_VERSION < 2 */