Re: [PATCH] options: don't error when unsetting OPTIND

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

 



Hi!

On Wed, Dec 14, 2022 at 05:36:22PM -0500, Michael Greenberg wrote:
> On 2022-12-14 at 06:49:46 PM, Steffen Nurpmeso wrote:
> 
> > Michael Greenberg wrote in
> >  <m2edt2nk5q.fsf@greenberg.science>:
> >  ...
> >  |Bafflingly, the POSIX spec gives the example `readonly HOME PWD`, but
> >  |with no explanation about why one might want to do that. (I get making
> >  |`HOME` readonly, but `PWD`? Is that supposed to stop `cd` from working?)
> >
> > PWD is a beast with lots of "behaviors of the cd and pwd utilities
> > are unspecified" in case of user edits, see "2.5.3 Shell Variables".
> 
> Is running `readonly PWD` the same as setting `PWD`?
> 
> The language in the documentation for `cd` and in 2.5.3 only talks about setting or
> unsetting `PWD`, but not the export/readonly bits.

Isssue 8 Draft 2.1, XBD, 8.1 Environment Variable Definition para. 11
(p. 156, ll. 5381-5390) says:
  Additionally, a subset of the above variables are manipulated by shell built-in utilities outside of
  shell assignments. If an attempt is made to mark any of the following variables as readonly, then
  either the readonly utility shall reject the attempt, or readonly shall succeed but the shell can still
  modify the variables outside of assignment context, or readonly shall succeed but use of a shell
  built-in that would otherwise modify such a variable shall fail.
    LINENO
    OLDPWD
    OPTARG
    OPTIND
    PWD

I.e., readonly PWD; cd /; PWD=dupa must:
  * fail in readonly, or
  * fail in the cd, or
  * fail in the assignment
at implementer's choice. Which is just a long-winded way of saying "just
do whatever but only accept readonly if it does anything".

Dash appears to do option 2:
  $ ./dash -c 'readonly PWD; cd /'
  ./dash: 1: cd: PWD: is read only
  $ ./dash -c 'readonly OPTIND; getopts a a -a'
  ./dash: 1: getopts: OPTIND: is read only
which is fine.
OLDPWD behaves like PWD and OPTARG like OPTIND.
LINENO behaves like option 3 for obvious reasons.

(For completeness: in ibid., para. 12 (ll. 5391-5392)
 there's a provision for special treatment for any non-standard
 variable we want for our built-ins:
   Implementations may provide an implementation-defined set of additional variables which are
   manipulated by implementation-specific built-in utilities not defined in this standard. The
   readonly utility shall not reject marking these additional variables as readonly, but when marked
   readonly, those extension utilities shall either continue to modify the variables, or shall fail
   because the variable is readonly. None of the variables defined by this standard shall be in this
   implementation-defined set.)

Best,
наб

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux