122717Sdist /* 242833Sbostic * Copyright (c) 1983 Eric P. Allman 3*63589Sbostic * Copyright (c) 1988, 1993 4*63589Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642833Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822717Sdist 922717Sdist #ifndef lint 10*63589Sbostic static char sccsid[] = "@(#)util.c 8.1 (Berkeley) 06/27/93"; 1133731Sbostic #endif /* not lint */ 1222717Sdist 1358332Seric # include "sendmail.h" 14298Seric # include <sysexits.h> 1557135Seric /* 16298Seric ** STRIPQUOTES -- Strip quotes & quote bits from a string. 17298Seric ** 18298Seric ** Runs through a string and strips off unquoted quote 19298Seric ** characters and quote bits. This is done in place. 20298Seric ** 21298Seric ** Parameters: 22298Seric ** s -- the string to strip. 23298Seric ** 24298Seric ** Returns: 25298Seric ** none. 26298Seric ** 27298Seric ** Side Effects: 28298Seric ** none. 29298Seric ** 30298Seric ** Called By: 31298Seric ** deliver 32298Seric */ 33298Seric 3454983Seric stripquotes(s) 35298Seric char *s; 36298Seric { 37298Seric register char *p; 38298Seric register char *q; 39298Seric register char c; 40298Seric 414101Seric if (s == NULL) 424101Seric return; 434101Seric 4454983Seric p = q = s; 4554983Seric do 46298Seric { 4754983Seric c = *p++; 4854983Seric if (c == '\\') 4954983Seric c = *p++; 5054983Seric else if (c == '"') 5154983Seric continue; 5254983Seric *q++ = c; 5354983Seric } while (c != '\0'); 54298Seric } 55298Seric /* 56298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 57298Seric ** 58298Seric ** THIS IS A CLUDGE. This should be made to give a proper 59298Seric ** error -- but after all, what can we do? 60298Seric ** 61298Seric ** Parameters: 62298Seric ** sz -- size of area to allocate. 63298Seric ** 64298Seric ** Returns: 65298Seric ** pointer to data region. 66298Seric ** 67298Seric ** Side Effects: 68298Seric ** Memory is allocated. 69298Seric */ 70298Seric 71298Seric char * 72298Seric xalloc(sz) 737007Seric register int sz; 74298Seric { 75298Seric register char *p; 76298Seric 7723121Seric p = malloc((unsigned) sz); 78298Seric if (p == NULL) 79298Seric { 80298Seric syserr("Out of memory!!"); 8110685Seric abort(); 8210685Seric /* exit(EX_UNAVAILABLE); */ 83298Seric } 84298Seric return (p); 85298Seric } 86298Seric /* 873151Seric ** COPYPLIST -- copy list of pointers. 883151Seric ** 893151Seric ** This routine is the equivalent of newstr for lists of 903151Seric ** pointers. 913151Seric ** 923151Seric ** Parameters: 933151Seric ** list -- list of pointers to copy. 943151Seric ** Must be NULL terminated. 953151Seric ** copycont -- if TRUE, copy the contents of the vector 963151Seric ** (which must be a string) also. 973151Seric ** 983151Seric ** Returns: 993151Seric ** a copy of 'list'. 1003151Seric ** 1013151Seric ** Side Effects: 1023151Seric ** none. 1033151Seric */ 1043151Seric 1053151Seric char ** 1063151Seric copyplist(list, copycont) 1073151Seric char **list; 1083151Seric bool copycont; 1093151Seric { 1103151Seric register char **vp; 1113151Seric register char **newvp; 1123151Seric 1133151Seric for (vp = list; *vp != NULL; vp++) 1143151Seric continue; 1153151Seric 1163151Seric vp++; 1173151Seric 11816897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 11916897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1203151Seric 1213151Seric if (copycont) 1223151Seric { 1233151Seric for (vp = newvp; *vp != NULL; vp++) 1243151Seric *vp = newstr(*vp); 1253151Seric } 1263151Seric 1273151Seric return (newvp); 1283151Seric } 1293151Seric /* 13058170Seric ** COPYQUEUE -- copy address queue. 13158170Seric ** 13258170Seric ** This routine is the equivalent of newstr for address queues 13358170Seric ** addresses marked with QDONTSEND aren't copied 13458170Seric ** 13558170Seric ** Parameters: 13658170Seric ** addr -- list of address structures to copy. 13758170Seric ** 13858170Seric ** Returns: 13958170Seric ** a copy of 'addr'. 14058170Seric ** 14158170Seric ** Side Effects: 14258170Seric ** none. 14358170Seric */ 14458170Seric 14558170Seric ADDRESS * 14658170Seric copyqueue(addr) 14758170Seric ADDRESS *addr; 14858170Seric { 14958170Seric register ADDRESS *newaddr; 15058170Seric ADDRESS *ret; 15158170Seric register ADDRESS **tail = &ret; 15258170Seric 15358170Seric while (addr != NULL) 15458170Seric { 15558170Seric if (!bitset(QDONTSEND, addr->q_flags)) 15658170Seric { 15758170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 15858170Seric STRUCTCOPY(*addr, *newaddr); 15958170Seric *tail = newaddr; 16058170Seric tail = &newaddr->q_next; 16158170Seric } 16258170Seric addr = addr->q_next; 16358170Seric } 16458170Seric *tail = NULL; 16558170Seric 16658170Seric return ret; 16758170Seric } 16858170Seric /* 1693151Seric ** PRINTAV -- print argument vector. 1703151Seric ** 1713151Seric ** Parameters: 1723151Seric ** av -- argument vector. 1733151Seric ** 1743151Seric ** Returns: 1753151Seric ** none. 1763151Seric ** 1773151Seric ** Side Effects: 1783151Seric ** prints av. 1793151Seric */ 1803151Seric 1813151Seric printav(av) 1823151Seric register char **av; 1833151Seric { 1843151Seric while (*av != NULL) 1853151Seric { 1868063Seric if (tTd(0, 44)) 1878063Seric printf("\n\t%08x=", *av); 1888063Seric else 18923105Seric (void) putchar(' '); 1903151Seric xputs(*av++); 1913151Seric } 19223105Seric (void) putchar('\n'); 1933151Seric } 1943151Seric /* 1953151Seric ** LOWER -- turn letter into lower case. 1963151Seric ** 1973151Seric ** Parameters: 1983151Seric ** c -- character to turn into lower case. 1993151Seric ** 2003151Seric ** Returns: 2013151Seric ** c, in lower case. 2023151Seric ** 2033151Seric ** Side Effects: 2043151Seric ** none. 2053151Seric */ 2063151Seric 2073151Seric char 2083151Seric lower(c) 2093151Seric register char c; 2103151Seric { 21158050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2123151Seric } 2133151Seric /* 2143151Seric ** XPUTS -- put string doing control escapes. 2153151Seric ** 2163151Seric ** Parameters: 2173151Seric ** s -- string to put. 2183151Seric ** 2193151Seric ** Returns: 2203151Seric ** none. 2213151Seric ** 2223151Seric ** Side Effects: 2233151Seric ** output to stdout 2243151Seric */ 2253151Seric 2263151Seric xputs(s) 2273151Seric register char *s; 2283151Seric { 22958050Seric register int c; 23051781Seric register struct metamac *mp; 23151781Seric extern struct metamac MetaMacros[]; 2323151Seric 2338055Seric if (s == NULL) 2348055Seric { 2358055Seric printf("<null>"); 2368055Seric return; 2378055Seric } 23858050Seric while ((c = (*s++ & 0377)) != '\0') 2393151Seric { 2403151Seric if (!isascii(c)) 2413151Seric { 24258050Seric if (c == MATCHREPL || c == MACROEXPAND) 24358050Seric { 24458050Seric putchar('$'); 24558050Seric continue; 24658050Seric } 24758050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 24858050Seric { 24958050Seric if ((mp->metaval & 0377) == c) 25058050Seric { 25158050Seric printf("$%c", mp->metaname); 25258050Seric break; 25358050Seric } 25458050Seric } 25558050Seric if (mp->metaname != '\0') 25658050Seric continue; 25723105Seric (void) putchar('\\'); 2583151Seric c &= 0177; 2593151Seric } 26057589Seric if (isprint(c)) 2613151Seric { 26257589Seric putchar(c); 26357589Seric continue; 26457589Seric } 26552050Seric 26657589Seric /* wasn't a meta-macro -- find another way to print it */ 26757589Seric switch (c) 26857589Seric { 26957589Seric case '\0': 27057589Seric continue; 27152050Seric 27257589Seric case '\n': 27357589Seric c = 'n'; 27457589Seric break; 27552050Seric 27657589Seric case '\r': 27757589Seric c = 'r'; 27857589Seric break; 27952637Seric 28057589Seric case '\t': 28157589Seric c = 't'; 28257589Seric break; 28357589Seric 28457589Seric default: 28557589Seric (void) putchar('^'); 28657589Seric (void) putchar(c ^ 0100); 28757589Seric continue; 2883151Seric } 2893151Seric } 2904086Seric (void) fflush(stdout); 2913151Seric } 2923151Seric /* 2933151Seric ** MAKELOWER -- Translate a line into lower case 2943151Seric ** 2953151Seric ** Parameters: 2963151Seric ** p -- the string to translate. If NULL, return is 2973151Seric ** immediate. 2983151Seric ** 2993151Seric ** Returns: 3003151Seric ** none. 3013151Seric ** 3023151Seric ** Side Effects: 3033151Seric ** String pointed to by p is translated to lower case. 3043151Seric ** 3053151Seric ** Called By: 3063151Seric ** parse 3073151Seric */ 3083151Seric 3093151Seric makelower(p) 3103151Seric register char *p; 3113151Seric { 3123151Seric register char c; 3133151Seric 3143151Seric if (p == NULL) 3153151Seric return; 3163151Seric for (; (c = *p) != '\0'; p++) 3173151Seric if (isascii(c) && isupper(c)) 31833724Sbostic *p = tolower(c); 3193151Seric } 3204059Seric /* 3215196Seric ** BUILDFNAME -- build full name from gecos style entry. 3224375Seric ** 3235196Seric ** This routine interprets the strange entry that would appear 3245196Seric ** in the GECOS field of the password file. 3255196Seric ** 3264375Seric ** Parameters: 3275196Seric ** p -- name to build. 3285196Seric ** login -- the login name of this user (for &). 3295196Seric ** buf -- place to put the result. 3304375Seric ** 3314375Seric ** Returns: 3324375Seric ** none. 3334375Seric ** 3344375Seric ** Side Effects: 3354375Seric ** none. 3364375Seric */ 3374375Seric 33854984Seric buildfname(gecos, login, buf) 33954984Seric register char *gecos; 3405196Seric char *login; 3414375Seric char *buf; 3424375Seric { 34354984Seric register char *p; 3444375Seric register char *bp = buf; 34554984Seric int l; 3464375Seric 34754984Seric if (*gecos == '*') 34854984Seric gecos++; 34954984Seric 35057123Seric /* find length of final string */ 35154984Seric l = 0; 35254984Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 35354984Seric { 35454984Seric if (*p == '&') 35554984Seric l += strlen(login); 35654984Seric else 35754984Seric l++; 35854984Seric } 35954984Seric 36054984Seric /* now fill in buf */ 36155193Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3624375Seric { 3634375Seric if (*p == '&') 3644375Seric { 3655196Seric (void) strcpy(bp, login); 3664375Seric *bp = toupper(*bp); 3674375Seric while (*bp != '\0') 3684375Seric bp++; 3694375Seric } 3704375Seric else 37155193Seric *bp++ = *p; 3724375Seric } 3734375Seric *bp = '\0'; 3744375Seric } 3754375Seric /* 3764538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 3774538Seric ** 3784538Seric ** Parameters: 3794538Seric ** fn -- filename to check. 3804538Seric ** uid -- uid to compare against. 3814538Seric ** mode -- mode bits that must match. 3824538Seric ** 3834538Seric ** Returns: 38458247Seric ** 0 if fn exists, is owned by uid, and matches mode. 38558247Seric ** An errno otherwise. The actual errno is cleared. 3864538Seric ** 3874538Seric ** Side Effects: 3884538Seric ** none. 3894538Seric */ 3904538Seric 39163581Seric #ifndef S_IXOTH 39263581Seric # define S_IXOTH (S_IEXEC >> 6) 39363581Seric #endif 39463581Seric 39558247Seric int 3964538Seric safefile(fn, uid, mode) 3974538Seric char *fn; 39855372Seric uid_t uid; 3994538Seric int mode; 4004538Seric { 40163581Seric register char *p; 4024538Seric struct stat stbuf; 4034538Seric 40463581Seric if (tTd(54, 4)) 40563581Seric printf("safefile(%s, %d, %o): ", fn, uid, mode); 40663581Seric errno = 0; 40763581Seric 40863581Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 40963581Seric { 41063581Seric *p = '\0'; 41163581Seric if (stat(fn, &stbuf) < 0 || !bitset(S_IXOTH, stbuf.st_mode)) 41263581Seric { 41363581Seric int ret = errno; 41463581Seric 41563581Seric if (ret == 0) 41663581Seric ret = EACCES; 41763581Seric if (tTd(54, 4)) 41863581Seric printf("[dir %s] %s\n", fn, errstring(ret)); 41963581Seric *p = '/'; 42063581Seric return ret; 42163581Seric } 42263581Seric } 42363581Seric 42458247Seric if (stat(fn, &stbuf) < 0) 42558247Seric { 42658247Seric int ret = errno; 42758247Seric 42863581Seric if (tTd(54, 4)) 42963581Seric printf("%s\n", errstring(ret)); 43063581Seric 43158247Seric errno = 0; 43258247Seric return ret; 43358247Seric } 43463581Seric if (stbuf.st_uid != uid && uid == 0) 43563581Seric mode >>= 6; 43663581Seric if (tTd(54, 4)) 43763581Seric printf("[uid %d, stat %o] ", stbuf.st_uid, stbuf.st_mode); 43863581Seric if ((stbuf.st_uid == uid || uid == 0) && 43963581Seric (stbuf.st_mode & mode) == mode) 44063581Seric { 44163581Seric if (tTd(54, 4)) 44263581Seric printf("OK\n"); 44358247Seric return 0; 44463581Seric } 44563581Seric if (tTd(54, 4)) 44663581Seric printf("EACCES\n"); 44763581Seric return EACCES; 4484538Seric } 4494538Seric /* 4504557Seric ** FIXCRLF -- fix <CR><LF> in line. 4514557Seric ** 4524557Seric ** Looks for the <CR><LF> combination and turns it into the 4534557Seric ** UNIX canonical <NL> character. It only takes one line, 4544557Seric ** i.e., it is assumed that the first <NL> found is the end 4554557Seric ** of the line. 4564557Seric ** 4574557Seric ** Parameters: 4584557Seric ** line -- the line to fix. 4594557Seric ** stripnl -- if true, strip the newline also. 4604557Seric ** 4614557Seric ** Returns: 4624557Seric ** none. 4634557Seric ** 4644557Seric ** Side Effects: 4654557Seric ** line is changed in place. 4664557Seric */ 4674557Seric 4684557Seric fixcrlf(line, stripnl) 4694557Seric char *line; 4704557Seric bool stripnl; 4714557Seric { 4724557Seric register char *p; 4734557Seric 47456795Seric p = strchr(line, '\n'); 4754557Seric if (p == NULL) 4764557Seric return; 47736291Sbostic if (p > line && p[-1] == '\r') 4784557Seric p--; 4794557Seric if (!stripnl) 4804557Seric *p++ = '\n'; 4814557Seric *p = '\0'; 4824557Seric } 4834557Seric /* 4846890Seric ** DFOPEN -- determined file open 4856890Seric ** 4866890Seric ** This routine has the semantics of fopen, except that it will 4876890Seric ** keep trying a few times to make this happen. The idea is that 4886890Seric ** on very loaded systems, we may run out of resources (inodes, 4896890Seric ** whatever), so this tries to get around it. 4906890Seric */ 4916890Seric 49259745Seric struct omodes 49359745Seric { 49459745Seric int mask; 49559745Seric int mode; 49659745Seric char *farg; 49759745Seric } OpenModes[] = 49859745Seric { 49959745Seric O_ACCMODE, O_RDONLY, "r", 50059745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 50159745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 50259745Seric O_TRUNC, 0, "w+", 50359745Seric O_APPEND, O_APPEND, "a+", 50459745Seric 0, 0, "r+", 50559745Seric }; 50659745Seric 5076890Seric FILE * 50859745Seric dfopen(filename, omode, cmode) 5096890Seric char *filename; 51059745Seric int omode; 51159745Seric int cmode; 5126890Seric { 5136890Seric register int tries; 51459745Seric int fd; 51559745Seric register struct omodes *om; 51659431Seric struct stat st; 5176890Seric 51859745Seric for (om = OpenModes; om->mask != 0; om++) 51959745Seric if ((omode & om->mask) == om->mode) 52059745Seric break; 52159745Seric 5226890Seric for (tries = 0; tries < 10; tries++) 5236890Seric { 52425618Seric sleep((unsigned) (10 * tries)); 5256890Seric errno = 0; 52659745Seric fd = open(filename, omode, cmode); 52759745Seric if (fd >= 0) 5286890Seric break; 5299376Seric if (errno != ENFILE && errno != EINTR) 5309376Seric break; 5316890Seric } 53259745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 53356328Seric { 53456328Seric int locktype; 53556328Seric 53656328Seric /* lock the file to avoid accidental conflicts */ 53759745Seric if ((omode & O_ACCMODE) != O_RDONLY) 53856328Seric locktype = LOCK_EX; 53956328Seric else 54056328Seric locktype = LOCK_SH; 54159745Seric (void) lockfile(fd, filename, locktype); 54256328Seric errno = 0; 54356328Seric } 54459745Seric return fdopen(fd, om->farg); 5456890Seric } 5467124Seric /* 5477124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 5487124Seric ** 5497753Seric ** This routine always guarantees outputing a newline (or CRLF, 5507753Seric ** as appropriate) at the end of the string. 5517753Seric ** 5527124Seric ** Parameters: 5537124Seric ** l -- line to put. 5547124Seric ** fp -- file to put it onto. 55510172Seric ** m -- the mailer used to control output. 5567124Seric ** 5577124Seric ** Returns: 5587124Seric ** none 5597124Seric ** 5607124Seric ** Side Effects: 5617124Seric ** output of l to fp. 5627124Seric */ 5637124Seric 56410172Seric putline(l, fp, m) 5657753Seric register char *l; 5667124Seric FILE *fp; 56710172Seric MAILER *m; 5687124Seric { 5697124Seric register char *p; 57047157Sbostic register char svchar; 5717124Seric 57211275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 57352106Seric if (bitnset(M_7BITS, m->m_flags)) 57411275Seric { 57561707Seric for (p = l; (svchar = *p) != '\0'; ++p) 57661707Seric if (bitset(0200, svchar)) 57747157Sbostic *p = svchar &~ 0200; 57811275Seric } 57911275Seric 5807753Seric do 5817124Seric { 5827753Seric /* find the end of the line */ 58356795Seric p = strchr(l, '\n'); 5847753Seric if (p == NULL) 5857753Seric p = &l[strlen(l)]; 5867124Seric 5877753Seric /* check for line overflow */ 58852106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 5897753Seric { 59052106Seric register char *q = &l[m->m_linelimit - 1]; 5917124Seric 5927753Seric svchar = *q; 5937753Seric *q = '\0'; 59410685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 59523105Seric (void) putc('.', fp); 5967753Seric fputs(l, fp); 59723105Seric (void) putc('!', fp); 59810326Seric fputs(m->m_eol, fp); 5997753Seric *q = svchar; 6007753Seric l = q; 6017753Seric } 6027124Seric 6037753Seric /* output last part */ 60410685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 60523105Seric (void) putc('.', fp); 60647157Sbostic for ( ; l < p; ++l) 60747157Sbostic (void) putc(*l, fp); 60810326Seric fputs(m->m_eol, fp); 6097753Seric if (*l == '\n') 61047157Sbostic ++l; 6117753Seric } while (l[0] != '\0'); 6127124Seric } 6137676Seric /* 6147676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 6157676Seric ** 6167676Seric ** Parameters: 6177676Seric ** f -- name of file to unlink. 6187676Seric ** 6197676Seric ** Returns: 6207676Seric ** none. 6217676Seric ** 6227676Seric ** Side Effects: 6237676Seric ** f is unlinked. 6247676Seric */ 6257676Seric 6267676Seric xunlink(f) 6277676Seric char *f; 6287676Seric { 6297676Seric register int i; 6307676Seric 6317676Seric # ifdef LOG 63258020Seric if (LogLevel > 98) 63358020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 63456795Seric # endif /* LOG */ 6357676Seric 6367676Seric i = unlink(f); 6377676Seric # ifdef LOG 63858020Seric if (i < 0 && LogLevel > 97) 6397942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 64056795Seric # endif /* LOG */ 6417676Seric } 6427685Seric /* 64358680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 64458680Seric ** 64558680Seric ** Parameters: 64658680Seric ** fp -- file pointer for the file to close 64758680Seric ** a, b -- miscellaneous crud to print for debugging 64858680Seric ** 64958680Seric ** Returns: 65058680Seric ** none. 65158680Seric ** 65258680Seric ** Side Effects: 65358680Seric ** fp is closed. 65458680Seric */ 65558680Seric 65658680Seric xfclose(fp, a, b) 65758680Seric FILE *fp; 65858680Seric char *a, *b; 65958680Seric { 66058796Seric if (tTd(53, 99)) 66158680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 66258796Seric if (fclose(fp) < 0 && tTd(53, 99)) 66358680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 66458680Seric } 66558680Seric /* 66614885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 6677685Seric ** 6687685Seric ** Parameters: 6697685Seric ** buf -- place to put the input line. 6707685Seric ** siz -- size of buf. 6717685Seric ** fp -- file to read from. 67257384Seric ** timeout -- the timeout before error occurs. 67361093Seric ** during -- what we are trying to read (for error messages). 6747685Seric ** 6757685Seric ** Returns: 67615533Seric ** NULL on error (including timeout). This will also leave 67715533Seric ** buf containing a null string. 6787685Seric ** buf otherwise. 6797685Seric ** 6807685Seric ** Side Effects: 6817685Seric ** none. 6827685Seric */ 6837685Seric 68414885Seric static jmp_buf CtxReadTimeout; 6857685Seric 6867685Seric char * 68761093Seric sfgets(buf, siz, fp, timeout, during) 6887685Seric char *buf; 6897685Seric int siz; 6907685Seric FILE *fp; 69157384Seric time_t timeout; 69261093Seric char *during; 6937685Seric { 6947942Seric register EVENT *ev = NULL; 6957685Seric register char *p; 69646928Sbostic static int readtimeout(); 6977685Seric 69814885Seric /* set the timeout */ 69957384Seric if (timeout != 0) 70014885Seric { 70114885Seric if (setjmp(CtxReadTimeout) != 0) 70214885Seric { 70336233Skarels # ifdef LOG 70436230Skarels syslog(LOG_NOTICE, 70561093Seric "timeout waiting for input from %s during %s\n", 70661093Seric CurHostName? CurHostName: "local", during); 70736233Skarels # endif 70836230Skarels errno = 0; 70961093Seric usrerr("451 timeout waiting for input during %s", 71061093Seric during); 71119037Seric buf[0] = '\0'; 71214885Seric return (NULL); 71314885Seric } 71457384Seric ev = setevent(timeout, readtimeout, 0); 71514885Seric } 71614885Seric 71714885Seric /* try to read */ 71815533Seric p = NULL; 71915533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 7207942Seric { 7217942Seric errno = 0; 7227942Seric p = fgets(buf, siz, fp); 72315533Seric if (errno == EINTR) 72415533Seric clearerr(fp); 72515533Seric } 72614885Seric 72714885Seric /* clear the event if it has not sprung */ 7287685Seric clrevent(ev); 72914885Seric 73014885Seric /* clean up the books and exit */ 7318055Seric LineNumber++; 73215533Seric if (p == NULL) 73316880Seric { 73415533Seric buf[0] = '\0'; 73516880Seric return (NULL); 73616880Seric } 73759709Seric if (SevenBit) 73852106Seric for (p = buf; *p != '\0'; p++) 73952106Seric *p &= ~0200; 74016880Seric return (buf); 7417685Seric } 7427685Seric 7437685Seric static 7447685Seric readtimeout() 7457685Seric { 74614885Seric longjmp(CtxReadTimeout, 1); 7477685Seric } 7487786Seric /* 7497786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 7507786Seric ** 7517786Seric ** Parameters: 7527786Seric ** buf -- place to put result. 7537786Seric ** n -- bytes available. 7547786Seric ** f -- file to read from. 7557786Seric ** 7567786Seric ** Returns: 75757135Seric ** input line(s) on success, NULL on error or EOF. 75857135Seric ** This will normally be buf -- unless the line is too 75957135Seric ** long, when it will be xalloc()ed. 7607786Seric ** 7617786Seric ** Side Effects: 7627786Seric ** buf gets lines from f, with continuation lines (lines 7637786Seric ** with leading white space) appended. CRLF's are mapped 7647786Seric ** into single newlines. Any trailing NL is stripped. 7657786Seric */ 7667786Seric 7677786Seric char * 7687786Seric fgetfolded(buf, n, f) 7697786Seric char *buf; 7707786Seric register int n; 7717786Seric FILE *f; 7727786Seric { 7737786Seric register char *p = buf; 77457135Seric char *bp = buf; 7757786Seric register int i; 7767786Seric 7777786Seric n--; 77817350Seric while ((i = getc(f)) != EOF) 7797786Seric { 78017350Seric if (i == '\r') 78117350Seric { 78217350Seric i = getc(f); 78317350Seric if (i != '\n') 78417350Seric { 78517350Seric if (i != EOF) 78623105Seric (void) ungetc(i, f); 78717350Seric i = '\r'; 78817350Seric } 78917350Seric } 79057135Seric if (--n <= 0) 79157135Seric { 79257135Seric /* allocate new space */ 79357135Seric char *nbp; 79457135Seric int nn; 79557135Seric 79657135Seric nn = (p - bp); 79757232Seric if (nn < MEMCHUNKSIZE) 79857135Seric nn *= 2; 79957135Seric else 80057232Seric nn += MEMCHUNKSIZE; 80157135Seric nbp = xalloc(nn); 80257135Seric bcopy(bp, nbp, p - bp); 80357135Seric p = &nbp[p - bp]; 80457135Seric if (bp != buf) 80557135Seric free(bp); 80657135Seric bp = nbp; 80757135Seric n = nn - (p - bp); 80857135Seric } 80957135Seric *p++ = i; 81017350Seric if (i == '\n') 81117350Seric { 81217350Seric LineNumber++; 81317350Seric i = getc(f); 81417350Seric if (i != EOF) 81523105Seric (void) ungetc(i, f); 81617350Seric if (i != ' ' && i != '\t') 81752647Seric break; 81817350Seric } 8197786Seric } 82057135Seric if (p == bp) 82152647Seric return (NULL); 82252647Seric *--p = '\0'; 82357135Seric return (bp); 8247786Seric } 8257860Seric /* 8267886Seric ** CURTIME -- return current time. 8277886Seric ** 8287886Seric ** Parameters: 8297886Seric ** none. 8307886Seric ** 8317886Seric ** Returns: 8327886Seric ** the current time. 8337886Seric ** 8347886Seric ** Side Effects: 8357886Seric ** none. 8367886Seric */ 8377886Seric 8387886Seric time_t 8397886Seric curtime() 8407886Seric { 8417886Seric auto time_t t; 8427886Seric 8437886Seric (void) time(&t); 8447886Seric return (t); 8457886Seric } 8468264Seric /* 8478264Seric ** ATOBOOL -- convert a string representation to boolean. 8488264Seric ** 8498264Seric ** Defaults to "TRUE" 8508264Seric ** 8518264Seric ** Parameters: 8528264Seric ** s -- string to convert. Takes "tTyY" as true, 8538264Seric ** others as false. 8548264Seric ** 8558264Seric ** Returns: 8568264Seric ** A boolean representation of the string. 8578264Seric ** 8588264Seric ** Side Effects: 8598264Seric ** none. 8608264Seric */ 8618264Seric 8628264Seric bool 8638264Seric atobool(s) 8648264Seric register char *s; 8658264Seric { 86656795Seric if (*s == '\0' || strchr("tTyY", *s) != NULL) 8678264Seric return (TRUE); 8688264Seric return (FALSE); 8698264Seric } 8709048Seric /* 8719048Seric ** ATOOCT -- convert a string representation to octal. 8729048Seric ** 8739048Seric ** Parameters: 8749048Seric ** s -- string to convert. 8759048Seric ** 8769048Seric ** Returns: 8779048Seric ** An integer representing the string interpreted as an 8789048Seric ** octal number. 8799048Seric ** 8809048Seric ** Side Effects: 8819048Seric ** none. 8829048Seric */ 8839048Seric 8849048Seric atooct(s) 8859048Seric register char *s; 8869048Seric { 8879048Seric register int i = 0; 8889048Seric 8899048Seric while (*s >= '0' && *s <= '7') 8909048Seric i = (i << 3) | (*s++ - '0'); 8919048Seric return (i); 8929048Seric } 8939376Seric /* 8949376Seric ** WAITFOR -- wait for a particular process id. 8959376Seric ** 8969376Seric ** Parameters: 8979376Seric ** pid -- process id to wait for. 8989376Seric ** 8999376Seric ** Returns: 9009376Seric ** status of pid. 9019376Seric ** -1 if pid never shows up. 9029376Seric ** 9039376Seric ** Side Effects: 9049376Seric ** none. 9059376Seric */ 9069376Seric 9079376Seric waitfor(pid) 9089376Seric int pid; 9099376Seric { 9109376Seric auto int st; 9119376Seric int i; 9129376Seric 9139376Seric do 9149376Seric { 9159376Seric errno = 0; 9169376Seric i = wait(&st); 9179376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 9189376Seric if (i < 0) 9199376Seric st = -1; 9209376Seric return (st); 9219376Seric } 9229376Seric /* 92310685Seric ** BITINTERSECT -- tell if two bitmaps intersect 92410685Seric ** 92510685Seric ** Parameters: 92610685Seric ** a, b -- the bitmaps in question 92710685Seric ** 92810685Seric ** Returns: 92910685Seric ** TRUE if they have a non-null intersection 93010685Seric ** FALSE otherwise 93110685Seric ** 93210685Seric ** Side Effects: 93310685Seric ** none. 93410685Seric */ 93510685Seric 93610685Seric bool 93710685Seric bitintersect(a, b) 93810685Seric BITMAP a; 93910685Seric BITMAP b; 94010685Seric { 94110685Seric int i; 94210685Seric 94310685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 94410685Seric if ((a[i] & b[i]) != 0) 94510685Seric return (TRUE); 94610685Seric return (FALSE); 94710685Seric } 94810685Seric /* 94910685Seric ** BITZEROP -- tell if a bitmap is all zero 95010685Seric ** 95110685Seric ** Parameters: 95210685Seric ** map -- the bit map to check 95310685Seric ** 95410685Seric ** Returns: 95510685Seric ** TRUE if map is all zero. 95610685Seric ** FALSE if there are any bits set in map. 95710685Seric ** 95810685Seric ** Side Effects: 95910685Seric ** none. 96010685Seric */ 96110685Seric 96210685Seric bool 96310685Seric bitzerop(map) 96410685Seric BITMAP map; 96510685Seric { 96610685Seric int i; 96710685Seric 96810685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 96910685Seric if (map[i] != 0) 97010685Seric return (FALSE); 97110685Seric return (TRUE); 97210685Seric } 97358247Seric /* 97458318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 97558318Seric ** 97658318Seric ** Parameters: 97758318Seric ** a -- possible substring. 97858318Seric ** b -- possible superstring. 97958318Seric ** 98058318Seric ** Returns: 98158318Seric ** TRUE if a is contained in b. 98258318Seric ** FALSE otherwise. 98358318Seric */ 98458318Seric 98558318Seric bool 98658318Seric strcontainedin(a, b) 98758318Seric register char *a; 98858318Seric register char *b; 98958318Seric { 99058318Seric int l; 99158318Seric 99258318Seric l = strlen(a); 99358318Seric for (;;) 99458318Seric { 99558318Seric b = strchr(b, a[0]); 99658318Seric if (b == NULL) 99758318Seric return FALSE; 99858318Seric if (strncmp(a, b, l) == 0) 99958318Seric return TRUE; 100058318Seric b++; 100158318Seric } 100258318Seric } 1003