Edit: In hindsight, a dynamic buffer would require returning ENOMEM errors (which might lead to some unexpected failures), while a static buffer would limit the value length. I think you might be right about the API being broken.
Edit: In hindsight, a dynamic buffer would require returning ENOMEM errors (which might lead to some unexpected failures), while a static buffer would limit the value length. I think you might be right about the API being broken.
You could also require callers free the returned memory when they're done, but that would be another change of API.
This would break very simple single-threaded programs that e.g. print two env vars in one printf call.
You provide the storage and free it
The problem is these non-direct uses. They each need to switch to •_r and manage the buffer, or offer _r versions themselves and sort of pass through the problem
- return a pointer - the library owns the allocation - the state is global and mutable
Thread safe
There are, however, other problems (discussed elsewhere in this thread) that complicate such an API in the context of getenv().
We need a new API which is not broken like in NetBSD, and a multi-year migration of all core libraries to it. Well a pity it wasn't started years ago though, could've been 95% done by now.