X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Fcontext_read.c;h=e6cdfff03f275abe1f2810fddf4b30ba5b92ccc2;hp=49b4fc36cba11828895fd6e75ad7f16f8785c210;hb=5afa7072e3fe7fc42287e2149e1c23b363700795;hpb=945aa62b814966a1266a5524bd089a74e0999cd1 diff --git a/sbr/context_read.c b/sbr/context_read.c index 49b4fc3..e6cdfff 100644 --- a/sbr/context_read.c +++ b/sbr/context_read.c @@ -1,4 +1,3 @@ - /* * context_read.c -- find and read profile and context files * @@ -7,109 +6,128 @@ * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. + * + * This function must be called early on in any nmh utility, and + * may only be called once. It does the following: + * + * o Sets the global variable "mypath" to the home directory path. + * + * o Sets the global variable "defpath" to the absolute path of + * the profile file. + * + * o Reads in the profile file. Bails out if it can't. + * + * o Makes sure that the mail directory exists, prompting for + * creation if it doesn't. + * + * o Reads the context file either as set by the MHCONTEXT + * environment variable or by the profile. */ -#include -#include -#include +#include /* mh internals */ +#include /* system call errors */ +#include /* structure for getpwuid() results */ -extern int errno; +extern int errno; /* system call error number */ void context_read (void) { - pid_t pid; - register char *cp, *pp; - char buf[BUFSIZ]; - struct stat st; - register struct passwd *pw; - register FILE *ib; - - if (defpath) - return; + char buf[BUFSIZ]; /* path name buffer */ + char *cp; /* miscellaneous pointer */ + char *nd; /* nmh directory pointer */ + struct stat st; /* stat() results */ + register struct passwd *pw; /* getpwuid() results */ + register FILE *ib; /* profile and context file pointer */ /* - * Find user's home directory + * Find user's home directory. Try the HOME environment variable first, + * the home directory field in the password file if that's not found. */ - if (!mypath) { - if ((mypath = getenv ("HOME"))) - mypath = getcpy (mypath); + + if ((mypath = getenv("HOME")) == (char *)0) { + if ((pw = getpwuid(getuid())) == (struct passwd *)0 || *pw->pw_dir == '\0') + adios(NULL, "cannot determine your home directory"); else - if ((pw = getpwuid (getuid ())) == NULL - || pw->pw_dir == NULL - || *pw->pw_dir == 0) - adios (NULL, "no HOME envariable"); - else - mypath = getcpy (pw->pw_dir); - if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/') - *cp = 0; + mypath = pw->pw_dir; } /* - * open and read user's profile + * Find and read user's profile. Check for the existence of an MH environment + * variable first with non-empty contents. Convert any relative path name + * found there to an absolute one. Look for the profile in the user's home + * directory if the MH environment variable isn't set. */ - if ((cp = getenv ("MH")) && *cp != '\0') { - defpath = path (cp, TFILE); - if ((ib = fopen (defpath, "r")) == NULL) - adios (defpath, "unable to read"); - if (*cp != '/') - m_putenv ("MH", defpath); - } else { - defpath = concat (mypath, "/", mh_profile, NULL); - - if ((ib = fopen (defpath, "r")) == NULL) { - switch (pid = vfork ()) { - case -1: - adios ("fork", "unable to"); - - case 0: - setgid (getgid ()); - setuid (getuid ()); - - execlp (installproc, "install-mh", "-auto", NULL); - fprintf (stderr, "unable to exec "); - perror (installproc); - _exit (-1); - - default: - if (pidwait (pid, 0) - || (ib = fopen (defpath, "r")) == NULL) - adios (NULL, "[install-mh aborted]"); - } - } + + if ((cp = getenv("MH")) && *cp != '\0') { + defpath = path(cp, TFILE); + + if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0) + adios((char *)0, "`%s' specified by your MH environment variable is not a normal file", cp); + + if ((ib = fopen(defpath, "r")) == (FILE *)0) + adios((char *)0, "unable to read the `%s' profile specified by your MH environment variable", defpath); + } + else { + defpath = concat(mypath, "/", mh_profile, NULL); + + if ((ib = fopen(defpath, "r")) == (FILE *)0) + adios((char *)0, "Doesn't look like nmh is installed. Run install-mh to do so."); + + cp = mh_profile; } - readconfig (&m_defs, ib, mh_profile, 0); + + readconfig (&m_defs, ib, cp, 0); fclose (ib); /* - * Find user's nmh directory + * Find the user's nmh directory, which is specified by the "path" profile component. + * Convert a relative path name to an absolute one rooted in the home directory. */ - if ((pp = context_find ("path")) && *pp != '\0') { - if (*pp != '/') - snprintf (buf, sizeof(buf), "%s/%s", mypath, pp); - else - strncpy (buf, pp, sizeof(buf)); - if (stat(buf, &st) == -1) { - if (errno != ENOENT) - adios (buf, "error opening"); - cp = concat ("Your MH-directory \"", buf, - "\" doesn't exist; Create it? ", NULL); - if (!getanswer(cp)) - adios (NULL, "unable to access MH-directory \"%s\"", buf); - free (cp); - if (!makedir (buf)) - adios (NULL, "unable to create", buf); - } + + if ((cp = context_find ("path")) == (char *)0) + adios(NULL, "Your %s file does not contain a path entry.", defpath); + + if (*cp == '\0') + adios(NULL, "Your `%s' profile file does not contain a valid path entry.", defpath); + + if (*cp != '/') + (void)snprintf (nd = buf, sizeof(buf), "%s/%s", mypath, cp); + else + nd = cp; + + if (stat(nd, &st) == -1) { + if (errno != ENOENT) + adios (nd, "error opening"); + + cp = concat ("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL); + + if (!getanswer(cp)) + adios (NULL, "unable to access MH-directory \"%s\"", nd); + + free (cp); + + if (!makedir (nd)) + adios (NULL, "unable to create", nd); } + else if ((st.st_mode & S_IFDIR) == 0) + adios ((char *)0, "`%s' is not a directory", nd); + /* - * open and read user's context file + * Open and read user's context file. The name of the context file comes from the + * profile unless overridden by the MHCONTEXT environment variable. */ - if (!(cp = getenv ("MHCONTEXT")) || *cp == '\0') + + if ((cp = getenv ("MHCONTEXT")) == (char *)0 || *cp == '\0') cp = context; + ctxpath = getcpy (m_maildir (cp)); + if ((ib = fopen (ctxpath, "r"))) { readconfig ((struct node **) 0, ib, cp, 1); fclose (ib); } + + return; }