xref: /freebsd/usr.bin/mail/cmd2.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
310c3a8314SMike Heffner #if 0
329b50d902SRodney W. Grimes static char sccsid[] = "@(#)cmd2.c	8.1 (Berkeley) 6/6/93";
330c3a8314SMike Heffner #endif
349b50d902SRodney W. Grimes #endif /* not lint */
35e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
36e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include "rcv.h"
399b50d902SRodney W. Grimes #include <sys/wait.h>
409b50d902SRodney W. Grimes #include "extern.h"
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes /*
439b50d902SRodney W. Grimes  * Mail -- a mail program
449b50d902SRodney W. Grimes  *
459b50d902SRodney W. Grimes  * More user commands.
469b50d902SRodney W. Grimes  */
479b50d902SRodney W. Grimes 
489ce73e90SMike Heffner extern int wait_status;
499ce73e90SMike Heffner 
509b50d902SRodney W. Grimes /*
519b50d902SRodney W. Grimes  * If any arguments were given, go to the next applicable argument
529b50d902SRodney W. Grimes  * following dot, otherwise, go to the next applicable message.
539b50d902SRodney W. Grimes  * If given as first command with no arguments, print first message.
549b50d902SRodney W. Grimes  */
559b50d902SRodney W. Grimes int
56b948550dSPedro F. Giffuni next(void *v)
579b50d902SRodney W. Grimes {
589ce73e90SMike Heffner 	struct message *mp;
59b948550dSPedro F. Giffuni 	int *msgvec = v;
609ce73e90SMike Heffner 	int *ip, *ip2, list[2], mdot;
619b50d902SRodney W. Grimes 
62d030d2d2SPoul-Henning Kamp 	if (*msgvec != 0) {
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes 		/*
659b50d902SRodney W. Grimes 		 * If some messages were supplied, find the
669b50d902SRodney W. Grimes 		 * first applicable one following dot using
679b50d902SRodney W. Grimes 		 * wrap around.
689b50d902SRodney W. Grimes 		 */
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes 		mdot = dot - &message[0] + 1;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes 		/*
739b50d902SRodney W. Grimes 		 * Find the first message in the supplied
749b50d902SRodney W. Grimes 		 * message list which follows dot.
759b50d902SRodney W. Grimes 		 */
769b50d902SRodney W. Grimes 
77d030d2d2SPoul-Henning Kamp 		for (ip = msgvec; *ip != 0; ip++)
789b50d902SRodney W. Grimes 			if (*ip > mdot)
799b50d902SRodney W. Grimes 				break;
80d030d2d2SPoul-Henning Kamp 		if (*ip == 0)
819b50d902SRodney W. Grimes 			ip = msgvec;
829b50d902SRodney W. Grimes 		ip2 = ip;
839b50d902SRodney W. Grimes 		do {
849b50d902SRodney W. Grimes 			mp = &message[*ip2 - 1];
859b50d902SRodney W. Grimes 			if ((mp->m_flag & MDELETED) == 0) {
869b50d902SRodney W. Grimes 				dot = mp;
879b50d902SRodney W. Grimes 				goto hitit;
889b50d902SRodney W. Grimes 			}
89d030d2d2SPoul-Henning Kamp 			if (*ip2 != 0)
909b50d902SRodney W. Grimes 				ip2++;
91d030d2d2SPoul-Henning Kamp 			if (*ip2 == 0)
929b50d902SRodney W. Grimes 				ip2 = msgvec;
939b50d902SRodney W. Grimes 		} while (ip2 != ip);
949b50d902SRodney W. Grimes 		printf("No messages applicable\n");
959b50d902SRodney W. Grimes 		return (1);
969b50d902SRodney W. Grimes 	}
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes 	/*
999b50d902SRodney W. Grimes 	 * If this is the first command, select message 1.
1009b50d902SRodney W. Grimes 	 * Note that this must exist for us to get here at all.
1019b50d902SRodney W. Grimes 	 */
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	if (!sawcom)
1049b50d902SRodney W. Grimes 		goto hitit;
1059b50d902SRodney W. Grimes 
1069b50d902SRodney W. Grimes 	/*
1079b50d902SRodney W. Grimes 	 * Just find the next good message after dot, no
1089b50d902SRodney W. Grimes 	 * wraparound.
1099b50d902SRodney W. Grimes 	 */
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes 	for (mp = dot+1; mp < &message[msgCount]; mp++)
1129b50d902SRodney W. Grimes 		if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
1139b50d902SRodney W. Grimes 			break;
1149b50d902SRodney W. Grimes 	if (mp >= &message[msgCount]) {
1159b50d902SRodney W. Grimes 		printf("At EOF\n");
1169b50d902SRodney W. Grimes 		return (0);
1179b50d902SRodney W. Grimes 	}
1189b50d902SRodney W. Grimes 	dot = mp;
1199b50d902SRodney W. Grimes hitit:
1209b50d902SRodney W. Grimes 	/*
1219b50d902SRodney W. Grimes 	 * Print dot.
1229b50d902SRodney W. Grimes 	 */
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 	list[0] = dot - &message[0] + 1;
125d030d2d2SPoul-Henning Kamp 	list[1] = 0;
1269b50d902SRodney W. Grimes 	return (type(list));
1279b50d902SRodney W. Grimes }
1289b50d902SRodney W. Grimes 
1299b50d902SRodney W. Grimes /*
1309b50d902SRodney W. Grimes  * Save a message in a file.  Mark the message as saved
1319b50d902SRodney W. Grimes  * so we can discard when the user quits.
1329b50d902SRodney W. Grimes  */
1339b50d902SRodney W. Grimes int
134b22a8699SPedro F. Giffuni save(void *v)
1359b50d902SRodney W. Grimes {
136b22a8699SPedro F. Giffuni 	char *str = v;
1379b50d902SRodney W. Grimes 
1389ce73e90SMike Heffner 	return (save1(str, 1, "save", saveignore));
1399b50d902SRodney W. Grimes }
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes /*
1429b50d902SRodney W. Grimes  * Copy a message to a file without affected its saved-ness
1439b50d902SRodney W. Grimes  */
1449b50d902SRodney W. Grimes int
145b22a8699SPedro F. Giffuni copycmd(void *v)
1469b50d902SRodney W. Grimes {
147b22a8699SPedro F. Giffuni 	char *str = v;
1489b50d902SRodney W. Grimes 
1499ce73e90SMike Heffner 	return (save1(str, 0, "copy", saveignore));
1509b50d902SRodney W. Grimes }
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes /*
1539b50d902SRodney W. Grimes  * Save/copy the indicated messages at the end of the passed file name.
1549b50d902SRodney W. Grimes  * If mark is true, mark the message "saved."
1559b50d902SRodney W. Grimes  */
1569b50d902SRodney W. Grimes int
1576d8484b0SPhilippe Charnier save1(char str[], int mark, const char *cmd, struct ignoretab *ignore)
1589b50d902SRodney W. Grimes {
1599ce73e90SMike Heffner 	struct message *mp;
1609ce73e90SMike Heffner 	char *file;
1619ce73e90SMike Heffner 	const char *disp;
1629ce73e90SMike Heffner 	int f, *msgvec, *ip;
1639b50d902SRodney W. Grimes 	FILE *obuf;
1649b50d902SRodney W. Grimes 
1659ce73e90SMike Heffner 	msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec));
1669ce73e90SMike Heffner 	if ((file = snarf(str, &f)) == NULL)
1679b50d902SRodney W. Grimes 		return (1);
1689b50d902SRodney W. Grimes 	if (!f) {
1699b50d902SRodney W. Grimes 		*msgvec = first(0, MMNORM);
170d030d2d2SPoul-Henning Kamp 		if (*msgvec == 0) {
1719b50d902SRodney W. Grimes 			printf("No messages to %s.\n", cmd);
1729b50d902SRodney W. Grimes 			return (1);
1739b50d902SRodney W. Grimes 		}
174d030d2d2SPoul-Henning Kamp 		msgvec[1] = 0;
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes 	if (f && getmsglist(str, msgvec, 0) < 0)
1779b50d902SRodney W. Grimes 		return (1);
1789ce73e90SMike Heffner 	if ((file = expand(file)) == NULL)
1799b50d902SRodney W. Grimes 		return (1);
1809b50d902SRodney W. Grimes 	printf("\"%s\" ", file);
1819ce73e90SMike Heffner 	(void)fflush(stdout);
1829b50d902SRodney W. Grimes 	if (access(file, 0) >= 0)
1839b50d902SRodney W. Grimes 		disp = "[Appended]";
1849b50d902SRodney W. Grimes 	else
1859b50d902SRodney W. Grimes 		disp = "[New file]";
1869b50d902SRodney W. Grimes 	if ((obuf = Fopen(file, "a")) == NULL) {
1879ce73e90SMike Heffner 		warn((char *)NULL);
1889b50d902SRodney W. Grimes 		return (1);
1899b50d902SRodney W. Grimes 	}
1909b50d902SRodney W. Grimes 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
1919b50d902SRodney W. Grimes 		mp = &message[*ip - 1];
1929b50d902SRodney W. Grimes 		touch(mp);
1939ce73e90SMike Heffner 		if (sendmessage(mp, obuf, ignore, NULL) < 0) {
1940c3a8314SMike Heffner 			warnx("%s", file);
1959ce73e90SMike Heffner 			(void)Fclose(obuf);
1969b50d902SRodney W. Grimes 			return (1);
1979b50d902SRodney W. Grimes 		}
1989b50d902SRodney W. Grimes 		if (mark)
1999b50d902SRodney W. Grimes 			mp->m_flag |= MSAVED;
2009b50d902SRodney W. Grimes 	}
2019ce73e90SMike Heffner 	(void)fflush(obuf);
2029b50d902SRodney W. Grimes 	if (ferror(obuf))
2030c3a8314SMike Heffner 		warn("%s", file);
2049ce73e90SMike Heffner 	(void)Fclose(obuf);
2059b50d902SRodney W. Grimes 	printf("%s\n", disp);
2069b50d902SRodney W. Grimes 	return (0);
2079b50d902SRodney W. Grimes }
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes /*
2109b50d902SRodney W. Grimes  * Write the indicated messages at the end of the passed
2119b50d902SRodney W. Grimes  * file name, minus header and trailing blank line.
2129b50d902SRodney W. Grimes  */
2139b50d902SRodney W. Grimes int
214b948550dSPedro F. Giffuni swrite(void *v)
2159b50d902SRodney W. Grimes {
216b948550dSPedro F. Giffuni 	char *str = v;
2179b50d902SRodney W. Grimes 
2189ce73e90SMike Heffner 	return (save1(str, 1, "write", ignoreall));
2199b50d902SRodney W. Grimes }
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes /*
2229b50d902SRodney W. Grimes  * Snarf the file from the end of the command line and
2239b50d902SRodney W. Grimes  * return a pointer to it.  If there is no file attached,
2249ce73e90SMike Heffner  * just return NULL.  Put a null in front of the file
2259b50d902SRodney W. Grimes  * name so that the message list processing won't see it,
2269b50d902SRodney W. Grimes  * unless the file name is the only thing on the line, in
2279b50d902SRodney W. Grimes  * which case, return 0 in the reference flag variable.
2289b50d902SRodney W. Grimes  */
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes char *
231b948550dSPedro F. Giffuni snarf(char *linebuf, int *flag)
2329b50d902SRodney W. Grimes {
2339ce73e90SMike Heffner 	char *cp;
2349b50d902SRodney W. Grimes 
2359b50d902SRodney W. Grimes 	*flag = 1;
2369b50d902SRodney W. Grimes 	cp = strlen(linebuf) + linebuf - 1;
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes 	/*
2399b50d902SRodney W. Grimes 	 * Strip away trailing blanks.
2409b50d902SRodney W. Grimes 	 */
2419b50d902SRodney W. Grimes 
2426d48fa43SAndrey A. Chernov 	while (cp > linebuf && isspace((unsigned char)*cp))
2439b50d902SRodney W. Grimes 		cp--;
2449ce73e90SMike Heffner 	*++cp = '\0';
2459b50d902SRodney W. Grimes 
2469b50d902SRodney W. Grimes 	/*
2479b50d902SRodney W. Grimes 	 * Now search for the beginning of the file name.
2489b50d902SRodney W. Grimes 	 */
2499b50d902SRodney W. Grimes 
2506d48fa43SAndrey A. Chernov 	while (cp > linebuf && !isspace((unsigned char)*cp))
2519b50d902SRodney W. Grimes 		cp--;
2529b50d902SRodney W. Grimes 	if (*cp == '\0') {
2539b50d902SRodney W. Grimes 		printf("No file specified.\n");
2549ce73e90SMike Heffner 		return (NULL);
2559b50d902SRodney W. Grimes 	}
2566d48fa43SAndrey A. Chernov 	if (isspace((unsigned char)*cp))
2579ce73e90SMike Heffner 		*cp++ = '\0';
2589b50d902SRodney W. Grimes 	else
2599b50d902SRodney W. Grimes 		*flag = 0;
2609b50d902SRodney W. Grimes 	return (cp);
2619b50d902SRodney W. Grimes }
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes /*
2649b50d902SRodney W. Grimes  * Delete messages.
2659b50d902SRodney W. Grimes  */
2669b50d902SRodney W. Grimes int
267b948550dSPedro F. Giffuni deletecmd(void *v)
2689b50d902SRodney W. Grimes {
269b948550dSPedro F. Giffuni 	int *msgvec = v;
2709ce73e90SMike Heffner 
2719b50d902SRodney W. Grimes 	delm(msgvec);
2729ce73e90SMike Heffner 	return (0);
2739b50d902SRodney W. Grimes }
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes /*
2769b50d902SRodney W. Grimes  * Delete messages, then type the new dot.
2779b50d902SRodney W. Grimes  */
2789b50d902SRodney W. Grimes int
279b948550dSPedro F. Giffuni deltype(void *v)
2809b50d902SRodney W. Grimes {
281b948550dSPedro F. Giffuni 	int *msgvec = v;
2829b50d902SRodney W. Grimes 	int list[2];
2839b50d902SRodney W. Grimes 	int lastdot;
2849b50d902SRodney W. Grimes 
2859b50d902SRodney W. Grimes 	lastdot = dot - &message[0] + 1;
2869b50d902SRodney W. Grimes 	if (delm(msgvec) >= 0) {
2879b50d902SRodney W. Grimes 		list[0] = dot - &message[0] + 1;
2889b50d902SRodney W. Grimes 		if (list[0] > lastdot) {
2899b50d902SRodney W. Grimes 			touch(dot);
290d030d2d2SPoul-Henning Kamp 			list[1] = 0;
2919b50d902SRodney W. Grimes 			return (type(list));
2929b50d902SRodney W. Grimes 		}
2939b50d902SRodney W. Grimes 		printf("At EOF\n");
2949b50d902SRodney W. Grimes 	} else
2959b50d902SRodney W. Grimes 		printf("No more messages\n");
2969b50d902SRodney W. Grimes 	return (0);
2979b50d902SRodney W. Grimes }
2989b50d902SRodney W. Grimes 
2999b50d902SRodney W. Grimes /*
3009b50d902SRodney W. Grimes  * Delete the indicated messages.
3019b50d902SRodney W. Grimes  * Set dot to some nice place afterwards.
3029b50d902SRodney W. Grimes  * Internal interface.
3039b50d902SRodney W. Grimes  */
3049b50d902SRodney W. Grimes int
3056d8484b0SPhilippe Charnier delm(int *msgvec)
3069b50d902SRodney W. Grimes {
3079ce73e90SMike Heffner 	struct message *mp;
3089ce73e90SMike Heffner 	int *ip, last;
3099b50d902SRodney W. Grimes 
310d030d2d2SPoul-Henning Kamp 	last = 0;
311d030d2d2SPoul-Henning Kamp 	for (ip = msgvec; *ip != 0; ip++) {
3129b50d902SRodney W. Grimes 		mp = &message[*ip - 1];
3139b50d902SRodney W. Grimes 		touch(mp);
3149b50d902SRodney W. Grimes 		mp->m_flag |= MDELETED|MTOUCH;
3159b50d902SRodney W. Grimes 		mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
3169b50d902SRodney W. Grimes 		last = *ip;
3179b50d902SRodney W. Grimes 	}
318d030d2d2SPoul-Henning Kamp 	if (last != 0) {
3199b50d902SRodney W. Grimes 		dot = &message[last-1];
3209b50d902SRodney W. Grimes 		last = first(0, MDELETED);
321d030d2d2SPoul-Henning Kamp 		if (last != 0) {
3229b50d902SRodney W. Grimes 			dot = &message[last-1];
3239b50d902SRodney W. Grimes 			return (0);
3249b50d902SRodney W. Grimes 		}
3259b50d902SRodney W. Grimes 		else {
3269b50d902SRodney W. Grimes 			dot = &message[0];
3279b50d902SRodney W. Grimes 			return (-1);
3289b50d902SRodney W. Grimes 		}
3299b50d902SRodney W. Grimes 	}
3309b50d902SRodney W. Grimes 
3319b50d902SRodney W. Grimes 	/*
3329b50d902SRodney W. Grimes 	 * Following can't happen -- it keeps lint happy
3339b50d902SRodney W. Grimes 	 */
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes 	return (-1);
3369b50d902SRodney W. Grimes }
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes /*
3399b50d902SRodney W. Grimes  * Undelete the indicated messages.
3409b50d902SRodney W. Grimes  */
3419b50d902SRodney W. Grimes int
342b948550dSPedro F. Giffuni undeletecmd(void *v)
3439b50d902SRodney W. Grimes {
344b948550dSPedro F. Giffuni 	int *msgvec = v;
3459ce73e90SMike Heffner 	int *ip;
346b948550dSPedro F. Giffuni 	struct message *mp;
3479b50d902SRodney W. Grimes 
3489b50d902SRodney W. Grimes 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
3499b50d902SRodney W. Grimes 		mp = &message[*ip - 1];
3509b50d902SRodney W. Grimes 		touch(mp);
3519b50d902SRodney W. Grimes 		dot = mp;
3529b50d902SRodney W. Grimes 		mp->m_flag &= ~MDELETED;
3539b50d902SRodney W. Grimes 	}
3549ce73e90SMike Heffner 	return (0);
3559b50d902SRodney W. Grimes }
3569b50d902SRodney W. Grimes 
3579b50d902SRodney W. Grimes /*
3589b50d902SRodney W. Grimes  * Interactively dump core on "core"
3599b50d902SRodney W. Grimes  */
3609b50d902SRodney W. Grimes int
3616d8484b0SPhilippe Charnier core(void)
3629b50d902SRodney W. Grimes {
3639b50d902SRodney W. Grimes 	int pid;
3649b50d902SRodney W. Grimes 
36546ccc3e9SBruce Evans 	switch (pid = fork()) {
3669b50d902SRodney W. Grimes 	case -1:
3670c3a8314SMike Heffner 		warn("fork");
3689b50d902SRodney W. Grimes 		return (1);
3699b50d902SRodney W. Grimes 	case 0:
3709b50d902SRodney W. Grimes 		abort();
3719b50d902SRodney W. Grimes 		_exit(1);
3729b50d902SRodney W. Grimes 	}
3739b50d902SRodney W. Grimes 	printf("Okie dokie");
3749ce73e90SMike Heffner 	(void)fflush(stdout);
3759b50d902SRodney W. Grimes 	wait_child(pid);
3760c3a8314SMike Heffner 	if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status))
3779b50d902SRodney W. Grimes 		printf(" -- Core dumped.\n");
3789b50d902SRodney W. Grimes 	else
3799b50d902SRodney W. Grimes 		printf(" -- Can't dump core.\n");
3809ce73e90SMike Heffner 	return (0);
3819b50d902SRodney W. Grimes }
3829b50d902SRodney W. Grimes 
3839b50d902SRodney W. Grimes /*
3849b50d902SRodney W. Grimes  * Clobber as many bytes of stack as the user requests.
3859b50d902SRodney W. Grimes  */
3869b50d902SRodney W. Grimes int
3876d8484b0SPhilippe Charnier clobber(char **argv)
3889b50d902SRodney W. Grimes {
3899ce73e90SMike Heffner 	int times;
3909b50d902SRodney W. Grimes 
3919b50d902SRodney W. Grimes 	if (argv[0] == 0)
3929b50d902SRodney W. Grimes 		times = 1;
3939b50d902SRodney W. Grimes 	else
3949b50d902SRodney W. Grimes 		times = (atoi(argv[0]) + 511) / 512;
3959b50d902SRodney W. Grimes 	clob1(times);
3969ce73e90SMike Heffner 	return (0);
3979b50d902SRodney W. Grimes }
3989b50d902SRodney W. Grimes 
3999b50d902SRodney W. Grimes /*
4009b50d902SRodney W. Grimes  * Clobber the stack.
4019b50d902SRodney W. Grimes  */
4029b50d902SRodney W. Grimes void
4036d8484b0SPhilippe Charnier clob1(int n)
4049b50d902SRodney W. Grimes {
4059b50d902SRodney W. Grimes 	char buf[512];
4069ce73e90SMike Heffner 	char *cp;
4079b50d902SRodney W. Grimes 
4089b50d902SRodney W. Grimes 	if (n <= 0)
4099b50d902SRodney W. Grimes 		return;
4109b50d902SRodney W. Grimes 	for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
4119b50d902SRodney W. Grimes 		;
4129b50d902SRodney W. Grimes 	clob1(n - 1);
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes /*
4169b50d902SRodney W. Grimes  * Add the given header fields to the retained list.
4179b50d902SRodney W. Grimes  * If no arguments, print the current list of retained fields.
4189b50d902SRodney W. Grimes  */
4199b50d902SRodney W. Grimes int
420b948550dSPedro F. Giffuni retfield(void *v)
4219b50d902SRodney W. Grimes {
422b948550dSPedro F. Giffuni 	char **list = v;
4239b50d902SRodney W. Grimes 
4249ce73e90SMike Heffner 	return (ignore1(list, ignore + 1, "retained"));
4259b50d902SRodney W. Grimes }
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes /*
4289b50d902SRodney W. Grimes  * Add the given header fields to the ignored list.
4299b50d902SRodney W. Grimes  * If no arguments, print the current list of ignored fields.
4309b50d902SRodney W. Grimes  */
4319b50d902SRodney W. Grimes int
432b948550dSPedro F. Giffuni igfield(void *v)
4339b50d902SRodney W. Grimes {
434b948550dSPedro F. Giffuni 	char **list = v;
4359b50d902SRodney W. Grimes 
4369ce73e90SMike Heffner 	return (ignore1(list, ignore, "ignored"));
4379b50d902SRodney W. Grimes }
4389b50d902SRodney W. Grimes 
4399b50d902SRodney W. Grimes int
440b948550dSPedro F. Giffuni saveretfield(void *v)
4419b50d902SRodney W. Grimes {
442b948550dSPedro F. Giffuni 	char **list = v;
4439b50d902SRodney W. Grimes 
4449ce73e90SMike Heffner 	return (ignore1(list, saveignore + 1, "retained"));
4459b50d902SRodney W. Grimes }
4469b50d902SRodney W. Grimes 
4479b50d902SRodney W. Grimes int
448b948550dSPedro F. Giffuni saveigfield(void *v)
4499b50d902SRodney W. Grimes {
450b948550dSPedro F. Giffuni 	char **list = v;
4519b50d902SRodney W. Grimes 
4529ce73e90SMike Heffner 	return (ignore1(list, saveignore, "ignored"));
4539b50d902SRodney W. Grimes }
4549b50d902SRodney W. Grimes 
4559b50d902SRodney W. Grimes int
456b948550dSPedro F. Giffuni ignore1(char **list, struct ignoretab *tab, const char *which)
4579b50d902SRodney W. Grimes {
4580c3a8314SMike Heffner 	char field[LINESIZE];
4599b50d902SRodney W. Grimes 	char **ap;
460b948550dSPedro F. Giffuni 	struct ignore *igp;
461b948550dSPedro F. Giffuni 	int h;
4629b50d902SRodney W. Grimes 
4639ce73e90SMike Heffner 	if (*list == NULL)
4649ce73e90SMike Heffner 		return (igshow(tab, which));
4659b50d902SRodney W. Grimes 	for (ap = list; *ap != 0; ap++) {
4660c3a8314SMike Heffner 		istrncpy(field, *ap, sizeof(field));
4679b50d902SRodney W. Grimes 		if (member(field, tab))
4689b50d902SRodney W. Grimes 			continue;
4699b50d902SRodney W. Grimes 		h = hash(field);
4709ce73e90SMike Heffner 		igp = calloc(1, sizeof(struct ignore));
4719b50d902SRodney W. Grimes 		igp->i_field = calloc((unsigned)strlen(field) + 1,
4729b50d902SRodney W. Grimes 		    sizeof(char));
4739b50d902SRodney W. Grimes 		strcpy(igp->i_field, field);
4749b50d902SRodney W. Grimes 		igp->i_link = tab->i_head[h];
4759b50d902SRodney W. Grimes 		tab->i_head[h] = igp;
4769b50d902SRodney W. Grimes 		tab->i_count++;
4779b50d902SRodney W. Grimes 	}
4789ce73e90SMike Heffner 	return (0);
4799b50d902SRodney W. Grimes }
4809b50d902SRodney W. Grimes 
4819b50d902SRodney W. Grimes /*
4829b50d902SRodney W. Grimes  * Print out all currently retained fields.
4839b50d902SRodney W. Grimes  */
4849b50d902SRodney W. Grimes int
4856d8484b0SPhilippe Charnier igshow(struct ignoretab *tab, const char *which)
4869b50d902SRodney W. Grimes {
4879ce73e90SMike Heffner 	int h;
4889b50d902SRodney W. Grimes 	struct ignore *igp;
4899b50d902SRodney W. Grimes 	char **ap, **ring;
4909b50d902SRodney W. Grimes 
4919b50d902SRodney W. Grimes 	if (tab->i_count == 0) {
4929b50d902SRodney W. Grimes 		printf("No fields currently being %s.\n", which);
4939ce73e90SMike Heffner 		return (0);
4949b50d902SRodney W. Grimes 	}
4959b50d902SRodney W. Grimes 	ring = (char **)salloc((tab->i_count + 1) * sizeof(char *));
4969b50d902SRodney W. Grimes 	ap = ring;
4979b50d902SRodney W. Grimes 	for (h = 0; h < HSHSIZE; h++)
4989ce73e90SMike Heffner 		for (igp = tab->i_head[h]; igp != NULL; igp = igp->i_link)
4999b50d902SRodney W. Grimes 			*ap++ = igp->i_field;
5009b50d902SRodney W. Grimes 	*ap = 0;
5019b50d902SRodney W. Grimes 	qsort(ring, tab->i_count, sizeof(char *), igcomp);
5029b50d902SRodney W. Grimes 	for (ap = ring; *ap != 0; ap++)
5039b50d902SRodney W. Grimes 		printf("%s\n", *ap);
5049ce73e90SMike Heffner 	return (0);
5059b50d902SRodney W. Grimes }
5069b50d902SRodney W. Grimes 
5079b50d902SRodney W. Grimes /*
5089b50d902SRodney W. Grimes  * Compare two names for sorting ignored field list.
5099b50d902SRodney W. Grimes  */
5109b50d902SRodney W. Grimes int
5116d8484b0SPhilippe Charnier igcomp(const void *l, const void *r)
5129b50d902SRodney W. Grimes {
5139ce73e90SMike Heffner 
5149ce73e90SMike Heffner 	return (strcmp(*(const char **)l, *(const char **)r));
5159b50d902SRodney W. Grimes }
516