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.
20 /** \file glibcompat.c
21 * \brief Source: old glib compatibility
23 * Following code was copied from glib to GNU Midnight Commander to
24 * provide compatibility with older versions of glib.
34 #include "glibcompat.h"
36 #if GLIB_MAJOR_VERSION < 2
38 /* Functions g_strlcpy and g_strlcat were originally developed by
39 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
40 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
41 * for more information.
46 g_strlcpy (gchar
*dest
,
50 g_return_val_if_fail (dest
!= NULL
, 0);
51 g_return_val_if_fail (src
!= NULL
, 0);
53 return strlcpy (dest
, src
, dest_size
);
57 g_strlcat (gchar
*dest
,
61 g_return_val_if_fail (dest
!= NULL
, 0);
62 g_return_val_if_fail (src
!= NULL
, 0);
64 return strlcat (dest
, src
, dest_size
);
67 #else /* ! HAVE_STRLCPY */
70 * Copy string src to buffer dest (of buffer size dest_size). At most
71 * dest_size-1 characters will be copied. Always NUL terminates
72 * (unless dest_size == 0). This function does NOT allocate memory.
73 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
74 * Returns size of attempted result, strlen(src),
75 * so if retval >= dest_size, truncation occurred.
78 g_strlcpy (gchar
*dest
,
82 register gchar
*d
= dest
;
83 register const gchar
*s
= src
;
84 register gsize n
= dest_size
;
86 g_return_val_if_fail (dest
!= NULL
, 0);
87 g_return_val_if_fail (src
!= NULL
, 0);
89 /* Copy as many bytes as will fit */
90 if (n
!= 0 && --n
!= 0)
93 register gchar c
= *s
++;
101 /* If not enough room in dest, add NUL and traverse rest of src */
110 return s
- src
- 1; /* count does not include NUL */
112 #endif /* ! HAVE_STRLCPY */
114 #endif /* GLIB_MAJOR_VERSION < 2 */