xref: /titanic_52/usr/src/cmd/sdiff/sdiff.c (revision a85fbef1bf8ee04c3a4b2a244ce74b631fe41193)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 	/*
347c478bd9Sstevel@tonic-gate 	 *	sdiff [-l] [-s] [-w #] [-o output] file1 file2
357c478bd9Sstevel@tonic-gate 	 *	does side by side diff listing
367c478bd9Sstevel@tonic-gate 	 *	-l leftside only for identical lines
377c478bd9Sstevel@tonic-gate 	 *	-s silent; only print differences
387c478bd9Sstevel@tonic-gate 	 *	-w # width of output
397c478bd9Sstevel@tonic-gate 	 *	-o output  interactive creation of new output commands:
407c478bd9Sstevel@tonic-gate 	 *		s	silent; do not print identical lines
417c478bd9Sstevel@tonic-gate 	 *		v	turn off silent
427c478bd9Sstevel@tonic-gate 	 *		l	copy left side to output
437c478bd9Sstevel@tonic-gate 	 *		r	copy right side to output
447c478bd9Sstevel@tonic-gate 	 *		e l	call ed with left side
457c478bd9Sstevel@tonic-gate 	 *		e r	call ed with right side
467c478bd9Sstevel@tonic-gate 	 *		e b	call ed with cat of left and right
477c478bd9Sstevel@tonic-gate 	 *		e	call ed with empty file
487c478bd9Sstevel@tonic-gate 	 *		q	exit from program
497c478bd9Sstevel@tonic-gate 	 *
507c478bd9Sstevel@tonic-gate 	 *	functions:
517c478bd9Sstevel@tonic-gate 	 *	cmd	decode diff commands
527c478bd9Sstevel@tonic-gate 	 *	put1	output left side
537c478bd9Sstevel@tonic-gate 	 *	put2	output right side
547c478bd9Sstevel@tonic-gate 	 *	putmid	output gutter
557c478bd9Sstevel@tonic-gate 	 *	putline	output n chars to indicated file
567c478bd9Sstevel@tonic-gate 	 *	getlen	calculate length of strings with tabs
577c478bd9Sstevel@tonic-gate 	 *	cmdin	read and process interactive cmds
587c478bd9Sstevel@tonic-gate 	 *	cpp	copy from file to file
597c478bd9Sstevel@tonic-gate 	 *	edit	call ed with file
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <stdio.h>
637c478bd9Sstevel@tonic-gate #include <ctype.h>
647c478bd9Sstevel@tonic-gate #include <signal.h>
657c478bd9Sstevel@tonic-gate #include <sys/types.h>
667c478bd9Sstevel@tonic-gate #include <sys/stat.h>
677c478bd9Sstevel@tonic-gate #include <sys/wait.h>
687c478bd9Sstevel@tonic-gate #include <unistd.h>
697c478bd9Sstevel@tonic-gate #include <stdlib.h>
707c478bd9Sstevel@tonic-gate #include <locale.h>
717c478bd9Sstevel@tonic-gate #include <limits.h>
727c478bd9Sstevel@tonic-gate #include <string.h>
737c478bd9Sstevel@tonic-gate #include <wchar.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	LMAX	BUFSIZ
767c478bd9Sstevel@tonic-gate #define	BMAX	BUFSIZ
777c478bd9Sstevel@tonic-gate #define	STDOUT	1
787c478bd9Sstevel@tonic-gate #define	WGUTTER	6
797c478bd9Sstevel@tonic-gate #define	WLEN	(WGUTTER * 2 + WGUTTER + 2)
807c478bd9Sstevel@tonic-gate #define	PROMPT	'%'
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static const char	twoblanks[3] = "  ";
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static const char	*DIFF	= "diff -b ";
857c478bd9Sstevel@tonic-gate static char	diffcmd[BMAX];
867c478bd9Sstevel@tonic-gate static char	inbuf[10];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int	llen = 130;	/* Default maximum line length written out */
897c478bd9Sstevel@tonic-gate static int	hlen;		/* Half line length with space for gutter */
907c478bd9Sstevel@tonic-gate static int	len1;		/* Calculated length of left side */
917c478bd9Sstevel@tonic-gate static int	nchars;		/* Number of characters in left side - */
927c478bd9Sstevel@tonic-gate 					/* used for tab expansion */
937c478bd9Sstevel@tonic-gate static char	change = ' ';
947c478bd9Sstevel@tonic-gate static int	leftonly = 0;	/* if set print left side only for */
957c478bd9Sstevel@tonic-gate 					/* identical lines */
967c478bd9Sstevel@tonic-gate static int	silent = 0;	/* if set do not print identical lines */
977c478bd9Sstevel@tonic-gate static int	midflg = 0;	/* set after middle was output */
987c478bd9Sstevel@tonic-gate static int	rcode = 0;	/* return code */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static char	*file1;
1027c478bd9Sstevel@tonic-gate static FILE	*fdes1;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char	*file2;
1057c478bd9Sstevel@tonic-gate static FILE	*fdes2;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static FILE	*diffdes;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static int oflag;
1107c478bd9Sstevel@tonic-gate static char	*ofile;
1117c478bd9Sstevel@tonic-gate static FILE	*odes;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static char	*ltemp;
1147c478bd9Sstevel@tonic-gate static FILE	*left;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static char	*rtemp;
1177c478bd9Sstevel@tonic-gate static FILE	*right;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static FILE *tempdes;
1207c478bd9Sstevel@tonic-gate static char *temp;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* decoded diff cmd- left side from to; right side from, to */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int from1, to1, from2, to2;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int num1, num2;		/* line count for left side file and right */
1277c478bd9Sstevel@tonic-gate static int tempfd = -1;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static char	*filename(char *, char *);
1307c478bd9Sstevel@tonic-gate static char	*fgetline(FILE *);
1317c478bd9Sstevel@tonic-gate static int	put1(void);
1327c478bd9Sstevel@tonic-gate static int	put2(void);
1337c478bd9Sstevel@tonic-gate static void	putline(FILE *, char *, int);
1347c478bd9Sstevel@tonic-gate static int	cmd(char *);
1357c478bd9Sstevel@tonic-gate static int	getlen(int, char *);
1367c478bd9Sstevel@tonic-gate static void	putmid(int);
1377c478bd9Sstevel@tonic-gate static void	error(char *, char *);
1387c478bd9Sstevel@tonic-gate static void	onintr(void);
1397c478bd9Sstevel@tonic-gate static void	sremove(void);
1407c478bd9Sstevel@tonic-gate static void	cmdin(void);
1417c478bd9Sstevel@tonic-gate static void	cpp(char *, FILE *, FILE *);
1427c478bd9Sstevel@tonic-gate static void	edit(char *);
1437c478bd9Sstevel@tonic-gate 
144*a85fbef1Sakaplan int
1457c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	int	com;
1487c478bd9Sstevel@tonic-gate 	int	n1, n2, n;
1497c478bd9Sstevel@tonic-gate 	char	*bp;
1507c478bd9Sstevel@tonic-gate 	int	lfd = -1;
1517c478bd9Sstevel@tonic-gate 	int	rfd = -1;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1547c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGHUP, (void (*)(int))onintr);
1557c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1567c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGINT, (void (*)(int))onintr);
1577c478bd9Sstevel@tonic-gate 	if (signal(SIGPIPE, SIG_IGN) != SIG_IGN)
1587c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGPIPE, (void (*)(int))onintr);
1597c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1607c478bd9Sstevel@tonic-gate 		(void) signal((int)SIGTERM, (void (*)(int))onintr);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1637c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1647c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	while (--argc > 1 && **++argv == '-') {
1697c478bd9Sstevel@tonic-gate 		switch (*++*argv) {
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		case 'w':
1727c478bd9Sstevel@tonic-gate 			/* -w# instead of -w # */
1737c478bd9Sstevel@tonic-gate 			if (*++*argv)
1747c478bd9Sstevel@tonic-gate 				llen = atoi(*argv);
1757c478bd9Sstevel@tonic-gate 			else {
1767c478bd9Sstevel@tonic-gate 				argc--;
1777c478bd9Sstevel@tonic-gate 				llen = atoi(*++argv);
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 			if (llen < WLEN)
1807c478bd9Sstevel@tonic-gate 				error(gettext("Wrong line length %s"), *argv);
1817c478bd9Sstevel@tonic-gate 			if (llen > LMAX)
1827c478bd9Sstevel@tonic-gate 				llen = LMAX;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		case 'l':
1867c478bd9Sstevel@tonic-gate 			leftonly++;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		case 's':
1907c478bd9Sstevel@tonic-gate 			silent++;
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 		case 'o':
1937c478bd9Sstevel@tonic-gate 			oflag++;
1947c478bd9Sstevel@tonic-gate 			argc--;
1957c478bd9Sstevel@tonic-gate 			ofile = *++argv;
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 		default:
1987c478bd9Sstevel@tonic-gate 			error(gettext("Illegal argument: %s"), *argv);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (argc != 2) {
2027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2037c478bd9Sstevel@tonic-gate 		"Usage: sdiff [-l] [-s] [-o output] [-w #] file1 file2\n"));
204*a85fbef1Sakaplan 		return (2);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	file1 = *argv++;
2087c478bd9Sstevel@tonic-gate 	file2 = *argv;
2097c478bd9Sstevel@tonic-gate 	file1 = filename(file1, file2);
2107c478bd9Sstevel@tonic-gate 	file2 = filename(file2, file1);
2117c478bd9Sstevel@tonic-gate 	hlen = (llen - WGUTTER +1)/2;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((fdes1 = fopen(file1, "r")) == NULL)
2147c478bd9Sstevel@tonic-gate 		error(gettext("Cannot open: %s"), file1);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if ((fdes2 = fopen(file2, "r")) == NULL)
2177c478bd9Sstevel@tonic-gate 		error(gettext("Cannot open: %s"), file2);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (oflag) {
2207c478bd9Sstevel@tonic-gate 		if (tempfd == -1) {
2217c478bd9Sstevel@tonic-gate 			temp = strdup("/tmp/sdiffXXXXXX");
2227c478bd9Sstevel@tonic-gate 			tempfd = mkstemp(temp);
2237c478bd9Sstevel@tonic-gate 			if (tempfd == -1) {
2247c478bd9Sstevel@tonic-gate 				error(gettext(
2257c478bd9Sstevel@tonic-gate 					"Cannot open/create temp %s"), temp);
2267c478bd9Sstevel@tonic-gate 				free(temp);
2277c478bd9Sstevel@tonic-gate 				temp = 0;
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		ltemp = strdup("/tmp/sdifflXXXXXX");
2317c478bd9Sstevel@tonic-gate 		if ((lfd = mkstemp(ltemp)) == -1 ||
2327c478bd9Sstevel@tonic-gate 			(left = fdopen(lfd, "w")) == NULL)
2337c478bd9Sstevel@tonic-gate 				error(gettext(
2347c478bd9Sstevel@tonic-gate 					"Cannot open/create temp %s"),
2357c478bd9Sstevel@tonic-gate 					ltemp);
2367c478bd9Sstevel@tonic-gate 		rtemp = strdup("/tmp/sdiffrXXXXXX");
2377c478bd9Sstevel@tonic-gate 		if ((rfd = mkstemp(rtemp)) == -1 ||
2387c478bd9Sstevel@tonic-gate 			(right = fdopen(rfd, "w")) == NULL)
2397c478bd9Sstevel@tonic-gate 				error(gettext(
2407c478bd9Sstevel@tonic-gate 					"Cannot open/create temp file %s"),
2417c478bd9Sstevel@tonic-gate 					rtemp);
2427c478bd9Sstevel@tonic-gate 		if ((odes = fopen(ofile, "w")) == NULL)
2437c478bd9Sstevel@tonic-gate 			error(gettext("Cannot open output %s"), ofile);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	/* Call DIFF command */
2467c478bd9Sstevel@tonic-gate 	(void) strcpy(diffcmd, DIFF);
2477c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, file1);
2487c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, " ");
2497c478bd9Sstevel@tonic-gate 	(void) strcat(diffcmd, file2);
2507c478bd9Sstevel@tonic-gate 	diffdes = popen(diffcmd, "r");
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	num1 = num2 = 0;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Read in diff output and decode commands
2567c478bd9Sstevel@tonic-gate 	 * "change" is used to determine character to put in gutter
2577c478bd9Sstevel@tonic-gate 	 *  num1 and num2 counts the number of lines in file1 and 2
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	n = 0;
2617c478bd9Sstevel@tonic-gate 	while ((bp = fgetline(diffdes)) != NULL) {
2627c478bd9Sstevel@tonic-gate 		change = ' ';
2637c478bd9Sstevel@tonic-gate 		com = cmd(bp);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * handles all diff output that is not cmd
2677c478bd9Sstevel@tonic-gate 	 * lines starting with <, >, ., ---
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 		if (com == 0)
2707c478bd9Sstevel@tonic-gate 			continue;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* Catch up to from1 and from2 */
2737c478bd9Sstevel@tonic-gate 		rcode = 1;
2747c478bd9Sstevel@tonic-gate 		n1 = from1 - num1;
2757c478bd9Sstevel@tonic-gate 		n2 = from2 - num2;
2767c478bd9Sstevel@tonic-gate 		n = n1 > n2 ? n2 : n1;
2777c478bd9Sstevel@tonic-gate 		if (com == 'c' && n > 0)
2787c478bd9Sstevel@tonic-gate 			n--;
2797c478bd9Sstevel@tonic-gate 		if (silent)
2807c478bd9Sstevel@tonic-gate 			(void) fputs(bp, stdout);
2817c478bd9Sstevel@tonic-gate 		while (n-- > 0) {
2827c478bd9Sstevel@tonic-gate 			(void) put1();
2837c478bd9Sstevel@tonic-gate 			(void) put2();
2847c478bd9Sstevel@tonic-gate 			if (!silent)
2857c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
2867c478bd9Sstevel@tonic-gate 			midflg = 0;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* Process diff cmd */
2907c478bd9Sstevel@tonic-gate 		switch (com) {
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		case 'a':
2937c478bd9Sstevel@tonic-gate 			change = '>';
2947c478bd9Sstevel@tonic-gate 			while (num2 < to2) {
2957c478bd9Sstevel@tonic-gate 				(void) put2();
2967c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
2977c478bd9Sstevel@tonic-gate 				midflg = 0;
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		case 'd':
3027c478bd9Sstevel@tonic-gate 			change = '<';
3037c478bd9Sstevel@tonic-gate 			while (num1 < to1) {
3047c478bd9Sstevel@tonic-gate 				(void) put1();
3057c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3067c478bd9Sstevel@tonic-gate 				midflg = 0;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		case 'c':
3117c478bd9Sstevel@tonic-gate 			n1 = to1 - from1;
3127c478bd9Sstevel@tonic-gate 			n2 = to2 - from2;
3137c478bd9Sstevel@tonic-gate 			n = n1 > n2 ? n2 : n1;
3147c478bd9Sstevel@tonic-gate 			change = '|';
3157c478bd9Sstevel@tonic-gate 			do {
3167c478bd9Sstevel@tonic-gate 				(void) put1();
3177c478bd9Sstevel@tonic-gate 				(void) put2();
3187c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3197c478bd9Sstevel@tonic-gate 				midflg = 0;
3207c478bd9Sstevel@tonic-gate 			} while (n--);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			change = '<';
3237c478bd9Sstevel@tonic-gate 			while (num1 < to1) {
3247c478bd9Sstevel@tonic-gate 				(void) put1();
3257c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3267c478bd9Sstevel@tonic-gate 				midflg = 0;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			change = '>';
3307c478bd9Sstevel@tonic-gate 			while (num2 < to2) {
3317c478bd9Sstevel@tonic-gate 				(void) put2();
3327c478bd9Sstevel@tonic-gate 				(void) putc('\n', stdout);
3337c478bd9Sstevel@tonic-gate 				midflg = 0;
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		default:
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3397c478bd9Sstevel@tonic-gate 				"%c: cmd not found\n"), cmd);
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		if (oflag == 1 && com != 0) {
3447c478bd9Sstevel@tonic-gate 			cmdin();
3457c478bd9Sstevel@tonic-gate 			if ((left = fopen(ltemp, "w")) == NULL)
3467c478bd9Sstevel@tonic-gate 				error(gettext(
3477c478bd9Sstevel@tonic-gate 					"main: Cannot open temp %s"), ltemp);
3487c478bd9Sstevel@tonic-gate 			if ((right = fopen(rtemp, "w")) == NULL)
3497c478bd9Sstevel@tonic-gate 				error(gettext(
3507c478bd9Sstevel@tonic-gate 					"main: Cannot open temp %s"), rtemp);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	/* put out remainder of input files */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	while (put1()) {
3567c478bd9Sstevel@tonic-gate 		(void) put2();
3577c478bd9Sstevel@tonic-gate 		if (!silent)
3587c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
3597c478bd9Sstevel@tonic-gate 		midflg = 0;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	if (odes)
3627c478bd9Sstevel@tonic-gate 		(void) fclose(odes);
3637c478bd9Sstevel@tonic-gate 	sremove();
364*a85fbef1Sakaplan 	return (rcode);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int
3687c478bd9Sstevel@tonic-gate put1(void)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	/* len1 = length of left side */
3717c478bd9Sstevel@tonic-gate 	/* nchars = num of chars including tabs */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	char	*bp;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((bp = fgetline(fdes1)) != NULL) {
3777c478bd9Sstevel@tonic-gate 		len1 = getlen(0, bp);
3787c478bd9Sstevel@tonic-gate 		if ((!silent || change != ' ') && len1 != 0)
3797c478bd9Sstevel@tonic-gate 			putline(stdout, bp, nchars);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (oflag) {
3827c478bd9Sstevel@tonic-gate 		/*
3837c478bd9Sstevel@tonic-gate 		 * put left side either to output file
3847c478bd9Sstevel@tonic-gate 		 * if identical to right
3857c478bd9Sstevel@tonic-gate 		 * or left temp file if not
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 			if (change == ' ')
3897c478bd9Sstevel@tonic-gate 				putline(odes, bp, strlen(bp));
3907c478bd9Sstevel@tonic-gate 			else
3917c478bd9Sstevel@tonic-gate 				putline(left, bp, strlen(bp));
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		if (change != ' ')
3947c478bd9Sstevel@tonic-gate 			putmid(1);
3957c478bd9Sstevel@tonic-gate 		num1++;
3967c478bd9Sstevel@tonic-gate 		return (1);
3977c478bd9Sstevel@tonic-gate 	} else
3987c478bd9Sstevel@tonic-gate 		return (0);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static int
4027c478bd9Sstevel@tonic-gate put2(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	char	*bp;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if ((bp = fgetline(fdes2)) != NULL) {
4077c478bd9Sstevel@tonic-gate 		(void) getlen((hlen + WGUTTER) % 8, bp);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * if the left and right are different they are always
4117c478bd9Sstevel@tonic-gate 		 * printed.
4127c478bd9Sstevel@tonic-gate 		 * If the left and right are identical
4137c478bd9Sstevel@tonic-gate 		 * right is only printed if leftonly is not specified
4147c478bd9Sstevel@tonic-gate 		 * or silent mode is not specified
4157c478bd9Sstevel@tonic-gate 		 * or the right contains other than white space (len1 !=0)
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		if (change != ' ') {
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		/*
4207c478bd9Sstevel@tonic-gate 		 * put right side to right temp file only
4217c478bd9Sstevel@tonic-gate 		 * because left side was written to output for
4227c478bd9Sstevel@tonic-gate 		 * identical lines
4237c478bd9Sstevel@tonic-gate 		 */
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			if (oflag)
4267c478bd9Sstevel@tonic-gate 				putline(right, bp, strlen(bp));
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 			if (midflg == 0)
4297c478bd9Sstevel@tonic-gate 				putmid(1);
4307c478bd9Sstevel@tonic-gate 			putline(stdout, bp, nchars);
4317c478bd9Sstevel@tonic-gate 		} else
4327c478bd9Sstevel@tonic-gate 			if (!silent && !leftonly && len1 != 0) {
4337c478bd9Sstevel@tonic-gate 				if (midflg == 0)
4347c478bd9Sstevel@tonic-gate 					putmid(1);
4357c478bd9Sstevel@tonic-gate 				putline(stdout, bp, nchars);
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 		num2++;
4387c478bd9Sstevel@tonic-gate 		len1 = 0;
4397c478bd9Sstevel@tonic-gate 		return (1);
4407c478bd9Sstevel@tonic-gate 	} else {
4417c478bd9Sstevel@tonic-gate 		len1 = 0;
4427c478bd9Sstevel@tonic-gate 		return (0);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static void
4477c478bd9Sstevel@tonic-gate putline(FILE *file, char *start, int num)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	char	*cp, *end;
4507c478bd9Sstevel@tonic-gate 	int	i, len, d_col;
4517c478bd9Sstevel@tonic-gate 	wchar_t	wc;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	cp = start;
4547c478bd9Sstevel@tonic-gate 	end = cp + num;
4557c478bd9Sstevel@tonic-gate 	while (cp < end) {
4567c478bd9Sstevel@tonic-gate 		if (isascii(*cp)) {
4577c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4587c478bd9Sstevel@tonic-gate 			continue;
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if ((len = end - cp) > MB_LEN_MAX)
4627c478bd9Sstevel@tonic-gate 			len = MB_LEN_MAX;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, cp, len)) <= 0) {
4657c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4667c478bd9Sstevel@tonic-gate 			continue;
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		if ((d_col = wcwidth(wc)) <= 0)
4707c478bd9Sstevel@tonic-gate 			d_col = len;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		if ((cp + d_col) > end)
4737c478bd9Sstevel@tonic-gate 			return;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
4767c478bd9Sstevel@tonic-gate 			(void) putc(*cp++, file);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
4817c478bd9Sstevel@tonic-gate cmd(char *start)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	unsigned char	*cp;
4847c478bd9Sstevel@tonic-gate 	char	*cps;
4857c478bd9Sstevel@tonic-gate 	int	com;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (*start == '>' || *start == '<' || *start == '-' || *start == '.')
4887c478bd9Sstevel@tonic-gate 		return (0);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	cp = (unsigned char *)start;
4917c478bd9Sstevel@tonic-gate 	cps = start;
4927c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
4937c478bd9Sstevel@tonic-gate 		cp++;
4947c478bd9Sstevel@tonic-gate 	from1 = atoi(cps);
4957c478bd9Sstevel@tonic-gate 	to1 = from1;
4967c478bd9Sstevel@tonic-gate 	if (*cp == ',') {
4977c478bd9Sstevel@tonic-gate 		cp++;
4987c478bd9Sstevel@tonic-gate 		cps = (char *)cp;
4997c478bd9Sstevel@tonic-gate 		while (isdigit(*cp))
5007c478bd9Sstevel@tonic-gate 			cp++;
5017c478bd9Sstevel@tonic-gate 		to1 = atoi(cps);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	com = *cp++;
5057c478bd9Sstevel@tonic-gate 	cps = (char *)cp;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
5087c478bd9Sstevel@tonic-gate 		cp++;
5097c478bd9Sstevel@tonic-gate 	from2 = atoi(cps);
5107c478bd9Sstevel@tonic-gate 	to2 = from2;
5117c478bd9Sstevel@tonic-gate 	if (*cp == ',') {
5127c478bd9Sstevel@tonic-gate 		cp++;
5137c478bd9Sstevel@tonic-gate 		cps = (char *)cp;
5147c478bd9Sstevel@tonic-gate 		while (isdigit(*cp))
5157c478bd9Sstevel@tonic-gate 			cp++;
5167c478bd9Sstevel@tonic-gate 		to2 = atoi(cps);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	return (com);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static int
5227c478bd9Sstevel@tonic-gate getlen(int startpos, char *buffer)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * get the length of the string in buffer
5267c478bd9Sstevel@tonic-gate 	 *  expand tabs to next multiple of 8
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	unsigned char	*cp;
5297c478bd9Sstevel@tonic-gate 	int	slen, tlen, len, d_col;
5307c478bd9Sstevel@tonic-gate 	int	notspace;
5317c478bd9Sstevel@tonic-gate 	wchar_t	wc;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	nchars = 0;
5347c478bd9Sstevel@tonic-gate 	notspace = 0;
5357c478bd9Sstevel@tonic-gate 	tlen = startpos;
5367c478bd9Sstevel@tonic-gate 	for (cp = (unsigned char *)buffer; (*cp != '\n') && (*cp); cp++) {
5377c478bd9Sstevel@tonic-gate 		if (*cp == '\t') {
5387c478bd9Sstevel@tonic-gate 			slen = tlen;
5397c478bd9Sstevel@tonic-gate 			tlen += 8 - (tlen % 8);
5407c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5417c478bd9Sstevel@tonic-gate 				tlen = slen;
5427c478bd9Sstevel@tonic-gate 				break;
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 			nchars++;
5457c478bd9Sstevel@tonic-gate 			continue;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		if (isascii(*cp)) {
5497c478bd9Sstevel@tonic-gate 			slen = tlen;
5507c478bd9Sstevel@tonic-gate 			tlen++;
5517c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5527c478bd9Sstevel@tonic-gate 				tlen = slen;
5537c478bd9Sstevel@tonic-gate 				break;
5547c478bd9Sstevel@tonic-gate 			}
5557c478bd9Sstevel@tonic-gate 			if (!isspace(*cp))
5567c478bd9Sstevel@tonic-gate 				notspace = 1;
5577c478bd9Sstevel@tonic-gate 			nchars++;
5587c478bd9Sstevel@tonic-gate 			continue;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)cp, MB_LEN_MAX)) <= 0) {
5627c478bd9Sstevel@tonic-gate 			slen = tlen;
5637c478bd9Sstevel@tonic-gate 			tlen++;
5647c478bd9Sstevel@tonic-gate 			if (tlen >= hlen) {
5657c478bd9Sstevel@tonic-gate 				tlen = slen;
5667c478bd9Sstevel@tonic-gate 				break;
5677c478bd9Sstevel@tonic-gate 			}
5687c478bd9Sstevel@tonic-gate 			notspace = 1;
5697c478bd9Sstevel@tonic-gate 			nchars++;
5707c478bd9Sstevel@tonic-gate 			continue;
5717c478bd9Sstevel@tonic-gate 		}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		if ((d_col = wcwidth(wc)) <= 0)
5747c478bd9Sstevel@tonic-gate 			d_col = len;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		slen = tlen;
5777c478bd9Sstevel@tonic-gate 		tlen += d_col;
5787c478bd9Sstevel@tonic-gate 		if (tlen > hlen) {
5797c478bd9Sstevel@tonic-gate 			tlen = slen;
5807c478bd9Sstevel@tonic-gate 			break;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		notspace = 1;
5837c478bd9Sstevel@tonic-gate 		cp += len - 1;
5847c478bd9Sstevel@tonic-gate 		nchars += len;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (notspace ? tlen : 0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate static void
5907c478bd9Sstevel@tonic-gate putmid(int bflag)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	int	i;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * len1 set by getlen to the possibly truncated
5967c478bd9Sstevel@tonic-gate 	 *  length of left side
5977c478bd9Sstevel@tonic-gate 	 *  hlen is length of half line
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	midflg = 1;
6017c478bd9Sstevel@tonic-gate 	if (bflag) {
6027c478bd9Sstevel@tonic-gate 		for (i = 0; i < hlen - len1; i++)
6037c478bd9Sstevel@tonic-gate 			(void) putc(' ', stdout);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	(void) fputs(twoblanks, stdout);
6067c478bd9Sstevel@tonic-gate 	(void) putc((int)change, stdout);
6077c478bd9Sstevel@tonic-gate 	(void) fputs(twoblanks, stdout);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate static void
6117c478bd9Sstevel@tonic-gate error(char *s1, char *s2)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sdiff: ");
6147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, s1, s2);
6157c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
6167c478bd9Sstevel@tonic-gate 	sremove();
6177c478bd9Sstevel@tonic-gate 	exit(2);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static void
6217c478bd9Sstevel@tonic-gate onintr(void)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	sremove();
6247c478bd9Sstevel@tonic-gate 	exit(rcode);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static void
6287c478bd9Sstevel@tonic-gate sremove(void)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	if (ltemp) {
6317c478bd9Sstevel@tonic-gate 		(void) unlink(ltemp);
6327c478bd9Sstevel@tonic-gate 		free(ltemp);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	if (rtemp) {
6357c478bd9Sstevel@tonic-gate 		(void) unlink(rtemp);
6367c478bd9Sstevel@tonic-gate 		free(rtemp);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	if (temp) {
6397c478bd9Sstevel@tonic-gate 		(void) unlink(temp);
6407c478bd9Sstevel@tonic-gate 		free(temp);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static void
6457c478bd9Sstevel@tonic-gate cmdin(void)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	char	*cp, *ename;
6487c478bd9Sstevel@tonic-gate 	int	notacc;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	(void) fclose(left);
6517c478bd9Sstevel@tonic-gate 	(void) fclose(right);
6527c478bd9Sstevel@tonic-gate 	notacc = 1;
6537c478bd9Sstevel@tonic-gate 	while (notacc) {
6547c478bd9Sstevel@tonic-gate 		(void) putc(PROMPT, stdout);
6557c478bd9Sstevel@tonic-gate 		if ((cp = fgets(inbuf, 10, stdin)) == NULL) {
6567c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
6577c478bd9Sstevel@tonic-gate 			break;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 		switch (*cp) {
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		case 's':
6627c478bd9Sstevel@tonic-gate 			silent = 1;
6637c478bd9Sstevel@tonic-gate 			break;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		case 'v':
6667c478bd9Sstevel@tonic-gate 			silent = 0;
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		case 'q':
6707c478bd9Sstevel@tonic-gate 			sremove();
6717c478bd9Sstevel@tonic-gate 			exit(rcode);
6727c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		case 'l':
6767c478bd9Sstevel@tonic-gate 			cpp(ltemp, left, odes);
6777c478bd9Sstevel@tonic-gate 			notacc = 0;
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		case 'r':
6817c478bd9Sstevel@tonic-gate 			cpp(rtemp, right, odes);
6827c478bd9Sstevel@tonic-gate 			notacc = 0;
6837c478bd9Sstevel@tonic-gate 			break;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		case 'e':
6867c478bd9Sstevel@tonic-gate 			while (*++cp == ' ')
6877c478bd9Sstevel@tonic-gate 				;
6887c478bd9Sstevel@tonic-gate 			switch (*cp) {
6897c478bd9Sstevel@tonic-gate 			case 'l':
6907c478bd9Sstevel@tonic-gate 			case '<':
6917c478bd9Sstevel@tonic-gate 				notacc = 0;
6927c478bd9Sstevel@tonic-gate 				ename = ltemp;
6937c478bd9Sstevel@tonic-gate 				edit(ename);
6947c478bd9Sstevel@tonic-gate 				break;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			case 'r':
6977c478bd9Sstevel@tonic-gate 			case '>':
6987c478bd9Sstevel@tonic-gate 				notacc = 0;
6997c478bd9Sstevel@tonic-gate 				ename = rtemp;
7007c478bd9Sstevel@tonic-gate 				edit(ename);
7017c478bd9Sstevel@tonic-gate 				break;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			case 'b':
7047c478bd9Sstevel@tonic-gate 			case '|':
7057c478bd9Sstevel@tonic-gate 				if ((tempdes = fopen(temp, "w")) == NULL)
7067c478bd9Sstevel@tonic-gate 					error(gettext(
7077c478bd9Sstevel@tonic-gate 						"Cannot open temp file %s"),
7087c478bd9Sstevel@tonic-gate 						temp);
7097c478bd9Sstevel@tonic-gate 				cpp(ltemp, left, tempdes);
7107c478bd9Sstevel@tonic-gate 				cpp(rtemp, right, tempdes);
7117c478bd9Sstevel@tonic-gate 				(void) fclose(tempdes);
7127c478bd9Sstevel@tonic-gate 				notacc = 0;
7137c478bd9Sstevel@tonic-gate 				ename = temp;
7147c478bd9Sstevel@tonic-gate 				edit(ename);
7157c478bd9Sstevel@tonic-gate 				break;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			case '\n':
7187c478bd9Sstevel@tonic-gate 				if ((tempdes = fopen(temp, "w")) == NULL)
7197c478bd9Sstevel@tonic-gate 					error(gettext(
7207c478bd9Sstevel@tonic-gate 						"Cannot open temp file %s"),
7217c478bd9Sstevel@tonic-gate 						temp);
7227c478bd9Sstevel@tonic-gate 				(void) fclose(tempdes);
7237c478bd9Sstevel@tonic-gate 				notacc = 0;
7247c478bd9Sstevel@tonic-gate 				ename = temp;
7257c478bd9Sstevel@tonic-gate 				edit(ename);
7267c478bd9Sstevel@tonic-gate 				break;
7277c478bd9Sstevel@tonic-gate 			default:
7287c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7297c478bd9Sstevel@tonic-gate 					"Illegal command %s reenter\n"),
7307c478bd9Sstevel@tonic-gate 					cp);
7317c478bd9Sstevel@tonic-gate 				break;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 			if (notacc == 0)
7347c478bd9Sstevel@tonic-gate 				cpp(ename, tempdes, odes);
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		default:
7387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
7397c478bd9Sstevel@tonic-gate 				"Illegal command reenter\n"));
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static void
7467c478bd9Sstevel@tonic-gate cpp(char *from, FILE *fromdes, FILE *todes)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	char	tempbuf[BMAX + 1];
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if ((fromdes = fopen(from, "r")) == NULL)
7517c478bd9Sstevel@tonic-gate 		error(gettext(
7527c478bd9Sstevel@tonic-gate 			"cpp: Cannot open %s"), from);
7537c478bd9Sstevel@tonic-gate 	while ((fgets(tempbuf, BMAX, fromdes) != NULL))
7547c478bd9Sstevel@tonic-gate 		(void) fputs(tempbuf, todes);
7557c478bd9Sstevel@tonic-gate 	(void) fclose(fromdes);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate static void
7597c478bd9Sstevel@tonic-gate edit(char *file)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	int	i;
7627c478bd9Sstevel@tonic-gate 	pid_t	pid;
7637c478bd9Sstevel@tonic-gate 	void (*oldintr)(int);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	switch (pid = fork()) {
7667c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
7677c478bd9Sstevel@tonic-gate 		error(gettext("Cannot fork"), NULL);
7687c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7697c478bd9Sstevel@tonic-gate 		break;
7707c478bd9Sstevel@tonic-gate 	case (pid_t)0:
7717c478bd9Sstevel@tonic-gate 		(void) execl("/usr/bin/ed", "ed", file, NULL);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, SIG_IGN);	/* ignore interrupts in ed */
7757c478bd9Sstevel@tonic-gate 	while (pid != wait(&i))
7767c478bd9Sstevel@tonic-gate 		;
7777c478bd9Sstevel@tonic-gate 	/* restore previous interrupt proc */
7787c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, oldintr);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate static char *
7827c478bd9Sstevel@tonic-gate filename(char *pa1, char *pa2)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	int	c;
7857c478bd9Sstevel@tonic-gate 	char 	*a1, *b1, *a2;
7867c478bd9Sstevel@tonic-gate 	struct stat	stbuf;
7877c478bd9Sstevel@tonic-gate 	a1 = pa1;
7887c478bd9Sstevel@tonic-gate 	a2 = pa2;
7897c478bd9Sstevel@tonic-gate 	if (stat(a1, &stbuf) != -1 && ((stbuf.st_mode&S_IFMT) == S_IFDIR)) {
7907c478bd9Sstevel@tonic-gate 		b1 = pa1 = (char *)malloc(strlen(a1) + strlen(a2) + 2);
7917c478bd9Sstevel@tonic-gate 		while (*b1++ = *a1++);
7927c478bd9Sstevel@tonic-gate 		b1[-1] = '/';
7937c478bd9Sstevel@tonic-gate 		a1 = b1;
7947c478bd9Sstevel@tonic-gate 		while (*a1++ = *a2++)
7957c478bd9Sstevel@tonic-gate 			if (*a2 && *a2 != '/' && a2[-1] == '/')
7967c478bd9Sstevel@tonic-gate 				a1 = b1;
7977c478bd9Sstevel@tonic-gate 	} else if (a1[0] == '-' && a1[1] == 0 && temp == 0) {
7987c478bd9Sstevel@tonic-gate 		if (fstat(fileno(stdin), &stbuf) == -1)
7997c478bd9Sstevel@tonic-gate 			error(gettext("Cannot process stdin"), NULL);
8007c478bd9Sstevel@tonic-gate 		pa1 = temp = strdup("/tmp/sdiffXXXXXX");
8017c478bd9Sstevel@tonic-gate 		if ((tempfd = mkstemp(temp)) == -1 ||
8027c478bd9Sstevel@tonic-gate 			(tempdes = fdopen(tempfd, "w")) == NULL)
8037c478bd9Sstevel@tonic-gate 				error(gettext("Cannot open/create temp %s"),
8047c478bd9Sstevel@tonic-gate 					temp);
8057c478bd9Sstevel@tonic-gate 		while ((c = getc(stdin)) != EOF)
8067c478bd9Sstevel@tonic-gate 			(void) putc(c, tempdes);
8077c478bd9Sstevel@tonic-gate 		(void) fclose(tempdes);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	return (pa1);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * like fgets, but reads upto and including a newline,
8147c478bd9Sstevel@tonic-gate  * the data is stored in a reusable dynamic buffer that grows to fit
8157c478bd9Sstevel@tonic-gate  * the largest line in the file, the buffer is NULL terminated
8167c478bd9Sstevel@tonic-gate  * returns a pointer to the dynamic buffer.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static char *
8197c478bd9Sstevel@tonic-gate fgetline(FILE *fp)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	static char	*bp = NULL;
8227c478bd9Sstevel@tonic-gate 	static int	blen = 0;
8237c478bd9Sstevel@tonic-gate 	int	sl;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
8267c478bd9Sstevel@tonic-gate 		/* allocate it for the first time */
8277c478bd9Sstevel@tonic-gate 		bp = (char *)malloc(BUFSIZ);
8287c478bd9Sstevel@tonic-gate 		if (bp == NULL)
8297c478bd9Sstevel@tonic-gate 			error(gettext("fgetline: malloc failed"), NULL);
8307c478bd9Sstevel@tonic-gate 		blen = BUFSIZ;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/* check for error or nothing read */
8347c478bd9Sstevel@tonic-gate 	if (fgets(bp, blen, fp) == NULL)
8357c478bd9Sstevel@tonic-gate 		return (NULL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (feof(fp))
8387c478bd9Sstevel@tonic-gate 		return (bp);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	while ((sl = strlen(bp)) == blen-1 && *(bp+blen-2) != '\n') {
8417c478bd9Sstevel@tonic-gate 		/* still more data, grow the buffer */
8427c478bd9Sstevel@tonic-gate 		blen *= 2;
8437c478bd9Sstevel@tonic-gate 		bp = (char *)realloc(bp, blen);
8447c478bd9Sstevel@tonic-gate 		if (bp == NULL)
8457c478bd9Sstevel@tonic-gate 			error(gettext("fgetline: realloc failed"), NULL);
8467c478bd9Sstevel@tonic-gate 		/* continue reading and add to end of buffer */
8477c478bd9Sstevel@tonic-gate 		(void) fgets(bp+sl, blen-sl, fp);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 	return (bp);
8507c478bd9Sstevel@tonic-gate }
851