xref: /original-bsd/usr.sbin/sendmail/src/util.c (revision 5fb3de76)
1 # include <stdio.h>
2 # include <sysexits.h>
3 # include "useful.h"
4 # include <ctype.h>
5 
6 static char	SccsId[] = "@(#)util.c	3.4	03/09/81";
7 
8 /*
9 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
10 **
11 **	Runs through a string and strips off unquoted quote
12 **	characters and quote bits.  This is done in place.
13 **
14 **	Parameters:
15 **		s -- the string to strip.
16 **
17 **	Returns:
18 **		none.
19 **
20 **	Side Effects:
21 **		none.
22 **
23 **	Called By:
24 **		deliver
25 */
26 
27 stripquotes(s)
28 	char *s;
29 {
30 	register char *p;
31 	register char *q;
32 	register char c;
33 
34 	for (p = q = s; (c = *p++) != '\0'; )
35 	{
36 		if (c != '"')
37 			*q++ = c & 0177;
38 	}
39 	*q = '\0';
40 }
41 /*
42 **  CAPITALIZE -- return a copy of a string, properly capitalized.
43 **
44 **	Parameters:
45 **		s -- the string to capitalize.
46 **
47 **	Returns:
48 **		a pointer to a properly capitalized string.
49 **
50 **	Side Effects:
51 **		none.
52 */
53 
54 char *
55 capitalize(s)
56 	register char *s;
57 {
58 	static char buf[50];
59 	register char *p;
60 
61 	p = buf;
62 
63 	for (;;)
64 	{
65 		while (!isalpha(*s) && *s != '\0')
66 			*p++ = *s++;
67 		if (*s == '\0')
68 			break;
69 		*p++ = toupper(*s++);
70 		while (isalpha(*s))
71 			*p++ = *s++;
72 	}
73 
74 	*p = '\0';
75 	return (buf);
76 }
77 /*
78 **  XALLOC -- Allocate memory and bitch wildly on failure.
79 **
80 **	THIS IS A CLUDGE.  This should be made to give a proper
81 **	error -- but after all, what can we do?
82 **
83 **	Parameters:
84 **		sz -- size of area to allocate.
85 **
86 **	Returns:
87 **		pointer to data region.
88 **
89 **	Side Effects:
90 **		Memory is allocated.
91 */
92 
93 char *
94 xalloc(sz)
95 	register unsigned int sz;
96 {
97 	register char *p;
98 	extern char *malloc();
99 
100 	p = malloc(sz);
101 	if (p == NULL)
102 	{
103 		syserr("Out of memory!!");
104 		exit(EX_UNAVAILABLE);
105 	}
106 	return (p);
107 }
108 /*
109 **  NEWSTR -- make copy of string.
110 **
111 **	Space is allocated for it using xalloc.
112 **
113 **	Parameters:
114 **		string to copy.
115 **
116 **	Returns:
117 **		pointer to new string.
118 **
119 **	Side Effects:
120 **		none.
121 */
122 
123 char *
124 newstr(s)
125 	register char *s;
126 {
127 	register char *p;
128 	extern char *strcpy();
129 
130 	p = xalloc((unsigned) (strlen(s) + 1));
131 	strcpy(p, s);
132 	return (p);
133 }
134 /*
135 **  COPYPLIST -- copy list of pointers.
136 **
137 **	This routine is the equivalent of newstr for lists of
138 **	pointers.
139 **
140 **	Parameters:
141 **		list -- list of pointers to copy.
142 **			Must be NULL terminated.
143 **		copycont -- if TRUE, copy the contents of the vector
144 **			(which must be a string) also.
145 **
146 **	Returns:
147 **		a copy of 'list'.
148 **
149 **	Side Effects:
150 **		none.
151 */
152 
153 char **
154 copyplist(list, copycont)
155 	char **list;
156 	bool copycont;
157 {
158 	register char **vp;
159 	register char **newvp;
160 	extern char *xalloc();
161 
162 	for (vp = list; *vp != NULL; vp++)
163 		continue;
164 
165 	vp++;
166 
167 	newvp = (char **) xalloc((vp - list) * sizeof *vp);
168 	bmove(list, newvp, (vp - list) * sizeof *vp);
169 
170 	if (copycont)
171 	{
172 		for (vp = newvp; *vp != NULL; vp++)
173 			*vp = newstr(*vp);
174 	}
175 
176 	return (newvp);
177 }
178 /*
179 **  PRINTAV -- print argument vector.
180 **
181 **	Parameters:
182 **		av -- argument vector.
183 **
184 **	Returns:
185 **		none.
186 **
187 **	Side Effects:
188 **		prints av.
189 */
190 
191 # ifdef DEBUG
192 printav(av)
193 	register char **av;
194 {
195 	while (*av != NULL)
196 	{
197 		printf("\t%08x=", *av);
198 		xputs(*av++);
199 		putchar('\n');
200 	}
201 }
202 # endif DEBUG
203 /*
204 **  LOWER -- turn letter into lower case.
205 **
206 **	Parameters:
207 **		c -- character to turn into lower case.
208 **
209 **	Returns:
210 **		c, in lower case.
211 **
212 **	Side Effects:
213 **		none.
214 */
215 
216 char
217 lower(c)
218 	register char c;
219 {
220 	if (isascii(c) && isupper(c))
221 		c = c - 'A' + 'a';
222 	return (c);
223 }
224 /*
225 **  XPUTS -- put string doing control escapes.
226 **
227 **	Parameters:
228 **		s -- string to put.
229 **
230 **	Returns:
231 **		none.
232 **
233 **	Side Effects:
234 **		output to stdout
235 */
236 
237 # ifdef DEBUG
238 xputs(s)
239 	register char *s;
240 {
241 	register char c;
242 
243 	while ((c = *s++) != '\0')
244 	{
245 		if (!isascii(c))
246 		{
247 			putchar('\\');
248 			c &= 0177;
249 		}
250 		if (iscntrl(c))
251 		{
252 			putchar('^');
253 			c |= 0100;
254 		}
255 		putchar(c);
256 	}
257 	fflush(stdout);
258 }
259 # endif DEBUG
260 /*
261 **  MAKELOWER -- Translate a line into lower case
262 **
263 **	Parameters:
264 **		p -- the string to translate.  If NULL, return is
265 **			immediate.
266 **
267 **	Returns:
268 **		none.
269 **
270 **	Side Effects:
271 **		String pointed to by p is translated to lower case.
272 **
273 **	Called By:
274 **		parse
275 */
276 
277 makelower(p)
278 	register char *p;
279 {
280 	register char c;
281 
282 	if (p == NULL)
283 		return;
284 	for (; (c = *p) != '\0'; p++)
285 		if (isascii(c) && isupper(c))
286 			*p = c - 'A' + 'a';
287 }
288