getoptv.c.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. getoptv.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='getifname.c.html' title=' getifname.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='getpib.c.html' title=' getpib.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * int getoptv (int argc, char const * argv[], char const * optv[]);
  24. *
  25. * getoptv.h
  26. *
  27. * this is a posix compliant getopt() function that supports no
  28. * extensions; see the posix website for specification details;
  29. *
  30. * &lt;http://www.opengroup.org/onlinepubs/007904975/functions/getopt.html&gt;
  31. *
  32. * we implemented this function to ensure that linux and windows
  33. * consoles act the same; microsoft c++ would not compile the
  34. * debian version of getopt and so, after trying to fix things,
  35. * we decided to start fresh; the debian version is too complex;
  36. *
  37. * this function conforms to posix standard; it does not support
  38. * gnu style extensions like &quot;--option&quot; for arguments or &quot;ab::c&quot;
  39. * for operands; if you don't know what that means then you won't
  40. * care, either; you should avoid such extentions, anyway;
  41. *
  42. * the posix standard says that command options and operands must
  43. * precede other arguments; this version of getopt allows options
  44. * and operands to appear anywhere and makes non-compliant argv[]
  45. * compliant in the process;
  46. *
  47. * we define characters instead of coding them so that microsoft
  48. * folks can use '/' instead of '-' and still preserve the posix
  49. * behaviour;
  50. *
  51. * we declare optarg as &quot;char const *&quot; so that the target cannot
  52. * be changed by the application; this is not POSIX compliant so
  53. * it will conflict with other getopt variants; getopt.h is often
  54. * included with unistd.h which is a common file;
  55. *
  56. * systems.
  57. *
  58. * this version calls virtually no functions and should compile
  59. * on any posix system;
  60. *
  61. * you may include getoptv.h or declare these variables:
  62. *
  63. * extern char const *optarg;
  64. * extern int optopt;
  65. * extern int optind;
  66. * extern int opterr;
  67. *
  68. * you may cut and paste this c language code segment to get you
  69. * started; you must insert your own code and case breaks;
  70. *
  71. * signed c;
  72. * optind = 1;
  73. * opterr = 1;
  74. *
  75. * while ((c = getoptv(argc, argv, * optv)) != -1)
  76. * {
  77. * switch(c)
  78. * {
  79. * case 'a': // optopt is 'a'; optarg is NULL;
  80. * case 'b': // optopt is 'b'; optarg is operand;
  81. * case ':': // optopt is option; optarg is NULL; missing operand;
  82. * case '?': // optopt is option; optarg is NULL; illegal option;
  83. * default: // optopt is option: optarg is NULL; illegal option;
  84. * }
  85. * }
  86. *
  87. * after options and operands are processed, optind points to
  88. * the next argv [] string; loop until optind equals argc or
  89. * argv[optind] is NULL; we check both but either will do;
  90. *
  91. * while ((optind &lt; argc) &amp;&amp; (argv [optind]))
  92. * {
  93. * // do stuff to argv[optind++].
  94. * }
  95. *
  96. * alternately, and even better, the following works just fine:
  97. *
  98. * argc -= optind;
  99. * argv += optind;
  100. * while ((argc) &amp;&amp; (* argv))
  101. * {
  102. * // do stuff to * argv.
  103. *
  104. * }
  105. *
  106. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  107. * Copyright (c) 2001-2006 by Charles Maier Associates;
  108. * licensed under the Internet Software Consortium License;
  109. *
  110. *--------------------------------------------------------------------*/
  111. #ifndef GETOPTV_SOURCE
  112. #define GETOPTV_SOURCE
  113. #include &lt;stdio.h&gt;
  114. #include &lt;stdlib.h&gt;
  115. #include &lt;string.h&gt;
  116. #include &quot;../tools/getoptv.h&quot;
  117. #include &quot;../tools/putoptv.h&quot;
  118. #include &quot;../tools/version.h&quot;
  119. #include &quot;../tools/error.h&quot;
  120. char const * program_name = &quot;program&quot;;
  121. char * optarg = (char *) (0);
  122. signed optopt = (char) (0);
  123. signed optind = 1;
  124. signed opterr = 1;
  125. signed optmin = 0;
  126. signed getoptv (int argc, char const * argv [], char const * optv [])
  127. {
  128. static char const * string;
  129. static char const * option;
  130. static signed count;
  131. signed index;
  132. if ((optind == 0) || (optind == 1))
  133. {
  134. for (program_name = string = * argv; * string; string++)
  135. {
  136. if ((* string == '/') || (* string == '\\'))
  137. {
  138. program_name = string + 1;
  139. }
  140. }
  141. string = (char *) (0);
  142. if (argc == optmin)
  143. {
  144. putoptv (optv);
  145. exit (0);
  146. }
  147. count = optind = 1;
  148. }
  149. while ((count &lt; argc) || (string))
  150. {
  151. if (string)
  152. {
  153. if (*string)
  154. {
  155. optarg = (char *) (0);
  156. optopt = *string++;
  157. for (option = * optv; * option; option++)
  158. {
  159. if (optopt == GETOPTV_C_OPERAND)
  160. {
  161. continue;
  162. }
  163. if (*option == GETOPTV_C_OPERAND)
  164. {
  165. continue;
  166. }
  167. if (*option == optopt)
  168. {
  169. option++;
  170. if (*option != GETOPTV_C_OPERAND)
  171. {
  172. return (optopt);
  173. }
  174. if (*string)
  175. {
  176. optarg = (char *) (string);
  177. string = (char *) (0);
  178. return (optopt);
  179. }
  180. if (count &lt; argc)
  181. {
  182. optarg = (char *)(argv [count]);
  183. for (index = count++; index &gt; optind; index--)
  184. {
  185. argv [index] = argv [index - 1];
  186. }
  187. argv [optind++] = optarg;
  188. return (optopt);
  189. }
  190. if (opterr)
  191. {
  192. error (1, 0, &quot;option '%c' needs an operand.&quot;, optopt);
  193. }
  194. if (** optv == GETOPTV_C_OPERAND)
  195. {
  196. return (GETOPTV_C_OPERAND);
  197. }
  198. return (GETOPTV_C_ILLEGAL);
  199. }
  200. }
  201. if (opterr)
  202. {
  203. error (1, 0, &quot;option '%c' has no meaning.&quot;, optopt);
  204. }
  205. return (GETOPTV_C_ILLEGAL);
  206. }
  207. else
  208. {
  209. string = (char *) (0);
  210. }
  211. }
  212. if (count &lt; argc)
  213. {
  214. string = argv [count];
  215. if (*string == GETOPTV_C_OPTION)
  216. {
  217. for (index = count; index &gt; optind; index--)
  218. {
  219. argv [index] = argv [index - 1];
  220. }
  221. argv [optind++] = string++;
  222. if (*string == GETOPTV_C_VERSION)
  223. {
  224. version ();
  225. exit (0);
  226. }
  227. if (*string == GETOPTV_C_SUMMARY)
  228. {
  229. putoptv (optv);
  230. exit (0);
  231. }
  232. if (*string == GETOPTV_C_OPTION)
  233. {
  234. string++;
  235. if (!strcmp (string, &quot;&quot;))
  236. {
  237. optarg = (char *) (0);
  238. optopt = (char) (0);
  239. return (-1);
  240. }
  241. if (!strcmp (string, &quot;version&quot;))
  242. {
  243. version ();
  244. exit (0);
  245. }
  246. if (!strcmp (string, &quot;help&quot;))
  247. {
  248. putoptv (optv);
  249. exit (0);
  250. }
  251. optarg = (char *)(string);
  252. optopt = GETOPTV_C_OPTION;
  253. return (-1);
  254. }
  255. }
  256. else
  257. {
  258. string = (char *) (0);
  259. }
  260. count++;
  261. }
  262. }
  263. optarg = (char *) (0);
  264. optopt = (char) (0);
  265. return (-1);
  266. }
  267. #endif
  268. </pre>
  269. <div class='footerlink'>
  270. [<a href='getifname.c.html' title=' getifname.c '>PREV</a>]
  271. [<a href='toolkit.html' title=' Index '>HOME</a>]
  272. [<a href='getpib.c.html' title=' getpib.c '>NEXT</a>]
  273. </div>
  274. </body>
  275. </html>