All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ozgur Akan <akan@aiqa.com>
To: "netfilter-devel@lists.netfilter.org"
	<netfilter-devel@lists.netfilter.org>,
	Harald Welte <laforge@netfilter.org>
Subject: Re: argv[optind-1] == optarg
Date: Mon, 24 May 2004 08:26:17 +0300	[thread overview]
Message-ID: <40B18779.8010606@aiqa.com> (raw)
In-Reply-To: <40AF9F99.7070305@aiqa.com>


[-- Attachment #1.1: Type: text/plain, Size: 823 bytes --]

Hi,
I attached the patch file for optarg. It includes patches for iptables.c 
and ip6tables.c.

regards,
Ozgur Akan

Ozgur Akan wrote:

> Hi,
>
> I think using optarg instead of argv[optind-1] will be better. In 
> iptables.c; while parsing iptables options with getopt_long, 
> argv[optind-1] is used in 8 places.
>
> optarg (option argument) gives us the value of the option we are 
> parsing. So when we do something with the argument of (for example) A 
> option the easiest way to get the value of A`s argument is using 
> optarg. optarg is designed to be used with getopt_long.
>
> Using optind (option index) is more tricky because optind starts from 
> 1 and when we are parsing an option with an index of value n of argv 
> array it shows n+2. That`s why we substract 1 from optind.
>
> best wishes,
> Ozgur Akan


[-- Attachment #1.2: Type: text/html, Size: 1440 bytes --]

[-- Attachment #2: optarg.PATCH --]
[-- Type: text/plain, Size: 4091 bytes --]

diff -Naur iptables/ip6tables.c iptables_dev/ip6tables.c
--- iptables/ip6tables.c	2004-05-24 08:07:59.782220912 +0300
+++ iptables_dev/ip6tables.c	2004-05-24 08:16:48.830793304 +0300
@@ -1853,10 +1853,10 @@
 				   invert);
 
 			/* Canonicalize into lower case */
-			for (protocol = argv[optind-1]; *protocol; protocol++)
+			for (protocol = optarg; *protocol; protocol++)
 				*protocol = tolower(*protocol);
 
-			protocol = argv[optind-1];
+			protocol = optarg;
 			if ( strcmp(protocol,"ipv6-icmp") == 0)
 				protocol = icmp6p;
 			fw.ipv6.proto = parse_protocol(protocol);
@@ -1873,7 +1873,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
 				   invert);
-			shostnetworkmask = argv[optind-1];
+			shostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP6_SRC;
 			break;
 
@@ -1881,7 +1881,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
 				   invert);
-			dhostnetworkmask = argv[optind-1];
+			dhostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP6_DST;
 			break;
 
@@ -1911,7 +1911,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ipv6.iniface,
 					fw.ipv6.iniface_mask);
 			fw.nfcache |= NFC_IP6_IF_IN;
@@ -1921,7 +1921,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ipv6.outiface,
 					fw.ipv6.outiface_mask);
 			fw.nfcache |= NFC_IP6_IF_OUT;
@@ -1961,7 +1961,7 @@
 			if (invert)
 				exit_error(PARAMETER_PROBLEM,
 					   "unexpected ! flag before --table");
-			*table = argv[optind-1];
+			*table = optarg;
 			break;
 
 		case 'x':
@@ -2100,7 +2100,7 @@
 				if (!m)
 					exit_error(PARAMETER_PROBLEM,
 						   "Unknown arg `%s'",
-						   argv[optind-1]);
+						   optarg);
 			}
 		}
 		invert = FALSE;
diff -Naur iptables/iptables.c iptables_dev/iptables.c
--- iptables/iptables.c	2004-05-24 08:08:00.804065568 +0300
+++ iptables_dev/iptables.c	2004-05-24 08:11:02.797398376 +0300
@@ -1850,10 +1850,10 @@
 				   invert);
 
 			/* Canonicalize into lower case */
-			for (protocol = argv[optind-1]; *protocol; protocol++)
+			for (protocol = optard; *protocol; protocol++)
 				*protocol = tolower(*protocol);
 
-			protocol = argv[optind-1];
+			protocol = optarg;
 			fw.ip.proto = parse_protocol(protocol);
 
 			if (fw.ip.proto == 0
@@ -1867,7 +1867,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_SOURCE, &fw.ip.invflags,
 				   invert);
-			shostnetworkmask = argv[optind-1];
+			shostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP_SRC;
 			break;
 
@@ -1875,7 +1875,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
 				   invert);
-			dhostnetworkmask = argv[optind-1];
+			dhostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP_DST;
 			break;
 
@@ -1905,7 +1905,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ip.iniface,
 					fw.ip.iniface_mask);
 			fw.nfcache |= NFC_IP_IF_IN;
@@ -1915,7 +1915,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ip.outiface,
 					fw.ip.outiface_mask);
 			fw.nfcache |= NFC_IP_IF_OUT;
@@ -1962,7 +1962,7 @@
 			if (invert)
 				exit_error(PARAMETER_PROBLEM,
 					   "unexpected ! flag before --table");
-			*table = argv[optind-1];
+			*table = optarg;
 			break;
 
 		case 'x':
@@ -2100,7 +2100,7 @@
 				if (!m)
 					exit_error(PARAMETER_PROBLEM,
 						   "Unknown arg `%s'",
-						   argv[optind-1]);
+						   optarg);
 			}
 		}
 		invert = FALSE;

  reply	other threads:[~2004-05-24  5:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-22 18:44 argv[optind-1] == optarg Ozgur Akan
2004-05-24  5:26 ` Ozgur Akan [this message]
2004-05-25 19:24 ` Henrik Nordstrom
2004-05-26 13:00   ` Ozgur Akan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40B18779.8010606@aiqa.com \
    --to=akan@aiqa.com \
    --cc=laforge@netfilter.org \
    --cc=netfilter-devel@lists.netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.