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