getopt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*****************************************************************************
  2. *
  3. * MODULE NAME : GETOPT.C
  4. *
  5. * COPYRIGHTS:
  6. * This module contains code made available by IBM
  7. * Corporation on an AS IS basis. Any one receiving the
  8. * module is considered to be licensed under IBM copyrights
  9. * to use the IBM-provided source code in any way he or she
  10. * deems fit, including copying it, compiling it, modifying
  11. * it, and redistributing it, with or without
  12. * modifications. No license under any IBM patents or
  13. * patent applications is to be implied from this copyright
  14. * license.
  15. *
  16. * A user of the module should understand that IBM cannot
  17. * provide technical support for the module and will not be
  18. * responsible for any consequences of use of the program.
  19. *
  20. * Any notices, including this one, are not to be removed
  21. * from the module without the prior written consent of
  22. * IBM.
  23. *
  24. * AUTHOR: Original author:
  25. * G. R. Blair (BOBBLAIR at AUSVM1)
  26. * Internet: bobblair@bobblair.austin.ibm.com
  27. *
  28. * Extensively revised by:
  29. * John Q. Walker II, Ph.D. (JOHHQ at RALVM6)
  30. * Internet: johnq@ralvm6.vnet.ibm.com
  31. *
  32. *****************************************************************************/
  33. /******************************************************************************
  34. * getopt()
  35. *
  36. * The getopt() function is a command line parser. It returns the next
  37. * option character in argv that matches an option character in opstring.
  38. *
  39. * The argv argument points to an array of argc+1 elements containing argc
  40. * pointers to character strings followed by a null pointer.
  41. *
  42. * The opstring argument points to a string of option characters; if an
  43. * option character is followed by a colon, the option is expected to have
  44. * an argument that may or may not be separated from it by white space.
  45. * The external variable optarg is set to point to the start of the option
  46. * argument on return from getopt().
  47. *
  48. * The getopt() function places in optind the argv index of the next argument
  49. * to be processed. The system initializes the external variable optind to
  50. * 1 before the first call to getopt().
  51. *
  52. * When all options have been processed (that is, up to the first nonoption
  53. * argument), getopt() returns EOF. The special option "--" may be used to
  54. * delimit the end of the options; EOF will be returned, and "--" will be
  55. * skipped.
  56. *
  57. * The getopt() function returns a question mark (?) when it encounters an
  58. * option character not included in opstring. This error message can be
  59. * disabled by setting opterr to zero. Otherwise, it returns the option
  60. * character that was detected.
  61. *
  62. * If the special option "--" is detected, or all options have been
  63. * processed, EOF is returned.
  64. *
  65. * Options are marked by either a minus sign (-) or a slash (/).
  66. *
  67. * No errors are defined.
  68. *****************************************************************************/
  69. #include <stdio.h> /* for EOF */
  70. #include <string.h> /* for strchr() */
  71. /* static (global) variables that are specified as exported by getopt() */
  72. char *optarg = NULL; /* pointer to the start of the option argument */
  73. int optind = 1; /* number of the next argv[] to be evaluated */
  74. int opterr = 1; /* non-zero if a question mark should be returned
  75. when a non-valid option character is detected */
  76. /* handle possible future character set concerns by putting this in a macro */
  77. #define _next_char(string) (char)(*(string+1))
  78. int getopt(int argc, char *argv[], char *opstring)
  79. {
  80. static char *pIndexPosition = NULL; /* place inside current argv string */
  81. char *pArgString = NULL; /* where to start from next */
  82. char *pOptString; /* the string in our program */
  83. if (pIndexPosition != NULL) {
  84. /* we last left off inside an argv string */
  85. if (*(++pIndexPosition)) {
  86. /* there is more to come in the most recent argv */
  87. pArgString = pIndexPosition;
  88. }
  89. }
  90. if (pArgString == NULL) {
  91. /* we didn't leave off in the middle of an argv string */
  92. if (optind >= argc) {
  93. /* more command-line arguments than the argument count */
  94. pIndexPosition = NULL; /* not in the middle of anything */
  95. return EOF; /* used up all command-line arguments */
  96. }
  97. /*---------------------------------------------------------------------
  98. * If the next argv[] is not an option, there can be no more options.
  99. *-------------------------------------------------------------------*/
  100. pArgString = argv[optind++]; /* set this to the next argument ptr */
  101. if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */
  102. ('-' != *pArgString)) {
  103. --optind; /* point to current arg once we're done */
  104. optarg = NULL; /* no argument follows the option */
  105. pIndexPosition = NULL; /* not in the middle of anything */
  106. return EOF; /* used up all the command-line flags */
  107. }
  108. /* check for special end-of-flags markers */
  109. if ((strcmp(pArgString, "-") == 0) ||
  110. (strcmp(pArgString, "--") == 0)) {
  111. optarg = NULL; /* no argument follows the option */
  112. pIndexPosition = NULL; /* not in the middle of anything */
  113. return EOF; /* encountered the special flag */
  114. }
  115. pArgString++; /* look past the / or - */
  116. }
  117. if (':' == *pArgString) { /* is it a colon? */
  118. /*---------------------------------------------------------------------
  119. * Rare case: if opterr is non-zero, return a question mark;
  120. * otherwise, just return the colon we're on.
  121. *-------------------------------------------------------------------*/
  122. return (opterr ? (int)'?' : (int)':');
  123. }
  124. else if ((pOptString = strchr(opstring, *pArgString)) == 0) {
  125. /*---------------------------------------------------------------------
  126. * The letter on the command-line wasn't any good.
  127. *-------------------------------------------------------------------*/
  128. optarg = NULL; /* no argument follows the option */
  129. pIndexPosition = NULL; /* not in the middle of anything */
  130. return (opterr ? (int)'?' : (int)*pArgString);
  131. }
  132. else {
  133. /*---------------------------------------------------------------------
  134. * The letter on the command-line matches one we expect to see
  135. *-------------------------------------------------------------------*/
  136. if (':' == _next_char(pOptString)) { /* is the next letter a colon? */
  137. /* It is a colon. Look for an argument string. */
  138. if ('\0' != _next_char(pArgString)) { /* argument in this argv? */
  139. optarg = &pArgString[1]; /* Yes, it is */
  140. }
  141. else {
  142. /*-------------------------------------------------------------
  143. * The argument string must be in the next argv.
  144. * But, what if there is none (bad input from the user)?
  145. * In that case, return the letter, and optarg as NULL.
  146. *-----------------------------------------------------------*/
  147. if (optind < argc)
  148. optarg = argv[optind++];
  149. else {
  150. optarg = NULL;
  151. return (opterr ? (int)'?' : (int)*pArgString);
  152. }
  153. }
  154. pIndexPosition = NULL; /* not in the middle of anything */
  155. }
  156. else {
  157. /* it's not a colon, so just return the letter */
  158. optarg = NULL; /* no argument follows the option */
  159. pIndexPosition = pArgString; /* point to the letter we're on */
  160. }
  161. return (int)*pArgString; /* return the letter that matched */
  162. }
  163. }