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*59431Seric static char sccsid[] = "@(#)util.c 6.15 (Berkeley) 04/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 /* 562900Seric ** CAPITALIZE -- return a copy of a string, properly capitalized. 572900Seric ** 582900Seric ** Parameters: 592900Seric ** s -- the string to capitalize. 602900Seric ** 612900Seric ** Returns: 622900Seric ** a pointer to a properly capitalized string. 632900Seric ** 642900Seric ** Side Effects: 652900Seric ** none. 662900Seric */ 672900Seric 682900Seric char * 692900Seric capitalize(s) 702900Seric register char *s; 712900Seric { 722900Seric static char buf[50]; 732900Seric register char *p; 742900Seric 752900Seric p = buf; 762900Seric 772900Seric for (;;) 782900Seric { 7958050Seric while (!(isascii(*s) && isalpha(*s)) && *s != '\0') 802900Seric *p++ = *s++; 812900Seric if (*s == '\0') 822900Seric break; 8340999Sbostic *p++ = toupper(*s); 8440999Sbostic s++; 8558050Seric while (isascii(*s) && isalpha(*s)) 862900Seric *p++ = *s++; 872900Seric } 882900Seric 892900Seric *p = '\0'; 902900Seric return (buf); 912900Seric } 922900Seric /* 93298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 94298Seric ** 95298Seric ** THIS IS A CLUDGE. This should be made to give a proper 96298Seric ** error -- but after all, what can we do? 97298Seric ** 98298Seric ** Parameters: 99298Seric ** sz -- size of area to allocate. 100298Seric ** 101298Seric ** Returns: 102298Seric ** pointer to data region. 103298Seric ** 104298Seric ** Side Effects: 105298Seric ** Memory is allocated. 106298Seric */ 107298Seric 108298Seric char * 109298Seric xalloc(sz) 1107007Seric register int sz; 111298Seric { 112298Seric register char *p; 113298Seric 11423121Seric p = malloc((unsigned) sz); 115298Seric if (p == NULL) 116298Seric { 117298Seric syserr("Out of memory!!"); 11810685Seric abort(); 11910685Seric /* exit(EX_UNAVAILABLE); */ 120298Seric } 121298Seric return (p); 122298Seric } 123298Seric /* 1243151Seric ** COPYPLIST -- copy list of pointers. 1253151Seric ** 1263151Seric ** This routine is the equivalent of newstr for lists of 1273151Seric ** pointers. 1283151Seric ** 1293151Seric ** Parameters: 1303151Seric ** list -- list of pointers to copy. 1313151Seric ** Must be NULL terminated. 1323151Seric ** copycont -- if TRUE, copy the contents of the vector 1333151Seric ** (which must be a string) also. 1343151Seric ** 1353151Seric ** Returns: 1363151Seric ** a copy of 'list'. 1373151Seric ** 1383151Seric ** Side Effects: 1393151Seric ** none. 1403151Seric */ 1413151Seric 1423151Seric char ** 1433151Seric copyplist(list, copycont) 1443151Seric char **list; 1453151Seric bool copycont; 1463151Seric { 1473151Seric register char **vp; 1483151Seric register char **newvp; 1493151Seric 1503151Seric for (vp = list; *vp != NULL; vp++) 1513151Seric continue; 1523151Seric 1533151Seric vp++; 1543151Seric 15516897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 15616897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1573151Seric 1583151Seric if (copycont) 1593151Seric { 1603151Seric for (vp = newvp; *vp != NULL; vp++) 1613151Seric *vp = newstr(*vp); 1623151Seric } 1633151Seric 1643151Seric return (newvp); 1653151Seric } 1663151Seric /* 16758170Seric ** COPYQUEUE -- copy address queue. 16858170Seric ** 16958170Seric ** This routine is the equivalent of newstr for address queues 17058170Seric ** addresses marked with QDONTSEND aren't copied 17158170Seric ** 17258170Seric ** Parameters: 17358170Seric ** addr -- list of address structures to copy. 17458170Seric ** 17558170Seric ** Returns: 17658170Seric ** a copy of 'addr'. 17758170Seric ** 17858170Seric ** Side Effects: 17958170Seric ** none. 18058170Seric */ 18158170Seric 18258170Seric ADDRESS * 18358170Seric copyqueue(addr) 18458170Seric ADDRESS *addr; 18558170Seric { 18658170Seric register ADDRESS *newaddr; 18758170Seric ADDRESS *ret; 18858170Seric register ADDRESS **tail = &ret; 18958170Seric 19058170Seric while (addr != NULL) 19158170Seric { 19258170Seric if (!bitset(QDONTSEND, addr->q_flags)) 19358170Seric { 19458170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 19558170Seric STRUCTCOPY(*addr, *newaddr); 19658170Seric *tail = newaddr; 19758170Seric tail = &newaddr->q_next; 19858170Seric } 19958170Seric addr = addr->q_next; 20058170Seric } 20158170Seric *tail = NULL; 20258170Seric 20358170Seric return ret; 20458170Seric } 20558170Seric /* 2063151Seric ** PRINTAV -- print argument vector. 2073151Seric ** 2083151Seric ** Parameters: 2093151Seric ** av -- argument vector. 2103151Seric ** 2113151Seric ** Returns: 2123151Seric ** none. 2133151Seric ** 2143151Seric ** Side Effects: 2153151Seric ** prints av. 2163151Seric */ 2173151Seric 2183151Seric printav(av) 2193151Seric register char **av; 2203151Seric { 2213151Seric while (*av != NULL) 2223151Seric { 2238063Seric if (tTd(0, 44)) 2248063Seric printf("\n\t%08x=", *av); 2258063Seric else 22623105Seric (void) putchar(' '); 2273151Seric xputs(*av++); 2283151Seric } 22923105Seric (void) putchar('\n'); 2303151Seric } 2313151Seric /* 2323151Seric ** LOWER -- turn letter into lower case. 2333151Seric ** 2343151Seric ** Parameters: 2353151Seric ** c -- character to turn into lower case. 2363151Seric ** 2373151Seric ** Returns: 2383151Seric ** c, in lower case. 2393151Seric ** 2403151Seric ** Side Effects: 2413151Seric ** none. 2423151Seric */ 2433151Seric 2443151Seric char 2453151Seric lower(c) 2463151Seric register char c; 2473151Seric { 24858050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2493151Seric } 2503151Seric /* 2513151Seric ** XPUTS -- put string doing control escapes. 2523151Seric ** 2533151Seric ** Parameters: 2543151Seric ** s -- string to put. 2553151Seric ** 2563151Seric ** Returns: 2573151Seric ** none. 2583151Seric ** 2593151Seric ** Side Effects: 2603151Seric ** output to stdout 2613151Seric */ 2623151Seric 2633151Seric xputs(s) 2643151Seric register char *s; 2653151Seric { 26658050Seric register int c; 26751781Seric register struct metamac *mp; 26851781Seric extern struct metamac MetaMacros[]; 2693151Seric 2708055Seric if (s == NULL) 2718055Seric { 2728055Seric printf("<null>"); 2738055Seric return; 2748055Seric } 27558050Seric while ((c = (*s++ & 0377)) != '\0') 2763151Seric { 2773151Seric if (!isascii(c)) 2783151Seric { 27958050Seric if (c == MATCHREPL || c == MACROEXPAND) 28058050Seric { 28158050Seric putchar('$'); 28258050Seric continue; 28358050Seric } 28458050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 28558050Seric { 28658050Seric if ((mp->metaval & 0377) == c) 28758050Seric { 28858050Seric printf("$%c", mp->metaname); 28958050Seric break; 29058050Seric } 29158050Seric } 29258050Seric if (mp->metaname != '\0') 29358050Seric continue; 29423105Seric (void) putchar('\\'); 2953151Seric c &= 0177; 2963151Seric } 29757589Seric if (isprint(c)) 2983151Seric { 29957589Seric putchar(c); 30057589Seric continue; 30157589Seric } 30252050Seric 30357589Seric /* wasn't a meta-macro -- find another way to print it */ 30457589Seric switch (c) 30557589Seric { 30657589Seric case '\0': 30757589Seric continue; 30852050Seric 30957589Seric case '\n': 31057589Seric c = 'n'; 31157589Seric break; 31252050Seric 31357589Seric case '\r': 31457589Seric c = 'r'; 31557589Seric break; 31652637Seric 31757589Seric case '\t': 31857589Seric c = 't'; 31957589Seric break; 32057589Seric 32157589Seric default: 32257589Seric (void) putchar('^'); 32357589Seric (void) putchar(c ^ 0100); 32457589Seric continue; 3253151Seric } 3263151Seric } 3274086Seric (void) fflush(stdout); 3283151Seric } 3293151Seric /* 3303151Seric ** MAKELOWER -- Translate a line into lower case 3313151Seric ** 3323151Seric ** Parameters: 3333151Seric ** p -- the string to translate. If NULL, return is 3343151Seric ** immediate. 3353151Seric ** 3363151Seric ** Returns: 3373151Seric ** none. 3383151Seric ** 3393151Seric ** Side Effects: 3403151Seric ** String pointed to by p is translated to lower case. 3413151Seric ** 3423151Seric ** Called By: 3433151Seric ** parse 3443151Seric */ 3453151Seric 3463151Seric makelower(p) 3473151Seric register char *p; 3483151Seric { 3493151Seric register char c; 3503151Seric 3513151Seric if (p == NULL) 3523151Seric return; 3533151Seric for (; (c = *p) != '\0'; p++) 3543151Seric if (isascii(c) && isupper(c)) 35533724Sbostic *p = tolower(c); 3563151Seric } 3574059Seric /* 3585196Seric ** BUILDFNAME -- build full name from gecos style entry. 3594375Seric ** 3605196Seric ** This routine interprets the strange entry that would appear 3615196Seric ** in the GECOS field of the password file. 3625196Seric ** 3634375Seric ** Parameters: 3645196Seric ** p -- name to build. 3655196Seric ** login -- the login name of this user (for &). 3665196Seric ** buf -- place to put the result. 3674375Seric ** 3684375Seric ** Returns: 3694375Seric ** none. 3704375Seric ** 3714375Seric ** Side Effects: 3724375Seric ** none. 3734375Seric */ 3744375Seric 37554984Seric buildfname(gecos, login, buf) 37654984Seric register char *gecos; 3775196Seric char *login; 3784375Seric char *buf; 3794375Seric { 38054984Seric register char *p; 3814375Seric register char *bp = buf; 38254984Seric int l; 3834375Seric 38454984Seric if (*gecos == '*') 38554984Seric gecos++; 38654984Seric 38757123Seric /* find length of final string */ 38854984Seric l = 0; 38954984Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 39054984Seric { 39154984Seric if (*p == '&') 39254984Seric l += strlen(login); 39354984Seric else 39454984Seric l++; 39554984Seric } 39654984Seric 39754984Seric /* now fill in buf */ 39855193Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3994375Seric { 4004375Seric if (*p == '&') 4014375Seric { 4025196Seric (void) strcpy(bp, login); 4034375Seric *bp = toupper(*bp); 4044375Seric while (*bp != '\0') 4054375Seric bp++; 4064375Seric } 4074375Seric else 40855193Seric *bp++ = *p; 4094375Seric } 4104375Seric *bp = '\0'; 4114375Seric } 4124375Seric /* 4134538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 4144538Seric ** 4154538Seric ** Parameters: 4164538Seric ** fn -- filename to check. 4174538Seric ** uid -- uid to compare against. 4184538Seric ** mode -- mode bits that must match. 4194538Seric ** 4204538Seric ** Returns: 42158247Seric ** 0 if fn exists, is owned by uid, and matches mode. 42258247Seric ** An errno otherwise. The actual errno is cleared. 4234538Seric ** 4244538Seric ** Side Effects: 4254538Seric ** none. 4264538Seric */ 4274538Seric 42858247Seric int 4294538Seric safefile(fn, uid, mode) 4304538Seric char *fn; 43155372Seric uid_t uid; 4324538Seric int mode; 4334538Seric { 4344538Seric struct stat stbuf; 4354538Seric 43658247Seric if (stat(fn, &stbuf) < 0) 43758247Seric { 43858247Seric int ret = errno; 43958247Seric 44058247Seric errno = 0; 44158247Seric return ret; 44258247Seric } 44358247Seric if (stbuf.st_uid == uid && (stbuf.st_mode & mode) == mode) 44458247Seric return 0; 44558247Seric return EPERM; 4464538Seric } 4474538Seric /* 4484557Seric ** FIXCRLF -- fix <CR><LF> in line. 4494557Seric ** 4504557Seric ** Looks for the <CR><LF> combination and turns it into the 4514557Seric ** UNIX canonical <NL> character. It only takes one line, 4524557Seric ** i.e., it is assumed that the first <NL> found is the end 4534557Seric ** of the line. 4544557Seric ** 4554557Seric ** Parameters: 4564557Seric ** line -- the line to fix. 4574557Seric ** stripnl -- if true, strip the newline also. 4584557Seric ** 4594557Seric ** Returns: 4604557Seric ** none. 4614557Seric ** 4624557Seric ** Side Effects: 4634557Seric ** line is changed in place. 4644557Seric */ 4654557Seric 4664557Seric fixcrlf(line, stripnl) 4674557Seric char *line; 4684557Seric bool stripnl; 4694557Seric { 4704557Seric register char *p; 4714557Seric 47256795Seric p = strchr(line, '\n'); 4734557Seric if (p == NULL) 4744557Seric return; 47536291Sbostic if (p > line && p[-1] == '\r') 4764557Seric p--; 4774557Seric if (!stripnl) 4784557Seric *p++ = '\n'; 4794557Seric *p = '\0'; 4804557Seric } 4814557Seric /* 4826890Seric ** DFOPEN -- determined file open 4836890Seric ** 4846890Seric ** This routine has the semantics of fopen, except that it will 4856890Seric ** keep trying a few times to make this happen. The idea is that 4866890Seric ** on very loaded systems, we may run out of resources (inodes, 4876890Seric ** whatever), so this tries to get around it. 4886890Seric */ 4896890Seric 4906890Seric FILE * 4916890Seric dfopen(filename, mode) 4926890Seric char *filename; 4936890Seric char *mode; 4946890Seric { 4956890Seric register int tries; 4966890Seric register FILE *fp; 497*59431Seric struct stat st; 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 } 509*59431Seric if (fp != NULL && fstat(fileno(fp), &st) >= 0 && S_ISREG(st.st_mode)) 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 { 63858796Seric if (tTd(53, 99)) 63958680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 64058796Seric 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