This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

getsubopt


Hello.  When adding a getsubopt module to gnulib, some problems with
getsubopt.c in libc was discovered, see:

http://lists.gnu.org/archive/html/bug-gnulib/2004-08/msg00002.html

The patch below aim to fix these issues, and make it possible to use
verbatim getsubopt.c from libc in gnulib.

* The use of "restrict" in the stdlib.h prototype violate the POSIX
  function signature, see:
  http://www.opengroup.org/onlinepubs/000095399/functions/getsubopt.html
  The patch below remove restrict.  (This assume the implementation
  doesn't require inputs to be non-overlapping.)

* Change K&R prototype in getsubopt.c to ANSI C prototype.

* Add some #if !_LIBC code to make the same file work in gnulib.
  Compare argp/argp-namefrob.h for similar behaviour.

* Use strncmp instead of memcmp, as memcmp isn't guaranteed to go
  left-to-right.

Thanks.

PS.  If an earlier version of this post end up on the mailing list,
please ignore it.  This post supersede the previous post completely.

2004-08-02  Simon Josefsson  <jas@extundo.com>

	* stdlib/stdlib.h (getsubopt): Remove "restrict" keyword, for
	POSIX conformance.
	* stdlib/getsubopt.c [!_LIBC]: Include strchrnul.h, and map
	__strchrnul to strchrnul.  This make it possible to use the
	verbatim glibc file in gnulib.
	(getsubopt): Change K&R prototype to ANSI C.
	(getsubopt): Use strncmp instead of memcmp.

Index: getsubopt.c
===================================================================
RCS file: /cvs/glibc/libc/stdlib/getsubopt.c,v
retrieving revision 1.6
diff -u -p -r1.6 getsubopt.c
--- getsubopt.c	6 Jul 2001 04:55:41 -0000	1.6
+++ getsubopt.c	2 Aug 2004 10:48:53 -0000
@@ -1,5 +1,5 @@
 /* Parse comma separate list into words.
-   Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
@@ -21,6 +21,14 @@
 #include <stdlib.h>
 #include <string.h>
 
+#if !_LIBC
+/* This code is written for inclusion in gnu-libc, and uses names in
+   the namespace reserved for libc.  If we're compiling in gnulib,
+   define those names to be the normal ones instead.  */
+# include "strchrnul.h"
+# undef __strchrnul
+# define __strchrnul strchrnul
+#endif
 
 /* Parse comma separated suboption from *OPTIONP and match against
    strings in TOKENS.  If found return index and set *VALUEP to
@@ -29,10 +37,7 @@
    suboption.  On exit *OPTIONP is set to the beginning of the next
    token or at the terminating NUL character.  */
 int
-getsubopt (optionp, tokens, valuep)
-     char **optionp;
-     char *const *tokens;
-     char **valuep;
+getsubopt (char **optionp, char *const *tokens, char **valuep)
 {
   char *endp, *vstart;
   int cnt;
@@ -51,7 +56,7 @@ getsubopt (optionp, tokens, valuep)
   /* Try to match the characters between *OPTIONP and VSTART against
      one of the TOKENS.  */
   for (cnt = 0; tokens[cnt] != NULL; ++cnt)
-    if (memcmp (*optionp, tokens[cnt], vstart - *optionp) == 0
+    if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0
 	&& tokens[cnt][vstart - *optionp] == '\0')
       {
 	/* We found the current option in TOKENS.  */
Index: stdlib.h
===================================================================
RCS file: /cvs/glibc/libc/stdlib/stdlib.h,v
retrieving revision 1.103
diff -u -p -r1.103 stdlib.h
--- stdlib.h	2 Sep 2003 04:43:07 -0000	1.103
+++ stdlib.h	2 Aug 2004 10:48:54 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2002, 2003 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2002, 2003, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -863,9 +863,9 @@ extern int rpmatch (__const char *__resp
    not part of TOKENS return in *VALUEP beginning of unknown
    suboption.  On exit *OPTIONP is set to the beginning of the next
    token or at the terminating NUL character.  */
-extern int getsubopt (char **__restrict __optionp,
-		      char *__const *__restrict __tokens,
-		      char **__restrict __valuep) __THROW;
+extern int getsubopt (char **__optionp,
+		      char *__const *__tokens,
+		      char **__valuep) __THROW;
 #endif
 
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]