Posted By

xxtjaxx on 10/27/09


Tagged

homeunixlinuxeasy


Versions (?)

get the home diectory of the current user unix way


 / Published in: C++
 

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.

  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  

You need to login to post a comment.