diff options
author | Eric S. Raymond <esr@thyrsus.com> | 1999-04-25 16:19:13 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 1999-04-25 16:19:13 +0000 |
commit | 0c4534a4c91e2e082dcfa2567a24bad4e50edcf6 (patch) | |
tree | e508a5ca32ddf4daa7ecd7347b3687de92d5d47a /env.c | |
parent | eee69ed12a7129303607fc3e338132a23f23b3f4 (diff) | |
download | fetchmail-0c4534a4c91e2e082dcfa2567a24bad4e50edcf6.tar.gz fetchmail-0c4534a4c91e2e082dcfa2567a24bad4e50edcf6.tar.bz2 fetchmail-0c4534a4c91e2e082dcfa2567a24bad4e50edcf6.zip |
Improved environment-query logic.
svn path=/trunk/; revision=2452
Diffstat (limited to 'env.c')
-rw-r--r-- | env.c | 57 |
1 files changed, 40 insertions, 17 deletions
@@ -33,30 +33,53 @@ extern char *program_name; void envquery(int argc, char **argv) /* set up basic stuff from the environment (including the rc file name) */ { - struct passwd *pw; + struct passwd by_name, by_uid, *pwp; - if ((program_name = strrchr(argv[0], '/')) != NULL) - ++program_name; - else - program_name = argv[0]; + if (!(user = getenv("LOGNAME"))) + user = getenv("USER"); - if ((pw = getpwuid(getuid())) != NULL) - { - user = pw->pw_name; - home = pw->pw_dir; - } - else if ((home = getenv("HOME"))) - { - if ((user = getenv("LOGNAME")) == (char *)NULL || user[0] == '\0') - user = getenv("USER"); - } - else + if (!(pwp = getpwuid(getuid()))) { fprintf(stderr, - _("%s: can't find your name and home directory!\n"), + _("%s: You don't exist. Go away.\n"), program_name); exit(PS_UNDEFINED); } + else + { + memcpy(&by_uid, pwp, sizeof(struct passwd)); + if (!user) + pwp = &by_uid; + else if ((pwp = getpwnam(user))) + { + /* + * This logic is needed to handle gracefully the possibility + * that multiple names might be mapped to one UID + */ + memcpy(&by_name, pwp, sizeof(struct passwd)); + + if (by_name.pw_uid == by_uid.pw_uid) + pwp = &by_name; + else + pwp = &by_uid; + } + else + { + fprintf(stderr, + _("%s: can't find your name and home directory!\n"), + program_name); + exit(PS_UNDEFINED); + } + user = xstrdup(pwp->pw_name); + } + + if (!(home = getenv("HOME"))) + home = pwp->pw_dir; + + if ((program_name = strrchr(argv[0], '/')) != NULL) + ++program_name; + else + program_name = argv[0]; #define RCFILE_NAME ".fetchmailrc" rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2); |