Many changes: add option 2; fix FIN_WAIT_2 problem; fix some mail
[unix-history] / usr / src / usr.sbin / sendmail / src / util.c
index 6c28802..9702b43 100644 (file)
@@ -1,33 +1,19 @@
 /*
 /*
- * Copyright (c) 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
- *
- *  Sendmail
- *  Copyright (c) 1983  Eric P. Allman
- *  Berkeley, California
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)util.c     5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)util.c     8.5 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-# include <stdio.h>
-# include <pwd.h>
-# include <sys/types.h>
-# include <sys/stat.h>
-# include <sysexits.h>
-# include <errno.h>
 # include "sendmail.h"
 # include "sendmail.h"
+# include <sysexits.h>
 # include "conf.h"
 # include "conf.h"
-
-/*
+\f/*
 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
 **
 **     Runs through a string and strips off unquoted quote
 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
 **
 **     Runs through a string and strips off unquoted quote
@@ -35,8 +21,6 @@ static char sccsid[] = "@(#)util.c    5.17 (Berkeley) %G%";
 **
 **     Parameters:
 **             s -- the string to strip.
 **
 **     Parameters:
 **             s -- the string to strip.
-**             qf -- if set, remove actual `` " '' characters
-**                     as well as the quote bits.
 **
 **     Returns:
 **             none.
 **
 **     Returns:
 **             none.
@@ -48,9 +32,8 @@ static char sccsid[] = "@(#)util.c    5.17 (Berkeley) %G%";
 **             deliver
 */
 
 **             deliver
 */
 
-stripquotes(s, qf)
+stripquotes(s)
        char *s;
        char *s;
-       bool qf;
 {
        register char *p;
        register char *q;
 {
        register char *p;
        register char *q;
@@ -59,76 +42,16 @@ stripquotes(s, qf)
        if (s == NULL)
                return;
 
        if (s == NULL)
                return;
 
-       for (p = q = s; (c = *p++) != '\0'; )
-       {
-               if (c != '"' || !qf)
-                       *q++ = c & 0177;
-       }
-       *q = '\0';
-}
-\f/*
-**  QSTRLEN -- give me the string length assuming 0200 bits add a char
-**
-**     Parameters:
-**             s -- the string to measure.
-**
-**     Reurns:
-**             The length of s, including space for backslash escapes.
-**
-**     Side Effects:
-**             none.
-*/
-
-qstrlen(s)
-       register char *s;
-{
-       register int l = 0;
-       register char c;
-
-       while ((c = *s++) != '\0')
-       {
-               if (bitset(0200, c))
-                       l++;
-               l++;
-       }
-       return (l);
-}
-\f/*
-**  CAPITALIZE -- return a copy of a string, properly capitalized.
-**
-**     Parameters:
-**             s -- the string to capitalize.
-**
-**     Returns:
-**             a pointer to a properly capitalized string.
-**
-**     Side Effects:
-**             none.
-*/
-
-char *
-capitalize(s)
-       register char *s;
-{
-       static char buf[50];
-       register char *p;
-
-       p = buf;
-
-       for (;;)
+       p = q = s;
+       do
        {
        {
-               while (!isalpha(*s) && *s != '\0')
-                       *p++ = *s++;
-               if (*s == '\0')
-                       break;
-               *p++ = toupper(*s);
-               s++;
-               while (isalpha(*s))
-                       *p++ = *s++;
-       }
-
-       *p = '\0';
-       return (buf);
+               c = *p++;
+               if (c == '\\')
+                       c = *p++;
+               else if (c == '"')
+                       continue;
+               *q++ = c;
+       } while (c != '\0');
 }
 \f/*
 **  XALLOC -- Allocate memory and bitch wildly on failure.
 }
 \f/*
 **  XALLOC -- Allocate memory and bitch wildly on failure.
@@ -151,7 +74,6 @@ xalloc(sz)
        register int sz;
 {
        register char *p;
        register int sz;
 {
        register char *p;
-       extern char *malloc();
 
        p = malloc((unsigned) sz);
        if (p == NULL)
 
        p = malloc((unsigned) sz);
        if (p == NULL)
@@ -206,6 +128,45 @@ copyplist(list, copycont)
        return (newvp);
 }
 \f/*
        return (newvp);
 }
 \f/*
+**  COPYQUEUE -- copy address queue.
+**
+**     This routine is the equivalent of newstr for address queues
+**     addresses marked with QDONTSEND aren't copied
+**
+**     Parameters:
+**             addr -- list of address structures to copy.
+**
+**     Returns:
+**             a copy of 'addr'.
+**
+**     Side Effects:
+**             none.
+*/
+
+ADDRESS *
+copyqueue(addr)
+       ADDRESS *addr;
+{
+       register ADDRESS *newaddr;
+       ADDRESS *ret;
+       register ADDRESS **tail = &ret;
+
+       while (addr != NULL)
+       {
+               if (!bitset(QDONTSEND, addr->q_flags))
+               {
+                       newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
+                       STRUCTCOPY(*addr, *newaddr);
+                       *tail = newaddr;
+                       tail = &newaddr->q_next;
+               }
+               addr = addr->q_next;
+       }
+       *tail = NULL;
+       
+       return ret;
+}
+\f/*
 **  PRINTAV -- print argument vector.
 **
 **     Parameters:
 **  PRINTAV -- print argument vector.
 **
 **     Parameters:
@@ -248,7 +209,7 @@ char
 lower(c)
        register char c;
 {
 lower(c)
        register char c;
 {
-       return(isascii(c) && isupper(c) ? tolower(c) : c);
+       return((isascii(c) && isupper(c)) ? tolower(c) : c);
 }
 \f/*
 **  XPUTS -- put string doing control escapes.
 }
 \f/*
 **  XPUTS -- put string doing control escapes.
@@ -266,29 +227,67 @@ lower(c)
 xputs(s)
        register char *s;
 {
 xputs(s)
        register char *s;
 {
-       register char c;
+       register int c;
+       register struct metamac *mp;
+       extern struct metamac MetaMacros[];
 
        if (s == NULL)
        {
                printf("<null>");
                return;
        }
 
        if (s == NULL)
        {
                printf("<null>");
                return;
        }
-       (void) putchar('"');
-       while ((c = *s++) != '\0')
+       while ((c = (*s++ & 0377)) != '\0')
        {
                if (!isascii(c))
                {
        {
                if (!isascii(c))
                {
+                       if (c == MATCHREPL || c == MACROEXPAND)
+                       {
+                               putchar('$');
+                               continue;
+                       }
+                       for (mp = MetaMacros; mp->metaname != '\0'; mp++)
+                       {
+                               if ((mp->metaval & 0377) == c)
+                               {
+                                       printf("$%c", mp->metaname);
+                                       break;
+                               }
+                       }
+                       if (mp->metaname != '\0')
+                               continue;
                        (void) putchar('\\');
                        c &= 0177;
                }
                        (void) putchar('\\');
                        c &= 0177;
                }
-               if (c < 040 || c >= 0177)
+               if (isprint(c))
+               {
+                       putchar(c);
+                       continue;
+               }
+
+               /* wasn't a meta-macro -- find another way to print it */
+               switch (c)
                {
                {
+                 case '\0':
+                       continue;
+
+                 case '\n':
+                       c = 'n';
+                       break;
+
+                 case '\r':
+                       c = 'r';
+                       break;
+
+                 case '\t':
+                       c = 't';
+                       break;
+
+                 default:
                        (void) putchar('^');
                        (void) putchar('^');
-                       c ^= 0100;
+                       (void) putchar(c ^ 0100);
+                       continue;
                }
                }
-               (void) putchar(c);
        }
        }
-       (void) putchar('"');
        (void) fflush(stdout);
 }
 \f/*
        (void) fflush(stdout);
 }
 \f/*
@@ -337,12 +336,26 @@ fullname(pw, buf)
        register struct passwd *pw;
        char *buf;
 {
        register struct passwd *pw;
        char *buf;
 {
+       register char *p;
        register char *bp = buf;
        register char *bp = buf;
+       int l;
        register char *p = pw->pw_gecos;
 
        register char *p = pw->pw_gecos;
 
-       if (*p == '*')
-               p++;
-       while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
+       if (*gecos == '*')
+               gecos++;
+
+       /* find length of final string */
+       l = 0;
+       for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
+       {
+               if (*p == '&')
+                       l += strlen(login);
+               else
+                       l++;
+       }
+
+       /* now fill in buf */
+       for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
        {
                if (*p == '&')
                {
        {
                if (*p == '&')
                {
@@ -350,10 +363,9 @@ fullname(pw, buf)
                        *bp = toupper(*bp);
                        while (*bp != '\0')
                                bp++;
                        *bp = toupper(*bp);
                        while (*bp != '\0')
                                bp++;
-                       p++;
                }
                else
                }
                else
-                       *bp++ = *p++;
+                       *bp++ = *p;
        }
        *bp = '\0';
 }
        }
        *bp = '\0';
 }
@@ -363,29 +375,81 @@ fullname(pw, buf)
 **     Parameters:
 **             fn -- filename to check.
 **             uid -- uid to compare against.
 **     Parameters:
 **             fn -- filename to check.
 **             uid -- uid to compare against.
+**             mustown -- to be safe, this uid must own the file.
 **             mode -- mode bits that must match.
 **
 **     Returns:
 **             mode -- mode bits that must match.
 **
 **     Returns:
-**             TRUE if fn exists, is owned by uid, and matches mode.
-**             FALSE otherwise.
+**             0 if fn exists, is owned by uid, and matches mode.
+**             An errno otherwise.  The actual errno is cleared.
 **
 **     Side Effects:
 **             none.
 */
 
 **
 **     Side Effects:
 **             none.
 */
 
-bool
-safefile(fn, uid, mode)
+#ifndef S_IXOTH
+# define S_IXOTH       (S_IEXEC >> 6)
+#endif
+
+#ifndef S_IXUSR
+# define S_IXUSR       (S_IEXEC)
+#endif
+
+int
+safefile(fn, uid, mustown, mode)
        char *fn;
        char *fn;
-       int uid;
+       uid_t uid;
+       bool mustown;
        int mode;
 {
        int mode;
 {
+       register char *p;
        struct stat stbuf;
 
        struct stat stbuf;
 
-       if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
-           (stbuf.st_mode & mode) == mode)
-               return (TRUE);
+       if (tTd(54, 4))
+               printf("safefile(%s, %d, %d, %o): ", fn, uid, mustown, mode);
        errno = 0;
        errno = 0;
-       return (FALSE);
+
+       for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
+       {
+               *p = '\0';
+               if (stat(fn, &stbuf) < 0 ||
+                   !bitset(stbuf.st_uid == uid ? S_IXUSR : S_IXOTH,
+                           stbuf.st_mode))
+               {
+                       int ret = errno;
+
+                       if (ret == 0)
+                               ret = EACCES;
+                       if (tTd(54, 4))
+                               printf("[dir %s] %s\n", fn, errstring(ret));
+                       *p = '/';
+                       return ret;
+               }
+       }
+
+       if (stat(fn, &stbuf) < 0)
+       {
+               int ret = errno;
+
+               if (tTd(54, 4))
+                       printf("%s\n", errstring(ret));
+
+               errno = 0;
+               return ret;
+       }
+       if (stbuf.st_uid != uid || uid == 0 || !mustown)
+               mode >>= 6;
+       if (tTd(54, 4))
+               printf("[uid %d, stat %o] ", stbuf.st_uid, stbuf.st_mode);
+       if ((stbuf.st_uid == uid || uid == 0 || !mustown) &&
+           (stbuf.st_mode & mode) == mode)
+       {
+               if (tTd(54, 4))
+                       printf("OK\n");
+               return 0;
+       }
+       if (tTd(54, 4))
+               printf("EACCES\n");
+       return EACCES;
 }
 \f/*
 **  FIXCRLF -- fix <CR><LF> in line.
 }
 \f/*
 **  FIXCRLF -- fix <CR><LF> in line.
@@ -412,7 +476,7 @@ fixcrlf(line, stripnl)
 {
        register char *p;
 
 {
        register char *p;
 
-       p = index(line, '\n');
+       p = strchr(line, '\n');
        if (p == NULL)
                return;
        if (p > line && p[-1] == '\r')
        if (p == NULL)
                return;
        if (p > line && p[-1] == '\r')
@@ -430,26 +494,66 @@ fixcrlf(line, stripnl)
 **     whatever), so this tries to get around it.
 */
 
 **     whatever), so this tries to get around it.
 */
 
+#ifndef O_ACCMODE
+# define O_ACCMODE     (O_RDONLY|O_WRONLY|O_RDWR)
+#endif
+
+struct omodes
+{
+       int     mask;
+       int     mode;
+       char    *farg;
+} OpenModes[] =
+{
+       O_ACCMODE,              O_RDONLY,               "r",
+       O_ACCMODE|O_APPEND,     O_WRONLY,               "w",
+       O_ACCMODE|O_APPEND,     O_WRONLY|O_APPEND,      "a",
+       O_TRUNC,                0,                      "w+",
+       O_APPEND,               O_APPEND,               "a+",
+       0,                      0,                      "r+",
+};
+
 FILE *
 FILE *
-dfopen(filename, mode)
+dfopen(filename, omode, cmode)
        char *filename;
        char *filename;
-       char *mode;
+       int omode;
+       int cmode;
 {
        register int tries;
 {
        register int tries;
-       register FILE *fp;
+       int fd;
+       register struct omodes *om;
+       struct stat st;
+
+       for (om = OpenModes; om->mask != 0; om++)
+               if ((omode & om->mask) == om->mode)
+                       break;
 
        for (tries = 0; tries < 10; tries++)
        {
                sleep((unsigned) (10 * tries));
                errno = 0;
 
        for (tries = 0; tries < 10; tries++)
        {
                sleep((unsigned) (10 * tries));
                errno = 0;
-               fp = fopen(filename, mode);
-               if (fp != NULL)
+               fd = open(filename, omode, cmode);
+               if (fd >= 0)
                        break;
                if (errno != ENFILE && errno != EINTR)
                        break;
        }
                        break;
                if (errno != ENFILE && errno != EINTR)
                        break;
        }
-       errno = 0;
-       return (fp);
+       if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
+       {
+               int locktype;
+
+               /* lock the file to avoid accidental conflicts */
+               if ((omode & O_ACCMODE) != O_RDONLY)
+                       locktype = LOCK_EX;
+               else
+                       locktype = LOCK_SH;
+               (void) lockfile(fd, filename, locktype);
+               errno = 0;
+       }
+       if (fd < 0)
+               return NULL;
+       else
+               return fdopen(fd, om->farg);
 }
 \f/*
 **  PUTLINE -- put a line like fputs obeying SMTP conventions
 }
 \f/*
 **  PUTLINE -- put a line like fputs obeying SMTP conventions
@@ -469,58 +573,69 @@ dfopen(filename, mode)
 **             output of l to fp.
 */
 
 **             output of l to fp.
 */
 
-# define SMTPLINELIM   990     /* maximum line length */
-
 putline(l, fp, m)
        register char *l;
        FILE *fp;
        MAILER *m;
 {
        register char *p;
 putline(l, fp, m)
        register char *l;
        FILE *fp;
        MAILER *m;
 {
        register char *p;
-       char svchar;
+       register char svchar;
 
        /* strip out 0200 bits -- these can look like TELNET protocol */
 
        /* strip out 0200 bits -- these can look like TELNET protocol */
-       if (bitnset(M_LIMITS, m->m_flags))
+       if (bitnset(M_7BITS, m->m_flags))
        {
        {
-               p = l;
-               while ((*p++ &= ~0200) != 0)
-                       continue;
+               for (p = l; (svchar = *p) != '\0'; ++p)
+                       if (bitset(0200, svchar))
+                               *p = svchar &~ 0200;
        }
 
        do
        {
                /* find the end of the line */
        }
 
        do
        {
                /* find the end of the line */
-               p = index(l, '\n');
+               p = strchr(l, '\n');
                if (p == NULL)
                        p = &l[strlen(l)];
 
                if (p == NULL)
                        p = &l[strlen(l)];
 
+               if (TrafficLogFile != NULL)
+                       fprintf(TrafficLogFile, "%05d >>> ", getpid());
+
                /* check for line overflow */
                /* check for line overflow */
-               while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
+               while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
                {
                {
-                       register char *q = &l[SMTPLINELIM - 1];
+                       register char *q = &l[m->m_linelimit - 1];
 
                        svchar = *q;
                        *q = '\0';
                        if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
 
                        svchar = *q;
                        *q = '\0';
                        if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
+                       {
                                (void) putc('.', fp);
                                (void) putc('.', fp);
+                               if (TrafficLogFile != NULL)
+                                       (void) putc('.', TrafficLogFile);
+                       }
                        fputs(l, fp);
                        (void) putc('!', fp);
                        fputs(m->m_eol, fp);
                        fputs(l, fp);
                        (void) putc('!', fp);
                        fputs(m->m_eol, fp);
+                       if (TrafficLogFile != NULL)
+                               fprintf(TrafficLogFile, "%s!\n%05d >>> ",
+                                       l, getpid());
                        *q = svchar;
                        l = q;
                }
 
                /* output last part */
                        *q = svchar;
                        l = q;
                }
 
                /* output last part */
-               svchar = *p;
-               *p = '\0';
                if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
                if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
+               {
                        (void) putc('.', fp);
                        (void) putc('.', fp);
-               fputs(l, fp);
+                       if (TrafficLogFile != NULL)
+                               (void) putc('.', TrafficLogFile);
+               }
+               if (TrafficLogFile != NULL)
+                       fprintf(TrafficLogFile, "%.*s\n", p - l, l);
+               for ( ; l < p; ++l)
+                       (void) putc(*l, fp);
                fputs(m->m_eol, fp);
                fputs(m->m_eol, fp);
-               *p = svchar;
-               l = p;
                if (*l == '\n')
                if (*l == '\n')
-                       l++;
+                       ++l;
        } while (l[0] != '\0');
 }
 \f/*
        } while (l[0] != '\0');
 }
 \f/*
@@ -542,15 +657,38 @@ xunlink(f)
        register int i;
 
 # ifdef LOG
        register int i;
 
 # ifdef LOG
-       if (LogLevel > 20)
-               syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
-# endif LOG
+       if (LogLevel > 98)
+               syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
+# endif /* LOG */
 
        i = unlink(f);
 # ifdef LOG
 
        i = unlink(f);
 # ifdef LOG
-       if (i < 0 && LogLevel > 21)
+       if (i < 0 && LogLevel > 97)
                syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
                syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
-# endif LOG
+# endif /* LOG */
+}
+\f/*
+**  XFCLOSE -- close a file, doing logging as appropriate.
+**
+**     Parameters:
+**             fp -- file pointer for the file to close
+**             a, b -- miscellaneous crud to print for debugging
+**
+**     Returns:
+**             none.
+**
+**     Side Effects:
+**             fp is closed.
+*/
+
+xfclose(fp, a, b)
+       FILE *fp;
+       char *a, *b;
+{
+       if (tTd(53, 99))
+               printf("xfclose(%x) %s %s\n", fp, a, b);
+       if (fclose(fp) < 0 && tTd(53, 99))
+               printf("xfclose FAILURE: %s\n", errstring(errno));
 }
 \f/*
 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
 }
 \f/*
 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
@@ -559,6 +697,8 @@ xunlink(f)
 **             buf -- place to put the input line.
 **             siz -- size of buf.
 **             fp -- file to read from.
 **             buf -- place to put the input line.
 **             siz -- size of buf.
 **             fp -- file to read from.
+**             timeout -- the timeout before error occurs.
+**             during -- what we are trying to read (for error messages).
 **
 **     Returns:
 **             NULL on error (including timeout).  This will also leave
 **
 **     Returns:
 **             NULL on error (including timeout).  This will also leave
@@ -570,33 +710,39 @@ xunlink(f)
 */
 
 static jmp_buf CtxReadTimeout;
 */
 
 static jmp_buf CtxReadTimeout;
+static int     readtimeout();
 
 char *
 
 char *
-sfgets(buf, siz, fp)
+sfgets(buf, siz, fp, timeout, during)
        char *buf;
        int siz;
        FILE *fp;
        char *buf;
        int siz;
        FILE *fp;
+       time_t timeout;
+       char *during;
 {
        register EVENT *ev = NULL;
        register char *p;
 {
        register EVENT *ev = NULL;
        register char *p;
-       extern readtimeout();
 
        /* set the timeout */
 
        /* set the timeout */
-       if (ReadTimeout != 0)
+       if (timeout != 0)
        {
                if (setjmp(CtxReadTimeout) != 0)
                {
 # ifdef LOG
                        syslog(LOG_NOTICE,
        {
                if (setjmp(CtxReadTimeout) != 0)
                {
 # ifdef LOG
                        syslog(LOG_NOTICE,
-                           "timeout waiting for input from %s\n",
-                           RealHostName? RealHostName: "local");
+                           "timeout waiting for input from %s during %s\n",
+                           CurHostName? CurHostName: "local", during);
 # endif
                        errno = 0;
 # endif
                        errno = 0;
-                       usrerr("451 timeout waiting for input");
+                       usrerr("451 timeout waiting for input during %s",
+                               during);
                        buf[0] = '\0';
                        buf[0] = '\0';
+#ifdef XDEBUG
+                       checkfd012(during);
+#endif
                        return (NULL);
                }
                        return (NULL);
                }
-               ev = setevent((time_t) ReadTimeout, readtimeout, 0);
+               ev = setevent(timeout, readtimeout, 0);
        }
 
        /* try to read */
        }
 
        /* try to read */
@@ -617,10 +763,15 @@ sfgets(buf, siz, fp)
        if (p == NULL)
        {
                buf[0] = '\0';
        if (p == NULL)
        {
                buf[0] = '\0';
+               if (TrafficLogFile != NULL)
+                       fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
                return (NULL);
        }
                return (NULL);
        }
-       for (p = buf; *p != '\0'; p++)
-               *p &= ~0200;
+       if (TrafficLogFile != NULL)
+               fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
+       if (SevenBit)
+               for (p = buf; *p != '\0'; p++)
+                       *p &= ~0200;
        return (buf);
 }
 
        return (buf);
 }
 
@@ -638,7 +789,9 @@ readtimeout()
 **             f -- file to read from.
 **
 **     Returns:
 **             f -- file to read from.
 **
 **     Returns:
-**             buf on success, NULL on error or EOF.
+**             input line(s) on success, NULL on error or EOF.
+**             This will normally be buf -- unless the line is too
+**                     long, when it will be xalloc()ed.
 **
 **     Side Effects:
 **             buf gets lines from f, with continuation lines (lines
 **
 **     Side Effects:
 **             buf gets lines from f, with continuation lines (lines
@@ -653,6 +806,7 @@ fgetfolded(buf, n, f)
        FILE *f;
 {
        register char *p = buf;
        FILE *f;
 {
        register char *p = buf;
+       char *bp = buf;
        register int i;
 
        n--;
        register int i;
 
        n--;
@@ -668,8 +822,26 @@ fgetfolded(buf, n, f)
                                i = '\r';
                        }
                }
                                i = '\r';
                        }
                }
-               if (--n > 0)
-                       *p++ = i;
+               if (--n <= 0)
+               {
+                       /* allocate new space */
+                       char *nbp;
+                       int nn;
+
+                       nn = (p - bp);
+                       if (nn < MEMCHUNKSIZE)
+                               nn *= 2;
+                       else
+                               nn += MEMCHUNKSIZE;
+                       nbp = xalloc(nn);
+                       bcopy(bp, nbp, p - bp);
+                       p = &nbp[p - bp];
+                       if (bp != buf)
+                               free(bp);
+                       bp = nbp;
+                       n = nn - (p - bp);
+               }
+               *p++ = i;
                if (i == '\n')
                {
                        LineNumber++;
                if (i == '\n')
                {
                        LineNumber++;
@@ -677,13 +849,13 @@ fgetfolded(buf, n, f)
                        if (i != EOF)
                                (void) ungetc(i, f);
                        if (i != ' ' && i != '\t')
                        if (i != EOF)
                                (void) ungetc(i, f);
                        if (i != ' ' && i != '\t')
-                       {
-                               *--p = '\0';
-                               return (buf);
-                       }
+                               break;
                }
        }
                }
        }
-       return (NULL);
+       if (p == bp)
+               return (NULL);
+       *--p = '\0';
+       return (bp);
 }
 \f/*
 **  CURTIME -- return current time.
 }
 \f/*
 **  CURTIME -- return current time.
@@ -726,7 +898,7 @@ bool
 atobool(s)
        register char *s;
 {
 atobool(s)
        register char *s;
 {
-       if (*s == '\0' || index("tTyY", *s) != NULL)
+       if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
                return (TRUE);
        return (FALSE);
 }
                return (TRUE);
        return (FALSE);
 }
@@ -833,3 +1005,71 @@ bitzerop(map)
                        return (FALSE);
        return (TRUE);
 }
                        return (FALSE);
        return (TRUE);
 }
+\f/*
+**  STRCONTAINEDIN -- tell if one string is contained in another
+**
+**     Parameters:
+**             a -- possible substring.
+**             b -- possible superstring.
+**
+**     Returns:
+**             TRUE if a is contained in b.
+**             FALSE otherwise.
+*/
+
+bool
+strcontainedin(a, b)
+       register char *a;
+       register char *b;
+{
+       int l;
+
+       l = strlen(a);
+       for (;;)
+       {
+               b = strchr(b, a[0]);
+               if (b == NULL)
+                       return FALSE;
+               if (strncmp(a, b, l) == 0)
+                       return TRUE;
+               b++;
+       }
+}
+\f/*
+**  CHECKFD012 -- check low numbered file descriptors
+**
+**     File descriptors 0, 1, and 2 should be open at all times.
+**     This routine verifies that, and fixes it if not true.
+**
+**     Parameters:
+**             where -- a tag printed if the assertion failed
+**
+**     Returns:
+**             none
+*/
+
+checkfd012(where)
+       char *where;
+{
+#ifdef XDEBUG
+       register int i;
+       struct stat stbuf;
+
+       for (i = 0; i < 3; i++)
+       {
+               if (fstat(i, &stbuf) < 0)
+               {
+                       /* oops.... */
+                       int fd;
+
+                       syserr("%s: fd %d not open", where, i);
+                       fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
+                       if (fd != i)
+                       {
+                               (void) dup2(fd, i);
+                               (void) close(fd);
+                       }
+               }
+       }
+#endif /* XDEBUG */
+}