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*58796Seric static char sccsid[] = "@(#)util.c 6.14 (Berkeley) 03/23/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 int locktype; 51258689Seric extern bool lockfile(); 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; 51958689Seric (void) lockfile(fileno(fp), filename, locktype); 52056328Seric errno = 0; 52156328Seric } 5226890Seric return (fp); 5236890Seric } 5247124Seric /* 5257124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 5267124Seric ** 5277753Seric ** This routine always guarantees outputing a newline (or CRLF, 5287753Seric ** as appropriate) at the end of the string. 5297753Seric ** 5307124Seric ** Parameters: 5317124Seric ** l -- line to put. 5327124Seric ** fp -- file to put it onto. 53310172Seric ** m -- the mailer used to control output. 5347124Seric ** 5357124Seric ** Returns: 5367124Seric ** none 5377124Seric ** 5387124Seric ** Side Effects: 5397124Seric ** output of l to fp. 5407124Seric */ 5417124Seric 54210172Seric putline(l, fp, m) 5437753Seric register char *l; 5447124Seric FILE *fp; 54510172Seric MAILER *m; 5467124Seric { 5477124Seric register char *p; 54847157Sbostic register char svchar; 5497124Seric 55011275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 55152106Seric if (bitnset(M_7BITS, m->m_flags)) 55211275Seric { 55347157Sbostic for (p = l; svchar = *p; ++p) 55447157Sbostic if (svchar & 0200) 55547157Sbostic *p = svchar &~ 0200; 55611275Seric } 55711275Seric 5587753Seric do 5597124Seric { 5607753Seric /* find the end of the line */ 56156795Seric p = strchr(l, '\n'); 5627753Seric if (p == NULL) 5637753Seric p = &l[strlen(l)]; 5647124Seric 5657753Seric /* check for line overflow */ 56652106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 5677753Seric { 56852106Seric register char *q = &l[m->m_linelimit - 1]; 5697124Seric 5707753Seric svchar = *q; 5717753Seric *q = '\0'; 57210685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 57323105Seric (void) putc('.', fp); 5747753Seric fputs(l, fp); 57523105Seric (void) putc('!', fp); 57610326Seric fputs(m->m_eol, fp); 5777753Seric *q = svchar; 5787753Seric l = q; 5797753Seric } 5807124Seric 5817753Seric /* output last part */ 58210685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 58323105Seric (void) putc('.', fp); 58447157Sbostic for ( ; l < p; ++l) 58547157Sbostic (void) putc(*l, fp); 58610326Seric fputs(m->m_eol, fp); 5877753Seric if (*l == '\n') 58847157Sbostic ++l; 5897753Seric } while (l[0] != '\0'); 5907124Seric } 5917676Seric /* 5927676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 5937676Seric ** 5947676Seric ** Parameters: 5957676Seric ** f -- name of file to unlink. 5967676Seric ** 5977676Seric ** Returns: 5987676Seric ** none. 5997676Seric ** 6007676Seric ** Side Effects: 6017676Seric ** f is unlinked. 6027676Seric */ 6037676Seric 6047676Seric xunlink(f) 6057676Seric char *f; 6067676Seric { 6077676Seric register int i; 6087676Seric 6097676Seric # ifdef LOG 61058020Seric if (LogLevel > 98) 61158020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 61256795Seric # endif /* LOG */ 6137676Seric 6147676Seric i = unlink(f); 6157676Seric # ifdef LOG 61658020Seric if (i < 0 && LogLevel > 97) 6177942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 61856795Seric # endif /* LOG */ 6197676Seric } 6207685Seric /* 62158680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 62258680Seric ** 62358680Seric ** Parameters: 62458680Seric ** fp -- file pointer for the file to close 62558680Seric ** a, b -- miscellaneous crud to print for debugging 62658680Seric ** 62758680Seric ** Returns: 62858680Seric ** none. 62958680Seric ** 63058680Seric ** Side Effects: 63158680Seric ** fp is closed. 63258680Seric */ 63358680Seric 63458680Seric xfclose(fp, a, b) 63558680Seric FILE *fp; 63658680Seric char *a, *b; 63758680Seric { 638*58796Seric if (tTd(53, 99)) 63958680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 640*58796Seric if (fclose(fp) < 0 && tTd(53, 99)) 64158680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 64258680Seric } 64358680Seric /* 64414885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 6457685Seric ** 6467685Seric ** Parameters: 6477685Seric ** buf -- place to put the input line. 6487685Seric ** siz -- size of buf. 6497685Seric ** fp -- file to read from. 65057384Seric ** timeout -- the timeout before error occurs. 6517685Seric ** 6527685Seric ** Returns: 65315533Seric ** NULL on error (including timeout). This will also leave 65415533Seric ** buf containing a null string. 6557685Seric ** buf otherwise. 6567685Seric ** 6577685Seric ** Side Effects: 6587685Seric ** none. 6597685Seric */ 6607685Seric 66114885Seric static jmp_buf CtxReadTimeout; 6627685Seric 6637685Seric char * 66457384Seric sfgets(buf, siz, fp, timeout) 6657685Seric char *buf; 6667685Seric int siz; 6677685Seric FILE *fp; 66857384Seric time_t timeout; 6697685Seric { 6707942Seric register EVENT *ev = NULL; 6717685Seric register char *p; 67246928Sbostic static int readtimeout(); 6737685Seric 67414885Seric /* set the timeout */ 67557384Seric if (timeout != 0) 67614885Seric { 67714885Seric if (setjmp(CtxReadTimeout) != 0) 67814885Seric { 67936233Skarels # ifdef LOG 68036230Skarels syslog(LOG_NOTICE, 68136230Skarels "timeout waiting for input from %s\n", 68257642Seric CurHostName? CurHostName: "local"); 68336233Skarels # endif 68436230Skarels errno = 0; 68540964Sbostic usrerr("451 timeout waiting for input"); 68619037Seric buf[0] = '\0'; 68714885Seric return (NULL); 68814885Seric } 68957384Seric ev = setevent(timeout, readtimeout, 0); 69014885Seric } 69114885Seric 69214885Seric /* try to read */ 69315533Seric p = NULL; 69415533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 6957942Seric { 6967942Seric errno = 0; 6977942Seric p = fgets(buf, siz, fp); 69815533Seric if (errno == EINTR) 69915533Seric clearerr(fp); 70015533Seric } 70114885Seric 70214885Seric /* clear the event if it has not sprung */ 7037685Seric clrevent(ev); 70414885Seric 70514885Seric /* clean up the books and exit */ 7068055Seric LineNumber++; 70715533Seric if (p == NULL) 70816880Seric { 70915533Seric buf[0] = '\0'; 71016880Seric return (NULL); 71116880Seric } 71252106Seric if (!EightBit) 71352106Seric for (p = buf; *p != '\0'; p++) 71452106Seric *p &= ~0200; 71516880Seric return (buf); 7167685Seric } 7177685Seric 7187685Seric static 7197685Seric readtimeout() 7207685Seric { 72114885Seric longjmp(CtxReadTimeout, 1); 7227685Seric } 7237786Seric /* 7247786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 7257786Seric ** 7267786Seric ** Parameters: 7277786Seric ** buf -- place to put result. 7287786Seric ** n -- bytes available. 7297786Seric ** f -- file to read from. 7307786Seric ** 7317786Seric ** Returns: 73257135Seric ** input line(s) on success, NULL on error or EOF. 73357135Seric ** This will normally be buf -- unless the line is too 73457135Seric ** long, when it will be xalloc()ed. 7357786Seric ** 7367786Seric ** Side Effects: 7377786Seric ** buf gets lines from f, with continuation lines (lines 7387786Seric ** with leading white space) appended. CRLF's are mapped 7397786Seric ** into single newlines. Any trailing NL is stripped. 7407786Seric */ 7417786Seric 7427786Seric char * 7437786Seric fgetfolded(buf, n, f) 7447786Seric char *buf; 7457786Seric register int n; 7467786Seric FILE *f; 7477786Seric { 7487786Seric register char *p = buf; 74957135Seric char *bp = buf; 7507786Seric register int i; 7517786Seric 7527786Seric n--; 75317350Seric while ((i = getc(f)) != EOF) 7547786Seric { 75517350Seric if (i == '\r') 75617350Seric { 75717350Seric i = getc(f); 75817350Seric if (i != '\n') 75917350Seric { 76017350Seric if (i != EOF) 76123105Seric (void) ungetc(i, f); 76217350Seric i = '\r'; 76317350Seric } 76417350Seric } 76557135Seric if (--n <= 0) 76657135Seric { 76757135Seric /* allocate new space */ 76857135Seric char *nbp; 76957135Seric int nn; 77057135Seric 77157135Seric nn = (p - bp); 77257232Seric if (nn < MEMCHUNKSIZE) 77357135Seric nn *= 2; 77457135Seric else 77557232Seric nn += MEMCHUNKSIZE; 77657135Seric nbp = xalloc(nn); 77757135Seric bcopy(bp, nbp, p - bp); 77857135Seric p = &nbp[p - bp]; 77957135Seric if (bp != buf) 78057135Seric free(bp); 78157135Seric bp = nbp; 78257135Seric n = nn - (p - bp); 78357135Seric } 78457135Seric *p++ = i; 78517350Seric if (i == '\n') 78617350Seric { 78717350Seric LineNumber++; 78817350Seric i = getc(f); 78917350Seric if (i != EOF) 79023105Seric (void) ungetc(i, f); 79117350Seric if (i != ' ' && i != '\t') 79252647Seric break; 79317350Seric } 7947786Seric } 79557135Seric if (p == bp) 79652647Seric return (NULL); 79752647Seric *--p = '\0'; 79857135Seric return (bp); 7997786Seric } 8007860Seric /* 8017886Seric ** CURTIME -- return current time. 8027886Seric ** 8037886Seric ** Parameters: 8047886Seric ** none. 8057886Seric ** 8067886Seric ** Returns: 8077886Seric ** the current time. 8087886Seric ** 8097886Seric ** Side Effects: 8107886Seric ** none. 8117886Seric */ 8127886Seric 8137886Seric time_t 8147886Seric curtime() 8157886Seric { 8167886Seric auto time_t t; 8177886Seric 8187886Seric (void) time(&t); 8197886Seric return (t); 8207886Seric } 8218264Seric /* 8228264Seric ** ATOBOOL -- convert a string representation to boolean. 8238264Seric ** 8248264Seric ** Defaults to "TRUE" 8258264Seric ** 8268264Seric ** Parameters: 8278264Seric ** s -- string to convert. Takes "tTyY" as true, 8288264Seric ** others as false. 8298264Seric ** 8308264Seric ** Returns: 8318264Seric ** A boolean representation of the string. 8328264Seric ** 8338264Seric ** Side Effects: 8348264Seric ** none. 8358264Seric */ 8368264Seric 8378264Seric bool 8388264Seric atobool(s) 8398264Seric register char *s; 8408264Seric { 84156795Seric if (*s == '\0' || strchr("tTyY", *s) != NULL) 8428264Seric return (TRUE); 8438264Seric return (FALSE); 8448264Seric } 8459048Seric /* 8469048Seric ** ATOOCT -- convert a string representation to octal. 8479048Seric ** 8489048Seric ** Parameters: 8499048Seric ** s -- string to convert. 8509048Seric ** 8519048Seric ** Returns: 8529048Seric ** An integer representing the string interpreted as an 8539048Seric ** octal number. 8549048Seric ** 8559048Seric ** Side Effects: 8569048Seric ** none. 8579048Seric */ 8589048Seric 8599048Seric atooct(s) 8609048Seric register char *s; 8619048Seric { 8629048Seric register int i = 0; 8639048Seric 8649048Seric while (*s >= '0' && *s <= '7') 8659048Seric i = (i << 3) | (*s++ - '0'); 8669048Seric return (i); 8679048Seric } 8689376Seric /* 8699376Seric ** WAITFOR -- wait for a particular process id. 8709376Seric ** 8719376Seric ** Parameters: 8729376Seric ** pid -- process id to wait for. 8739376Seric ** 8749376Seric ** Returns: 8759376Seric ** status of pid. 8769376Seric ** -1 if pid never shows up. 8779376Seric ** 8789376Seric ** Side Effects: 8799376Seric ** none. 8809376Seric */ 8819376Seric 8829376Seric waitfor(pid) 8839376Seric int pid; 8849376Seric { 8859376Seric auto int st; 8869376Seric int i; 8879376Seric 8889376Seric do 8899376Seric { 8909376Seric errno = 0; 8919376Seric i = wait(&st); 8929376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 8939376Seric if (i < 0) 8949376Seric st = -1; 8959376Seric return (st); 8969376Seric } 8979376Seric /* 89810685Seric ** BITINTERSECT -- tell if two bitmaps intersect 89910685Seric ** 90010685Seric ** Parameters: 90110685Seric ** a, b -- the bitmaps in question 90210685Seric ** 90310685Seric ** Returns: 90410685Seric ** TRUE if they have a non-null intersection 90510685Seric ** FALSE otherwise 90610685Seric ** 90710685Seric ** Side Effects: 90810685Seric ** none. 90910685Seric */ 91010685Seric 91110685Seric bool 91210685Seric bitintersect(a, b) 91310685Seric BITMAP a; 91410685Seric BITMAP b; 91510685Seric { 91610685Seric int i; 91710685Seric 91810685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 91910685Seric if ((a[i] & b[i]) != 0) 92010685Seric return (TRUE); 92110685Seric return (FALSE); 92210685Seric } 92310685Seric /* 92410685Seric ** BITZEROP -- tell if a bitmap is all zero 92510685Seric ** 92610685Seric ** Parameters: 92710685Seric ** map -- the bit map to check 92810685Seric ** 92910685Seric ** Returns: 93010685Seric ** TRUE if map is all zero. 93110685Seric ** FALSE if there are any bits set in map. 93210685Seric ** 93310685Seric ** Side Effects: 93410685Seric ** none. 93510685Seric */ 93610685Seric 93710685Seric bool 93810685Seric bitzerop(map) 93910685Seric BITMAP map; 94010685Seric { 94110685Seric int i; 94210685Seric 94310685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 94410685Seric if (map[i] != 0) 94510685Seric return (FALSE); 94610685Seric return (TRUE); 94710685Seric } 94858247Seric /* 94958318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 95058318Seric ** 95158318Seric ** Parameters: 95258318Seric ** a -- possible substring. 95358318Seric ** b -- possible superstring. 95458318Seric ** 95558318Seric ** Returns: 95658318Seric ** TRUE if a is contained in b. 95758318Seric ** FALSE otherwise. 95858318Seric */ 95958318Seric 96058318Seric bool 96158318Seric strcontainedin(a, b) 96258318Seric register char *a; 96358318Seric register char *b; 96458318Seric { 96558318Seric int l; 96658318Seric 96758318Seric l = strlen(a); 96858318Seric for (;;) 96958318Seric { 97058318Seric b = strchr(b, a[0]); 97158318Seric if (b == NULL) 97258318Seric return FALSE; 97358318Seric if (strncmp(a, b, l) == 0) 97458318Seric return TRUE; 97558318Seric b++; 97658318Seric } 97758318Seric } 978