1PUTENV(3P) POSIX Programmer's Manual PUTENV(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 putenv — change or add a value to an environment
14
16 #include <stdlib.h>
17
18 int putenv(char *string);
19
21 The putenv() function shall use the string argument to set environment
22 variable values. The string argument should point to a string of the
23 form "name=value". The putenv() function shall make the value of the
24 environment variable name equal to value by altering an existing vari‐
25 able or creating a new one. In either case, the string pointed to by
26 string shall become part of the environment, so altering the string
27 shall change the environment.
28
29 The putenv() function need not be thread-safe.
30
32 Upon successful completion, putenv() shall return 0; otherwise, it
33 shall return a non-zero value and set errno to indicate the error.
34
36 The putenv() function may fail if:
37
38 ENOMEM Insufficient memory was available.
39
40 The following sections are informative.
41
43 Changing the Value of an Environment Variable
44 The following example changes the value of the HOME environment vari‐
45 able to the value /usr/home.
46
47 #include <stdlib.h>
48 ...
49 static char *var = "HOME=/usr/home";
50 int ret;
51
52 ret = putenv(var);
53
55 The putenv() function manipulates the environment pointed to by envi‐
56 ron, and can be used in conjunction with getenv().
57
58 See exec() for restrictions on changing the environment in multi-
59 threaded applications.
60
61 This routine may use malloc() to enlarge the environment.
62
63 A potential error is to call putenv() with an automatic variable as the
64 argument, then return from the calling function while string is still
65 part of the environment.
66
67 Although the space used by string is no longer used once a new string
68 which defines name is passed to putenv(), if any thread in the applica‐
69 tion has used getenv() to retrieve a pointer to this variable, it
70 should not be freed by calling free(). If the changed environment
71 variable is one known by the system (such as the locale environment
72 variables) the application should never free the buffer used by earlier
73 calls to putenv() for the same variable.
74
75 The setenv() function is preferred over this function. One reason is
76 that putenv() is optional and therefore less portable. Another is that
77 using putenv() can slow down environment searches, as explained in the
78 RATIONALE section for getenv().
79
81 Refer to the RATIONALE section in setenv().
82
84 None.
85
87 exec, free(), getenv(), malloc(), setenv()
88
89 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
90
92 Portions of this text are reprinted and reproduced in electronic form
93 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
94 -- Portable Operating System Interface (POSIX), The Open Group Base
95 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
96 cal and Electronics Engineers, Inc and The Open Group. (This is
97 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
98 event of any discrepancy between this version and the original IEEE and
99 The Open Group Standard, the original IEEE and The Open Group Standard
100 is the referee document. The original Standard can be obtained online
101 at http://www.unix.org/online.html .
102
103 Any typographical or formatting errors that appear in this page are
104 most likely to have been introduced during the conversion of the source
105 files to man page format. To report such errors, see https://www.ker‐
106 nel.org/doc/man-pages/reporting_bugs.html .
107
108
109
110IEEE/The Open Group 2013 PUTENV(3P)