File: putenv.3

package info (click to toggle)
manpages 6.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,184 kB
  • sloc: sh: 575; python: 222; perl: 190; makefile: 29; lisp: 22
file content (152 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'\" t
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH putenv 3 2025-05-17 "Linux man-pages (unreleased)"
.SH NAME
putenv \- change or add an environment variable
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #include <stdlib.h>
.P
.BI "int putenv(char *" string );
.\" Not: const char *
.fi
.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.P
.BR putenv ():
.nf
    _XOPEN_SOURCE
        || /* glibc >= 2.19: */ _DEFAULT_SOURCE
        || /* glibc <= 2.19: */ _SVID_SOURCE
.fi
.SH DESCRIPTION
The
.BR putenv ()
function adds or changes the value of environment
variables.
The argument
.I string
is of the form
.IR name = value .
If
.I name
does not already exist in the environment, then
.I string
is added to the environment.
If
.I name
does exist,
then the value of
.I name
in the environment is changed to
.IR value .
The string pointed to by
.I string
becomes part of the environment,
so altering the string changes the environment.
.SH RETURN VALUE
The
.BR putenv ()
function returns zero on success.
On failure, it returns a nonzero value, and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B ENOMEM
Insufficient space to allocate new environment.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR putenv ()
T}	Thread safety	MT-Unsafe const:env
.TE
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
POSIX.1-2001, SVr2, 4.3BSD-Reno.
.P
The
.BR putenv ()
function is not required to be reentrant, and the
one in glibc 2.0 is not, but the glibc 2.1 version is.
.\" .P
.\" Description for libc4, libc5, glibc:
.\" If the argument
.\" .I string
.\" is of the form
.\" .IR name ,
.\" and does not contain an \[aq]=\[aq] character, then the variable
.\" .I name
.\" is removed from the environment.
.\" If
.\" .BR putenv ()
.\" has to allocate a new array
.\" .IR environ ,
.\" and the previous array was also allocated by
.\" .BR putenv (),
.\" then it will be freed.
.\" In no case will the old storage associated
.\" to the environment variable itself be freed.
.P
Since glibc 2.1.2, the glibc implementation conforms to SUSv2:
the pointer
.I string
given to
.BR putenv ()
is used.
In particular, this string becomes part of the environment;
changing it later will change the environment.
(Thus, it is an error to call
.BR putenv ()
with an automatic variable
as the argument, then return from the calling function while
.I string
is still part of the environment.)
However, from glibc 2.0 to glibc 2.1.1, it differs:
a copy of the string is used.
On the one hand this causes a memory leak, and on the other hand
it violates SUSv2.
.P
The 4.3BSD-Reno version, like glibc 2.0, uses a copy;
this is fixed in all modern BSDs.
.P
SUSv2 removes the
.I const
from the prototype, and so does glibc 2.1.3.
.P
The GNU C library implementation provides a nonstandard extension.
If
.I string
does not include an equal sign:
.P
.in +4n
.EX
putenv("NAME");
.EE
.in
.P
then the named variable is removed from the caller's environment.
.SH SEE ALSO
.BR clearenv (3),
.BR getenv (3),
.BR setenv (3),
.BR unsetenv (3),
.BR environ (7)