utils_getopt.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2017-2019 Tencent Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "utils_getopt.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "qcloud_iot_import.h"
  22. static int utils_opterr = 1; /* if error message should be printed */
  23. static int utils_optind = 1; /* index into parent argv vector */
  24. static int utils_optopt; /* character checked for validity */
  25. static int utils_optreset = 1; /* reset getopt */
  26. char *utils_optarg; /* argument associated with option */
  27. int utils_getopt(int nargc, char *const *nargv, const char *options)
  28. {
  29. #define BADCH (int)'?'
  30. #define BADARG (int)':'
  31. #define EMSG ""
  32. static char *place = EMSG; /* option letter processing */
  33. const char * oli; /* option letter list index */
  34. if (utils_optreset || !*place) { /* update scanning pointer */
  35. utils_optreset = 0;
  36. if (utils_optind >= nargc || *(place = nargv[utils_optind]) != '-') {
  37. utils_optind = 1;
  38. utils_optreset = 1;
  39. place = EMSG;
  40. return (-1);
  41. }
  42. place++;
  43. }
  44. /* option letter okay? */
  45. if ((utils_optopt = (int)*place++) == (int)':' || !(oli = strchr(options, utils_optopt))) {
  46. /*
  47. * if the user didn't specify '-' as an option,
  48. * assume it means -1.
  49. */
  50. if (utils_optopt == (int)'-')
  51. return (-1);
  52. if (!*place)
  53. ++utils_optind;
  54. if (utils_opterr && *options != ':')
  55. HAL_Printf("illegal option - %c\n", utils_optopt);
  56. return (BADCH);
  57. }
  58. if (*++oli != ':') { /* don't need argument */
  59. utils_optarg = NULL;
  60. if (!*place)
  61. ++utils_optind;
  62. } else {
  63. /* need an argument */
  64. if (*place) /* no white space */
  65. utils_optarg = place;
  66. else if (nargc <= ++utils_optind) { /* no arg */
  67. place = EMSG;
  68. if (*options == ':')
  69. return (BADARG);
  70. if (utils_opterr)
  71. HAL_Printf("option requires an argument - %c\n", utils_optopt);
  72. return (BADCH);
  73. } else /* white space */
  74. utils_optarg = nargv[utils_optind];
  75. place = EMSG;
  76. ++utils_optind;
  77. }
  78. /* dump back option letter */
  79. return (utils_optopt);
  80. }