codebje commented on a change in pull request #3170: URL: https://github.com/apache/incubator-nuttx/pull/3170#discussion_r601888708
########## File path: libs/libc/unistd/unistd.h ########## @@ -0,0 +1,87 @@ +/**************************************************************************** + * libs/libc/unistd/unistd.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __LIBC_UNISTD_UNISTD_H +#define __LIBC_UNISTD_UNISTD_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* This structure encapsulates all variables associated with getopt(). */ + +struct getopt_s +{ + /* Part of the implementation of the public getopt() interface */ + + FAR char *go_optarg; /* Optional argument following option */ + int go_opterr; /* Print error message */ + int go_optind; /* Index into argv */ + int go_optopt; /* unrecognized option character */ + + /* Internal getopt() state */ + + FAR char *go_optptr; + FAR char * const *go_argv; + int go_argc; + bool go_binitialized; Review comment: You definitely need `go_binitialized` and `go_optptr`. Removing `go_argv` and `go_argc` changes the behaviour of `getopt` in a single task if it calls `getopt` with a different command line to parse (eg, a REPL or shell) without having either let the previous `getopt` call complete to returning -1 (which sets `go_binitialized`) or setting `optind` to -1 (which equivalently resets `getopt`'s state). Allowing `getopt` to be called repeatedly is a common extension on the POSIX specification - BSD introduces an `optreset` global for it, while GNU like the NuttX implementation will reset if `optind` is set to zero (GNU tests explicitly for zero, NuttX tests for < 1). The GNU implementation doesn't test for changed argc/argv, and I haven't looked at the BSD one. Long story short, removing the `go_argc` and go_argv` variables and test is a behavioural change even for a single threaded task, independent of thread-local storage or shared access to the getopt globals. I think it's probably pretty reasonable to remove it anyway in favour of expecting programs will Do The Right Thing™ and either reset getopt or always let it run to completion before calling it again. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
