mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Sidney Manning" <sidneym@codeaurora.org>
To: <musl@lists.openwall.com>
Subject: getopt_long_only
Date: Thu, 13 Oct 2016 09:42:40 -0500	[thread overview]
Message-ID: <002301d22560$0d79a610$286cf230$@codeaurora.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 1189 bytes --]

I may have found an issue with this API.  When a double hyphen is
encountered the API is supposed to return a -1 but I'm seeing a '?'.

My reference is from the Solaris docs which say: "The special option "--"
can be used to delimit the end of the options; when it is encountered, -1 is
returned and "--" is skipped.  I attached a test case to show the problem.

I made the following change it will cause the code to punt to getopt when
just  a "--" is found:
argv[optind][0] == '-'
argv[optind][1] == '-'
argv[optind][2] == NULL

diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 480c001..9764f56 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -53,7 +53,7 @@ static int __getopt_long_core(int argc, char *const *argv,
con
 {
        optarg = 0;
        if (longopts && argv[optind][0] == '-' &&
-               ((longonly && argv[optind][1]) ||
+               ((longonly && argv[optind][1]) &&
                 (argv[optind][1] == '-' && argv[optind][2])))
        {
                int colon =
optstring[optstring[0]=='+'||optstring[0]=='-']==':'


Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by
The Linux Foundation


[-- Attachment #2: getopt.c --]
[-- Type: text/plain, Size: 963 bytes --]

#define _GNU_SOURCE
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>

struct option options[] =
{
  {
    "stop-at-entry", 0, 0, 's'
  },
  {
    "shell", 1, 0, 'c'
  },
  {
    NULL, 0, 0, 0
  }
};

extern int optreset;
int main(void)
{
  int val;
  optind = 1;
  //optreset = 1;

  int argc = 3;
  char *argv[] = {"dummy_arg", "--shell=/bin/sh", "--", NULL};
  int longindex = -1;
  char *optstring = "sc::";

  printf("**optind is %d, argv[%d] is %s\n", optind, optind, argv[optind]);
  val = getopt_long_only(argc, argv, optstring, &options[0], &longindex);
  if (val != 'c') {
    printf ("%d: getopt_long_only FAIL\n", __LINE__);
    return 1;
  }
  printf("**optind is %d, argv[%d] is %s\n", optind, optind, argv[optind]);
  val = getopt_long_only(argc, argv, optstring, &options[0], &longindex);
  if (val != -1) {
    printf ("%d: getopt_long_only FAIL\n", __LINE__);
    return 1;
  }
  return 0;
}

             reply	other threads:[~2016-10-13 14:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-13 14:42 Sidney Manning [this message]
2016-10-13 17:43 ` getopt_long_only Daniel Sabogal
2016-10-13 18:40   ` getopt_long_only Rich Felker
2016-10-20  5:54     ` getopt_long_only Rich Felker
2016-10-20 16:17       ` getopt_long_only Rich Felker
2016-10-13 18:03 getopt_long_only Sidney Manning

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='002301d22560$0d79a610$286cf230$@codeaurora.org' \
    --to=sidneym@codeaurora.org \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).