add dfopen as a "determined fopen" -- it retries if it gets recoverable
[unix-history] / usr / src / usr.sbin / sendmail / src / util.c
# include <stdio.h>
# include <pwd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sysexits.h>
# include <errno.h>
# include <ctype.h>
# include "sendmail.h"
# include "conf.h"
SCCSID(@(#)util.c 3.15 %G%);
/*
** STRIPQUOTES -- Strip quotes & quote bits from a string.
**
** Runs through a string and strips off unquoted quote
** characters and quote bits. This is done in place.
**
** Parameters:
** s -- the string to strip.
** qf -- if set, remove actual `` " '' characters
** as well as the quote bits.
**
** Returns:
** none.
**
** Side Effects:
** none.
**
** Called By:
** deliver
*/
stripquotes(s, qf)
char *s;
bool qf;
{
register char *p;
register char *q;
register char c;
if (s == NULL)
return;
for (p = q = s; (c = *p++) != '\0'; )
{
if (c != '"' || !qf)
*q++ = c & 0177;
}
*q = '\0';
}
\f/*
** CAPITALIZE -- return a copy of a string, properly capitalized.
**
** Parameters:
** s -- the string to capitalize.
**
** Returns:
** a pointer to a properly capitalized string.
**
** Side Effects:
** none.
*/
char *
capitalize(s)
register char *s;
{
static char buf[50];
register char *p;
p = buf;
for (;;)
{
while (!isalpha(*s) && *s != '\0')
*p++ = *s++;
if (*s == '\0')
break;
*p++ = toupper(*s++);
while (isalpha(*s))
*p++ = *s++;
}
*p = '\0';
return (buf);
}
\f/*
** XALLOC -- Allocate memory and bitch wildly on failure.
**
** THIS IS A CLUDGE. This should be made to give a proper
** error -- but after all, what can we do?
**
** Parameters:
** sz -- size of area to allocate.
**
** Returns:
** pointer to data region.
**
** Side Effects:
** Memory is allocated.
*/
char *
xalloc(sz)
register unsigned int sz;
{
register char *p;
p = malloc(sz);
if (p == NULL)
{
syserr("Out of memory!!");
exit(EX_UNAVAILABLE);
}
return (p);
}
\f/*
** NEWSTR -- make copy of string.
**
** Space is allocated for it using xalloc.
**
** Parameters:
** string to copy.
**
** Returns:
** pointer to new string.
**
** Side Effects:
** none.
*/
char *
newstr(s)
register char *s;
{
register char *p;
p = xalloc((unsigned) (strlen(s) + 1));
strcpy(p, s);
return (p);
}
\f/*
** COPYPLIST -- copy list of pointers.
**
** This routine is the equivalent of newstr for lists of
** pointers.
**
** Parameters:
** list -- list of pointers to copy.
** Must be NULL terminated.
** copycont -- if TRUE, copy the contents of the vector
** (which must be a string) also.
**
** Returns:
** a copy of 'list'.
**
** Side Effects:
** none.
*/
char **
copyplist(list, copycont)
char **list;
bool copycont;
{
register char **vp;
register char **newvp;
for (vp = list; *vp != NULL; vp++)
continue;
vp++;
newvp = (char **) xalloc((unsigned) (vp - list) * sizeof *vp);
bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp);
if (copycont)
{
for (vp = newvp; *vp != NULL; vp++)
*vp = newstr(*vp);
}
return (newvp);
}
\f/*
** PRINTAV -- print argument vector.
**
** Parameters:
** av -- argument vector.
**
** Returns:
** none.
**
** Side Effects:
** prints av.
*/
# ifdef DEBUG
printav(av)
register char **av;
{
while (*av != NULL)
{
printf("\t%08x=", *av);
xputs(*av++);
putchar('\n');
}
}
# endif DEBUG
\f/*
** LOWER -- turn letter into lower case.
**
** Parameters:
** c -- character to turn into lower case.
**
** Returns:
** c, in lower case.
**
** Side Effects:
** none.
*/
char
lower(c)
register char c;
{
if (isascii(c) && isupper(c))
c = c - 'A' + 'a';
return (c);
}
\f/*
** XPUTS -- put string doing control escapes.
**
** Parameters:
** s -- string to put.
**
** Returns:
** none.
**
** Side Effects:
** output to stdout
*/
# ifdef DEBUG
xputs(s)
register char *s;
{
register char c;
while ((c = *s++) != '\0')
{
if (!isascii(c))
{
putchar('\\');
c &= 0177;
}
if (iscntrl(c))
{
putchar('^');
c |= 0100;
}
putchar(c);
}
(void) fflush(stdout);
}
# endif DEBUG
\f/*
** MAKELOWER -- Translate a line into lower case
**
** Parameters:
** p -- the string to translate. If NULL, return is
** immediate.
**
** Returns:
** none.
**
** Side Effects:
** String pointed to by p is translated to lower case.
**
** Called By:
** parse
*/
makelower(p)
register char *p;
{
register char c;
if (p == NULL)
return;
for (; (c = *p) != '\0'; p++)
if (isascii(c) && isupper(c))
*p = c - 'A' + 'a';
}
\f/*
** SAMEWORD -- return TRUE if the words are the same
**
** Ignores case.
**
** Parameters:
** a, b -- the words to compare.
**
** Returns:
** TRUE if a & b match exactly (modulo case)
** FALSE otherwise.
**
** Side Effects:
** none.
*/
bool
sameword(a, b)
register char *a, *b;
{
while (lower(*a) == lower(*b))
{
if (*a == '\0')
return (TRUE);
a++;
b++;
}
return (FALSE);
}
\f/*
** CLEAR -- clear a block of memory
**
** Parameters:
** p -- location to clear.
** l -- number of bytes to clear.
**
** Returns:
** none.
**
** Side Effects:
** none.
*/
clear(p, l)
register char *p;
register int l;
{
while (l-- > 0)
*p++ = 0;
}
\f/*
** FULLNAME -- extract full name from a passwd file entry.
**
** Parameters:
** pw -- password entry to start from.
** buf -- buffer to store result in.
**
** Returns:
** none.
**
** Side Effects:
** none.
*/
fullname(pw, buf)
register struct passwd *pw;
char *buf;
{
register char *bp = buf;
register char *p = pw->pw_gecos;
if (*p == '*')
p++;
while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
{
if (*p == '&')
{
(void) strcpy(bp, pw->pw_name);
*bp = toupper(*bp);
while (*bp != '\0')
bp++;
p++;
}
else
*bp++ = *p++;
}
*bp = '\0';
}
\f/*
** SAFEFILE -- return true if a file exists and is safe for a user.
**
** Parameters:
** fn -- filename to check.
** uid -- uid to compare against.
** mode -- mode bits that must match.
**
** Returns:
** TRUE if fn exists, is owned by uid, and matches mode.
** FALSE otherwise.
**
** Side Effects:
** none.
*/
bool
safefile(fn, uid, mode)
char *fn;
int uid;
int mode;
{
struct stat stbuf;
if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
(stbuf.st_mode & mode) == mode)
return (TRUE);
return (FALSE);
}
\f/*
** FIXCRLF -- fix <CR><LF> in line.
**
** Looks for the <CR><LF> combination and turns it into the
** UNIX canonical <NL> character. It only takes one line,
** i.e., it is assumed that the first <NL> found is the end
** of the line.
**
** Parameters:
** line -- the line to fix.
** stripnl -- if true, strip the newline also.
**
** Returns:
** none.
**
** Side Effects:
** line is changed in place.
*/
fixcrlf(line, stripnl)
char *line;
bool stripnl;
{
register char *p;
p = index(line, '\n');
if (p == NULL)
return;
if (p[-1] == '\r')
p--;
if (!stripnl)
*p++ = '\n';
*p = '\0';
}
\f/*
** SYSLOG -- fake entry to fool lint
*/
# ifdef LOG
# ifdef lint
/*VARARGS2*/
syslog(pri, fmt, args)
int pri;
char *fmt;
{
pri = *fmt;
args = pri;
pri = args;
}
# endif lint
# endif LOG
\f/*
** DFOPEN -- determined file open
**
** This routine has the semantics of fopen, except that it will
** keep trying a few times to make this happen. The idea is that
** on very loaded systems, we may run out of resources (inodes,
** whatever), so this tries to get around it.
*/
FILE *
dfopen(filename, mode)
char *filename;
char *mode;
{
register int tries;
register FILE *fp;
extern int errno;
for (tries = 0; tries < 10; tries++)
{
sleep(10 * tries);
errno = 0;
fp = fopen(filename, mode);
if (fp != NULL || errno != ENFILE)
break;
}
return (fp);
}