Original patch as attached on the bugreport
[midnight-commander.git] / src / glibcompat.c
blob9c1c5041a1093d99ac5e505a56ddb1255b484b44
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.
25 #include <config.h>
27 #include <string.h>
29 #include <glib.h>
31 #include "global.h"
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.
42 #ifdef HAVE_STRLCPY
43 gsize
44 g_strlcpy (gchar *dest,
45 const gchar *src,
46 gsize dest_size)
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);
54 gsize
55 g_strlcat (gchar *dest,
56 const gchar *src,
57 gsize dest_size)
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 */
66 /* g_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.
75 gsize
76 g_strlcpy (gchar *dest,
77 const gchar *src,
78 gsize dest_size)
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++;
93 *d++ = c;
94 if (c == 0)
95 break;
97 while (--n != 0);
99 /* If not enough room in dest, add NUL and traverse rest of src */
100 if (n == 0)
102 if (dest_size != 0)
103 *d = 0;
104 while (*s++)
108 return s - src - 1; /* count does not include NUL */
110 #endif /* ! HAVE_STRLCPY */
112 #endif /* GLIB_MAJOR_VERSION < 2 */