logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

getsubopt.3p (7571B)


  1. '\" et
  2. .TH GETSUBOPT "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. getsubopt
  12. \(em parse suboption arguments from a string
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdlib.h>
  17. .P
  18. int getsubopt(char **\fIoptionp\fP, char * const *\fIkeylistp\fP, char **\fIvaluep\fP);
  19. .fi
  20. .SH DESCRIPTION
  21. The
  22. \fIgetsubopt\fR()
  23. function shall parse suboption arguments in a flag argument. Such
  24. options often result from the use of
  25. \fIgetopt\fR().
  26. .P
  27. The
  28. \fIgetsubopt\fR()
  29. argument
  30. .IR optionp
  31. is a pointer to a pointer to the option argument string. The suboption
  32. arguments shall be separated by
  33. <comma>
  34. characters and each may consist of either a single token, or a token-value
  35. pair separated by an
  36. <equals-sign>.
  37. .P
  38. The
  39. .IR keylistp
  40. argument shall be a pointer to a vector of strings. The end of the
  41. vector is identified by a null pointer. Each entry in the vector is one
  42. of the possible tokens that might be found in *\fIoptionp\fP. Since
  43. <comma>
  44. characters delimit suboption arguments in
  45. .IR optionp ,
  46. they should not appear in any of the strings pointed to by
  47. .IR keylistp .
  48. Similarly, because an
  49. <equals-sign>
  50. separates a token from its value, the application should not include an
  51. <equals-sign>
  52. in any of the strings pointed to by
  53. .IR keylistp .
  54. The
  55. \fIgetsubopt\fR()
  56. function shall not modify the
  57. .IR keylistp
  58. vector.
  59. .P
  60. The
  61. .IR valuep
  62. argument is the address of a value string pointer.
  63. .P
  64. If a
  65. <comma>
  66. appears in
  67. .IR optionp ,
  68. it shall be interpreted as a suboption separator. After
  69. <comma>
  70. characters have been processed, if there are one or more
  71. <equals-sign>
  72. characters in a suboption string, the first
  73. <equals-sign>
  74. in any suboption string shall be interpreted as a separator between a
  75. token and a value. Subsequent
  76. <equals-sign>
  77. characters in a suboption string shall be interpreted as part of the
  78. value.
  79. .P
  80. If the string at *\fIoptionp\fP contains only one suboption argument
  81. (equivalently, no
  82. <comma>
  83. characters),
  84. \fIgetsubopt\fR()
  85. shall update *\fIoptionp\fP to point to the null character at the end of
  86. the string. Otherwise, it shall isolate the suboption argument by
  87. replacing the
  88. <comma>
  89. separator with a null character, and shall update *\fIoptionp\fP to point
  90. to the start of the next suboption argument. If the suboption argument
  91. has an associated value (equivalently, contains an
  92. <equals-sign>),
  93. \fIgetsubopt\fR()
  94. shall update *\fIvaluep\fP to point to the value's first character.
  95. Otherwise, it shall set *\fIvaluep\fP to a null pointer. The calling
  96. application may use this information to determine whether the presence
  97. or absence of a value for the suboption is an error.
  98. .P
  99. Additionally, when
  100. \fIgetsubopt\fR()
  101. fails to match the suboption argument with a token in the
  102. .IR keylistp
  103. array, the calling application should decide if this is an error, or if
  104. the unrecognized option should be processed in another way.
  105. .SH "RETURN VALUE"
  106. The
  107. \fIgetsubopt\fR()
  108. function shall return the index of the matched token string, or \-1
  109. if no token strings were matched.
  110. .SH ERRORS
  111. No errors are defined.
  112. .LP
  113. .IR "The following sections are informative."
  114. .SH EXAMPLES
  115. .SS "Parsing Suboptions"
  116. .P
  117. The following example uses the
  118. \fIgetsubopt\fR()
  119. function to parse a
  120. .IR value
  121. argument in the
  122. .IR optarg
  123. external variable returned by a call to
  124. \fIgetopt\fR().
  125. .sp
  126. .RS 4
  127. .nf
  128. #include <stdio.h>
  129. #include <stdlib.h>
  130. #include <unistd.h>
  131. .P
  132. int do_all;
  133. const char *type;
  134. int read_size;
  135. int write_size;
  136. int read_only;
  137. .P
  138. enum
  139. {
  140. RO_OPTION = 0,
  141. RW_OPTION,
  142. READ_SIZE_OPTION,
  143. WRITE_SIZE_OPTION
  144. };
  145. .P
  146. const char *mount_opts[] =
  147. {
  148. [RO_OPTION] = "ro",
  149. [RW_OPTION] = "rw",
  150. [READ_SIZE_OPTION] = "rsize",
  151. [WRITE_SIZE_OPTION] = "wsize",
  152. NULL
  153. };
  154. .P
  155. int
  156. main(int argc, char *argv[])
  157. {
  158. char *subopts, *value;
  159. int opt;
  160. .P
  161. while ((opt = getopt(argc, argv, "at:o:")) != -1)
  162. switch(opt)
  163. {
  164. case \(aqa\(aq:
  165. do_all = 1;
  166. break;
  167. case \(aqt\(aq:
  168. type = optarg;
  169. break;
  170. case \(aqo\(aq:
  171. subopts = optarg;
  172. while (*subopts != \(aq\0\(aq)
  173. {
  174. char *saved = subopts;
  175. switch(getsubopt(&subopts, (char **)mount_opts,
  176. &value))
  177. {
  178. case RO_OPTION:
  179. read_only = 1;
  180. break;
  181. case RW_OPTION:
  182. read_only = 0;
  183. break;
  184. case READ_SIZE_OPTION:
  185. if (value == NULL)
  186. abort();
  187. read_size = atoi(value);
  188. break;
  189. case WRITE_SIZE_OPTION:
  190. if (value == NULL)
  191. abort();
  192. write_size = atoi(value);
  193. break;
  194. default:
  195. /* Unknown suboption. */
  196. printf("Unknown suboption `%s\(aq\en", saved);
  197. abort();
  198. }
  199. }
  200. break;
  201. default:
  202. abort();
  203. }
  204. .P
  205. /* Do the real work. */
  206. .P
  207. return 0;
  208. }
  209. .fi
  210. .P
  211. .RE
  212. .P
  213. If the above example is invoked with:
  214. .sp
  215. .RS 4
  216. .nf
  217. program -o ro,rsize=512
  218. .fi
  219. .P
  220. .RE
  221. .P
  222. then after option parsing, the variable
  223. .IR do_all
  224. will be 0,
  225. .IR type
  226. will be a null pointer,
  227. .IR read_size
  228. will be 512,
  229. .IR write_size
  230. will be 0, and
  231. .IR read_only
  232. will be 1. If it is invoked with:
  233. .sp
  234. .RS 4
  235. .nf
  236. program -o oops
  237. .fi
  238. .P
  239. .RE
  240. .P
  241. it will print:
  242. .sp
  243. .RS 4
  244. .nf
  245. "Unknown suboption `oops\(aq"
  246. .fi
  247. .P
  248. .RE
  249. .P
  250. before aborting.
  251. .SH "APPLICATION USAGE"
  252. The value of *\fIvaluep\fR when
  253. \fIgetsubopt\fR()
  254. returns \-1 is unspecified. Historical implementations provide various
  255. incompatible extensions to allow an application to access the suboption
  256. text that was not found in the
  257. .IR keylistp
  258. array.
  259. .SH RATIONALE
  260. The
  261. .IR keylistp
  262. argument of
  263. \fIgetsubopt\fR()
  264. is typed as
  265. .BR "char * const *"
  266. to match historical practice. However, the standard is clear that
  267. implementations will not modify either the array or the strings contained
  268. in the array, as if the argument had been typed
  269. .BR "const char * const *" .
  270. .SH "FUTURE DIRECTIONS"
  271. None.
  272. .SH "SEE ALSO"
  273. .IR "\fIgetopt\fR\^(\|)"
  274. .P
  275. The Base Definitions volume of POSIX.1\(hy2017,
  276. .IR "\fB<stdlib.h>\fP"
  277. .\"
  278. .SH COPYRIGHT
  279. Portions of this text are reprinted and reproduced in electronic form
  280. from IEEE Std 1003.1-2017, Standard for Information Technology
  281. -- Portable Operating System Interface (POSIX), The Open Group Base
  282. Specifications Issue 7, 2018 Edition,
  283. Copyright (C) 2018 by the Institute of
  284. Electrical and Electronics Engineers, Inc and The Open Group.
  285. In the event of any discrepancy between this version and the original IEEE and
  286. The Open Group Standard, the original IEEE and The Open Group Standard
  287. is the referee document. The original Standard can be obtained online at
  288. http://www.opengroup.org/unix/online.html .
  289. .PP
  290. Any typographical or formatting errors that appear
  291. in this page are most likely
  292. to have been introduced during the conversion of the source files to
  293. man page format. To report such errors, see
  294. https://www.kernel.org/doc/man-pages/reporting_bugs.html .