Thursday, May 20, 2004
Notes about getopt_long
Example command;
iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT
argc is 11
argv is "iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT"
-A is an option element
A is an option character
argv[2] is TEST
TEST is an optarg (option argument of -A)
10.1.1.1 is another optarg
optind is the index of an option
if optind == 3 then its value is argv[optind-1], argv[2] is TEST
argv[optind-1] is equal to optarg
getopt_long is used in a loop to parse all the command and find all the options. When it finds an option returns its option character. In every loop value of optarg and optind changes. getopt_long returns -1 when there is no option is left.
The difference between getopt and getopt_long is, getopt_long can take long parameters which are pointers to option names.
iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT
argc is 11
argv is "iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT"
-A is an option element
A is an option character
argv[2] is TEST
TEST is an optarg (option argument of -A)
10.1.1.1 is another optarg
optind is the index of an option
if optind == 3 then its value is argv[optind-1], argv[2] is TEST
argv[optind-1] is equal to optarg
getopt_long is used in a loop to parse all the command and find all the options. When it finds an option returns its option character. In every loop value of optarg and optind changes. getopt_long returns -1 when there is no option is left.
The difference between getopt and getopt_long is, getopt_long can take long parameters which are pointers to option names.