pkgsrc-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: [patch] shells/posh: several builtins are unusable; fix getopt(3) usage.



> PPS: There's another instance of ``optind = 0'' in exec.c, which I am unsure about; the attached patch does NOT touch it.
> |	/* We need to set optind to 0 or else +p won't work */
> |	optind = 0;
> |
> |	while ((optc = getopt(noargc, ap, "+p")) != -1) {
> |		switch (optc) {
> 
> "+p"? Is this a GNUism?
It turns out that the option string starting with '+' in fact is a GNUism, and it tells GNU getopt to stop looking for options after the first non-option argument; i.e. it causes it to essentially behave like regular getopt(3).

Again, even with GNU getopt that optind being 0 does not make sense, it should be set to 1 (in this branch, ap[0] will always be "command", since we're handling the "command" builtin at that point).

Attached another patch that turns that into ``optind = 1''.
It turns out that having a "+p" option string in our (non-GNU) getopt is unproblematic; an option like "-+" would lead into the 'default' case of the relevant getopt/switch construct anyway.

Cheers,
Timo Buhrmester
--- exec.c.orig	2015-08-30 05:58:21.000000000 +0200
+++ exec.c	2015-08-30 06:02:48.000000000 +0200
@@ -413,8 +413,10 @@
 	while (*arg++)
 		++noargc;
 
-	/* We need to set optind to 0 or else +p won't work */
-	optind = 0;
+	/* We need to set optind to 1 in order to start looking at the
+	 * first potential option argument (ap[0] is always "command")
+	 */
+	optind = 1;
 
 	while ((optc = getopt(noargc, ap, "+p")) != -1) {
 		switch (optc) {


Home | Main Index | Thread Index | Old Index