#89335 coreutils: Please add tool for printing user's home directory

#89335#5
Date:
2001-03-11 21:46:02 UTC
From:
To:
i'd like to have a tool for printing the home directory of a user
(real user / effective user / user passed on command line)
I've run across this need 3 times in shell scripting last week...
I wrote these scripts in perl then...

Maybe there is a simlar utility already in Debian, but i havn't found it and
everybody told me to awk /etc/passwd - but i'm running LDAP...

And i think such a tool belong's right next to "groups" and "id".

Might even be upstream-wishlist...

This surely isn't hard to write, basically just something like this:
(Sorry for the bad code, but this is the first C Program i ever wrote on my
own and havn't hacked some other's program ;)
-------
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
    struct passwd *user;

    if (argc <=1) {
        /* without parameters, get current user id's home */
        user=getpwuid(geteuid());
    } else if (strcmp(argv[1],"-r") == 0) {
        /* get real user id */
        user=getpwuid(getuid());
    } else if (strcmp(argv[1],"-h") == 0) {
        /* show help screen */
	printf("Usage: homedir [OPTION] [USERNAME]\n");
	printf("Print home directory of USERNAME, or the current user.\n\n");
	printf("  -r     print home of the real instead of effecive user\n");
	printf("  -h     display this help and exit\n");
        exit(0);
    } else {
        user=getpwnam(argv[1]);
    }
    if (user) {
	printf("%s\n",user->pw_dir);
    } else {
	fprintf(stderr, "Error: could not get home directory\n");
	exit(1);
    }
    exit(0);
}
-------

this lack's to course more detailed error messages and i18n/l10n...

Greetings,
Erich

#89335#10
Date:
2001-05-31 21:07:34 UTC
From:
To:
The following

	perl -e 'use User::pwent;printf "%s\n",getpwnam("eirik")->dir'

works for me.  Substitute for "eirik" as necessary.

#89335#15
Date:
2005-12-28 17:41:29 UTC
From:
To:
Erich Schubert wrote:

Eirik Fuller wrote:


Erich: Do you still wish the wish you wished?

#89335#20
Date:
2005-12-28 19:12:59 UTC
From:
To:
Hello Thomas,

Well, I currently don't remember where/what I used it for, but after all
this is a common task, identifying a users homedir... something I'd like
to avoid starting a perl interpreter for.
I'm pretty sure, the "homedir.c" program I used in the bug report is
still being used on the box I needed it on... IMHO it makes still sense
to have such a tool standardized, more than firing up a perl interpreter
and such.

When avoiding printf() and using write() instead, building it with diet
gcc,
the static binary is around 2k... ;-) and without diet it's less then
4k.
I mean, it's not a huge thing, is it?
I am aware that usually you can do things like "echo ~username" to
achieve the same result, but how would you e.g. obtain another users
home directory in procmail? Does procmail support ~username expansion?

I don't really care anymore, though. So go ahead and just close the bug.

best regards,
Erich Schubert