Looks like it was using $HOME which was set when sudo was used. The following fixed the problem:
Code:
// Get the users home directory.
    pw = getpwuid(atoi(getenv("SUDO_UID")));
// Change home directory back to the user and not root.
    setenv("HOME", pw->pw_dir, 1);