1 /* A more useful interface to strtol. 2 3 Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2011 Free Software 4 Foundation, Inc. 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #include <config.h> 20 #include "xstrtol.h" 21 22 #include <stdlib.h> 23 24 #include "error.h" 25 #include "exitfail.h" 26 #include "gettext.h" 27 28 #define N_(msgid) msgid 29 30 /* Report an error for an invalid integer in an option argument. 31 32 ERR is the error code returned by one of the xstrto* functions. 33 34 Use OPT_IDX to decide whether to print the short option string "C" 35 or "-C" or a long option string derived from LONG_OPTION. OPT_IDX 36 is -2 if the short option "C" was used, without any leading "-"; it 37 is -1 if the short option "-C" was used; otherwise it is an index 38 into LONG_OPTIONS, which should have a name preceded by two '-' 39 characters. 40 41 ARG is the option-argument containing the integer. 42 43 After reporting an error, exit with status EXIT_STATUS if it is 44 nonzero. */ 45 46 static void 47 xstrtol_error (enum strtol_error err, 48 int opt_idx, char c, struct option const *long_options, 49 char const *arg, 50 int exit_status) 51 { 52 char const *hyphens = "--"; 53 char const *msgid; 54 char const *option; 55 char option_buffer[2]; 56 57 switch (err) 58 { 59 default: 60 abort (); 61 62 case LONGINT_INVALID: 63 msgid = N_("invalid %s%s argument `%s'"); 64 break; 65 66 case LONGINT_INVALID_SUFFIX_CHAR: 67 case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW: 68 msgid = N_("invalid suffix in %s%s argument `%s'"); 69 break; 70 71 case LONGINT_OVERFLOW: 72 msgid = N_("%s%s argument `%s' too large"); 73 break; 74 } 75 76 if (opt_idx < 0) 77 { 78 hyphens -= opt_idx; 79 option_buffer[0] = c; 80 option_buffer[1] = '\0'; 81 option = option_buffer; 82 } 83 else 84 option = long_options[opt_idx].name; 85 86 error (exit_status, 0, gettext (msgid), hyphens, option, arg); 87 } 88 89 /* Like xstrtol_error, except exit with a failure status. */ 90 91 void 92 xstrtol_fatal (enum strtol_error err, 93 int opt_idx, char c, struct option const *long_options, 94 char const *arg) 95 { 96 xstrtol_error (err, opt_idx, c, long_options, arg, exit_failure); 97 abort (); 98 } 99