1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * David Korn
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * comm
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: comm (AT&T Research) 1999-04-28 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?comm - select or reject lines common to two files]"
1N/A"[+DESCRIPTION?\bcomm\b reads two files \afile1\a and \afile2\a "
1N/A "which should be ordered in the collating sequence of the "
1N/A "current locale, and produces three text columns as output:]{"
1N/A "[+1?Lines only in \afile1\a.]"
1N/A "[+2?Lines only in \afile2\a.]"
1N/A "[+3?Lines in both files.]"
1N/A "}"
1N/A"[+?If lines in either file are not ordered according to the collating "
1N/A "sequence of the current locale, the results are not specified.]"
1N/A"[+?If either \afile1\a or \afile2\a is \b-\b, \bcomm\b "
1N/A "uses standard input starting at the current location.]"
1N/A
1N/A"[1?Suppress the output column of lines unique to \afile1\a.]"
1N/A"[2?Suppress the output column of lines unique to \afile2\a.]"
1N/A"[3?Suppress the output column of lines duplicate in \afile1\a and \afile2\a.]"
1N/A"\n"
1N/A"\nfile1 file2\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]{"
1N/A "[+0?Both files processed successfully.]"
1N/A "[+>0?An error occurred.]"
1N/A"}"
1N/A"[+SEE ALSO?\bcmp\b(1), \bdiff\b(1)]"
1N/A;
1N/A
1N/A
1N/A#include <cmd.h>
1N/A
1N/A#define C_FILE1 1
1N/A#define C_FILE2 2
1N/A#define C_COMMON 4
1N/A#define C_ALL (C_FILE1|C_FILE2|C_COMMON)
1N/A
1N/Astatic int comm(Sfio_t *in1, Sfio_t *in2, register Sfio_t *out,register int mode)
1N/A{
1N/A register char *cp1, *cp2;
1N/A register int n1, n2, n, comp;
1N/A if(cp1 = sfgetr(in1,'\n',0))
1N/A n1 = sfvalue(in1);
1N/A if(cp2 = sfgetr(in2,'\n',0))
1N/A n2 = sfvalue(in2);
1N/A while(cp1 && cp2)
1N/A {
1N/A n=(n1<n2?n1:n2);
1N/A if((comp=memcmp(cp1,cp2,n-1))==0 && (comp=n1-n2)==0)
1N/A {
1N/A if(mode&C_COMMON)
1N/A {
1N/A if(mode!=C_COMMON)
1N/A {
1N/A sfputc(out,'\t');
1N/A if(mode==C_ALL)
1N/A sfputc(out,'\t');
1N/A }
1N/A if(sfwrite(out,cp1,n) < 0)
1N/A return(-1);
1N/A }
1N/A if(cp1 = sfgetr(in1,'\n',0))
1N/A n1 = sfvalue(in1);
1N/A if(cp2 = sfgetr(in2,'\n',0))
1N/A n2 = sfvalue(in2);
1N/A }
1N/A else if(comp > 0)
1N/A {
1N/A if(mode&C_FILE2)
1N/A {
1N/A if(mode&C_FILE1)
1N/A sfputc(out,'\t');
1N/A if(sfwrite(out,cp2,n2) < 0)
1N/A return(-1);
1N/A }
1N/A if(cp2 = sfgetr(in2,'\n',0))
1N/A n2 = sfvalue(in2);
1N/A }
1N/A else
1N/A {
1N/A if((mode&C_FILE1) && sfwrite(out,cp1,n1) < 0)
1N/A return(-1);
1N/A if(cp1 = sfgetr(in1,'\n',0))
1N/A n1 = sfvalue(in1);
1N/A }
1N/A }
1N/A n = 0;
1N/A if(cp2)
1N/A {
1N/A cp1 = cp2;
1N/A in1 = in2;
1N/A n1 = n2;
1N/A if(mode&C_FILE1)
1N/A n = 1;
1N/A mode &= C_FILE2;
1N/A }
1N/A else
1N/A mode &= C_FILE1;
1N/A if(!mode || !cp1)
1N/A {
1N/A if(cp1 && in1==sfstdin)
1N/A sfseek(in1,(Sfoff_t)0,SEEK_END);
1N/A return(0);
1N/A }
1N/A /* process the remaining stream */
1N/A while(1)
1N/A {
1N/A if(n)
1N/A sfputc(out,'\t');
1N/A if(sfwrite(out,cp1,n1) < 0)
1N/A return(-1);
1N/A if(!(cp1 = sfgetr(in1,'\n',0)))
1N/A return(0);
1N/A n1 = sfvalue(in1);
1N/A }
1N/A /* NOT REACHED */
1N/A}
1N/A
1N/Aint
1N/Ab_comm(int argc, char *argv[], void* context)
1N/A{
1N/A register int mode = C_FILE1|C_FILE2|C_COMMON;
1N/A register char *cp;
1N/A Sfio_t *f1, *f2;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case '1':
1N/A mode &= ~C_FILE1;
1N/A continue;
1N/A case '2':
1N/A mode &= ~C_FILE2;
1N/A continue;
1N/A case '3':
1N/A mode &= ~C_COMMON;
1N/A continue;
1N/A case ':':
1N/A error(2, "%s",opt_info.arg);
1N/A break;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s",opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A argc -= opt_info.index;
1N/A if(error_info.errors || argc!=2)
1N/A error(ERROR_usage(2),"%s",optusage(NiL));
1N/A cp = *argv++;
1N/A if(streq(cp,"-"))
1N/A f1 = sfstdin;
1N/A else if(!(f1 = sfopen(NiL, cp,"r")))
1N/A error(ERROR_system(1),"%s: cannot open",cp);
1N/A cp = *argv;
1N/A if(streq(cp,"-"))
1N/A f2 = sfstdin;
1N/A else if(!(f2 = sfopen(NiL, cp,"r")))
1N/A error(ERROR_system(1),"%s: cannot open",cp);
1N/A if(mode)
1N/A {
1N/A if(comm(f1,f2,sfstdout,mode) < 0)
1N/A error(ERROR_system(1)," write error");
1N/A }
1N/A else if(f1==sfstdin || f2==sfstdin)
1N/A sfseek(sfstdin,(Sfoff_t)0,SEEK_END);
1N/A if(f1!=sfstdin)
1N/A sfclose(f1);
1N/A if(f2!=sfstdin)
1N/A sfclose(f2);
1N/A return error_info.errors;
1N/A}