xref: /freebsd/share/man/man9/getenv.9 (revision 0aeed3e99367bed5755068d9218cd8041644ff2b)
1.\" -*- nroff -*-
2.\"
3.\" Copyright (c) 2013 Hudson River Trading LLC
4.\" Written by: John H. Baldwin <jhb@FreeBSD.org>
5.\" All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $FreeBSD$
29.\"
30.Dd October 22, 2015
31.Dt GETENV 9
32.Os
33.Sh NAME
34.Nm freeenv ,
35.Nm getenv ,
36.Nm getenv_int ,
37.Nm getenv_long ,
38.Nm getenv_string ,
39.Nm getenv_quad ,
40.Nm getenv_uint ,
41.Nm getenv_ulong ,
42.Nm setenv ,
43.Nm testenv ,
44.Nm unsetenv
45.Nd kernel environment variable functions
46.Sh SYNOPSIS
47.In sys/param.h
48.In sys/systm.h
49.Ft void
50.Fn freeenv "char *env"
51.Ft char *
52.Fn getenv "const char *name"
53.Ft int
54.Fn getenv_int "const char *name" "int *data"
55.Ft int
56.Fn getenv_long "const char *name" "long *data"
57.Ft int
58.Fn getenv_string "const char *name" "char *data" "int size"
59.Ft int
60.Fn getenv_quad "const char *name" "quad_t *data"
61.Ft int
62.Fn getenv_uint "const char *name" "unsigned int *data"
63.Ft int
64.Fn getenv_ulong "const char *name" "unsigned long *data"
65.Ft int
66.Fn setenv "const char *name" "const char *value"
67.Ft int
68.Fn testenv "const char *name"
69.Ft int
70.Fn unsetenv "const char *name"
71.Sh DESCRIPTION
72These functions set, unset, fetch, and parse variables from the kernel's
73environment.
74.Pp
75The
76.Fn getenv
77function obtains the current value of the kernel environment variable
78.Fa name
79and returns a pointer to the string value.
80The caller should not modify the string pointed to by the return value.
81The
82.Fn getenv
83function may allocate temporary storage,
84so the
85.Fn freeenv
86function must be called to release any allocated resources when the value
87returned by
88.Fn getenv
89is no longer needed.
90.Pp
91The
92.Fn freeenv
93function is used to release the resources allocated by a previous call to
94.Fn getenv .
95The
96.Fa env
97argument passed to
98.Fn freeenv
99is the pointer returned by the earlier call to
100.Fn getenv .
101Like
102.Xr free 3 ,
103the
104.Fa env
105argument can be
106.Va NULL ,
107in which case no action occurs.
108.Pp
109The
110.Fn setenv
111function inserts or resets the kernel environment variable
112.Fa name
113to
114.Fa value .
115If the variable
116.Fa name
117already exists,
118its value is replaced.
119This function can fail if an internal limit on the number of environment
120variables is exceeded.
121.Pp
122The
123.Fn unsetenv
124function deletes the kernel environment variable
125.Fa name .
126.Pp
127The
128.Fn testenv
129function is used to determine if a kernel environment variable exists.
130It returns a non-zero value if the variable
131.Fa name
132exists and zero if it does not.
133.Pp
134The
135.Fn getenv_int ,
136.Fn getenv_long ,
137.Fn getenv_quad ,
138.Fn getenv_uint ,
139and
140.Fn getenv_ulong
141functions look for a kernel environment variable
142.Fa name
143and parse it as a signed integer,
144long integer,
145signed 64-bit integer,
146unsigned integer,
147or an unsigned long integer,
148respectively.
149These functions fail and return zero if
150.Fa name
151does not exist or if any invalid characters are present in its value.
152On success,
153these function store the parsed value in the integer variable pointed to
154by
155.Fa data .
156If the parsed value overflows the integer type,
157a truncated value is stored in
158.Fa data
159and zero is returned.
160If the value begins with a prefix of
161.Dq 0x
162it is interpreted as hexadecimal.
163If it begins with a prefix of
164.Dq 0
165it is interpreted as octal.
166Otherwise,
167the value is interpreted as decimal.
168The value may contain a single character suffix specifying a unit for
169the value.
170The interpreted value is multiplied by the unit's magnitude before being
171returned.
172The following unit suffixes are supported:
173.Bl -column -offset indent ".Sy Unit" ".Sy Magnitude"
174.It Sy Unit Ta Sy Magnitude
175.It k Ta 2^10
176.It m Ta 2^20
177.It g Ta 2^30
178.It t Ta 2^40
179.El
180.Pp
181The
182.Fn getenv_string
183function stores a copy of the kernel environment variable
184.Fa name
185in the buffer described by
186.Fa data
187and
188.Fa size.
189If the variable does not exist,
190zero is returned.
191If the variable exists,
192up to
193.Fa size - 1
194characters of its value are copied to the buffer pointed to by
195.Fa data
196followed by a null character and a non-zero value is returned.
197.Sh RETURN VALUES
198The
199.Fn getenv
200function returns a pointer to an environment variable's value on success or
201.Dv NULL
202if the variable does not exist.
203.Pp
204The
205.Fn setenv
206and
207.Fn unsetenv
208functions return zero on success and -1 on failure.
209.Pp
210The
211.Fn testenv
212function returns zero if the specified environment variable does not exist and
213a non-zero value if it does exist.
214The
215.Fn getenv_int ,
216.Fn getenv_long ,
217.Fn getenv_string ,
218.Fn getenv_quad ,
219.Fn getenv_uint ,
220and
221.Fn getenv_ulong
222functions return a non-zero value on success and zero on failure.
223