I'm trying to parse a command-line argument for a -p option using getopt. This is what I'm getting in gdb though:
Code:
(gdb) print port_number
No symbol "port_number" in current context.
(gdb) step
handle_input (argv=0x7fffffffde68, argc=4)
at /home/me/Desktop/programs/scanr/src/scanr.c:44
44 while ((c = getopt (argc, argv, "t:ip:l:u:o:vh")) != -1)
(gdb) print port_number
$5 = <optimized out>
(gdb) step
46 switch (c)
(gdb) print port_number
$6 = <optimized out>
(gdb) step
59 port_number = atoi(optarg);
(gdb)
atoi (__nptr=<optimized out>) at /usr/include/stdlib.h:241
241 return (int) strtol (__nptr, (char **) NULL, 10);
(gdb) p port_number
No symbol "port_number" in current context.
(gdb) p optarg
$7 = 0x7fffffffe1dc "555"
What's going on is that port_number is initialized to 0 at the the start of the function, but when someone passes -p [portnumber], port_number is then supposed to be set to that number. However, something is happening as you can see, where atoi(optarg) is not setting port_number properly.. I'm guessing it's something with the __nptr=<optimized out> and line below that but I'm not really sure. You can see how port_number disappears from scope but that optarg is indeed set to the proper port number as specified with option -p. See code:
Code:
static void handle_input(int argc, char **argv)
{
int port_number = 0;
int t_flag = 0;
int i_flag = 0;
int p_flag = 0;
int l_flag = 0;
int u_flag = 0;
int o_flag = 0;
int v_flag = 0;
int h_flag = 0;
char *cvalue = NULL;
int index;
int c;
int opterr = 0;
while ((c = getopt (argc, argv, "t:ip:l:u:o:vh")) != -1)
{
switch (c)
{
case 't':
t_flag = 1;
int threads_count = atoi(optarg);
/*TODO: Add validation*/
break;
case 'i':
i_flag = 1;
break;
case 'p':
p_flag = 1;
/*get the argument supplied to p*/
port_number = atoi(optarg);
if(!validate_port_number(port_number)){
printf(INVALID_PORT);
return;
}
break;
case 'l':
l_flag = 1;
int port_number_lower = atoi(optarg);
if(!validate_port_number(port_number_lower)){
printf(INVALID_PORT);
return;
}
break;
case 'u':
u_flag = 1;
int port_number_upper = atoi(optarg);
if(!validate_port_number(port_number_upper)){
printf(INVALID_PORT);
return;
}
break;
case 'o':
o_flag = 1;
int conn_timeout_seconds = atoi(optarg);
/*TODO: Add validation*/
break;
case 'v':
printf("scanr %s\n", SCANR_VERSION);
printf("License MIT: The MIT License <https://opensource.org/licenses/MIT>\n");
exit(1);
break;
case '?':
if (optopt == 'o' || optopt == 'p'
|| optopt == 'l' || optopt == 'u'
|| optopt == 't'){
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt)){
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else{
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
}
case 'h':
default:
ui_print_help();
exit(1);
break;
}
}
}
static bool validate_port_number(int number)
{
return (number > 65535 || number < 1) ? true : false;
}
The only thing I could guess is, am I not using atoi properly? Not sure what's going on here. Thanks.
PS: On Linux (Fedora 25)