From c2cc01c920ebb4eaea0917186f8a571669739dfa Mon Sep 17 00:00:00 2001 From: Brendan Kelly Date: Fri, 18 Nov 2022 23:39:51 -0500 Subject: [PATCH] LibC: Treat argument "-" the same as arguments that don't start with "-" This causes `echo -` to output "-" instead of crashing --- Userland/Libraries/LibC/getopt.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibC/getopt.cpp b/Userland/Libraries/LibC/getopt.cpp index 9819f356d3..c588aff02d 100644 --- a/Userland/Libraries/LibC/getopt.cpp +++ b/Userland/Libraries/LibC/getopt.cpp @@ -321,15 +321,14 @@ bool OptionParser::find_next_option() for (m_arg_index = optind; m_arg_index < m_argc && m_argv[m_arg_index]; m_arg_index++) { StringView arg = current_arg(); // Anything that doesn't start with a "-" is not an option. - if (!arg.starts_with('-')) { + // As a special case, a single "-" is not an option either. + // (It's typically used by programs to refer to stdin). + if (!arg.starts_with('-') || arg == "-") { if (m_stop_on_first_non_option) return false; continue; } - // As a special case, a single "-" is not an option either. - // (It's typically used by programs to refer to stdin). - if (arg == "-") - continue; + // As another special case, a "--" is not an option either, and we stop // looking for further options if we encounter it. if (arg == "--")