xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 3151)
1*3151Seric # include <stdio.h>
2298Seric # include <sysexits.h>
3298Seric # include "useful.h"
42900Seric # include <ctype.h>
5298Seric 
6*3151Seric static char	SccsId[] = "@(#)util.c	3.4	03/09/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.
16298Seric **
17298Seric **	Returns:
18298Seric **		none.
19298Seric **
20298Seric **	Side Effects:
21298Seric **		none.
22298Seric **
23298Seric **	Called By:
24298Seric **		deliver
25298Seric */
26298Seric 
27298Seric stripquotes(s)
28298Seric 	char *s;
29298Seric {
30298Seric 	register char *p;
31298Seric 	register char *q;
32298Seric 	register char c;
33298Seric 
34298Seric 	for (p = q = s; (c = *p++) != '\0'; )
35298Seric 	{
36298Seric 		if (c != '"')
37298Seric 			*q++ = c & 0177;
38298Seric 	}
39298Seric 	*q = '\0';
40298Seric }
41298Seric /*
422900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
432900Seric **
442900Seric **	Parameters:
452900Seric **		s -- the string to capitalize.
462900Seric **
472900Seric **	Returns:
482900Seric **		a pointer to a properly capitalized string.
492900Seric **
502900Seric **	Side Effects:
512900Seric **		none.
522900Seric */
532900Seric 
542900Seric char *
552900Seric capitalize(s)
562900Seric 	register char *s;
572900Seric {
582900Seric 	static char buf[50];
592900Seric 	register char *p;
602900Seric 
612900Seric 	p = buf;
622900Seric 
632900Seric 	for (;;)
642900Seric 	{
652900Seric 		while (!isalpha(*s) && *s != '\0')
662900Seric 			*p++ = *s++;
672900Seric 		if (*s == '\0')
682900Seric 			break;
692900Seric 		*p++ = toupper(*s++);
702900Seric 		while (isalpha(*s))
712900Seric 			*p++ = *s++;
722900Seric 	}
732900Seric 
742900Seric 	*p = '\0';
752900Seric 	return (buf);
762900Seric }
772900Seric /*
78298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
79298Seric **
80298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
81298Seric **	error -- but after all, what can we do?
82298Seric **
83298Seric **	Parameters:
84298Seric **		sz -- size of area to allocate.
85298Seric **
86298Seric **	Returns:
87298Seric **		pointer to data region.
88298Seric **
89298Seric **	Side Effects:
90298Seric **		Memory is allocated.
91298Seric */
92298Seric 
93298Seric char *
94298Seric xalloc(sz)
95298Seric 	register unsigned int sz;
96298Seric {
97298Seric 	register char *p;
98298Seric 	extern char *malloc();
99298Seric 
100298Seric 	p = malloc(sz);
101298Seric 	if (p == NULL)
102298Seric 	{
103298Seric 		syserr("Out of memory!!");
1041598Seric 		exit(EX_UNAVAILABLE);
105298Seric 	}
106298Seric 	return (p);
107298Seric }
108298Seric /*
1092900Seric **  NEWSTR -- make copy of string.
1102900Seric **
1112900Seric **	Space is allocated for it using xalloc.
1122900Seric **
1132900Seric **	Parameters:
1142900Seric **		string to copy.
1152900Seric **
1162900Seric **	Returns:
1172900Seric **		pointer to new string.
1182900Seric **
1192900Seric **	Side Effects:
1202900Seric **		none.
1212900Seric */
1222900Seric 
1232900Seric char *
1242900Seric newstr(s)
1252900Seric 	register char *s;
1262900Seric {
1272900Seric 	register char *p;
1282992Seric 	extern char *strcpy();
1292900Seric 
1302992Seric 	p = xalloc((unsigned) (strlen(s) + 1));
1312900Seric 	strcpy(p, s);
1322900Seric 	return (p);
1332900Seric }
134*3151Seric /*
135*3151Seric **  COPYPLIST -- copy list of pointers.
136*3151Seric **
137*3151Seric **	This routine is the equivalent of newstr for lists of
138*3151Seric **	pointers.
139*3151Seric **
140*3151Seric **	Parameters:
141*3151Seric **		list -- list of pointers to copy.
142*3151Seric **			Must be NULL terminated.
143*3151Seric **		copycont -- if TRUE, copy the contents of the vector
144*3151Seric **			(which must be a string) also.
145*3151Seric **
146*3151Seric **	Returns:
147*3151Seric **		a copy of 'list'.
148*3151Seric **
149*3151Seric **	Side Effects:
150*3151Seric **		none.
151*3151Seric */
152*3151Seric 
153*3151Seric char **
154*3151Seric copyplist(list, copycont)
155*3151Seric 	char **list;
156*3151Seric 	bool copycont;
157*3151Seric {
158*3151Seric 	register char **vp;
159*3151Seric 	register char **newvp;
160*3151Seric 	extern char *xalloc();
161*3151Seric 
162*3151Seric 	for (vp = list; *vp != NULL; vp++)
163*3151Seric 		continue;
164*3151Seric 
165*3151Seric 	vp++;
166*3151Seric 
167*3151Seric 	newvp = (char **) xalloc((vp - list) * sizeof *vp);
168*3151Seric 	bmove(list, newvp, (vp - list) * sizeof *vp);
169*3151Seric 
170*3151Seric 	if (copycont)
171*3151Seric 	{
172*3151Seric 		for (vp = newvp; *vp != NULL; vp++)
173*3151Seric 			*vp = newstr(*vp);
174*3151Seric 	}
175*3151Seric 
176*3151Seric 	return (newvp);
177*3151Seric }
178*3151Seric /*
179*3151Seric **  PRINTAV -- print argument vector.
180*3151Seric **
181*3151Seric **	Parameters:
182*3151Seric **		av -- argument vector.
183*3151Seric **
184*3151Seric **	Returns:
185*3151Seric **		none.
186*3151Seric **
187*3151Seric **	Side Effects:
188*3151Seric **		prints av.
189*3151Seric */
190*3151Seric 
191*3151Seric # ifdef DEBUG
192*3151Seric printav(av)
193*3151Seric 	register char **av;
194*3151Seric {
195*3151Seric 	while (*av != NULL)
196*3151Seric 	{
197*3151Seric 		printf("\t%08x=", *av);
198*3151Seric 		xputs(*av++);
199*3151Seric 		putchar('\n');
200*3151Seric 	}
201*3151Seric }
202*3151Seric # endif DEBUG
203*3151Seric /*
204*3151Seric **  LOWER -- turn letter into lower case.
205*3151Seric **
206*3151Seric **	Parameters:
207*3151Seric **		c -- character to turn into lower case.
208*3151Seric **
209*3151Seric **	Returns:
210*3151Seric **		c, in lower case.
211*3151Seric **
212*3151Seric **	Side Effects:
213*3151Seric **		none.
214*3151Seric */
215*3151Seric 
216*3151Seric char
217*3151Seric lower(c)
218*3151Seric 	register char c;
219*3151Seric {
220*3151Seric 	if (isascii(c) && isupper(c))
221*3151Seric 		c = c - 'A' + 'a';
222*3151Seric 	return (c);
223*3151Seric }
224*3151Seric /*
225*3151Seric **  XPUTS -- put string doing control escapes.
226*3151Seric **
227*3151Seric **	Parameters:
228*3151Seric **		s -- string to put.
229*3151Seric **
230*3151Seric **	Returns:
231*3151Seric **		none.
232*3151Seric **
233*3151Seric **	Side Effects:
234*3151Seric **		output to stdout
235*3151Seric */
236*3151Seric 
237*3151Seric # ifdef DEBUG
238*3151Seric xputs(s)
239*3151Seric 	register char *s;
240*3151Seric {
241*3151Seric 	register char c;
242*3151Seric 
243*3151Seric 	while ((c = *s++) != '\0')
244*3151Seric 	{
245*3151Seric 		if (!isascii(c))
246*3151Seric 		{
247*3151Seric 			putchar('\\');
248*3151Seric 			c &= 0177;
249*3151Seric 		}
250*3151Seric 		if (iscntrl(c))
251*3151Seric 		{
252*3151Seric 			putchar('^');
253*3151Seric 			c |= 0100;
254*3151Seric 		}
255*3151Seric 		putchar(c);
256*3151Seric 	}
257*3151Seric 	fflush(stdout);
258*3151Seric }
259*3151Seric # endif DEBUG
260*3151Seric /*
261*3151Seric **  MAKELOWER -- Translate a line into lower case
262*3151Seric **
263*3151Seric **	Parameters:
264*3151Seric **		p -- the string to translate.  If NULL, return is
265*3151Seric **			immediate.
266*3151Seric **
267*3151Seric **	Returns:
268*3151Seric **		none.
269*3151Seric **
270*3151Seric **	Side Effects:
271*3151Seric **		String pointed to by p is translated to lower case.
272*3151Seric **
273*3151Seric **	Called By:
274*3151Seric **		parse
275*3151Seric */
276*3151Seric 
277*3151Seric makelower(p)
278*3151Seric 	register char *p;
279*3151Seric {
280*3151Seric 	register char c;
281*3151Seric 
282*3151Seric 	if (p == NULL)
283*3151Seric 		return;
284*3151Seric 	for (; (c = *p) != '\0'; p++)
285*3151Seric 		if (isascii(c) && isupper(c))
286*3151Seric 			*p = c - 'A' + 'a';
287*3151Seric }
288