xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 4435)
1 # include <stdio.h>
2 # include <sysexits.h>
3 # include "useful.h"
4 # include <ctype.h>
5 # include "conf.h"
6 
7 static char	SccsId[] = "@(#)util.c	3.8.1.1	09/23/81";
8 
9 /*
10 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
11 **
12 **	Runs through a string and strips off unquoted quote
13 **	characters and quote bits.  This is done in place.
14 **
15 **	Parameters:
16 **		s -- the string to strip.
17 **		qf -- if set, remove actual `` " '' characters
18 **			as well as the quote bits.
19 **
20 **	Returns:
21 **		none.
22 **
23 **	Side Effects:
24 **		none.
25 **
26 **	Called By:
27 **		deliver
28 */
29 
30 stripquotes(s, qf)
31 	char *s;
32 	bool qf;
33 {
34 	register char *p;
35 	register char *q;
36 	register char c;
37 
38 	if (s == NULL)
39 		return;
40 
41 	for (p = q = s; (c = *p++) != '\0'; )
42 	{
43 		if (c != '"' || !qf)
44 			*q++ = c & 0177;
45 	}
46 	*q = '\0';
47 }
48 /*
49 **  CAPITALIZE -- return a copy of a string, properly capitalized.
50 **
51 **	Parameters:
52 **		s -- the string to capitalize.
53 **
54 **	Returns:
55 **		a pointer to a properly capitalized string.
56 **
57 **	Side Effects:
58 **		none.
59 */
60 
61 char *
62 capitalize(s)
63 	register char *s;
64 {
65 	static char buf[50];
66 	register char *p;
67 
68 	p = buf;
69 
70 	for (;;)
71 	{
72 		while (!isalpha(*s) && *s != '\0')
73 			*p++ = *s++;
74 		if (*s == '\0')
75 			break;
76 		*p++ = toupper(*s++);
77 		while (isalpha(*s))
78 			*p++ = *s++;
79 	}
80 
81 	*p = '\0';
82 	return (buf);
83 }
84 /*
85 **  XALLOC -- Allocate memory and bitch wildly on failure.
86 **
87 **	THIS IS A CLUDGE.  This should be made to give a proper
88 **	error -- but after all, what can we do?
89 **
90 **	Parameters:
91 **		sz -- size of area to allocate.
92 **
93 **	Returns:
94 **		pointer to data region.
95 **
96 **	Side Effects:
97 **		Memory is allocated.
98 */
99 
100 char *
101 xalloc(sz)
102 	register unsigned int sz;
103 {
104 	register char *p;
105 
106 	p = malloc(sz);
107 	if (p == NULL)
108 	{
109 		syserr("Out of memory!!");
110 		exit(EX_UNAVAILABLE);
111 	}
112 	return (p);
113 }
114 /*
115 **  NEWSTR -- make copy of string.
116 **
117 **	Space is allocated for it using xalloc.
118 **
119 **	Parameters:
120 **		string to copy.
121 **
122 **	Returns:
123 **		pointer to new string.
124 **
125 **	Side Effects:
126 **		none.
127 */
128 
129 char *
130 newstr(s)
131 	register char *s;
132 {
133 	register char *p;
134 
135 	p = xalloc((unsigned) (strlen(s) + 1));
136 	strcpy(p, s);
137 	return (p);
138 }
139 /*
140 **  COPYPLIST -- copy list of pointers.
141 **
142 **	This routine is the equivalent of newstr for lists of
143 **	pointers.
144 **
145 **	Parameters:
146 **		list -- list of pointers to copy.
147 **			Must be NULL terminated.
148 **		copycont -- if TRUE, copy the contents of the vector
149 **			(which must be a string) also.
150 **
151 **	Returns:
152 **		a copy of 'list'.
153 **
154 **	Side Effects:
155 **		none.
156 */
157 
158 char **
159 copyplist(list, copycont)
160 	char **list;
161 	bool copycont;
162 {
163 	register char **vp;
164 	register char **newvp;
165 
166 	for (vp = list; *vp != NULL; vp++)
167 		continue;
168 
169 	vp++;
170 
171 	newvp = (char **) xalloc((unsigned) (vp - list) * sizeof *vp);
172 	bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp);
173 
174 	if (copycont)
175 	{
176 		for (vp = newvp; *vp != NULL; vp++)
177 			*vp = newstr(*vp);
178 	}
179 
180 	return (newvp);
181 }
182 /*
183 **  PRINTAV -- print argument vector.
184 **
185 **	Parameters:
186 **		av -- argument vector.
187 **
188 **	Returns:
189 **		none.
190 **
191 **	Side Effects:
192 **		prints av.
193 */
194 
195 # ifdef DEBUG
196 printav(av)
197 	register char **av;
198 {
199 	while (*av != NULL)
200 	{
201 		printf("\t%08x=", *av);
202 		xputs(*av++);
203 		putchar('\n');
204 	}
205 }
206 # endif DEBUG
207 /*
208 **  LOWER -- turn letter into lower case.
209 **
210 **	Parameters:
211 **		c -- character to turn into lower case.
212 **
213 **	Returns:
214 **		c, in lower case.
215 **
216 **	Side Effects:
217 **		none.
218 */
219 
220 char
221 lower(c)
222 	register char c;
223 {
224 	if (isascii(c) && isupper(c))
225 		c = c - 'A' + 'a';
226 	return (c);
227 }
228 /*
229 **  XPUTS -- put string doing control escapes.
230 **
231 **	Parameters:
232 **		s -- string to put.
233 **
234 **	Returns:
235 **		none.
236 **
237 **	Side Effects:
238 **		output to stdout
239 */
240 
241 # ifdef DEBUG
242 xputs(s)
243 	register char *s;
244 {
245 	register char c;
246 
247 	while ((c = *s++) != '\0')
248 	{
249 		if (!isascii(c))
250 		{
251 			putchar('\\');
252 			c &= 0177;
253 		}
254 		if (iscntrl(c))
255 		{
256 			putchar('^');
257 			c |= 0100;
258 		}
259 		putchar(c);
260 	}
261 	(void) fflush(stdout);
262 }
263 # endif DEBUG
264 /*
265 **  MAKELOWER -- Translate a line into lower case
266 **
267 **	Parameters:
268 **		p -- the string to translate.  If NULL, return is
269 **			immediate.
270 **
271 **	Returns:
272 **		none.
273 **
274 **	Side Effects:
275 **		String pointed to by p is translated to lower case.
276 **
277 **	Called By:
278 **		parse
279 */
280 
281 makelower(p)
282 	register char *p;
283 {
284 	register char c;
285 
286 	if (p == NULL)
287 		return;
288 	for (; (c = *p) != '\0'; p++)
289 		if (isascii(c) && isupper(c))
290 			*p = c - 'A' + 'a';
291 }
292 /*
293 **  SAMEWORD -- return TRUE if the words are the same
294 **
295 **	Ignores case.
296 **
297 **	Parameters:
298 **		a, b -- the words to compare.
299 **
300 **	Returns:
301 **		TRUE if a & b match exactly (modulo case)
302 **		FALSE otherwise.
303 **
304 **	Side Effects:
305 **		none.
306 */
307 
308 bool
309 sameword(a, b)
310 	register char *a, *b;
311 {
312 	while (lower(*a) == lower(*b))
313 	{
314 		if (*a == '\0')
315 			return (TRUE);
316 		a++;
317 		b++;
318 	}
319 	return (FALSE);
320 }
321 /*
322 **  CLEAR -- clear a block of memory
323 **
324 **	Parameters:
325 **		p -- location to clear.
326 **		l -- number of bytes to clear.
327 **
328 **	Returns:
329 **		none.
330 **
331 **	Side Effects:
332 **		none.
333 */
334 
335 clear(p, l)
336 	register char *p;
337 	register int l;
338 {
339 	while (l-- > 0)
340 		*p++ = 0;
341 }
342 /*
343 **  BUILDFNAME -- build full name from gecos style entry.
344 **
345 **	This routine interprets the strange entry that would appear
346 **	in the GECOS field of the password file.
347 **
348 **	Parameters:
349 **		p -- name to build.
350 **		login -- the login name of this user (for &).
351 **		buf -- place to put the result.
352 **
353 **	Returns:
354 **		none.
355 **
356 **	Side Effects:
357 **		none.
358 */
359 
360 buildfname(p, login, buf)
361 	register char *p;
362 	char *login;
363 	char *buf;
364 {
365 	register char *bp = buf;
366 
367 	while (*p != '\0' && *p != ',' && *p != ';')
368 	{
369 		if (*p == '&')
370 		{
371 			(void) strcpy(bp, login);
372 			*bp = toupper(*bp);
373 			while (*bp != '\0')
374 				bp++;
375 			p++;
376 		}
377 		else
378 			*bp++ = *p++;
379 	}
380 	*bp = '\0';
381 }
382 /*
383 **  SYSLOG -- fake entry to fool lint
384 */
385 
386 # ifdef LOG
387 # ifdef lint
388 
389 /*VARARGS2*/
390 syslog(pri, fmt, args)
391 	int pri;
392 	char *fmt;
393 {
394 	pri = *fmt;
395 	args = pri;
396 	pri = args;
397 }
398 
399 # endif lint
400 # endif LOG
401