Commit 5e0c63e2e3e7a0aefb01d877dd7bb731ba6f00f1

Authored by Horst Kronstorfer
Committed by Wolfgang Denk
1 parent d30011bf61

mkenvimage: Fix getopt() error handling

Since the original implementation indicates explicit error handling
we turn off getopt()'s internal error messaging to avoid duplicate
error messages.  Additionally we add ':' (missing option argument)
error handling.

Signed-off-by: Horst Kronstorfer <hkronsto@frequentis.com>

Showing 1 changed file with 10 additions and 2 deletions Side-by-side Diff

... ... @@ -80,8 +80,11 @@
80 80  
81 81 int fp, ep;
82 82  
  83 + /* Turn off getopt()'s internal error message */
  84 + opterr = 0;
  85 +
83 86 /* Parse the cmdline */
84   - while ((option = getopt(argc, argv, "s:o:rbp:h")) != -1) {
  87 + while ((option = getopt(argc, argv, ":s:o:rbp:h")) != -1) {
85 88 switch (option) {
86 89 case 's':
87 90 datasize = strtol(optarg, NULL, 0);
88 91  
... ... @@ -106,8 +109,13 @@
106 109 case 'h':
107 110 usage(argv[0]);
108 111 return EXIT_SUCCESS;
  112 + case ':':
  113 + fprintf(stderr, "Missing argument for option -%c\n",
  114 + optopt);
  115 + usage(argv[0]);
  116 + return EXIT_FAILURE;
109 117 default:
110   - fprintf(stderr, "Wrong option -%c\n", option);
  118 + fprintf(stderr, "Wrong option -%c\n", optopt);
111 119 usage(argv[0]);
112 120 return EXIT_FAILURE;
113 121 }