getsubopt.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/StringView.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsubopt.html
  11. int getsubopt(char** option_array, char* const* tokens, char** option_value)
  12. {
  13. if (**option_array == '\0')
  14. return -1;
  15. auto const* option_ptr = *option_array;
  16. StringView option_string { option_ptr, strlen(option_ptr) };
  17. auto possible_comma_location = option_string.find(',');
  18. char* option_end = const_cast<char*>(option_string.characters_without_null_termination()) + possible_comma_location.value_or(option_string.length());
  19. auto possible_equals_char_location = option_string.find('=');
  20. char* value_start = option_end;
  21. if (possible_equals_char_location.has_value()) {
  22. value_start = const_cast<char*>(option_string.characters_without_null_termination()) + possible_equals_char_location.value();
  23. }
  24. ScopeGuard ensure_end_array_contains_null_char([&]() {
  25. if (*option_end != '\0')
  26. *option_end++ = '\0';
  27. *option_array = option_end;
  28. });
  29. for (int count = 0; tokens[count] != NULL; ++count) {
  30. auto const* token = tokens[count];
  31. StringView token_stringview { token, strlen(token) };
  32. if (!option_string.starts_with(token_stringview))
  33. continue;
  34. if (tokens[count][value_start - *option_array] != '\0')
  35. continue;
  36. *option_value = value_start != option_end ? value_start + 1 : nullptr;
  37. return count;
  38. }
  39. // Note: The current sub-option does not match any option, so prepare to tell this
  40. // to the application.
  41. *option_value = *option_array;
  42. return -1;
  43. }