portability fix for bison-1.75

Paul Eggert eggert at CS.UCLA.EDU
Mon Jan 23 23:46:32 PST 2006


Dieter <freebsd at sopwith.solgatos.com> writes:

> Compiling [Bison] 2.1 gives:
>
> ../../lib/getopt.c:127: warning: function declaration isn't a prototype
> ../../lib/quotearg.c:591: warning: comparison is always false due to limited range of data type
> ../../lib/strndup.c:39: warning: no previous prototype for 'strndup'
> ../../lib/strnlen.c:42: warning: no previous prototype for 'rpl_strnlen'

Thanks for reporting these.  The quotearg problem is actually a bug in
GCC (it generates an improper warning).  Jim Meyering has already
installed a workaround that generates slightly-worse code but avoids
the warning, as described in
<http://lists.gnu.org/archive/html/bug-gnulib/2005-08/msg00239.html>.

I just now installed the following gnulib patches to fix the remaining
warnings (I hope; I can't test this easily myself).  This should fix
the problem for Bison 2.2, when it comes out.

I'll also propagate these patches to coreutils.

2006-01-23  Paul Eggert  <eggert at cs.ucla.edu>

	Work around porting bugs reported by Dieter in
	<http://lists.gnu.org/archive/html/bug-bison/2006-01/msg00049.html>.
	* lib/getopt.c (_NOPROTO): Remove; no longer needed.
	Include <stdlib.h> and <unistd.h> in all environments; it's safe now.
	Include "getopt.h" first, to check interface.
	(getenv): Declare only if defined HAVE_DECL_GETENV &&
	!HAVE_DECL_GETENV.
	* lib/strndup.c [!_LIBC]: Include "strndup.h" to get prototype.
	(__strndup): Revert to K&R-style function dfns, the glibc style.
	* lib/strnlen.c: Don't claim it's taken from glibc; it's not.
	(strnlen, __strnlen): Remove #defines and #undefs; not needed.
	Include strnlen.h first, to get prototype properly.
	(strnlen): Renamed from __strnlen.
	Remove weak alias.
	* m4/getopt.m4 (gl_PREREQ_GETOPT): Check for getenv decl.

Index: lib/getopt.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getopt.c,v
retrieving revision 1.47
diff -p -u -r1.47 getopt.c
--- lib/getopt.c	14 May 2005 06:03:58 -0000	1.47
+++ lib/getopt.c	24 Jan 2006 07:39:57 -0000
@@ -2,7 +2,7 @@
    NOTE: getopt is now part of the C library, so if you don't know what
    "Keep this file name-space clean" means, talk to drepper at gnu.org
    before changing it!
-   Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
+   Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006
 	Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -20,28 +20,16 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
-   Ditto for AIX 3.2 and <stdlib.h>.  */
-#ifndef _NO_PROTO
-# define _NO_PROTO
-#endif
-
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#include <stdio.h>
-
-/* This needs to come after some library #include
-   to get __GNU_LIBRARY__ defined.  */
-#ifdef	__GNU_LIBRARY__
-/* Don't include stdlib.h for non-GNU C libraries because some of them
-   contain conflicting prototypes for getopt.  */
-# include <stdlib.h>
-# include <unistd.h>
-#endif	/* GNU C library.  */
+#include "getopt.h"
 
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef VMS
 # include <unixlib.h>
@@ -76,7 +64,6 @@
    GNU application programs can use a third alternative mode in which
    they can distinguish the relative order of options and other arguments.  */
 
-#include "getopt.h"
 #include "getopt_int.h"
 
 /* For communication from `getopt' to the caller.
@@ -118,16 +105,9 @@ int optopt = '?';
 static struct _getopt_data getopt_data;
 
 
-#ifndef __GNU_LIBRARY__
-
-/* Avoid depending on library functions or files
-   whose names are inconsistent.  */
-
-#ifndef getenv
+#if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
 extern char *getenv ();
 #endif
-
-#endif /* not __GNU_LIBRARY__ */
 
 #ifdef _LIBC
 /* Stored original parameters.
@@ -556,10 +536,7 @@ _getopt_internal_r (int argc, char **arg
 		  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 		  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-		  if (_IO_fwide (stderr, 0) > 0)
-		    __fwprintf (stderr, L"%s", buf);
-		  else
-		    fputs (buf, stderr);
+		  __fxprintf (NULL, "%s", buf);
 
 		  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 		  _IO_funlockfile (stderr);
@@ -634,10 +611,7 @@ _getopt_internal_r (int argc, char **arg
 			  ((_IO_FILE *) stderr)->_flags2
 			    |= _IO_FLAGS2_NOTCANCEL;
 
-			  if (_IO_fwide (stderr, 0) > 0)
-			    __fwprintf (stderr, L"%s", buf);
-			  else
-			    fputs (buf, stderr);
+			  __fxprintf (NULL, "%s", buf);
 
 			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 			  _IO_funlockfile (stderr);
@@ -674,10 +648,7 @@ _getopt_internal_r (int argc, char **arg
 			  ((_IO_FILE *) stderr)->_flags2
 			    |= _IO_FLAGS2_NOTCANCEL;
 
-			  if (_IO_fwide (stderr, 0) > 0)
-			    __fwprintf (stderr, L"%s", buf);
-			  else
-			    fputs (buf, stderr);
+			  __fxprintf (NULL, "%s", buf);
 
 			  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 			  _IO_funlockfile (stderr);
@@ -751,10 +722,7 @@ _getopt_internal_r (int argc, char **arg
 		  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 		  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-		  if (_IO_fwide (stderr, 0) > 0)
-		    __fwprintf (stderr, L"%s", buf);
-		  else
-		    fputs (buf, stderr);
+		  __fxprintf (NULL, "%s", buf);
 
 		  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 		  _IO_funlockfile (stderr);
@@ -817,10 +785,7 @@ _getopt_internal_r (int argc, char **arg
 		int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 		((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-		if (_IO_fwide (stderr, 0) > 0)
-		  __fwprintf (stderr, L"%s", buf);
-		else
-		  fputs (buf, stderr);
+		__fxprintf (NULL, "%s", buf);
 
 		((_IO_FILE *) stderr)->_flags2 = old_flags2;
 		_IO_funlockfile (stderr);
@@ -868,10 +833,7 @@ _getopt_internal_r (int argc, char **arg
 		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-		    if (_IO_fwide (stderr, 0) > 0)
-		      __fwprintf (stderr, L"%s", buf);
-		    else
-		      fputs (buf, stderr);
+		    __fxprintf (NULL, "%s", buf);
 
 		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 		    _IO_funlockfile (stderr);
@@ -940,10 +902,7 @@ _getopt_internal_r (int argc, char **arg
 		    int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 		    ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-		    if (_IO_fwide (stderr, 0) > 0)
-		      __fwprintf (stderr, L"%s", buf);
-		    else
-		      fputs (buf, stderr);
+		    __fxprintf (NULL, "%s", buf);
 
 		    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 		    _IO_funlockfile (stderr);
@@ -985,10 +944,7 @@ _getopt_internal_r (int argc, char **arg
 			    ((_IO_FILE *) stderr)->_flags2
 			      |= _IO_FLAGS2_NOTCANCEL;
 
-			    if (_IO_fwide (stderr, 0) > 0)
-			      __fwprintf (stderr, L"%s", buf);
-			    else
-			      fputs (buf, stderr);
+			    __fxprintf (NULL, "%s", buf);
 
 			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 			    _IO_funlockfile (stderr);
@@ -1027,10 +983,7 @@ _getopt_internal_r (int argc, char **arg
 			    ((_IO_FILE *) stderr)->_flags2
 			      |= _IO_FLAGS2_NOTCANCEL;
 
-			    if (_IO_fwide (stderr, 0) > 0)
-			      __fwprintf (stderr, L"%s", buf);
-			    else
-			      fputs (buf, stderr);
+			    __fxprintf (NULL, "%s", buf);
 
 			    ((_IO_FILE *) stderr)->_flags2 = old_flags2;
 			    _IO_funlockfile (stderr);
@@ -1101,10 +1054,7 @@ _getopt_internal_r (int argc, char **arg
 			int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
 			((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
 
-			if (_IO_fwide (stderr, 0) > 0)
-			  __fwprintf (stderr, L"%s", buf);
-			else
-			  fputs (buf, stderr);
+			__fxprintf (NULL, "%s", buf);
 
 			((_IO_FILE *) stderr)->_flags2 = old_flags2;
 			_IO_funlockfile (stderr);
Index: lib/strndup.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/strndup.c,v
retrieving revision 1.13
diff -p -u -r1.13 strndup.c
--- lib/strndup.c	19 Sep 2005 17:28:15 -0000	1.13
+++ lib/strndup.c	24 Jan 2006 07:39:57 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1996, 1997, 1998, 2000, 2003, 2005 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006 Free
+   Software Foundation, Inc.
 
    NOTE: The canonical source of this file is maintained with the GNU C Library.
    Bugs can be reported to bug-glibc at prep.ai.mit.edu.
@@ -20,12 +21,19 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#if !_LIBC
+# include "strndup.h"
+#endif
 
 #include <stdlib.h>
 #include <string.h>
 
-/* Get strnlen. */
-#include "strnlen.h"
+#if !_LIBC
+# include "strnlen.h"
+# ifndef __strnlen
+#  define __strnlen strnlen
+# endif
+#endif
 
 #undef __strndup
 #undef strndup
@@ -35,9 +43,11 @@
 #endif
 
 char *
-__strndup (const char *s, size_t n)
+__strndup (s, n)
+     const char *s;
+     size_t n;
 {
-  size_t len = strnlen (s, n);
+  size_t len = __strnlen (s, n);
   char *new = malloc (len + 1);
 
   if (new == NULL)
@@ -46,6 +56,9 @@ __strndup (const char *s, size_t n)
   new[len] = '\0';
   return memcpy (new, s, len);
 }
+#ifdef libc_hidden_def
+libc_hidden_def (__strndup)
+#endif
 #ifdef weak_alias
 weak_alias (__strndup, strndup)
 #endif
Index: lib/strnlen.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/strnlen.c,v
retrieving revision 1.12
diff -p -u -r1.12 strnlen.c
--- lib/strnlen.c	19 Sep 2005 17:28:15 -0000	1.12
+++ lib/strnlen.c	24 Jan 2006 07:39:57 -0000
@@ -1,6 +1,6 @@
 /* Find the length of STRING, but scan at most MAXLEN characters.
-   Copyright (C) 1996, 1997, 1998, 2000-2003 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
+   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Written by Simon Josefsson.
 
    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
@@ -12,37 +12,22 @@
    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,
+   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#undef strnlen
 
-#include <string.h>
-
-#undef __strnlen
-#undef strnlen
-
-#ifndef _LIBC
-# define strnlen rpl_strnlen
-#endif
-
-#ifndef weak_alias
-# define __strnlen strnlen
-#endif
+#include "strnlen.h"
 
 /* Find the length of STRING, but scan at most MAXLEN characters.
    If no '\0' terminator is found in that many characters, return MAXLEN.  */
 
 size_t
-__strnlen (const char *string, size_t maxlen)
+strnlen (const char *string, size_t maxlen)
 {
   const char *end = memchr (string, '\0', maxlen);
   return end ? (size_t) (end - string) : maxlen;
 }
-#ifdef weak_alias
-weak_alias (__strnlen, strnlen)
-#endif
Index: m4/getopt.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/getopt.m4,v
retrieving revision 1.12
diff -p -u -r1.12 getopt.m4
--- m4/getopt.m4	16 Aug 2005 18:45:51 -0000	1.12
+++ m4/getopt.m4	24 Jan 2006 07:39:57 -0000
@@ -1,5 +1,5 @@
-# getopt.m4 serial 11
-dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+# getopt.m4 serial 12
+dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -77,4 +77,7 @@ AC_DEFUN([gl_GETOPT_IFELSE],
 AC_DEFUN([gl_GETOPT], [gl_GETOPT_IFELSE([gl_GETOPT_SUBSTITUTE])])
 
 # Prerequisites of lib/getopt*.
-AC_DEFUN([gl_PREREQ_GETOPT], [:])
+AC_DEFUN([gl_PREREQ_GETOPT],
+[
+  AC_CHECK_DECLS_ONCE([getenv])
+])



More information about the freebsd-ports mailing list