]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/setenv.3
setenv.3: Improve ERRORS section
[thirdparty/man-pages.git] / man3 / setenv.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright (C) 2004, 2007 Michael kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .\" References consulted:
25 .\" Linux libc source code
26 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" 386BSD man pages
28 .\" Modified Sat Jul 24 18:20:58 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified Fri Feb 14 21:47:50 1997 by Andries Brouwer (aeb@cwi.nl)
30 .\" Modified 9 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Changed unsetenv() prototype; added EINVAL error
32 .\" Noted non-standard behavior of setenv() if name contains '='
33 .\" 2005-08-12, mtk, glibc 2.3.4 fixed the "name contains '='" bug
34 .\"
35 .TH SETENV 3 2009-09-20 "GNU" "Linux Programmer's Manual"
36 .SH NAME
37 setenv \- change or add an environment variable
38 .SH SYNOPSIS
39 .nf
40 .B #include <stdlib.h>
41 .sp
42 .BI "int setenv(const char *" name ", const char *" value ", int " overwrite );
43 .sp
44 .BI "int unsetenv(const char *" name );
45 .fi
46 .sp
47 .in -4n
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .in
51 .sp
52 .ad l
53 .BR setenv (),
54 .BR unsetenv ():
55 _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
56 .ad b
57 .SH DESCRIPTION
58 The
59 .BR setenv ()
60 function adds the variable \fIname\fP to the
61 environment with the value \fIvalue\fP, if \fIname\fP does not
62 already exist.
63 If \fIname\fP does exist in the environment, then
64 its value is changed to \fIvalue\fP if \fIoverwrite\fP is non-zero;
65 if \fIoverwrite\fP is zero, then the value of \fIname\fP is not
66 changed.
67 This function makes copies of the strings pointed to by
68 .I name
69 and
70 .I value
71 (by contrast with
72 .BR putenv (3)).
73 .PP
74 The
75 .BR unsetenv ()
76 function deletes the variable \fIname\fP from
77 the environment.
78 If
79 .I name
80 does not exist in the environment,
81 then the function succeeds, and the environment is unchanged.
82 .SH "RETURN VALUE"
83 The
84 .BR setenv ()
85 function returns zero on success,
86 or \-1 on error, with
87 .I errno
88 set to indicate the cause of the error.
89
90 The
91 .BR unsetenv ()
92 function returns zero on success,
93 or \-1 on error, with
94 .I errno
95 set to indicate the cause of the error.
96 .SH "ERRORS"
97 .TP
98 .B EINVAL
99 .I name
100 is NULL, points to a string of length 0,
101 or contains an \(aq=\(aq character.
102 .TP
103 .B ENOMEM
104 Insufficient memory to add a new variable to the environment.
105 .SH "CONFORMING TO"
106 4.3BSD, POSIX.1-2001.
107 .SH "NOTES"
108 POSIX.1-2001 does not require
109 .BR setenv ()
110 or
111 .BR unsetenv ()
112 to be reentrant.
113
114 Prior to glibc 2.2.2,
115 .BR unsetenv ()
116 was prototyped
117 as returning \fIvoid\fP; more recent glibc versions follow the
118 POSIX.1-2001-compliant prototype shown in the SYNOPSIS.
119 .SH BUGS
120 POSIX.1-2001 specifies that if
121 .I name
122 contains an \(aq=\(aq character, then
123 .BR setenv ()
124 should fail with the error
125 .BR EINVAL ;
126 however, versions of glibc before 2.3.4 allowed an \(aq=\(aq sign in
127 .IR name .
128 .SH "SEE ALSO"
129 .BR clearenv (3),
130 .BR getenv (3),
131 .BR putenv (3),
132 .BR environ (7)