From: Glynn Clements <glynn@gclements.plus.com>
To: HIToC <hitoc_mail@yahoo.it>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: HOME directory
Date: Mon, 25 Apr 2005 18:06:00 +0100 [thread overview]
Message-ID: <17005.9080.214244.56172@gargle.gargle.HOWL> (raw)
In-Reply-To: <200504251826.36321.hitoc_mail@yahoo.it>
HIToC wrote:
> 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().
--
Glynn Clements <glynn@gclements.plus.com>
next prev parent reply other threads:[~2005-04-25 17:06 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-04-25 16:27 HOME directory HIToC
[not found] ` <426D1DC8.2080900@hq.ntsp.nec.co.jp>
2005-04-25 16:42 ` Ron Michael Khu
2005-04-25 16:46 ` Ron Michael Khu
2005-04-25 16:43 ` Benjamin Machuletz
2005-04-25 16:59 ` Richard Nairn
2005-04-25 17:05 ` Steve Graegert
[not found] ` <426D38D3.5060901@tlen.pl>
[not found] ` <6a00c8d505042511529bd72c8@mail.gmail.com>
2005-04-26 11:26 ` Adam Dyga
2005-04-25 17:06 ` Glynn Clements [this message]
-- strict thread matches above, loose matches on Subject: below --
2005-04-25 16:50 Luciano Moreira - ht
2005-04-28 12:28 HIToC
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=17005.9080.214244.56172@gargle.gargle.HOWL \
--to=glynn@gclements.plus.com \
--cc=hitoc_mail@yahoo.it \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).