All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: HIToC <hitoc_mail@yahoo.it>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: HOME directory
Date: Mon, 25 Apr 2005 19:05:07 +0200	[thread overview]
Message-ID: <6a00c8d50504251005110319f2@mail.gmail.com> (raw)
In-Reply-To: <200504251826.36321.hitoc_mail@yahoo.it>

On 4/25/05, HIToC <hitoc_mail@yahoo.it> wrote:
> 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 <graegerts@gmail.com>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

  parent reply	other threads:[~2005-04-25 17:05 UTC|newest]

Thread overview: 14+ 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 [this message]
     [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
  -- strict thread matches above, loose matches on Subject: below --
2005-04-28 12:28 HIToC
2005-04-25 16:50 Luciano Moreira - ht
2002-06-03  9:56 home directory Кузнецова Нина
2002-06-03 10:32 ` Nicolas Costes
     [not found]   ` <3CFC76FF.F41AB199@ns.kinetics.nsc.ru>
2002-06-04  9:25     ` Nicolas Costes
2002-06-04 12:16       ` Milan P. Stanic

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=6a00c8d50504251005110319f2@mail.gmail.com \
    --to=graegerts@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.