uniq.c for Apple Aztec C system
mas at ecsvax.UUCP
mas at ecsvax.UUCP
Mon Feb 27 09:42:33 AEST 1984
=============================================================================
/* uniq - report repeated lines in file */
#include "stdio.h"
#define TRUE 1
#define FALSE 0
#define MAXLINE 256
#define NL '\n'
#define EOS '\0'
int count, oneonly, duponly;
char linebuf[2][MAXLINE];
main(argc, argv)
int argc ;
char *argv[] ;
{
char *s ;
FILE *input ;
count = oneonly = duponly = FALSE ;
while( --argc>0 && **++argv == '-' )
for( s= &argv[0][1] ; *s != EOS ; s++ )
switch( *s ) {
case 'u':
oneonly = TRUE;
break ;
case 'd':
duponly = TRUE;
break ;
case 'c':
count = TRUE ;
break ;
default:
fprintf(stderr, "uniq: unknown option %c\n", *s ) ;
argc = -1 ;
break ;
}
if( argc < 0 ) {
fprintf(stderr, "usage: uniq [ -udc ] files\n");
exit(1) ;
}
if (oneonly && duponly)
oneonly = duponly = FALSE;
if( argc == 0 )
uniq( stdin ) ;
else
for( ; argc>0 ; argc--,argv++)
if( (input=fopen(*argv,"r")) == NULL ) {
fprintf(stderr, "uniq: can't open %s\n", *argv) ;
exit(1) ;
}
else {
uniq( input ) ;
fclose( input ) ;
}
exit(0) ;
} /* end main */
/* uniq - report repeated lines in file */
uniq( in )
FILE *in ;
{
int nreps, p ; /* p keeps track of the buffer that is getting
current input then switches when lines aren't equal*/
nreps = p= 1;
if (getline(linebuf[0],in) == EOF)
return;
while(getline(linebuf[p],in) !=EOF ){
if (strcmp(linebuf[0],linebuf[1])==0)
nreps++;
else {
putlin(nreps,(p?--p:++p));
nreps=1;
}
}
putlin(nreps,(p?--p:++p));
} /* end uniq */
/* putlin */
putlin( cts, bufno )
int cts, bufno ;
{
if ((oneonly && cts > 1) || (duponly && cts ==1))
return;
if (count)
fprintf(stdout,"%3d",cts);
fprintf(stdout,"%s",linebuf[bufno]);
}/* end putlin */
/* getline - read input line
from net.sources ihuxl.740 sort.c by Dave Newkirk*/
getline( ptr, input )
char *ptr ;
FILE *input ;
{
int c ;
char *bufptr ;
bufptr = ptr ;
while( (c=agetc(input)) != NL ) {
if( c == EOF )
return EOF ;
*bufptr++ = c ;
}
*bufptr++ = NL ;
*bufptr++ = EOS ;
return bufptr - ptr ;
} /* end getline */
More information about the Comp.sources.unix
mailing list