1.\" 2.\" SPDX-License-Identifier: BSD-2-Clause 3.\" 4.\" Copyright 2020 Scott Long 5.\" Copyright 2020 Stefan Eßer 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 July 11, 2023 31.Dt GETLOCALBASE 3 32.Os 33.Sh NAME 34.Nm getlocalbase 35.Nd "return the path to the local software directory" 36.Sh LIBRARY 37.Lb libutil 38.Sh SYNOPSIS 39.In libutil.h 40.Ft const char* 41.Fn getlocalbase "void" 42.Sh DESCRIPTION 43The 44.Fn getlocalbase 45function returns the path to the local software base directory. 46Normally this is the 47.Pa /usr/local 48directory. 49First the 50.Ev LOCALBASE 51environment variable is checked. 52If that does not exist then the 53.Va user.localbase 54sysctl is checked. 55If that also does not exist then the value of the 56.Dv LOCALBASE_PATH 57compile-time variable is used. 58If that is undefined then the system default, 59.Pa _PATH_LOCALBASE 60is used. 61.Pp 62The contents of the string returned by the 63.Fn getlocalbase 64function shall not be modified. 65.Sh IMPLEMENTATION NOTES 66Calls to 67.Fn getlocalbase 68will perform a setugid check on the running binary before checking the 69environment. 70.Pp 71The address returned by 72.Fn getlocalbase 73will point into the executing processes environment if it is the result of 74.Fn getenv "LOCALBASE" , 75to a static buffer if it is the result of 76.Fn sysctl "user.localbase" , 77and to a constant string if the compiled in default value is returned. 78.Pp 79The same value will be returned on successive calls during the run-time 80of the program, ignoring any changes to the environment variable or the 81sysctl value that might have been made. 82.Pp 83The 84.Fn getlocalbase 85function can be compiled with a non-default value of LOCALBASE_CTL_LEN. 86A value of 0 will disable fetching of the sysctl value, a value less than 87MAXPATHLEN will put a limit on the maximum string length supported for 88this sysctl value. 89If built with a non-default value of LOCALBASE_CTL_LEN, a value of the 90user.localbase sysctl variable longer than this value will make 91.Fn getlocalbase 92return a valid string that is not a valid path prefix in any filesystem. 93.Sh RETURN VALUES 94The 95.Fn getlocalbase 96function returns a pointer to a string, whose length may exceed MAXPATHLEN, 97if it has been obtained from the environment. 98.Sh ENVIRONMENT 99The 100.Fn getlocalbase 101library function retrieves the 102.Ev LOCALBASE 103environment variable. 104.Sh ERRORS 105The 106.Fn getlocalbase 107function always succeeds and returns a valid pointer to a string. 108.Sh SEE ALSO 109.Xr env 1 , 110.Xr src.conf 5 , 111.Xr sysctl 8 112.Sh HISTORY 113The 114.Nm 115library function first appeared in 116.Fx 13.0 . 117.Sh AUTHORS 118.An -nosplit 119This 120manual page was written by 121.An Scott Long Aq Mt scottl@FreeBSD.org 122and 123.An Stefan Eßer Aq Mt se@FreeBSD.org . 124