2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/unistd/lib_getopt.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* 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.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-03-24 18:34:47 +01:00
|
|
|
#include "unistd.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getopt
|
|
|
|
*
|
2019-02-05 17:30:59 +01:00
|
|
|
* Description:
|
|
|
|
* getopt() parses command-line arguments. Its arguments argc and argv
|
|
|
|
* are the argument count and array as passed to the main() function on
|
|
|
|
* program invocation. An element of argv that starts with '-' is an
|
|
|
|
* option element. The characters of this element (aside from the initial
|
|
|
|
* '-') are option characters. If getopt() is called repeatedly, it
|
|
|
|
* returns successively each of the option characters from each of the
|
2012-11-10 17:06:01 +01:00
|
|
|
* option elements.
|
|
|
|
*
|
|
|
|
* If getopt() finds another option character, it returns that character,
|
|
|
|
* updating the external variable optind and a static variable nextchar so
|
|
|
|
* that the next call to getopt() can resume the scan with the following
|
|
|
|
* option character or argv-element.
|
|
|
|
*
|
|
|
|
* If there are no more option characters, getopt() returns -1. Then optind
|
|
|
|
* is the index in argv of the first argv-element that is not an option.
|
|
|
|
*
|
|
|
|
* The 'optstring' argument is a string containing the legitimate option
|
|
|
|
* characters. If such a character is followed by a colon, this indicates
|
|
|
|
* that the option requires an argument. If an argument is required for an
|
|
|
|
* option so getopt() places a pointer to the following text in the same
|
|
|
|
* argv-element, or the text of the following argv-element, in optarg.
|
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
* 1. opterr is not supported and this implementation of getopt() never
|
|
|
|
* printfs error messages.
|
|
|
|
* 2. getopt is NOT threadsafe!
|
|
|
|
* 3. This version of getopt() does not reset global variables until
|
|
|
|
* -1 is returned. As a result, your command line parsing loops
|
|
|
|
* must call getopt() repeatedly and continue to parse if other
|
|
|
|
* errors are returned ('?' or ':') until getopt() finally returns -1.
|
2021-04-04 21:47:39 +02:00
|
|
|
* (You can also set optind to -1 to force a reset).
|
|
|
|
* 4. Standard getopt() permutes the contents of argv as it scans, so that
|
|
|
|
* eventually all the nonoptions are at the end. This implementation
|
|
|
|
* does not do this.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Returned Value:
|
2018-02-01 17:00:02 +01:00
|
|
|
* If an option was successfully found, then getopt() returns the option
|
2020-02-07 03:06:03 +01:00
|
|
|
* character. If all command-line options have been parsed, then getopt()
|
2018-02-01 17:00:02 +01:00
|
|
|
* returns -1. If getopt() encounters an option character that was not
|
|
|
|
* in optstring, then '?' is returned. If getopt() encounters an option
|
|
|
|
* with a missing argument, then the return value depends on the first
|
|
|
|
* character in optstring: if it is ':', then ':' is returned; otherwise
|
|
|
|
* '?' is returned.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
int getopt(int argc, FAR char * const argv[], FAR const char *optstring)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2021-04-01 16:40:30 +02:00
|
|
|
return getopt_common(argc, argv, optstring, NULL, NULL, GETOPT_MODE);
|
2020-11-17 18:27:01 +01:00
|
|
|
}
|