xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 4375)
13151Seric # include <stdio.h>
2298Seric # include <sysexits.h>
3298Seric # include "useful.h"
42900Seric # include <ctype.h>
5298Seric 
6*4375Seric static char	SccsId[] = "@(#)util.c	3.8	09/12/81";
7409Seric 
8298Seric /*
9298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
10298Seric **
11298Seric **	Runs through a string and strips off unquoted quote
12298Seric **	characters and quote bits.  This is done in place.
13298Seric **
14298Seric **	Parameters:
15298Seric **		s -- the string to strip.
164101Seric **		qf -- if set, remove actual `` " '' characters
174101Seric **			as well as the quote bits.
18298Seric **
19298Seric **	Returns:
20298Seric **		none.
21298Seric **
22298Seric **	Side Effects:
23298Seric **		none.
24298Seric **
25298Seric **	Called By:
26298Seric **		deliver
27298Seric */
28298Seric 
294101Seric stripquotes(s, qf)
30298Seric 	char *s;
314101Seric 	bool qf;
32298Seric {
33298Seric 	register char *p;
34298Seric 	register char *q;
35298Seric 	register char c;
36298Seric 
374101Seric 	if (s == NULL)
384101Seric 		return;
394101Seric 
40298Seric 	for (p = q = s; (c = *p++) != '\0'; )
41298Seric 	{
424101Seric 		if (c != '"' || !qf)
43298Seric 			*q++ = c & 0177;
44298Seric 	}
45298Seric 	*q = '\0';
46298Seric }
47298Seric /*
482900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
492900Seric **
502900Seric **	Parameters:
512900Seric **		s -- the string to capitalize.
522900Seric **
532900Seric **	Returns:
542900Seric **		a pointer to a properly capitalized string.
552900Seric **
562900Seric **	Side Effects:
572900Seric **		none.
582900Seric */
592900Seric 
602900Seric char *
612900Seric capitalize(s)
622900Seric 	register char *s;
632900Seric {
642900Seric 	static char buf[50];
652900Seric 	register char *p;
662900Seric 
672900Seric 	p = buf;
682900Seric 
692900Seric 	for (;;)
702900Seric 	{
712900Seric 		while (!isalpha(*s) && *s != '\0')
722900Seric 			*p++ = *s++;
732900Seric 		if (*s == '\0')
742900Seric 			break;
752900Seric 		*p++ = toupper(*s++);
762900Seric 		while (isalpha(*s))
772900Seric 			*p++ = *s++;
782900Seric 	}
792900Seric 
802900Seric 	*p = '\0';
812900Seric 	return (buf);
822900Seric }
832900Seric /*
84298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
85298Seric **
86298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
87298Seric **	error -- but after all, what can we do?
88298Seric **
89298Seric **	Parameters:
90298Seric **		sz -- size of area to allocate.
91298Seric **
92298Seric **	Returns:
93298Seric **		pointer to data region.
94298Seric **
95298Seric **	Side Effects:
96298Seric **		Memory is allocated.
97298Seric */
98298Seric 
99298Seric char *
100298Seric xalloc(sz)
101298Seric 	register unsigned int sz;
102298Seric {
103298Seric 	register char *p;
104298Seric 
105298Seric 	p = malloc(sz);
106298Seric 	if (p == NULL)
107298Seric 	{
108298Seric 		syserr("Out of memory!!");
1091598Seric 		exit(EX_UNAVAILABLE);
110298Seric 	}
111298Seric 	return (p);
112298Seric }
113298Seric /*
1142900Seric **  NEWSTR -- make copy of string.
1152900Seric **
1162900Seric **	Space is allocated for it using xalloc.
1172900Seric **
1182900Seric **	Parameters:
1192900Seric **		string to copy.
1202900Seric **
1212900Seric **	Returns:
1222900Seric **		pointer to new string.
1232900Seric **
1242900Seric **	Side Effects:
1252900Seric **		none.
1262900Seric */
1272900Seric 
1282900Seric char *
1292900Seric newstr(s)
1302900Seric 	register char *s;
1312900Seric {
1322900Seric 	register char *p;
1332900Seric 
1342992Seric 	p = xalloc((unsigned) (strlen(s) + 1));
1352900Seric 	strcpy(p, s);
1362900Seric 	return (p);
1372900Seric }
1383151Seric /*
1393151Seric **  COPYPLIST -- copy list of pointers.
1403151Seric **
1413151Seric **	This routine is the equivalent of newstr for lists of
1423151Seric **	pointers.
1433151Seric **
1443151Seric **	Parameters:
1453151Seric **		list -- list of pointers to copy.
1463151Seric **			Must be NULL terminated.
1473151Seric **		copycont -- if TRUE, copy the contents of the vector
1483151Seric **			(which must be a string) also.
1493151Seric **
1503151Seric **	Returns:
1513151Seric **		a copy of 'list'.
1523151Seric **
1533151Seric **	Side Effects:
1543151Seric **		none.
1553151Seric */
1563151Seric 
1573151Seric char **
1583151Seric copyplist(list, copycont)
1593151Seric 	char **list;
1603151Seric 	bool copycont;
1613151Seric {
1623151Seric 	register char **vp;
1633151Seric 	register char **newvp;
1643151Seric 
1653151Seric 	for (vp = list; *vp != NULL; vp++)
1663151Seric 		continue;
1673151Seric 
1683151Seric 	vp++;
1693151Seric 
1704086Seric 	newvp = (char **) xalloc((unsigned) (vp - list) * sizeof *vp);
1714086Seric 	bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp);
1723151Seric 
1733151Seric 	if (copycont)
1743151Seric 	{
1753151Seric 		for (vp = newvp; *vp != NULL; vp++)
1763151Seric 			*vp = newstr(*vp);
1773151Seric 	}
1783151Seric 
1793151Seric 	return (newvp);
1803151Seric }
1813151Seric /*
1823151Seric **  PRINTAV -- print argument vector.
1833151Seric **
1843151Seric **	Parameters:
1853151Seric **		av -- argument vector.
1863151Seric **
1873151Seric **	Returns:
1883151Seric **		none.
1893151Seric **
1903151Seric **	Side Effects:
1913151Seric **		prints av.
1923151Seric */
1933151Seric 
1943151Seric # ifdef DEBUG
1953151Seric printav(av)
1963151Seric 	register char **av;
1973151Seric {
1983151Seric 	while (*av != NULL)
1993151Seric 	{
2003151Seric 		printf("\t%08x=", *av);
2013151Seric 		xputs(*av++);
2023151Seric 		putchar('\n');
2033151Seric 	}
2043151Seric }
2053151Seric # endif DEBUG
2063151Seric /*
2073151Seric **  LOWER -- turn letter into lower case.
2083151Seric **
2093151Seric **	Parameters:
2103151Seric **		c -- character to turn into lower case.
2113151Seric **
2123151Seric **	Returns:
2133151Seric **		c, in lower case.
2143151Seric **
2153151Seric **	Side Effects:
2163151Seric **		none.
2173151Seric */
2183151Seric 
2193151Seric char
2203151Seric lower(c)
2213151Seric 	register char c;
2223151Seric {
2233151Seric 	if (isascii(c) && isupper(c))
2243151Seric 		c = c - 'A' + 'a';
2253151Seric 	return (c);
2263151Seric }
2273151Seric /*
2283151Seric **  XPUTS -- put string doing control escapes.
2293151Seric **
2303151Seric **	Parameters:
2313151Seric **		s -- string to put.
2323151Seric **
2333151Seric **	Returns:
2343151Seric **		none.
2353151Seric **
2363151Seric **	Side Effects:
2373151Seric **		output to stdout
2383151Seric */
2393151Seric 
2403151Seric # ifdef DEBUG
2413151Seric xputs(s)
2423151Seric 	register char *s;
2433151Seric {
2443151Seric 	register char c;
2453151Seric 
2463151Seric 	while ((c = *s++) != '\0')
2473151Seric 	{
2483151Seric 		if (!isascii(c))
2493151Seric 		{
2503151Seric 			putchar('\\');
2513151Seric 			c &= 0177;
2523151Seric 		}
2533151Seric 		if (iscntrl(c))
2543151Seric 		{
2553151Seric 			putchar('^');
2563151Seric 			c |= 0100;
2573151Seric 		}
2583151Seric 		putchar(c);
2593151Seric 	}
2604086Seric 	(void) fflush(stdout);
2613151Seric }
2623151Seric # endif DEBUG
2633151Seric /*
2643151Seric **  MAKELOWER -- Translate a line into lower case
2653151Seric **
2663151Seric **	Parameters:
2673151Seric **		p -- the string to translate.  If NULL, return is
2683151Seric **			immediate.
2693151Seric **
2703151Seric **	Returns:
2713151Seric **		none.
2723151Seric **
2733151Seric **	Side Effects:
2743151Seric **		String pointed to by p is translated to lower case.
2753151Seric **
2763151Seric **	Called By:
2773151Seric **		parse
2783151Seric */
2793151Seric 
2803151Seric makelower(p)
2813151Seric 	register char *p;
2823151Seric {
2833151Seric 	register char c;
2843151Seric 
2853151Seric 	if (p == NULL)
2863151Seric 		return;
2873151Seric 	for (; (c = *p) != '\0'; p++)
2883151Seric 		if (isascii(c) && isupper(c))
2893151Seric 			*p = c - 'A' + 'a';
2903151Seric }
2914059Seric /*
2924059Seric **  SAMEWORD -- return TRUE if the words are the same
2934059Seric **
2944059Seric **	Ignores case.
2954059Seric **
2964059Seric **	Parameters:
2974059Seric **		a, b -- the words to compare.
2984059Seric **
2994059Seric **	Returns:
3004059Seric **		TRUE if a & b match exactly (modulo case)
3014059Seric **		FALSE otherwise.
3024059Seric **
3034059Seric **	Side Effects:
3044059Seric **		none.
3054059Seric */
3064059Seric 
3074059Seric bool
3084059Seric sameword(a, b)
3094059Seric 	register char *a, *b;
3104059Seric {
3114059Seric 	while (lower(*a) == lower(*b))
3124059Seric 	{
3134059Seric 		if (*a == '\0')
3144059Seric 			return (TRUE);
3154059Seric 		a++;
3164059Seric 		b++;
3174059Seric 	}
3184059Seric 	return (FALSE);
3194059Seric }
3204086Seric /*
3214101Seric **  CLEAR -- clear a block of memory
3224101Seric **
3234101Seric **	Parameters:
3244101Seric **		p -- location to clear.
3254101Seric **		l -- number of bytes to clear.
3264101Seric **
3274101Seric **	Returns:
3284101Seric **		none.
3294101Seric **
3304101Seric **	Side Effects:
3314101Seric **		none.
3324101Seric */
3334101Seric 
3344101Seric clear(p, l)
3354101Seric 	register char *p;
3364101Seric 	register int l;
3374101Seric {
3384101Seric 	while (l-- > 0)
3394101Seric 		*p++ = 0;
3404101Seric }
3414101Seric /*
342*4375Seric **  BUILDFNAME -- build full name from gecos style entry.
343*4375Seric **
344*4375Seric **	This routine interprets the strange entry that would appear
345*4375Seric **	in the GECOS field of the password file.
346*4375Seric **
347*4375Seric **	Parameters:
348*4375Seric **		p -- name to build.
349*4375Seric **		login -- the login name of this user (for &).
350*4375Seric **		buf -- place to put the result.
351*4375Seric **
352*4375Seric **	Returns:
353*4375Seric **		none.
354*4375Seric **
355*4375Seric **	Side Effects:
356*4375Seric **		none.
357*4375Seric */
358*4375Seric 
359*4375Seric buildfname(p, login, buf)
360*4375Seric 	register char *p;
361*4375Seric 	char *login;
362*4375Seric 	char *buf;
363*4375Seric {
364*4375Seric 	register char *bp = buf;
365*4375Seric 
366*4375Seric 	while (*p != '\0' && *p != ',' && *p != ';')
367*4375Seric 	{
368*4375Seric 		if (*p == '&')
369*4375Seric 		{
370*4375Seric 			(void) strcpy(bp, login);
371*4375Seric 			*bp = toupper(*bp);
372*4375Seric 			while (*bp != '\0')
373*4375Seric 				bp++;
374*4375Seric 			p++;
375*4375Seric 		}
376*4375Seric 		else
377*4375Seric 			*bp++ = *p++;
378*4375Seric 	}
379*4375Seric 	*bp = '\0';
380*4375Seric }
381*4375Seric /*
3824086Seric **  SYSLOG -- fake entry to fool lint
3834086Seric */
3844086Seric 
3854086Seric # ifdef LOG
3864086Seric # ifdef lint
3874086Seric 
3884086Seric /*VARARGS2*/
3894086Seric syslog(pri, fmt, args)
3904086Seric 	int pri;
3914086Seric 	char *fmt;
3924086Seric {
3934086Seric 	pri = *fmt;
3944086Seric 	args = pri;
3954086Seric 	pri = args;
3964086Seric }
3974086Seric 
3984086Seric # endif lint
3994086Seric # endif LOG
400