This implementation of getlocalbase() is based on the first version that I had submitted for review in D27236.
It uses a static buffer to retrieve the sysctl("user.localbase") value.
The size of this buffer can be reduced, but this will not have any effect on the execution size of this library, AFAICT.
The data segment size is slightly lower than 7KB without and 8KB with a buffer of size MAXPATHLEN, resulting in 8KB VM space being allocated in either case.
- If the function is compiled with LOCALBASE_CTL_LEN=0, then no sysctl() calls will be made and the functionality is identical to the previously existing getenv("LOCALBASE") in programs like "pkg" or "mailwrapper".
- If LOCALBASE_CTL_LEN is not overridden, then the buffer will be large enough to hold a string of length MAXPATHLEN, the maximum value supported by the sysctl variable "user.localbase".
- If LOCALBASE_CTL_LEN is between 1 and MAXPATHLEN-1, then a sysctl value too large to fit in the internal buffer has been set, then a string not suitable for use as a path name ("/\177", a slash followed by a <DEL> character) is returned (under the assumption, that a directory named "<DEL>" will never exist in the systems root directory and thus any look-up of a path with that prefix will fail).
In order to keep calling programs simple I want to always return a valid string.
When built with a default value of LOCALBASE_CTL_LEN, no error case exists.
If LOCALBASE_CTL_LEN is reduced and the kernel buffer for the user.localbase sysctl variable is reduced to the same value (e.g. 64 characters, which should be a reasonable upper limit for the LOCALBASE prefix), there is no error case.
I'd rather not complicate all programs that call this function to prevent an error case and thus propose the inline signaling of this error case by returning a valid string that is not a valid file name prefix.