Hi
I have a pogram that reads the network address and netmask from the command line! But when i run the program. it fails! It calls the usage() function! But, when i use gdb to trace over the code, it runs properly except that it gives the whole line after "-i" as an argument....Here is the code
Code:
int main(int argc,char *argv[])
{
// Use default values?
// All the defaults are defined in "program.h"
int useDefaults=0;
if (argc > 3)
usage(argv[0],0);
else if (argc==1)
useDefaults=1;
// Variables
int n_opt;
int i_flag=0;
int n_flag=0;
int p_flag=0;
string netaddr(getNetworkAddress(getLocalIP(),DEFNETMASK));
string netmask(DEFNETMASK);
string portRange(DEFMINPORT "-" DEFMAXPORT);
string s_option("hi:n:p:v");
const option l_option[]={
{ "help", 0, NULL, 'h'},
{ "ip", 1, NULL, 'i'},
{ "nm", 1, NULL, 'n'},
{ "port", 1, NULL, 'p'},
{ "version", 0, NULL, 'v'},
};
// Now, looking for the supplied arguments and options
if (!useDefaults )
{
do {
n_opt=getopt_long(argc,argv,s_option.c_str(),l_option,NULL);
switch(n_opt){
case 'h':
usage(argv[0],0);
break;
case 'i':
netaddr=optarg;
i_flag=1;
break;
case 'n':
netmask=optarg;
n_flag=1;
break;
case 'p':
portRange=optarg;
p_flag=1;
break;
case 'v':
cout << " " << argv[0] << " version " << VERSION_INFO << "." << endl;
cout << " Written by fnoyan." << endl;
exit(0);
case -1:
break;
default:
usage(argv[0],1);
} // switch
} while (n_opt!=-1);
} // if (!useDefaults)
else
{
cout << " No extra options are given! The default values will be used..." << endl;
cout << " \"program -h\" for a short list of available options." << endl;
}
cout << " program will use the values below..." << endl;
cout << " Network IPv4 Address : " << netaddr << endl;
cout << " Netmask for the Network : " << netmask << endl;
cout << " Ports that will be scanned : " << portRange << endl;
// Main try block
try
{ // just trying the Socket class....
Socket s;
s.connect("127.0.0.1",22);
} catch (SocketException e)
{
cout << e.description() << endl;
exit(1);
} // End of Main try block
return 0;
}
When i run the program
Code:
$ program -i 82.122.156.32 -n 255.255.0.0
i fails and calls usage() funstion. The interesting thing here is that, i cannot get the sane result inside GDB! While running the code with GDB, (r "-i 82.122.156.32 -n 255.255.0.0"), it just gets the "82.122.156.32 -n 255.255.0.0" part as the argument of "-i".
Thanks in advice....