Better method of finding the user home directory.

This commit is contained in:
onpon4 2015-03-12 12:38:17 -04:00
parent ea33be4591
commit 932341f0e6
1 changed files with 7 additions and 21 deletions

View File

@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "Starfighter.h" #include "Starfighter.h"
#include <sys/stat.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@ -103,30 +103,16 @@ void showErrorAndExit(int errorId, const char *name)
} }
/* /*
This bit is just for Linux users. It attempts to get the user's This gets the user's home directory, then creates the .parallelrealities
home directory, then creates the .parallelrealities and .parallelrealities/starfighter and .parallelrealities/starfighter directories so that saves and
directories so that saves and temporary data files can be written there. Good, eh? :) temporary data files can be written there.
*/ */
static void setupUserHomeDirectory() static void setupUserHomeDirectory()
{ {
char *userHome; const char *userHome;
char *name = getlogin(); if ((userHome = getenv("HOME")) == NULL)
userHome = getpwuid(getuid())->pw_dir;
passwd *pass;
if (name != NULL)
pass = getpwnam(name);
else
pass = getpwuid(geteuid());
if (pass == NULL)
{
printf("Couldn't determine the user home directory. Exitting.\n");
exit(1);
}
userHome = pass->pw_dir;
char dir[PATH_MAX]; char dir[PATH_MAX];
strcpy(dir, ""); strcpy(dir, "");