* newlib/libc/include/getopt.h (struct option): name field should be
"const char *". * newlib/libc/stdlib/getopt.c (getopt_internal): Use fputs()/fputc() instead of fprintf() to save code space. Fix signed/unsigned comparison.
This commit is contained in:
		| @@ -1,3 +1,11 @@ | |||||||
|  | 2013-11-13  Freddie Chopin  <freddie_chopin@op.pl> | ||||||
|  |  | ||||||
|  | 	* newlib/libc/include/getopt.h (struct option): name field should be | ||||||
|  | 	"const char *". | ||||||
|  | 	* newlib/libc/stdlib/getopt.c (getopt_internal): Use fputs()/fputc() | ||||||
|  | 	instead of fprintf() to save code space.  Fix signed/unsigned | ||||||
|  | 	comparison. | ||||||
|  |  | ||||||
| 2013-11-12  Sebastian Huber  <sebastian.huber@embedded-brains.de> | 2013-11-12  Sebastian Huber  <sebastian.huber@embedded-brains.de> | ||||||
|  |  | ||||||
| 	* libc/libc/stdlib/getopt.c (getopt_internal): Fix NULL pointer access. | 	* libc/libc/stdlib/getopt.c (getopt_internal): Fix NULL pointer access. | ||||||
|   | |||||||
| @@ -104,7 +104,7 @@ extern "C" | |||||||
| /* types defined by this include file */ | /* types defined by this include file */ | ||||||
|   struct option |   struct option | ||||||
|   { |   { | ||||||
|     char *name;			/* the name of the long option */ |     const char *name;		/* the name of the long option */ | ||||||
|     int has_arg;		/* one of the above macros */ |     int has_arg;		/* one of the above macros */ | ||||||
|     int *flag;			/* determines if getopt_long() returns a |     int *flag;			/* determines if getopt_long() returns a | ||||||
| 				 * value for a long option; if it is | 				 * value for a long option; if it is | ||||||
|   | |||||||
| @@ -177,7 +177,9 @@ write_globals (struct getopt_data *data) | |||||||
|   optwhere = data->optwhere; |   optwhere = data->optwhere; | ||||||
| } | } | ||||||
|  |  | ||||||
| /* getopt_internal:  the function that does all the dirty work */ | /* getopt_internal:  the function that does all the dirty work | ||||||
|  |    NOTE: to reduce the code and RAM footprint this function uses | ||||||
|  |    fputs()/fputc() to do output to stderr instead of fprintf(). */ | ||||||
| static int | static int | ||||||
| getopt_internal (int argc, char *const argv[], const char *shortopts, | getopt_internal (int argc, char *const argv[], const char *shortopts, | ||||||
| 		 const struct option *longopts, int *longind, int only, | 		 const struct option *longopts, int *longind, int only, | ||||||
| @@ -301,7 +303,7 @@ getopt_internal (int argc, char *const argv[], const char *shortopts, | |||||||
| 	       match_chars) == 0) | 	       match_chars) == 0) | ||||||
| 	    { | 	    { | ||||||
| 	      /* do we have an exact match? */ | 	      /* do we have an exact match? */ | ||||||
| 	      if (match_chars == (int) (strlen (longopts[optindex].name))) | 	      if (match_chars == strlen (longopts[optindex].name)) | ||||||
| 		{ | 		{ | ||||||
| 		  longopt_match = optindex; | 		  longopt_match = optindex; | ||||||
| 		  break; | 		  break; | ||||||
| @@ -315,12 +317,14 @@ getopt_internal (int argc, char *const argv[], const char *shortopts, | |||||||
| 		    { | 		    { | ||||||
| 		      /* we have ambiguous options */ | 		      /* we have ambiguous options */ | ||||||
| 		      if (data->opterr) | 		      if (data->opterr) | ||||||
| 			fprintf (stderr, "%s: option `%s' is ambiguous " | 			fputs (argv[0], stderr); | ||||||
| 				 "(could be `--%s' or `--%s')\n", | 			fputs (": option `", stderr); | ||||||
| 				 argv[0], | 			fputs (argv[data->optind], stderr); | ||||||
| 				 argv[data->optind], | 			fputs ("' is ambiguous (could be `--", stderr); | ||||||
| 				 longopts[longopt_match].name, | 			fputs (longopts[longopt_match].name, stderr); | ||||||
| 				 longopts[optindex].name); | 			fputs ("' or `--", stderr); | ||||||
|  | 			fputs (longopts[optindex].name, stderr); | ||||||
|  | 			fputs ("')\n", stderr); | ||||||
| 		      return (data->optopt = '?'); | 		      return (data->optopt = '?'); | ||||||
| 		    } | 		    } | ||||||
| 		} | 		} | ||||||
| @@ -338,9 +342,10 @@ getopt_internal (int argc, char *const argv[], const char *shortopts, | |||||||
| 	{ | 	{ | ||||||
| 	  /* couldn't find option in shortopts */ | 	  /* couldn't find option in shortopts */ | ||||||
| 	  if (data->opterr) | 	  if (data->opterr) | ||||||
| 	    fprintf (stderr, | 	    fputs (argv[0], stderr); | ||||||
| 		     "%s: invalid option -- `-%c'\n", | 	    fputs (": invalid option -- `-", stderr); | ||||||
| 		     argv[0], argv[data->optind][data->optwhere]); | 	    fputc (argv[data->optind][data->optwhere], stderr); | ||||||
|  | 	    fputs ("'\n", stderr); | ||||||
| 	  data->optwhere++; | 	  data->optwhere++; | ||||||
| 	  if (argv[data->optind][data->optwhere] == '\0') | 	  if (argv[data->optind][data->optwhere] == '\0') | ||||||
| 	    { | 	    { | ||||||
| @@ -377,17 +382,20 @@ getopt_internal (int argc, char *const argv[], const char *shortopts, | |||||||
| 	{ | 	{ | ||||||
| 	  if (data->opterr) | 	  if (data->opterr) | ||||||
| 	    { | 	    { | ||||||
| 	      fprintf (stderr, "%s: argument required for option `", argv[0]); | 	      fputs (argv[0], stderr); | ||||||
|  | 	      fputs (": argument required for option `-", stderr); | ||||||
| 	      if (longopt_match >= 0) | 	      if (longopt_match >= 0) | ||||||
| 		{ | 		{ | ||||||
| 		  fprintf (stderr, "--%s'\n", longopts[longopt_match].name); | 		  fputc ('-', stderr); | ||||||
|  | 		  fputs (longopts[longopt_match].name, stderr); | ||||||
| 		  data->optopt = initial_colon ? ':' : '\?'; | 		  data->optopt = initial_colon ? ':' : '\?'; | ||||||
| 		} | 		} | ||||||
| 	      else | 	      else | ||||||
| 		{ | 		{ | ||||||
| 		  fprintf (stderr, "-%c'\n", *cp); | 		  fputc (*cp, stderr); | ||||||
| 		  data->optopt = *cp; | 		  data->optopt = *cp; | ||||||
| 		} | 		} | ||||||
|  | 	      fputs ("'\n", stderr); | ||||||
| 	    } | 	    } | ||||||
| 	  data->optind++; | 	  data->optind++; | ||||||
| 	  return initial_colon ? ':' : '\?'; | 	  return initial_colon ? ':' : '\?'; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user