get the home diectory of the current user unix way


/ Published in: C++
Save to your folder(s)

This one just demonstrates an easy way to get the information via pwuid's. Might not the most elegnt way ut works on every unix I believe.


Copy this code and paste it in your HTML
  1. string get_home(void)
  2. {
  3. /* the passwd struct */
  4. struct passwd *passwd;
  5.  
  6.  
  7. /* we pass the current users UID to the struct variable*/
  8. passwd = getpwuid ( getuid());
  9.  
  10.  
  11. /*
  12.   then we pass the UID to pw_name which is a char value
  13.   of the /home/ directory
  14.   */
  15. string user(passwd->pw_name);
  16.  
  17.  
  18. /*then we make sure we have the full path + our secret config dir*/
  19. user = "/home/" + user + "/.mkproject/";
  20. return user;
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.