aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2023-03-28 23:15:27 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-03-28 23:15:27 +0000
commit4ddb4d6207abb7450741030ada819efb8f047154 (patch)
tree28523674f3e6fad32dbec47e22b3ba1d223b80eb
parent3fb5ab5447bf9a11c4b70072ee0d0d245c31df61 (diff)
parentd3baa759cc26a167b08db77b35c5cf954e22031d (diff)
downloadmusl-4ddb4d6207abb7450741030ada819efb8f047154.tar.gz
Implement getopt via getopt_long to support glibc-style non-options before options am: d3baa759cc
Original change: https://android-review.googlesource.com/c/platform/external/musl/+/2430840 Change-Id: I0bc2ccd7f4e4dd4891f5bca593ec9febabf67e15 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--src/misc/getopt.c4
-rw-r--r--src/misc/getopt_long.c21
2 files changed, 21 insertions, 4 deletions
diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index c3f66995..f8df7aea 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -25,7 +25,7 @@ void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
FUNLOCK(f);
}
-int getopt(int argc, char * const argv[], const char *optstring)
+int __posix_getopt(int argc, char * const argv[], const char *optstring)
{
int i;
wchar_t c, d;
@@ -101,5 +101,3 @@ int getopt(int argc, char * const argv[], const char *optstring)
}
return c;
}
-
-weak_alias(getopt, __posix_getopt);
diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 6949ab1c..117d1a1d 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -9,6 +9,8 @@
extern int __optpos, __optreset;
+int __posix_getopt(int argc, char * const argv[], const char *optstring);
+
static void permute(char *const *argv, int dest, int src)
{
char **av = (char **)argv;
@@ -134,7 +136,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
return '?';
}
}
- return getopt(argc, argv, optstring);
+ return __posix_getopt(argc, argv, optstring);
}
int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
@@ -146,3 +148,20 @@ int getopt_long_only(int argc, char *const *argv, const char *optstring, const s
{
return __getopt_long(argc, argv, optstring, longopts, idx, 1);
}
+
+/* ANDROID CHANGE: implement getopt via getopt_long to continue parsing options
+ * after the first non-option argument to match the user visible behavior of
+ * glibc.
+ */
+int getopt(int argc, char * const argv[], const char *optstring)
+{
+ static int posixly_correct = -1;
+
+ if (posixly_correct == -1 || __optreset)
+ posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
+
+ if (posixly_correct)
+ return __posix_getopt(argc, argv, optstring);
+ else
+ return __getopt_long(argc, argv, optstring, NULL, NULL, 0);
+}