This is the mail archive of the newlib@sourceware.org mailing list for the newlib 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]

[PATCH] getopt: Bug fix. Initialize optind


If getopt is called with optind == 0 and argc == 1, the EOF case is
not detected. optind should be initialized before testing for EOF.

Cheers,
Shaun

2006-07-07 Shaun Jackman <sjackman@gmail.com>

	* libc/stdlib/getopt.c (getopt_internal): Bug fix. Initialize
	optind and optwhere before checking for valid arguments.

Index: libc/stdlib/getopt.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/getopt.c,v
retrieving revision 1.2
diff -u -r1.2 getopt.c
--- libc/stdlib/getopt.c	4 Jan 2006 21:21:52 -0000	1.2
+++ libc/stdlib/getopt.c	8 Jul 2006 00:13:06 -0000
@@ -156,7 +156,10 @@
  char *cp;
  int arg_next = 0;

-  /* first, deal with silly parameters and easy stuff */
+  /* if this is our first time through */
+  if (optind == 0)
+    optind = optwhere = 1;
+  /* deal with silly parameters and easy stuff */
  if (argc == 0 || argv == NULL || (shortopts == NULL && longopts == NULL))
    return EOF;
  if (optind >= argc || argv[optind] == NULL)
@@ -166,9 +169,6 @@
      optind++;
      return EOF;
    }
-  /* if this is our first time through */
-  if (optind == 0)
-    optind = optwhere = 1;

  /* define ordering */
  if (shortopts != NULL && (*shortopts == '-' || *shortopts == '+'))

Attachment: newlib-getopt.diff
Description: Text document


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