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

getenv.3p (7045B)


  1. '\" et
  2. .TH GETENV "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. getenv
  12. \(em get value of an environment variable
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdlib.h>
  17. .P
  18. char *getenv(const char *\fIname\fP);
  19. .fi
  20. .SH DESCRIPTION
  21. The functionality described on this reference page is aligned with the
  22. ISO\ C standard. Any conflict between the requirements described here and the
  23. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  24. .P
  25. The
  26. \fIgetenv\fR()
  27. function shall search the environment of the calling process (see the Base Definitions volume of POSIX.1\(hy2017,
  28. .IR "Chapter 8" ", " "Environment Variables")
  29. for the environment variable
  30. .IR name
  31. if it exists and return a pointer to the value of the environment
  32. variable. If the specified environment variable cannot be found, a null
  33. pointer shall be returned. The application shall ensure that it does
  34. not modify the string pointed to by the
  35. \fIgetenv\fR()
  36. function.
  37. .P
  38. The returned string pointer might be invalidated or
  39. the string content might be overwritten by a subsequent call to
  40. \fIgetenv\fR(),
  41. \fIsetenv\fR(),
  42. \fIunsetenv\fR(),
  43. .br
  44. or (if supported)
  45. \fIputenv\fR()
  46. but they shall not be affected by a call to any other function in this volume of POSIX.1\(hy2017.
  47. .P
  48. The returned string pointer might also be invalidated if the calling
  49. thread is terminated.
  50. .P
  51. The
  52. \fIgetenv\fR()
  53. function need not be thread-safe.
  54. .SH "RETURN VALUE"
  55. Upon successful completion,
  56. \fIgetenv\fR()
  57. shall return a pointer to a string containing the
  58. .IR value
  59. for the specified
  60. .IR name .
  61. If the specified
  62. .IR name
  63. cannot be found in the environment of the calling process, a null
  64. pointer shall be returned.
  65. .SH ERRORS
  66. No errors are defined.
  67. .LP
  68. .IR "The following sections are informative."
  69. .SH EXAMPLES
  70. .SS "Getting the Value of an Environment Variable"
  71. .P
  72. The following example gets the value of the
  73. .IR HOME
  74. environment variable.
  75. .sp
  76. .RS 4
  77. .nf
  78. #include <stdlib.h>
  79. \&...
  80. const char *name = "HOME";
  81. char *value;
  82. .P
  83. value = getenv(name);
  84. .fi
  85. .P
  86. .RE
  87. .SH "APPLICATION USAGE"
  88. None.
  89. .SH RATIONALE
  90. The
  91. \fIclearenv\fR()
  92. function was considered but rejected. The
  93. \fIputenv\fR()
  94. function has now been included for alignment with the Single UNIX
  95. Specification.
  96. .P
  97. The
  98. \fIgetenv\fR()
  99. function is inherently not thread-safe because it returns a value
  100. pointing to static data.
  101. .P
  102. Conforming applications are required not to directly modify the pointers
  103. to which
  104. .IR environ
  105. points, but to use only the
  106. \fIsetenv\fR(),
  107. \fIunsetenv\fR(),
  108. and
  109. \fIputenv\fR()
  110. functions, or assignment to
  111. .IR environ
  112. itself, to manipulate the process environment. This constraint allows
  113. the implementation to properly manage the memory it allocates. This
  114. enables the implementation to free any space it has allocated to strings
  115. (and perhaps the pointers to them) stored in
  116. .IR environ
  117. when
  118. \fIunsetenv\fR()
  119. is called. A C runtime start-up procedure (that which invokes
  120. \fImain\fR()
  121. and perhaps initializes
  122. .IR environ )
  123. can also initialize a flag indicating that none of the environment has
  124. yet been copied to allocated storage, or that the separate table has
  125. not yet been initialized. If the application switches to a complete new
  126. environment by assigning a new value to
  127. .IR environ ,
  128. this can be detected by
  129. \fIgetenv\fR(),
  130. \fIsetenv\fR(),
  131. \fIunsetenv\fR(),
  132. or
  133. \fIputenv\fR()
  134. and the implementation can at that point reinitialize based on the new
  135. environment. (This may include copying the environment strings into a
  136. new array and assigning
  137. .IR environ
  138. to point to it.)
  139. .P
  140. In fact, for higher performance of
  141. \fIgetenv\fR(),
  142. implementations that do not provide
  143. \fIputenv\fR()
  144. could also maintain a separate copy of the environment in a data structure
  145. that could be searched much more quickly (such as an indexed hash table,
  146. or a binary tree), and update both it and the linear list at
  147. .IR environ
  148. when
  149. \fIsetenv\fR()
  150. or
  151. \fIunsetenv\fR()
  152. is invoked. On implementations that do provide
  153. \fIputenv\fR(),
  154. such a copy might still be worthwhile but would need to allow for the
  155. fact that applications can directly modify the content of environment
  156. strings added with
  157. \fIputenv\fR().
  158. For example, if an environment string found by searching the copy is
  159. one that was added using
  160. \fIputenv\fR(),
  161. the implementation would need to check that the string in
  162. .IR environ
  163. still has the same name (and value, if the copy includes values), and
  164. whenever searching the copy produces no match the implementation would
  165. then need to search each environment string in
  166. .IR environ
  167. that was added using
  168. \fIputenv\fR()
  169. in case any of them have changed their names and now match. Thus, each
  170. use of
  171. \fIputenv\fR()
  172. to add to the environment would reduce the speed advantage of having
  173. the copy.
  174. .P
  175. Performance of
  176. \fIgetenv\fR()
  177. can be important for applications which have large numbers of
  178. environment variables. Typically, applications like this use the
  179. environment as a resource database of user-configurable parameters.
  180. The fact that these variables are in the user's shell environment
  181. usually means that any other program that uses environment variables
  182. (such as
  183. .IR ls ,
  184. which attempts to use
  185. .IR COLUMNS ),
  186. or really almost any utility (\c
  187. .IR LANG ,
  188. .IR LC_ALL ,
  189. and so on) is similarly slowed down by the linear search through the
  190. variables.
  191. .P
  192. An implementation that maintains separate data structures, or even one
  193. that manages the memory it consumes, is not currently required as it
  194. was thought it would reduce consensus among implementors who do not
  195. want to change their historical implementations.
  196. .SH "FUTURE DIRECTIONS"
  197. A future version may add one or more functions to access and modify the
  198. environment in a thread-safe manner.
  199. .SH "SEE ALSO"
  200. .IR "\fIexec\fR\^",
  201. .IR "\fIputenv\fR\^(\|)",
  202. .IR "\fIsetenv\fR\^(\|)",
  203. .IR "\fIunsetenv\fR\^(\|)"
  204. .P
  205. The Base Definitions volume of POSIX.1\(hy2017,
  206. .IR "Chapter 8" ", " "Environment Variables",
  207. .IR "\fB<stdlib.h>\fP"
  208. .\"
  209. .SH COPYRIGHT
  210. Portions of this text are reprinted and reproduced in electronic form
  211. from IEEE Std 1003.1-2017, Standard for Information Technology
  212. -- Portable Operating System Interface (POSIX), The Open Group Base
  213. Specifications Issue 7, 2018 Edition,
  214. Copyright (C) 2018 by the Institute of
  215. Electrical and Electronics Engineers, Inc and The Open Group.
  216. In the event of any discrepancy between this version and the original IEEE and
  217. The Open Group Standard, the original IEEE and The Open Group Standard
  218. is the referee document. The original Standard can be obtained online at
  219. http://www.opengroup.org/unix/online.html .
  220. .PP
  221. Any typographical or formatting errors that appear
  222. in this page are most likely
  223. to have been introduced during the conversion of the source files to
  224. man page format. To report such errors, see
  225. https://www.kernel.org/doc/man-pages/reporting_bugs.html .