Ticket #1530
[pantumic.git] / src / glibcompat.c
blob4168281e5811df581ad1aa9f9619508a533352e2
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.
27 #include <config.h>
29 #include <string.h>
31 #include <glib.h>
33 #include "global.h"
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.
44 #ifdef HAVE_STRLCPY
45 gsize
46 g_strlcpy (gchar *dest,
47 const gchar *src,
48 gsize dest_size)
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);
56 gsize
57 g_strlcat (gchar *dest,
58 const gchar *src,
59 gsize dest_size)
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 */
68 /* g_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.
77 gsize
78 g_strlcpy (gchar *dest,
79 const gchar *src,
80 gsize dest_size)
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++;
95 *d++ = c;
96 if (c == 0)
97 break;
99 while (--n != 0);
101 /* If not enough room in dest, add NUL and traverse rest of src */
102 if (n == 0)
104 if (dest_size != 0)
105 *d = 0;
106 while (*s++)
110 return s - src - 1; /* count does not include NUL */
112 #endif /* ! HAVE_STRLCPY */
114 #endif /* GLIB_MAJOR_VERSION < 2 */