122717Sdist /* 242833Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642833Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822717Sdist 922717Sdist #ifndef lint 10*58680Seric static char sccsid[] = "@(#)util.c 6.12 (Berkeley) 03/16/93"; 1133731Sbostic #endif /* not lint */ 1222717Sdist 1358332Seric # include "sendmail.h" 144538Seric # include <sys/stat.h> 15298Seric # include <sysexits.h> 1657135Seric /* 17298Seric ** STRIPQUOTES -- Strip quotes & quote bits from a string. 18298Seric ** 19298Seric ** Runs through a string and strips off unquoted quote 20298Seric ** characters and quote bits. This is done in place. 21298Seric ** 22298Seric ** Parameters: 23298Seric ** s -- the string to strip. 24298Seric ** 25298Seric ** Returns: 26298Seric ** none. 27298Seric ** 28298Seric ** Side Effects: 29298Seric ** none. 30298Seric ** 31298Seric ** Called By: 32298Seric ** deliver 33298Seric */ 34298Seric 3554983Seric stripquotes(s) 36298Seric char *s; 37298Seric { 38298Seric register char *p; 39298Seric register char *q; 40298Seric register char c; 41298Seric 424101Seric if (s == NULL) 434101Seric return; 444101Seric 4554983Seric p = q = s; 4654983Seric do 47298Seric { 4854983Seric c = *p++; 4954983Seric if (c == '\\') 5054983Seric c = *p++; 5154983Seric else if (c == '"') 5254983Seric continue; 5354983Seric *q++ = c; 5454983Seric } while (c != '\0'); 55298Seric } 56298Seric /* 572900Seric ** CAPITALIZE -- return a copy of a string, properly capitalized. 582900Seric ** 592900Seric ** Parameters: 602900Seric ** s -- the string to capitalize. 612900Seric ** 622900Seric ** Returns: 632900Seric ** a pointer to a properly capitalized string. 642900Seric ** 652900Seric ** Side Effects: 662900Seric ** none. 672900Seric */ 682900Seric 692900Seric char * 702900Seric capitalize(s) 712900Seric register char *s; 722900Seric { 732900Seric static char buf[50]; 742900Seric register char *p; 752900Seric 762900Seric p = buf; 772900Seric 782900Seric for (;;) 792900Seric { 8058050Seric while (!(isascii(*s) && isalpha(*s)) && *s != '\0') 812900Seric *p++ = *s++; 822900Seric if (*s == '\0') 832900Seric break; 8440999Sbostic *p++ = toupper(*s); 8540999Sbostic s++; 8658050Seric while (isascii(*s) && isalpha(*s)) 872900Seric *p++ = *s++; 882900Seric } 892900Seric 902900Seric *p = '\0'; 912900Seric return (buf); 922900Seric } 932900Seric /* 94298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 95298Seric ** 96298Seric ** THIS IS A CLUDGE. This should be made to give a proper 97298Seric ** error -- but after all, what can we do? 98298Seric ** 99298Seric ** Parameters: 100298Seric ** sz -- size of area to allocate. 101298Seric ** 102298Seric ** Returns: 103298Seric ** pointer to data region. 104298Seric ** 105298Seric ** Side Effects: 106298Seric ** Memory is allocated. 107298Seric */ 108298Seric 109298Seric char * 110298Seric xalloc(sz) 1117007Seric register int sz; 112298Seric { 113298Seric register char *p; 114298Seric 11523121Seric p = malloc((unsigned) sz); 116298Seric if (p == NULL) 117298Seric { 118298Seric syserr("Out of memory!!"); 11910685Seric abort(); 12010685Seric /* exit(EX_UNAVAILABLE); */ 121298Seric } 122298Seric return (p); 123298Seric } 124298Seric /* 1253151Seric ** COPYPLIST -- copy list of pointers. 1263151Seric ** 1273151Seric ** This routine is the equivalent of newstr for lists of 1283151Seric ** pointers. 1293151Seric ** 1303151Seric ** Parameters: 1313151Seric ** list -- list of pointers to copy. 1323151Seric ** Must be NULL terminated. 1333151Seric ** copycont -- if TRUE, copy the contents of the vector 1343151Seric ** (which must be a string) also. 1353151Seric ** 1363151Seric ** Returns: 1373151Seric ** a copy of 'list'. 1383151Seric ** 1393151Seric ** Side Effects: 1403151Seric ** none. 1413151Seric */ 1423151Seric 1433151Seric char ** 1443151Seric copyplist(list, copycont) 1453151Seric char **list; 1463151Seric bool copycont; 1473151Seric { 1483151Seric register char **vp; 1493151Seric register char **newvp; 1503151Seric 1513151Seric for (vp = list; *vp != NULL; vp++) 1523151Seric continue; 1533151Seric 1543151Seric vp++; 1553151Seric 15616897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 15716897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1583151Seric 1593151Seric if (copycont) 1603151Seric { 1613151Seric for (vp = newvp; *vp != NULL; vp++) 1623151Seric *vp = newstr(*vp); 1633151Seric } 1643151Seric 1653151Seric return (newvp); 1663151Seric } 1673151Seric /* 16858170Seric ** COPYQUEUE -- copy address queue. 16958170Seric ** 17058170Seric ** This routine is the equivalent of newstr for address queues 17158170Seric ** addresses marked with QDONTSEND aren't copied 17258170Seric ** 17358170Seric ** Parameters: 17458170Seric ** addr -- list of address structures to copy. 17558170Seric ** 17658170Seric ** Returns: 17758170Seric ** a copy of 'addr'. 17858170Seric ** 17958170Seric ** Side Effects: 18058170Seric ** none. 18158170Seric */ 18258170Seric 18358170Seric ADDRESS * 18458170Seric copyqueue(addr) 18558170Seric ADDRESS *addr; 18658170Seric { 18758170Seric register ADDRESS *newaddr; 18858170Seric ADDRESS *ret; 18958170Seric register ADDRESS **tail = &ret; 19058170Seric 19158170Seric while (addr != NULL) 19258170Seric { 19358170Seric if (!bitset(QDONTSEND, addr->q_flags)) 19458170Seric { 19558170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 19658170Seric STRUCTCOPY(*addr, *newaddr); 19758170Seric *tail = newaddr; 19858170Seric tail = &newaddr->q_next; 19958170Seric } 20058170Seric addr = addr->q_next; 20158170Seric } 20258170Seric *tail = NULL; 20358170Seric 20458170Seric return ret; 20558170Seric } 20658170Seric /* 2073151Seric ** PRINTAV -- print argument vector. 2083151Seric ** 2093151Seric ** Parameters: 2103151Seric ** av -- argument vector. 2113151Seric ** 2123151Seric ** Returns: 2133151Seric ** none. 2143151Seric ** 2153151Seric ** Side Effects: 2163151Seric ** prints av. 2173151Seric */ 2183151Seric 2193151Seric printav(av) 2203151Seric register char **av; 2213151Seric { 2223151Seric while (*av != NULL) 2233151Seric { 2248063Seric if (tTd(0, 44)) 2258063Seric printf("\n\t%08x=", *av); 2268063Seric else 22723105Seric (void) putchar(' '); 2283151Seric xputs(*av++); 2293151Seric } 23023105Seric (void) putchar('\n'); 2313151Seric } 2323151Seric /* 2333151Seric ** LOWER -- turn letter into lower case. 2343151Seric ** 2353151Seric ** Parameters: 2363151Seric ** c -- character to turn into lower case. 2373151Seric ** 2383151Seric ** Returns: 2393151Seric ** c, in lower case. 2403151Seric ** 2413151Seric ** Side Effects: 2423151Seric ** none. 2433151Seric */ 2443151Seric 2453151Seric char 2463151Seric lower(c) 2473151Seric register char c; 2483151Seric { 24958050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2503151Seric } 2513151Seric /* 2523151Seric ** XPUTS -- put string doing control escapes. 2533151Seric ** 2543151Seric ** Parameters: 2553151Seric ** s -- string to put. 2563151Seric ** 2573151Seric ** Returns: 2583151Seric ** none. 2593151Seric ** 2603151Seric ** Side Effects: 2613151Seric ** output to stdout 2623151Seric */ 2633151Seric 2643151Seric xputs(s) 2653151Seric register char *s; 2663151Seric { 26758050Seric register int c; 26851781Seric register struct metamac *mp; 26951781Seric extern struct metamac MetaMacros[]; 2703151Seric 2718055Seric if (s == NULL) 2728055Seric { 2738055Seric printf("<null>"); 2748055Seric return; 2758055Seric } 27658050Seric while ((c = (*s++ & 0377)) != '\0') 2773151Seric { 2783151Seric if (!isascii(c)) 2793151Seric { 28058050Seric if (c == MATCHREPL || c == MACROEXPAND) 28158050Seric { 28258050Seric putchar('$'); 28358050Seric continue; 28458050Seric } 28558050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 28658050Seric { 28758050Seric if ((mp->metaval & 0377) == c) 28858050Seric { 28958050Seric printf("$%c", mp->metaname); 29058050Seric break; 29158050Seric } 29258050Seric } 29358050Seric if (mp->metaname != '\0') 29458050Seric continue; 29523105Seric (void) putchar('\\'); 2963151Seric c &= 0177; 2973151Seric } 29857589Seric if (isprint(c)) 2993151Seric { 30057589Seric putchar(c); 30157589Seric continue; 30257589Seric } 30352050Seric 30457589Seric /* wasn't a meta-macro -- find another way to print it */ 30557589Seric switch (c) 30657589Seric { 30757589Seric case '\0': 30857589Seric continue; 30952050Seric 31057589Seric case '\n': 31157589Seric c = 'n'; 31257589Seric break; 31352050Seric 31457589Seric case '\r': 31557589Seric c = 'r'; 31657589Seric break; 31752637Seric 31857589Seric case '\t': 31957589Seric c = 't'; 32057589Seric break; 32157589Seric 32257589Seric default: 32357589Seric (void) putchar('^'); 32457589Seric (void) putchar(c ^ 0100); 32557589Seric continue; 3263151Seric } 3273151Seric } 3284086Seric (void) fflush(stdout); 3293151Seric } 3303151Seric /* 3313151Seric ** MAKELOWER -- Translate a line into lower case 3323151Seric ** 3333151Seric ** Parameters: 3343151Seric ** p -- the string to translate. If NULL, return is 3353151Seric ** immediate. 3363151Seric ** 3373151Seric ** Returns: 3383151Seric ** none. 3393151Seric ** 3403151Seric ** Side Effects: 3413151Seric ** String pointed to by p is translated to lower case. 3423151Seric ** 3433151Seric ** Called By: 3443151Seric ** parse 3453151Seric */ 3463151Seric 3473151Seric makelower(p) 3483151Seric register char *p; 3493151Seric { 3503151Seric register char c; 3513151Seric 3523151Seric if (p == NULL) 3533151Seric return; 3543151Seric for (; (c = *p) != '\0'; p++) 3553151Seric if (isascii(c) && isupper(c)) 35633724Sbostic *p = tolower(c); 3573151Seric } 3584059Seric /* 3595196Seric ** BUILDFNAME -- build full name from gecos style entry. 3604375Seric ** 3615196Seric ** This routine interprets the strange entry that would appear 3625196Seric ** in the GECOS field of the password file. 3635196Seric ** 3644375Seric ** Parameters: 3655196Seric ** p -- name to build. 3665196Seric ** login -- the login name of this user (for &). 3675196Seric ** buf -- place to put the result. 3684375Seric ** 3694375Seric ** Returns: 3704375Seric ** none. 3714375Seric ** 3724375Seric ** Side Effects: 3734375Seric ** none. 3744375Seric */ 3754375Seric 37654984Seric buildfname(gecos, login, buf) 37754984Seric register char *gecos; 3785196Seric char *login; 3794375Seric char *buf; 3804375Seric { 38154984Seric register char *p; 3824375Seric register char *bp = buf; 38354984Seric int l; 3844375Seric 38554984Seric if (*gecos == '*') 38654984Seric gecos++; 38754984Seric 38857123Seric /* find length of final string */ 38954984Seric l = 0; 39054984Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 39154984Seric { 39254984Seric if (*p == '&') 39354984Seric l += strlen(login); 39454984Seric else 39554984Seric l++; 39654984Seric } 39754984Seric 39854984Seric /* now fill in buf */ 39955193Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 4004375Seric { 4014375Seric if (*p == '&') 4024375Seric { 4035196Seric (void) strcpy(bp, login); 4044375Seric *bp = toupper(*bp); 4054375Seric while (*bp != '\0') 4064375Seric bp++; 4074375Seric } 4084375Seric else 40955193Seric *bp++ = *p; 4104375Seric } 4114375Seric *bp = '\0'; 4124375Seric } 4134375Seric /* 4144538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 4154538Seric ** 4164538Seric ** Parameters: 4174538Seric ** fn -- filename to check. 4184538Seric ** uid -- uid to compare against. 4194538Seric ** mode -- mode bits that must match. 4204538Seric ** 4214538Seric ** Returns: 42258247Seric ** 0 if fn exists, is owned by uid, and matches mode. 42358247Seric ** An errno otherwise. The actual errno is cleared. 4244538Seric ** 4254538Seric ** Side Effects: 4264538Seric ** none. 4274538Seric */ 4284538Seric 42958247Seric int 4304538Seric safefile(fn, uid, mode) 4314538Seric char *fn; 43255372Seric uid_t uid; 4334538Seric int mode; 4344538Seric { 4354538Seric struct stat stbuf; 4364538Seric 43758247Seric if (stat(fn, &stbuf) < 0) 43858247Seric { 43958247Seric int ret = errno; 44058247Seric 44158247Seric errno = 0; 44258247Seric return ret; 44358247Seric } 44458247Seric if (stbuf.st_uid == uid && (stbuf.st_mode & mode) == mode) 44558247Seric return 0; 44658247Seric return EPERM; 4474538Seric } 4484538Seric /* 4494557Seric ** FIXCRLF -- fix <CR><LF> in line. 4504557Seric ** 4514557Seric ** Looks for the <CR><LF> combination and turns it into the 4524557Seric ** UNIX canonical <NL> character. It only takes one line, 4534557Seric ** i.e., it is assumed that the first <NL> found is the end 4544557Seric ** of the line. 4554557Seric ** 4564557Seric ** Parameters: 4574557Seric ** line -- the line to fix. 4584557Seric ** stripnl -- if true, strip the newline also. 4594557Seric ** 4604557Seric ** Returns: 4614557Seric ** none. 4624557Seric ** 4634557Seric ** Side Effects: 4644557Seric ** line is changed in place. 4654557Seric */ 4664557Seric 4674557Seric fixcrlf(line, stripnl) 4684557Seric char *line; 4694557Seric bool stripnl; 4704557Seric { 4714557Seric register char *p; 4724557Seric 47356795Seric p = strchr(line, '\n'); 4744557Seric if (p == NULL) 4754557Seric return; 47636291Sbostic if (p > line && p[-1] == '\r') 4774557Seric p--; 4784557Seric if (!stripnl) 4794557Seric *p++ = '\n'; 4804557Seric *p = '\0'; 4814557Seric } 4824557Seric /* 4836890Seric ** DFOPEN -- determined file open 4846890Seric ** 4856890Seric ** This routine has the semantics of fopen, except that it will 4866890Seric ** keep trying a few times to make this happen. The idea is that 4876890Seric ** on very loaded systems, we may run out of resources (inodes, 4886890Seric ** whatever), so this tries to get around it. 4896890Seric */ 4906890Seric 4916890Seric FILE * 4926890Seric dfopen(filename, mode) 4936890Seric char *filename; 4946890Seric char *mode; 4956890Seric { 4966890Seric register int tries; 4976890Seric register FILE *fp; 4986890Seric 4996890Seric for (tries = 0; tries < 10; tries++) 5006890Seric { 50125618Seric sleep((unsigned) (10 * tries)); 5026890Seric errno = 0; 5036890Seric fp = fopen(filename, mode); 5049376Seric if (fp != NULL) 5056890Seric break; 5069376Seric if (errno != ENFILE && errno != EINTR) 5079376Seric break; 5086890Seric } 50956328Seric if (fp != NULL) 51056328Seric { 51156328Seric #ifdef FLOCK 51256328Seric int locktype; 51356328Seric 51456328Seric /* lock the file to avoid accidental conflicts */ 51556328Seric if (*mode == 'w' || *mode == 'a') 51656328Seric locktype = LOCK_EX; 51756328Seric else 51856328Seric locktype = LOCK_SH; 51956328Seric (void) flock(fileno(fp), locktype); 52056328Seric #endif 52156328Seric errno = 0; 52256328Seric } 5236890Seric return (fp); 5246890Seric } 5257124Seric /* 5267124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 5277124Seric ** 5287753Seric ** This routine always guarantees outputing a newline (or CRLF, 5297753Seric ** as appropriate) at the end of the string. 5307753Seric ** 5317124Seric ** Parameters: 5327124Seric ** l -- line to put. 5337124Seric ** fp -- file to put it onto. 53410172Seric ** m -- the mailer used to control output. 5357124Seric ** 5367124Seric ** Returns: 5377124Seric ** none 5387124Seric ** 5397124Seric ** Side Effects: 5407124Seric ** output of l to fp. 5417124Seric */ 5427124Seric 54310172Seric putline(l, fp, m) 5447753Seric register char *l; 5457124Seric FILE *fp; 54610172Seric MAILER *m; 5477124Seric { 5487124Seric register char *p; 54947157Sbostic register char svchar; 5507124Seric 55111275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 55252106Seric if (bitnset(M_7BITS, m->m_flags)) 55311275Seric { 55447157Sbostic for (p = l; svchar = *p; ++p) 55547157Sbostic if (svchar & 0200) 55647157Sbostic *p = svchar &~ 0200; 55711275Seric } 55811275Seric 5597753Seric do 5607124Seric { 5617753Seric /* find the end of the line */ 56256795Seric p = strchr(l, '\n'); 5637753Seric if (p == NULL) 5647753Seric p = &l[strlen(l)]; 5657124Seric 5667753Seric /* check for line overflow */ 56752106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 5687753Seric { 56952106Seric register char *q = &l[m->m_linelimit - 1]; 5707124Seric 5717753Seric svchar = *q; 5727753Seric *q = '\0'; 57310685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 57423105Seric (void) putc('.', fp); 5757753Seric fputs(l, fp); 57623105Seric (void) putc('!', fp); 57710326Seric fputs(m->m_eol, fp); 5787753Seric *q = svchar; 5797753Seric l = q; 5807753Seric } 5817124Seric 5827753Seric /* output last part */ 58310685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 58423105Seric (void) putc('.', fp); 58547157Sbostic for ( ; l < p; ++l) 58647157Sbostic (void) putc(*l, fp); 58710326Seric fputs(m->m_eol, fp); 5887753Seric if (*l == '\n') 58947157Sbostic ++l; 5907753Seric } while (l[0] != '\0'); 5917124Seric } 5927676Seric /* 5937676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 5947676Seric ** 5957676Seric ** Parameters: 5967676Seric ** f -- name of file to unlink. 5977676Seric ** 5987676Seric ** Returns: 5997676Seric ** none. 6007676Seric ** 6017676Seric ** Side Effects: 6027676Seric ** f is unlinked. 6037676Seric */ 6047676Seric 6057676Seric xunlink(f) 6067676Seric char *f; 6077676Seric { 6087676Seric register int i; 6097676Seric 6107676Seric # ifdef LOG 61158020Seric if (LogLevel > 98) 61258020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 61356795Seric # endif /* LOG */ 6147676Seric 6157676Seric i = unlink(f); 6167676Seric # ifdef LOG 61758020Seric if (i < 0 && LogLevel > 97) 6187942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 61956795Seric # endif /* LOG */ 6207676Seric } 6217685Seric /* 622*58680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 623*58680Seric ** 624*58680Seric ** Parameters: 625*58680Seric ** fp -- file pointer for the file to close 626*58680Seric ** a, b -- miscellaneous crud to print for debugging 627*58680Seric ** 628*58680Seric ** Returns: 629*58680Seric ** none. 630*58680Seric ** 631*58680Seric ** Side Effects: 632*58680Seric ** fp is closed. 633*58680Seric */ 634*58680Seric 635*58680Seric xfclose(fp, a, b) 636*58680Seric FILE *fp; 637*58680Seric char *a, *b; 638*58680Seric { 639*58680Seric if (tTd(9, 99)) 640*58680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 641*58680Seric if (fclose(fp) < 0 && tTd(9, 99)) 642*58680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 643*58680Seric } 644*58680Seric /* 64514885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 6467685Seric ** 6477685Seric ** Parameters: 6487685Seric ** buf -- place to put the input line. 6497685Seric ** siz -- size of buf. 6507685Seric ** fp -- file to read from. 65157384Seric ** timeout -- the timeout before error occurs. 6527685Seric ** 6537685Seric ** Returns: 65415533Seric ** NULL on error (including timeout). This will also leave 65515533Seric ** buf containing a null string. 6567685Seric ** buf otherwise. 6577685Seric ** 6587685Seric ** Side Effects: 6597685Seric ** none. 6607685Seric */ 6617685Seric 66214885Seric static jmp_buf CtxReadTimeout; 6637685Seric 6647685Seric char * 66557384Seric sfgets(buf, siz, fp, timeout) 6667685Seric char *buf; 6677685Seric int siz; 6687685Seric FILE *fp; 66957384Seric time_t timeout; 6707685Seric { 6717942Seric register EVENT *ev = NULL; 6727685Seric register char *p; 67346928Sbostic static int readtimeout(); 6747685Seric 67514885Seric /* set the timeout */ 67657384Seric if (timeout != 0) 67714885Seric { 67814885Seric if (setjmp(CtxReadTimeout) != 0) 67914885Seric { 68036233Skarels # ifdef LOG 68136230Skarels syslog(LOG_NOTICE, 68236230Skarels "timeout waiting for input from %s\n", 68357642Seric CurHostName? CurHostName: "local"); 68436233Skarels # endif 68536230Skarels errno = 0; 68640964Sbostic usrerr("451 timeout waiting for input"); 68719037Seric buf[0] = '\0'; 68814885Seric return (NULL); 68914885Seric } 69057384Seric ev = setevent(timeout, readtimeout, 0); 69114885Seric } 69214885Seric 69314885Seric /* try to read */ 69415533Seric p = NULL; 69515533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 6967942Seric { 6977942Seric errno = 0; 6987942Seric p = fgets(buf, siz, fp); 69915533Seric if (errno == EINTR) 70015533Seric clearerr(fp); 70115533Seric } 70214885Seric 70314885Seric /* clear the event if it has not sprung */ 7047685Seric clrevent(ev); 70514885Seric 70614885Seric /* clean up the books and exit */ 7078055Seric LineNumber++; 70815533Seric if (p == NULL) 70916880Seric { 71015533Seric buf[0] = '\0'; 71116880Seric return (NULL); 71216880Seric } 71352106Seric if (!EightBit) 71452106Seric for (p = buf; *p != '\0'; p++) 71552106Seric *p &= ~0200; 71616880Seric return (buf); 7177685Seric } 7187685Seric 7197685Seric static 7207685Seric readtimeout() 7217685Seric { 72214885Seric longjmp(CtxReadTimeout, 1); 7237685Seric } 7247786Seric /* 7257786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 7267786Seric ** 7277786Seric ** Parameters: 7287786Seric ** buf -- place to put result. 7297786Seric ** n -- bytes available. 7307786Seric ** f -- file to read from. 7317786Seric ** 7327786Seric ** Returns: 73357135Seric ** input line(s) on success, NULL on error or EOF. 73457135Seric ** This will normally be buf -- unless the line is too 73557135Seric ** long, when it will be xalloc()ed. 7367786Seric ** 7377786Seric ** Side Effects: 7387786Seric ** buf gets lines from f, with continuation lines (lines 7397786Seric ** with leading white space) appended. CRLF's are mapped 7407786Seric ** into single newlines. Any trailing NL is stripped. 7417786Seric */ 7427786Seric 7437786Seric char * 7447786Seric fgetfolded(buf, n, f) 7457786Seric char *buf; 7467786Seric register int n; 7477786Seric FILE *f; 7487786Seric { 7497786Seric register char *p = buf; 75057135Seric char *bp = buf; 7517786Seric register int i; 7527786Seric 7537786Seric n--; 75417350Seric while ((i = getc(f)) != EOF) 7557786Seric { 75617350Seric if (i == '\r') 75717350Seric { 75817350Seric i = getc(f); 75917350Seric if (i != '\n') 76017350Seric { 76117350Seric if (i != EOF) 76223105Seric (void) ungetc(i, f); 76317350Seric i = '\r'; 76417350Seric } 76517350Seric } 76657135Seric if (--n <= 0) 76757135Seric { 76857135Seric /* allocate new space */ 76957135Seric char *nbp; 77057135Seric int nn; 77157135Seric 77257135Seric nn = (p - bp); 77357232Seric if (nn < MEMCHUNKSIZE) 77457135Seric nn *= 2; 77557135Seric else 77657232Seric nn += MEMCHUNKSIZE; 77757135Seric nbp = xalloc(nn); 77857135Seric bcopy(bp, nbp, p - bp); 77957135Seric p = &nbp[p - bp]; 78057135Seric if (bp != buf) 78157135Seric free(bp); 78257135Seric bp = nbp; 78357135Seric n = nn - (p - bp); 78457135Seric } 78557135Seric *p++ = i; 78617350Seric if (i == '\n') 78717350Seric { 78817350Seric LineNumber++; 78917350Seric i = getc(f); 79017350Seric if (i != EOF) 79123105Seric (void) ungetc(i, f); 79217350Seric if (i != ' ' && i != '\t') 79352647Seric break; 79417350Seric } 7957786Seric } 79657135Seric if (p == bp) 79752647Seric return (NULL); 79852647Seric *--p = '\0'; 79957135Seric return (bp); 8007786Seric } 8017860Seric /* 8027886Seric ** CURTIME -- return current time. 8037886Seric ** 8047886Seric ** Parameters: 8057886Seric ** none. 8067886Seric ** 8077886Seric ** Returns: 8087886Seric ** the current time. 8097886Seric ** 8107886Seric ** Side Effects: 8117886Seric ** none. 8127886Seric */ 8137886Seric 8147886Seric time_t 8157886Seric curtime() 8167886Seric { 8177886Seric auto time_t t; 8187886Seric 8197886Seric (void) time(&t); 8207886Seric return (t); 8217886Seric } 8228264Seric /* 8238264Seric ** ATOBOOL -- convert a string representation to boolean. 8248264Seric ** 8258264Seric ** Defaults to "TRUE" 8268264Seric ** 8278264Seric ** Parameters: 8288264Seric ** s -- string to convert. Takes "tTyY" as true, 8298264Seric ** others as false. 8308264Seric ** 8318264Seric ** Returns: 8328264Seric ** A boolean representation of the string. 8338264Seric ** 8348264Seric ** Side Effects: 8358264Seric ** none. 8368264Seric */ 8378264Seric 8388264Seric bool 8398264Seric atobool(s) 8408264Seric register char *s; 8418264Seric { 84256795Seric if (*s == '\0' || strchr("tTyY", *s) != NULL) 8438264Seric return (TRUE); 8448264Seric return (FALSE); 8458264Seric } 8469048Seric /* 8479048Seric ** ATOOCT -- convert a string representation to octal. 8489048Seric ** 8499048Seric ** Parameters: 8509048Seric ** s -- string to convert. 8519048Seric ** 8529048Seric ** Returns: 8539048Seric ** An integer representing the string interpreted as an 8549048Seric ** octal number. 8559048Seric ** 8569048Seric ** Side Effects: 8579048Seric ** none. 8589048Seric */ 8599048Seric 8609048Seric atooct(s) 8619048Seric register char *s; 8629048Seric { 8639048Seric register int i = 0; 8649048Seric 8659048Seric while (*s >= '0' && *s <= '7') 8669048Seric i = (i << 3) | (*s++ - '0'); 8679048Seric return (i); 8689048Seric } 8699376Seric /* 8709376Seric ** WAITFOR -- wait for a particular process id. 8719376Seric ** 8729376Seric ** Parameters: 8739376Seric ** pid -- process id to wait for. 8749376Seric ** 8759376Seric ** Returns: 8769376Seric ** status of pid. 8779376Seric ** -1 if pid never shows up. 8789376Seric ** 8799376Seric ** Side Effects: 8809376Seric ** none. 8819376Seric */ 8829376Seric 8839376Seric waitfor(pid) 8849376Seric int pid; 8859376Seric { 8869376Seric auto int st; 8879376Seric int i; 8889376Seric 8899376Seric do 8909376Seric { 8919376Seric errno = 0; 8929376Seric i = wait(&st); 8939376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 8949376Seric if (i < 0) 8959376Seric st = -1; 8969376Seric return (st); 8979376Seric } 8989376Seric /* 89910685Seric ** BITINTERSECT -- tell if two bitmaps intersect 90010685Seric ** 90110685Seric ** Parameters: 90210685Seric ** a, b -- the bitmaps in question 90310685Seric ** 90410685Seric ** Returns: 90510685Seric ** TRUE if they have a non-null intersection 90610685Seric ** FALSE otherwise 90710685Seric ** 90810685Seric ** Side Effects: 90910685Seric ** none. 91010685Seric */ 91110685Seric 91210685Seric bool 91310685Seric bitintersect(a, b) 91410685Seric BITMAP a; 91510685Seric BITMAP b; 91610685Seric { 91710685Seric int i; 91810685Seric 91910685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 92010685Seric if ((a[i] & b[i]) != 0) 92110685Seric return (TRUE); 92210685Seric return (FALSE); 92310685Seric } 92410685Seric /* 92510685Seric ** BITZEROP -- tell if a bitmap is all zero 92610685Seric ** 92710685Seric ** Parameters: 92810685Seric ** map -- the bit map to check 92910685Seric ** 93010685Seric ** Returns: 93110685Seric ** TRUE if map is all zero. 93210685Seric ** FALSE if there are any bits set in map. 93310685Seric ** 93410685Seric ** Side Effects: 93510685Seric ** none. 93610685Seric */ 93710685Seric 93810685Seric bool 93910685Seric bitzerop(map) 94010685Seric BITMAP map; 94110685Seric { 94210685Seric int i; 94310685Seric 94410685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 94510685Seric if (map[i] != 0) 94610685Seric return (FALSE); 94710685Seric return (TRUE); 94810685Seric } 94958247Seric /* 95058318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 95158318Seric ** 95258318Seric ** Parameters: 95358318Seric ** a -- possible substring. 95458318Seric ** b -- possible superstring. 95558318Seric ** 95658318Seric ** Returns: 95758318Seric ** TRUE if a is contained in b. 95858318Seric ** FALSE otherwise. 95958318Seric */ 96058318Seric 96158318Seric bool 96258318Seric strcontainedin(a, b) 96358318Seric register char *a; 96458318Seric register char *b; 96558318Seric { 96658318Seric int l; 96758318Seric 96858318Seric l = strlen(a); 96958318Seric for (;;) 97058318Seric { 97158318Seric b = strchr(b, a[0]); 97258318Seric if (b == NULL) 97358318Seric return FALSE; 97458318Seric if (strncmp(a, b, l) == 0) 97558318Seric return TRUE; 97658318Seric b++; 97758318Seric } 97858318Seric } 979