c function to find home directory? or $HOME?
I tried to google but had less than zero luck, well ok SHGetFolderpath ... though that same link hinted at $HOME as how to find home...
Anyway I am trying to find the user''s home directory... is there a C function for this? Or, do I have to get it from the HOME env var? (and can I rely on $HOME being available all the time/place?)
Thank you kindly
October 15, 2003 01:00 PM
quote: Original post by FeralofFireTop
I tried to google but had less than zero luck, well ok SHGetFolderpath ... though that same link hinted at $HOME as how to find home...
Anyway I am trying to find the user''s home directory... is there a C function for this? Or, do I have to get it from the HOME env var? (and can I rely on $HOME being available all the time/place?)
Thank you kindly
I assume u mean on a unix box since u posted this here. on linux/glibc (i''m not sure if this is posix compatible), use getpwent(). Do not rely on $HOME, esp if your program is going to be run suid/sgid.
*nod* *nod* Yup yup was mindfull of what forum I was posting in
Thank you for the help
What I came up with...
An I liked the simplicity of ''printf("!! %s\n", getenv("HOME") );'' too!
Thank you for the help
What I came up with...
// *nix things to find user''s home dir.#include <unistd.h> // getuid#include <pwd.h> // getpwent() and struct passwdvoid main(void){ char caBuffer[1024]; // I technically am using a std::string // uid_t should be a uint32 uid_t MyUID = getuid(); passwd * pPW = NULL; while( (pPW = getpwent() ) != NULL) { if ( pPW->pw_uid == MyUID) { break; // Break out of closest do, for, while } } if ( pPW != NULL) { strcpy(caBuffer, pPW->pw_dir); } endpwent(); // close printf("!! %s\n", caBuffer); // "!! /home/feral"}//END
An I liked the simplicity of ''printf("!! %s\n", getenv("HOME") );'' too!
October 15, 2003 02:49 PM
quote: Original post by FeralofFireTop
while( (pPW = getpwent() ) != NULL) { if ( pPW->pw_uid == MyUID) { break; // Break out of closest do, for, while } }
All that can be replaced with "pPW=getpwuid(getuid());"
Ahh! wonderful, thank you for pointing that out!
... now that (code) is the efficient and simplicity I was expecting to find (in *nix land), heh.
Anyway thank you again
... now that (code) is the efficient and simplicity I was expecting to find (in *nix land), heh.
Anyway thank you again
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement