HOME directory
Hello all!
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:
const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);
but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?
Thanks for any suggestion.
Signature · 5 lines
With regards, HIToC [email protected]
I dont know what specific system calls are used for retreiving the current user or home dir(if there are any). But perhaps u can make use of the getenv() function to retrieve the value of $HOME and $USER (it may not work on some shells, though, didnt test it)
Quoted 18 lines · HIToC
Quoted 16 lines
Hello all!
I am writing a piece of code that makes files, open directories
in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?Thanks for any suggestion.
hi,
if you rename your home-directory, you have to change your /etc/passwd to point your user entry to the new home-directory.
benjamin,
Quoted 13 lines · HIToC
Hello all!
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "
~/file.txt";
ofstreammy_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?Thanks for any suggestion.
Ooops... inapplicable solution...
scratch it...
Quoted 29 lines · Ron Michael Khu
I dont know what specific system calls are used for retreiving the current user or home dir(if there are any). But perhaps u can make use of the
getenv()function to retrieve the value of $HOME and $USER (it may not work on some shells, though, didnt test it)Quoted 18 lines · HIToC
Quoted 16 lines
Hello all!
I am writing a piece of code that makes files, open directories
in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?Thanks for any suggestion.
You can use the environment variables to determine what the home directory is. Look at the manpage for getenv(3) and environ(5)
Quoted 13 lines · HIToC
Hello all!
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?Thanks for any suggestion.
Signature · 4 lines
| Richard Nairn Specializing in Linux | Nairn Consulting Web / Database Solutions | Calgary, AB | [email protected]
Quoted 11 lines · HIToC
Hello all!
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?
All SUSv2/3 compliant systems know the system call getpwnam() to obtain information about a given user by querying the passwd database.
The following (untested) code snippet demonstrates its usage. Hope
this is what you're lookin' for.
----- BEGIN -----
#include <stdio.h>
#include <errno.h>
#include <pwd.h>
int main(int argc, char **argv)
{
char *login_name;
struct passwd *p;
if (argc == 1) {
if ((login_name = getlogin()) == NULL) {
perror("getlogin() failed.\n");
exit(errno);
}
} else if (argc == 2) {
login_name = argv[1];
}
if ((p = getpwnam(login_name)) == NULL) {
perror("getpwnam() failed.\n");
exit(errno);
}
printf("HOME dir for %s: %s\n", login_name, p->pw_dir);
return (0);
}----- END -----
If you have questions or suggestions, feel free to drop me a line.
Kind Regards
\Steve
--
Steve Graegert <[email protected]> Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176) 21 24 88 69
Office: +49 (9131) 71 26 40 9
Quoted 10 lines · HIToC
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:const char* filename = "~/file.txt"; ofstream my_file(filename, ios::out);but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?
The "~" syntax is part of the shell; library functions won't expand it. In the shell, "~/..." is equivalent to "$HOME/...".
If you want to respect $HOME (which is usually the case), use e.g.:
const char *home = getenv("HOME");
const char *file = "file.txt";
char *path = alloca(strlen(home) + strlen(file) + 2);
sprintf(path, "%s/%s", home, file);
ofstream my_file(filename, ios::out);If you want to always use the user's home directory from /etc/passwd instead of $HOME (e.g. for setuid programs), use getpwuid(), e.g.:
struct passwd *pw = getpwuid(getuid());
if (!pw)
fatal_error("user not listed in /etc/passwd");
const char *home = pw->pw_dir;
...Similarly, the ~user syntax for a specific user's home directory is implemented using getpwnam().
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 10 lines · Steve Graegert
Quoted 7 lines · Adam Dyga
Quoted 7 lines
All SUSv2/3 compliant systems know the system call
getpwnam()to obtain information about a given user by querying the passwd database. The following (untested) code snippet demonstrates its usage. Hope this is what you're lookin' for.
Quoted 7 lines
Quoted 3 lines
Note that for security reasons this way much better.
Relying on the shell environment is
usually a bad idea since it can be manipulated and tweaked in many different ways.
That's exactly what I meant ;)
Cheers
AD
PS: Sorry, my previous reply should have gone to the group....