xref: /onnv-gate/usr/src/lib/libcmd/common/comm.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * AT&T Bell Laboratories
254887Schin  *
264887Schin  * comm
274887Schin  */
284887Schin 
294887Schin static const char usage[] =
304887Schin "[-?\n@(#)$Id: comm (AT&T Research) 1999-04-28 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?comm - select or reject lines common to two files]"
334887Schin "[+DESCRIPTION?\bcomm\b reads two files \afile1\a and \afile2\a "
344887Schin 	"which should be ordered in the collating sequence of the "
354887Schin 	"current locale, and produces three text columns as output:]{"
364887Schin 	"[+1?Lines only in \afile1\a.]"
374887Schin 	"[+2?Lines only in \afile2\a.]"
384887Schin 	"[+3?Lines in both files.]"
394887Schin 	"}"
404887Schin "[+?If lines in either file are not ordered according to the collating "
414887Schin 	"sequence of the current locale, the results are not specified.]"
424887Schin "[+?If either \afile1\a or \afile2\a is \b-\b, \bcomm\b "
434887Schin         "uses standard input starting at the current location.]"
444887Schin 
454887Schin "[1?Suppress the output column of lines unique to \afile1\a.]"
464887Schin "[2?Suppress the output column of lines unique to \afile2\a.]"
474887Schin "[3?Suppress the output column of lines duplicate in \afile1\a and \afile2\a.]"
484887Schin "\n"
494887Schin "\nfile1 file2\n"
504887Schin "\n"
514887Schin "[+EXIT STATUS?]{"
524887Schin 	"[+0?Both files processed successfully.]"
534887Schin 	"[+>0?An error occurred.]"
544887Schin "}"
554887Schin "[+SEE ALSO?\bcmp\b(1), \bdiff\b(1)]"
564887Schin ;
574887Schin 
584887Schin 
594887Schin #include <cmd.h>
604887Schin 
614887Schin #define C_FILE1		1
624887Schin #define C_FILE2		2
634887Schin #define C_COMMON	4
644887Schin #define C_ALL		(C_FILE1|C_FILE2|C_COMMON)
654887Schin 
comm(Sfio_t * in1,Sfio_t * in2,register Sfio_t * out,register int mode)664887Schin static int comm(Sfio_t *in1, Sfio_t *in2, register Sfio_t *out,register int mode)
674887Schin {
684887Schin 	register char *cp1, *cp2;
694887Schin 	register int n1, n2, n, comp;
704887Schin 	if(cp1 = sfgetr(in1,'\n',0))
714887Schin 		n1 = sfvalue(in1);
724887Schin 	if(cp2 = sfgetr(in2,'\n',0))
734887Schin 		n2 = sfvalue(in2);
744887Schin 	while(cp1 && cp2)
754887Schin 	{
764887Schin 		n=(n1<n2?n1:n2);
774887Schin 		if((comp=memcmp(cp1,cp2,n-1))==0 && (comp=n1-n2)==0)
784887Schin 		{
794887Schin 			if(mode&C_COMMON)
804887Schin 			{
814887Schin 				if(mode!=C_COMMON)
824887Schin 				{
834887Schin 					sfputc(out,'\t');
844887Schin 					if(mode==C_ALL)
854887Schin 						sfputc(out,'\t');
864887Schin 				}
874887Schin 				if(sfwrite(out,cp1,n) < 0)
884887Schin 					return(-1);
894887Schin 			}
904887Schin 			if(cp1 = sfgetr(in1,'\n',0))
914887Schin 				n1 = sfvalue(in1);
924887Schin 			if(cp2 = sfgetr(in2,'\n',0))
934887Schin 				n2 = sfvalue(in2);
944887Schin 		}
954887Schin 		else if(comp > 0)
964887Schin 		{
974887Schin 			if(mode&C_FILE2)
984887Schin 			{
994887Schin 				if(mode&C_FILE1)
1004887Schin 					sfputc(out,'\t');
1014887Schin 				if(sfwrite(out,cp2,n2) < 0)
1024887Schin 					return(-1);
1034887Schin 			}
1044887Schin 			if(cp2 = sfgetr(in2,'\n',0))
1054887Schin 				n2 = sfvalue(in2);
1064887Schin 		}
1074887Schin 		else
1084887Schin 		{
1094887Schin 			if((mode&C_FILE1) && sfwrite(out,cp1,n1) < 0)
1104887Schin 				return(-1);
1114887Schin 			if(cp1 = sfgetr(in1,'\n',0))
1124887Schin 				n1 = sfvalue(in1);
1134887Schin 		}
1144887Schin 	}
1154887Schin 	n = 0;
1164887Schin 	if(cp2)
1174887Schin 	{
1184887Schin 		cp1 = cp2;
1194887Schin 		in1 = in2;
1204887Schin 		n1 = n2;
1214887Schin 		if(mode&C_FILE1)
1224887Schin 			n = 1;
1234887Schin 		mode &= C_FILE2;
1244887Schin 	}
1254887Schin 	else
1264887Schin 		mode &= C_FILE1;
1274887Schin 	if(!mode || !cp1)
1284887Schin 	{
1294887Schin 		if(cp1 && in1==sfstdin)
1304887Schin 			sfseek(in1,(Sfoff_t)0,SEEK_END);
1314887Schin 		return(0);
1324887Schin 	}
1334887Schin 	/* process the remaining stream */
1344887Schin 	while(1)
1354887Schin 	{
1364887Schin 		if(n)
1374887Schin 			sfputc(out,'\t');
1384887Schin 		if(sfwrite(out,cp1,n1) < 0)
1394887Schin 			return(-1);
1404887Schin 		if(!(cp1 = sfgetr(in1,'\n',0)))
1414887Schin 			return(0);
1424887Schin 		n1 = sfvalue(in1);
1434887Schin 	}
1444887Schin 	/* NOT REACHED */
1454887Schin }
1464887Schin 
1474887Schin int
b_comm(int argc,char * argv[],void * context)1484887Schin b_comm(int argc, char *argv[], void* context)
1494887Schin {
1504887Schin 	register int n;
1514887Schin 	register int mode = C_FILE1|C_FILE2|C_COMMON;
1524887Schin 	register char *cp;
1534887Schin 	Sfio_t *f1, *f2;
1544887Schin 
1554887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1564887Schin 	while (n = optget(argv, usage)) switch (n)
1574887Schin 	{
1584887Schin  	    case '1':
1594887Schin 		mode &= ~C_FILE1;
1604887Schin 		break;
1614887Schin 	    case '2':
1624887Schin 		mode &= ~C_FILE2;
1634887Schin 		break;
1644887Schin 	    case '3':
1654887Schin 		mode &= ~C_COMMON;
1664887Schin 		break;
1674887Schin 	    case ':':
1684887Schin 		error(2, "%s",opt_info.arg);
1694887Schin 		break;
1704887Schin 	    case '?':
1714887Schin 		error(ERROR_usage(2), "%s",opt_info.arg);
1724887Schin 		break;
1734887Schin 	}
1744887Schin 	argv += opt_info.index;
1754887Schin 	argc -= opt_info.index;
1764887Schin 	if(error_info.errors || argc!=2)
1774887Schin 		error(ERROR_usage(2),"%s",optusage(NiL));
1784887Schin 	cp = *argv++;
1794887Schin 	if(streq(cp,"-"))
1804887Schin 		f1 = sfstdin;
1814887Schin 	else if(!(f1 = sfopen(NiL, cp,"r")))
1824887Schin 		error(ERROR_system(1),"%s: cannot open",cp);
1834887Schin 	cp = *argv;
1844887Schin 	if(streq(cp,"-"))
1854887Schin 		f2 = sfstdin;
1864887Schin 	else if(!(f2 = sfopen(NiL, cp,"r")))
1874887Schin 		error(ERROR_system(1),"%s: cannot open",cp);
1884887Schin 	if(mode)
1894887Schin 	{
1904887Schin 		if(comm(f1,f2,sfstdout,mode) < 0)
1914887Schin 			error(ERROR_system(1)," write error");
1924887Schin 	}
1934887Schin 	else if(f1==sfstdin || f2==sfstdin)
1944887Schin 		sfseek(sfstdin,(Sfoff_t)0,SEEK_END);
1954887Schin 	if(f1!=sfstdin)
1964887Schin 		sfclose(f1);
1974887Schin 	if(f2!=sfstdin)
1984887Schin 		sfclose(f2);
1994887Schin 	return(error_info.errors);
2004887Schin }
2014887Schin 
202