I need a way to get user home directory in C++ program running on Linux. If the same code works on Unix, it would be nice. I don’t want to use HOME environment value.
AFAIK, root home directory is /root. Is it OK to create some files/folders in this directory, in the case my program is running by root user?
You need
getuidto get the user id of the current user and thengetpwuidto get the password entry (which includes the home directory) of that user:Note: if you need this in a threaded application, you’ll want to use
getpwuid_rinstead.