From 4520b25b366bf8f9b7be9d562588dcea2c17f8da Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Mon, 22 Sep 2003 20:21:48 +0000 Subject: [PATCH] * glibcompat.c: New file for compatibility with older versions of glib. * glibcompat.h: Likewise. * Makefile.am: Add the new files SOURCES. * global.h: Include glibcompat.h. --- src/ChangeLog | 6 +++ src/Makefile.am | 18 ++++---- src/glibcompat.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++ src/glibcompat.h | 25 +++++++++++ src/global.h | 1 + 5 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 src/glibcompat.c create mode 100644 src/glibcompat.h diff --git a/src/ChangeLog b/src/ChangeLog index dc5ad25e0..5938e7671 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,11 @@ 2003-09-22 Pavel Roskin + * glibcompat.c: New file for compatibility with older versions + of glib. + * glibcompat.h: Likewise. + * Makefile.am: Add the new files SOURCES. + * global.h: Include glibcompat.h. + * menu.c (menu_scan_hotkey): Use g_strlcpy() to avoid undefined behavior when using strcpy() on overlapping strings. * profile.c (GetSetProfile): Likewise. diff --git a/src/Makefile.am b/src/Makefile.am index 46e319814..f48057513 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,15 +48,15 @@ SRCS = achown.c achown.h background.c background.h boxes.c boxes.h \ cons.saver.h dialog.c dialog.h dir.c dir.h dlg.c dlg.h \ eregex.h execute.c execute.h ext.c ext.h file.c filegui.c \ filegui.h file.h filenot.c fileopctx.c fileopctx.h find.c \ - find.h findme.c findme.h fs.h fsusage.c fsusage.h global.h \ - help.c help.h hotlist.c hotlist.h i18n.h info.c info.h key.c \ - key.h keyxdef.c layout.c layout.h learn.c learn.h listmode.c \ - listmode.h main.c main.h menu.c menu.h \ - mountlist.c mountlist.h mouse.c mouse.h myslang.h option.c \ - option.h panel.h panelize.c panelize.h poptalloca.h popt.c \ - poptconfig.c popt.h popthelp.c poptint.h poptparse.c profile.c \ - profile.h regex.c rxvt.c screen.c setup.c setup.h slint.c \ - subshell.c subshell.h terms.c textconf.c textconf.h \ + find.h findme.c findme.h fs.h fsusage.c fsusage.h \ + glibcompat.c glibcompat.h global.h help.c help.h hotlist.c \ + hotlist.h i18n.h info.c info.h key.c key.h keyxdef.c layout.c \ + layout.h learn.c learn.h listmode.c listmode.h main.c main.h \ + menu.c menu.h mountlist.c mountlist.h mouse.c mouse.h myslang.h \ + option.c option.h panel.h panelize.c panelize.h poptalloca.h \ + popt.c poptconfig.c popt.h popthelp.c poptint.h poptparse.c \ + profile.c profile.h regex.c rxvt.c screen.c setup.c setup.h \ + slint.c subshell.c subshell.h terms.c textconf.c textconf.h \ tree.c tree.h treestore.c treestore.h tty.h user.c user.h \ util.c util.h utilunix.c view.c view.h widget.c widget.h \ win.c win.h wtools.c wtools.h diff --git a/src/glibcompat.c b/src/glibcompat.c new file mode 100644 index 000000000..80f2b9927 --- /dev/null +++ b/src/glibcompat.c @@ -0,0 +1,109 @@ +/* GLIB - Library of useful routines for C programming + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Following code was copied from glib to GNU Midnight Commander to + * provide compatibility with older versions of glib. + */ + +#include +#include +#include "glibcompat.h" + +#if GLIB_MAJOR_VERSION < 2 + +/* Functions g_strlcpy and g_strlcat were originally developed by + * Todd C. Miller to simplify writing secure code. + * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3 + * for more information. + */ + +#ifdef HAVE_STRLCPY +/* Use the native ones, if available; they might be implemented in assembly */ +gsize +g_strlcpy (gchar *dest, + const gchar *src, + gsize dest_size) +{ + g_return_val_if_fail (dest != NULL, 0); + g_return_val_if_fail (src != NULL, 0); + + return strlcpy (dest, src, dest_size); +} + +gsize +g_strlcat (gchar *dest, + const gchar *src, + gsize dest_size) +{ + g_return_val_if_fail (dest != NULL, 0); + g_return_val_if_fail (src != NULL, 0); + + return strlcat (dest, src, dest_size); +} + +#else /* ! HAVE_STRLCPY */ +/* g_strlcpy + * + * Copy string src to buffer dest (of buffer size dest_size). At most + * dest_size-1 characters will be copied. Always NUL terminates + * (unless dest_size == 0). This function does NOT allocate memory. + * Unlike strncpy, this function doesn't pad dest (so it's often faster). + * Returns size of attempted result, strlen(src), + * so if retval >= dest_size, truncation occurred. + */ +gsize +g_strlcpy (gchar *dest, + const gchar *src, + gsize dest_size) +{ + register gchar *d = dest; + register const gchar *s = src; + register gsize n = dest_size; + + g_return_val_if_fail (dest != NULL, 0); + g_return_val_if_fail (src != NULL, 0); + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) + do + { + register gchar c = *s++; + + *d++ = c; + if (c == 0) + break; + } + while (--n != 0); + + /* If not enough room in dest, add NUL and traverse rest of src */ + if (n == 0) + { + if (dest_size != 0) + *d = 0; + while (*s++) + ; + } + + return s - src - 1; /* count does not include NUL */ +} +#endif /* ! HAVE_STRLCPY */ + +#endif /* GLIB_MAJOR_VERSION < 2 */ + diff --git a/src/glibcompat.h b/src/glibcompat.h new file mode 100644 index 000000000..42ab22771 --- /dev/null +++ b/src/glibcompat.h @@ -0,0 +1,25 @@ +/* fsusage.h -- declarations for functions decared in glibcompat.c, + i.e. functions present only in recent versions of glib. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef __MC_GLIBCOMPAT_H +#define __MC_GLIBCOMPAT_H + +#if GLIB_MAJOR_VERSION < 2 +gsize g_strlcpy (gchar *dest, const gchar *src, gsize dest_size); +#endif /* GLIB_MAJOR_VERSION < 2 */ + +#endif /* !__MC_GLIBCOMPAT_H */ diff --git a/src/global.h b/src/global.h index f837377ca..963571082 100644 --- a/src/global.h +++ b/src/global.h @@ -103,6 +103,7 @@ struct timeval { #endif #include +#include "glibcompat.h" #if defined(HAVE_RX_H) && defined(HAVE_REGCOMP) #include