break some configuration into conf.h -- simplifies makefile changes
[unix-history] / usr / src / usr.sbin / sendmail / src / util.c
# include <stdio.h>
# include <sysexits.h>
# include "useful.h"
# include <ctype.h>
# include "conf.h"
static char SccsId[] = "@(#)util.c 3.8.1.1 %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/*
** BUILDFNAME -- build full name from gecos style entry.
**
** This routine interprets the strange entry that would appear
** in the GECOS field of the password file.
**
** Parameters:
** p -- name to build.
** login -- the login name of this user (for &).
** buf -- place to put the result.
**
** Returns:
** none.
**
** Side Effects:
** none.
*/
buildfname(p, login, buf)
register char *p;
char *login;
char *buf;
{
register char *bp = buf;
while (*p != '\0' && *p != ',' && *p != ';')
{
if (*p == '&')
{
(void) strcpy(bp, login);
*bp = toupper(*bp);
while (*bp != '\0')
bp++;
p++;
}
else
*bp++ = *p++;
}
*bp = '\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