3

I am writing small c++ code to access and edit certain text file in user's home directory. Currently I have following code (this is the relevant part):

bool core(void) {
    std::string autostart_entry = "";
    std::string user_entry = "";
    std::fstream username;
    username.open("username.txt", std::fstream::in);
    std::string location;
    std::string user_name;
    if (username.fail()) {
        username.open("username.txt", std::fstream::out);
        std::cout << "What's your system username? ";
        std::getline(std::cin, user_name);
        username << user_name;
    }
    else
        username >> user_name;
    username.close();
    location = "/home/" + user_name + "/.config/openbox/autostart";
    ...
}

This way, as you can see, I ask user for his username, and append it to the location string, is there any easy way to find user's home directory without asking for user's input? I have tried "~/..." and it doesn't work.

I know I could scan "/etc/passwd" file to find it from there but I am wondering if there is another way.

George Dirac
  • 168
  • 1
  • 7
  • by default the directory is at /home/user_name but not necessarily. It would be wiser to read /etc/passwd – Pooya Feb 23 '16 at 07:03
  • You don't need to ask the user for his username. On linux systems, one can use `chdir` to do the same. Check my answer below for a full disclosure. Please accept if it helps! ;) – gokul_uf Feb 23 '16 at 07:10

2 Answers2

8

Your best bet here is probably to use the getenv function:

#include <stdlib.h>

const char* homeDir = getenv("HOME");

The $HOME environment variable is generally always set under linux, and it will return you a string to a users home directory (even when it isn't under /home)

EDIT: This will only work for the home directory of the user running the program. If you want the home directory for a different user, you will need to use another approach

EDIT2: Actually, thinking about this for more than 1 second... the above will work, and you should use it first. However, if HOME is not set, you can use getpwuid:

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

const char *homedir = getenv("HOME");
if ( homedir == NULL ) {
    homedir = getpwuid(getuid())->pw_dir;
}
Signal Eleven
  • 231
  • 1
  • 6
  • Both approaches are giving me "/" instead of "/home/MY_USERNAME", even though I am logged in as my user in the system, instead of root. Do you know what could be the issue? – George Dirac Feb 23 '16 at 07:11
  • Not really; I just ran both on my linux machine, and both worked. You can check that HOME is set by typing "echo $HOME" in the terminal before you run your application, but it should be set. Are you running your application from the command line, or through an IDE? – Signal Eleven Feb 23 '16 at 07:15
  • I ran "echo $HOME" and it outputted correct path. I am running my application from the command line, same command line that I ran "echo $HOME" with. – George Dirac Feb 23 '16 at 07:19
  • Well, I am not sure what is going on. The getenv function should work; it can be used for all environment variables (including HOME), so you could test to see if you can get other variables as well... but if $HOME is defined, getenv() should really work. It is strange that both getenv and getpwuid are returning the same thing, and both are incorrect; I'm not sure what to suggest. Were there any warnings when you recompiled? (I'm grasping at straws really...) – Signal Eleven Feb 23 '16 at 07:24
  • 1
    I am very sorry, I messed up, getenv works very well, it's just that when I wanted to display homedir's value, I did "std::cout << *homedir;" which obviously outputted only first char which was "/". My mistake, thanks for the solution! – George Dirac Feb 23 '16 at 07:29
1

If you wanted to go to the home directory, just use chdir("~")

else,

This is a dirty hack but it works

#include <unistd.h>
char currdir[100];
char homedir[100];    
getcwd(currdir); //store the current directory in currdir
chdir("~"); // change the working directory to user's home directory
getcwd(homedir); // get the full address
chdir(currdir);   // go back to the previous directory
gokul_uf
  • 740
  • 1
  • 8
  • 21