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