xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 2900)
1298Seric # include <sysexits.h>
2298Seric # include "useful.h"
3*2900Seric # include <ctype.h>
4298Seric 
5*2900Seric static char	SccsId[] = "@(#)util.c	3.1	03/04/81";
6409Seric 
7298Seric /*
8298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
9298Seric **
10298Seric **	Runs through a string and strips off unquoted quote
11298Seric **	characters and quote bits.  This is done in place.
12298Seric **
13298Seric **	Parameters:
14298Seric **		s -- the string to strip.
15298Seric **
16298Seric **	Returns:
17298Seric **		none.
18298Seric **
19298Seric **	Side Effects:
20298Seric **		none.
21298Seric **
22298Seric **	Called By:
23298Seric **		deliver
24298Seric */
25298Seric 
26298Seric stripquotes(s)
27298Seric 	char *s;
28298Seric {
29298Seric 	register char *p;
30298Seric 	register char *q;
31298Seric 	register char c;
32298Seric 
33298Seric 	for (p = q = s; (c = *p++) != '\0'; )
34298Seric 	{
35298Seric 		if (c != '"')
36298Seric 			*q++ = c & 0177;
37298Seric 	}
38298Seric 	*q = '\0';
39298Seric }
40298Seric /*
41*2900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
42*2900Seric **
43*2900Seric **	Parameters:
44*2900Seric **		s -- the string to capitalize.
45*2900Seric **
46*2900Seric **	Returns:
47*2900Seric **		a pointer to a properly capitalized string.
48*2900Seric **
49*2900Seric **	Side Effects:
50*2900Seric **		none.
51*2900Seric */
52*2900Seric 
53*2900Seric char *
54*2900Seric capitalize(s)
55*2900Seric 	register char *s;
56*2900Seric {
57*2900Seric 	static char buf[50];
58*2900Seric 	register char *p;
59*2900Seric 
60*2900Seric 	p = buf;
61*2900Seric 
62*2900Seric 	for (;;)
63*2900Seric 	{
64*2900Seric 		while (!isalpha(*s) && *s != '\0')
65*2900Seric 			*p++ = *s++;
66*2900Seric 		if (*s == '\0')
67*2900Seric 			break;
68*2900Seric 		*p++ = toupper(*s++);
69*2900Seric 		while (isalpha(*s))
70*2900Seric 			*p++ = *s++;
71*2900Seric 	}
72*2900Seric 
73*2900Seric 	*p = '\0';
74*2900Seric 	return (buf);
75*2900Seric }
76*2900Seric /*
77298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
78298Seric **
79298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
80298Seric **	error -- but after all, what can we do?
81298Seric **
82298Seric **	Parameters:
83298Seric **		sz -- size of area to allocate.
84298Seric **
85298Seric **	Returns:
86298Seric **		pointer to data region.
87298Seric **
88298Seric **	Side Effects:
89298Seric **		Memory is allocated.
90298Seric */
91298Seric 
92298Seric char *
93298Seric xalloc(sz)
94298Seric 	register unsigned int sz;
95298Seric {
96298Seric 	register char *p;
97298Seric 	extern char *malloc();
98298Seric 
99298Seric 	p = malloc(sz);
100298Seric 	if (p == NULL)
101298Seric 	{
102298Seric 		syserr("Out of memory!!");
1031598Seric 		exit(EX_UNAVAILABLE);
104298Seric 	}
105298Seric 	return (p);
106298Seric }
107298Seric /*
108*2900Seric **  NEWSTR -- make copy of string.
109*2900Seric **
110*2900Seric **	Space is allocated for it using xalloc.
111*2900Seric **
112*2900Seric **	Parameters:
113*2900Seric **		string to copy.
114*2900Seric **
115*2900Seric **	Returns:
116*2900Seric **		pointer to new string.
117*2900Seric **
118*2900Seric **	Side Effects:
119*2900Seric **		none.
120*2900Seric */
121*2900Seric 
122*2900Seric char *
123*2900Seric newstr(s)
124*2900Seric 	register char *s;
125*2900Seric {
126*2900Seric 	register char *p;
127*2900Seric 
128*2900Seric 	p = xalloc(strlen(s) + 1);
129*2900Seric 	strcpy(p, s);
130*2900Seric 	return (p);
131*2900Seric }
132*2900Seric /*
133298Seric **  ANY -- Return TRUE if the character exists in the string.
134298Seric **
135298Seric **	Parameters:
136298Seric **		c -- the character.
137298Seric **		s -- the string
138298Seric **			(sounds like an avant garde script)
139298Seric **
140298Seric **	Returns:
141298Seric **		TRUE -- if c could be found in s.
142298Seric **		FALSE -- otherwise.
143298Seric **
144298Seric **	Side Effects:
145298Seric **		none.
146298Seric **
147298Seric **	Called By:
148298Seric **		prescan
149298Seric */
150298Seric 
151298Seric any(c, s)
152298Seric 	register char c;
153298Seric 	register char *s;
154298Seric {
155298Seric 	register char c2;
156298Seric 
157298Seric 	while ((c2 = *s++) != '\0')
158298Seric 		if (c2 == c)
159298Seric 			return (TRUE);
160298Seric 	return (FALSE);
161298Seric }
162