1: /* m_getdefs.c - read the user's MH environment */
   2: 
   3: #include "../h/mh.h"
   4: #include <stdio.h>
   5: #include <errno.h>
   6: #include <sys/types.h>
   7: #include <sys/stat.h>
   8: #include <pwd.h>
   9: 
  10: 
  11: extern int errno;
  12: 
  13: struct  passwd  *getpwuid ();
  14: 
  15: 
  16: #ifndef OVERHEAD
  17: #define FOpen(f,m,e,c)  fopen (f, m)
  18: #define FClose(f)   fclose (f)
  19: #else   OVERHEAD
  20: FILE   *FOpen ();
  21: int FClose ();
  22: 
  23: long    lseek ();
  24: #endif	OVERHEAD
  25: 
  26: 
  27: void m_getdefs() {
  28:     int     pid;
  29:     register char  *cp,
  30:                    *pp;
  31: #if defined (notdef) || defined (COMPAT)
  32:     register char  *ctx;
  33: #endif	notdef or COMPAT
  34:     char    buf[BUFSIZ];
  35:     struct stat st;
  36:     register struct passwd *pw;
  37:     register    FILE *ib;
  38: 
  39:     if (defpath)
  40:     return;
  41:     if (mypath == NULL) {
  42:     if (mypath = getenv ("HOME"))
  43:         mypath = getcpy (mypath);
  44:     else
  45:         if ((pw = getpwuid (getuid ())) == NULL
  46:             || pw -> pw_dir == NULL
  47:             || *pw -> pw_dir == NULL)
  48:         adios (NULLCP, "no HOME envariable");
  49:         else
  50:         mypath = getcpy (pw -> pw_dir);
  51:     if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
  52:         *cp = NULL;
  53:     }
  54: 
  55:     if ((cp = getenv ("MH")) && *cp) {
  56:     defpath = path (cp, TFILE);
  57:     if ((ib = FOpen (defpath, "r", "MHFD", 0)) == NULL)
  58:         adios (defpath, "unable to read");
  59:     if (*cp != '/')
  60:         (void) putenv ("MH", defpath);
  61:     }
  62:     else {
  63:     defpath = concat (mypath, "/", mh_profile, NULLCP);
  64: 
  65:     if ((ib = FOpen (defpath, "r", "MHFD", 0)) == NULL)
  66:         switch (pid = vfork ()) {
  67:         case NOTOK:
  68:             adios ("fork", "unable to");
  69: 
  70:         case OK:
  71:             (void) setgid (getgid ());
  72:             (void) setuid (getuid ());
  73: 
  74:             execlp (installproc, "install-mh", "-auto", NULLCP);
  75:             fprintf (stderr, "unable to exec ");
  76:             perror (installproc);
  77:             _exit (-1);
  78: 
  79:         default:
  80:             if (pidwait (pid, OK)
  81:                 || (ib = fopen (defpath, "r")) == NULL)
  82:             adios (NULLCP, "[install-mh aborted]");
  83:         }
  84:     }
  85: 
  86: #if defined (notdef) || defined (COMPAT)
  87:     ctx = context;
  88: #endif	notdef or COMPAT
  89:     m_readefs (&m_defs, ib, mh_profile, 0);
  90:     (void) FClose (ib);
  91: 
  92:     if ((pp = m_find ("path")) != NULL && *pp) {
  93:     if (*pp != '/')
  94:         (void) sprintf (buf, "%s/%s", mypath, pp);
  95:     else
  96:         (void) strcpy (buf, pp);
  97:     if (stat(buf, &st) == NOTOK) {
  98:         if (errno != ENOENT)
  99:         adios (buf, "error opening");
 100:         cp = concat ("Your MH-directory \"", buf,
 101:         "\" doesn't exist; Create it? ", NULLCP);
 102:         if (!getanswer(cp))
 103:         adios (NULLCP, "unable to access MH-directory \"%s\"", buf);
 104:         free (cp);
 105:         if (!makedir (buf))
 106:         adios (NULLCP, "unable to create", buf);
 107:     }
 108:     }
 109: 
 110: #ifdef  COMPAT
 111:     if (strcmp (ctx, "/dev/null") == 0)
 112:     return;         /* called by install-mh */
 113: 
 114:     if (access (ctxpath = getcpy (m_maildir (ctx)), 0) == NOTOK)
 115:     switch (pid = fork ()) {
 116:         case NOTOK:
 117:         adios ("fork", "unable to");
 118: 
 119:         case OK:
 120:         (void) setgid (getgid ());
 121:         (void) setuid (getuid ());
 122:         fprintf (stderr, "install-mh -compat\n");
 123: 
 124:         execlp (installproc, "install-mh", "-compat", NULLCP);
 125:         fprintf (stderr, "unable to exec ");
 126:         perror (installproc);
 127:         _exit (-1);
 128: 
 129:         default:
 130:         if (pidwait (pid, OK) || access (ctxpath, 0) == NOTOK)
 131:             adios (NULLCP, "[install-mh aborted]");
 132: 
 133:         if ((ib = fopen (defpath, "r")) == NULL)
 134:             adios (defpath, "unable to read");
 135:         m_readefs (&m_defs, ib, mh_profile, 0);
 136:         (void) FClose (ib);
 137:     }
 138:     free (ctxpath);
 139: #endif	COMPAT
 140: 
 141:     if ((cp = getenv ("MHCONTEXT")) == NULL || *cp == NULL)
 142:     cp = context;
 143:     if ((ib = FOpen (ctxpath = getcpy (m_maildir (cp)), "r", "MHCONTEXTFD", 1))
 144:         == NULL) {
 145: #ifdef  notdef          /* XXX */
 146:     if (cp != ctx)
 147:         adios (ctxpath, "unable to read");
 148: #endif	notdef
 149:     }
 150:     else {
 151:     m_readefs ((struct node **) 0, ib, cp, 1);
 152:     (void) FClose (ib);
 153:     }
 154: }
 155: 
 156: /*  */
 157: 
 158: #ifdef  OVERHEAD
 159: int fd_def = NOTOK;
 160: int fd_ctx = NOTOK;
 161: 
 162: 
 163: static FILE *FOpen (filename, mode, envariable, ctx)
 164: register char   *filename,
 165:                 *mode,
 166:             *envariable;
 167: register int     ctx;
 168: {
 169:     register int    fd;
 170:     register char  *cp;
 171:     char    buffer[10];
 172:     struct stat st1;
 173:     register    FILE * fp;
 174: 
 175:     if ((cp = getenv (envariable)) == NULL || *cp == NULL)
 176:     goto not_open;
 177: 
 178:     if ((fd = atoi (cp)) <= fileno (stderr)) {
 179:     advise (NULLCP, "bad value for $%s: %s", envariable, cp);
 180:     (void) unputenv (envariable);
 181:     return fopen (filename, mode);
 182:     }
 183:     if (ctx)
 184:     fd_ctx = fd;
 185:     else
 186:     fd_def = fd;
 187: 
 188:     if (fstat (fd, &st1) == NOTOK) {
 189: not_open: ;
 190:     if ((fp = fopen (filename, mode))
 191:         && !strcmp (mode, "r")
 192:         && fileno (fp) > fileno (stderr)) {
 193:         (void) sprintf (buffer, "%d", fileno (fp));
 194:         if (!putenv (envariable, buffer))
 195:         if (ctx)
 196:             fd_ctx = fileno (fp);
 197:         else
 198:             fd_def = fileno (fp);
 199:     }
 200: 
 201:     if ((cp = getenv ("MHFDEBUG")) && *cp)
 202:         fprintf (stderr, "FOpen of %s sets %d\n", filename, fileno (fp));
 203:     return fp;
 204:     }
 205: 
 206:     (void) lseek (fd, 0L, 0);
 207:     return fdopen (fd, mode);
 208: }
 209: 
 210: 
 211: static int  FClose (f)
 212: register FILE   *f;
 213: {
 214:     register int    d1,
 215:                     d2,
 216:                     i;
 217:     register char  *cp;
 218: 
 219:     if (f == NULL)
 220:     return OK;
 221: 
 222:     if ((d1 = fileno (f)) != fd_def && d1 != fd_ctx)
 223:     return fclose (f);
 224: 
 225:     d2 = dup (d1);
 226:     i = fclose (f);
 227: 
 228:     if (d2 != NOTOK) {
 229:     (void) dup2 (d2, d1);
 230:     (void) close (d2);
 231:     }
 232:     else
 233:     if (d1 == fd_def)
 234:         fd_def = NOTOK;
 235:     else
 236:         fd_ctx = NOTOK;
 237: 
 238:     if ((cp = getenv ("MHFDEBUG")) && *cp)
 239:     fprintf (stderr, "FClose emulating close of %d (%d)\n", d1, d2);
 240:     return i;
 241: }
 242: #endif	OVERHEAD

Defined functions

FClose defined in line 211; never used
FOpen defined in line 163; never used

Defined variables

fd_ctx defined in line 160; used 4 times
fd_def defined in line 159; used 5 times

Defined macros

FClose defined in line 18; used 4 times
FOpen defined in line 17; used 4 times
Last modified: 1985-11-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1416
Valid CSS Valid XHTML 1.0 Strict