#include <stdio.h>
#include <ctype.h>

/*   filter program to remove duplicate blank lines from
     a file.  With the -n option, removes all blank lines
     from a file.  If one or more file names are given, it
     operates on those files; if no file name is given, it
     reads from standard input.  In all cases, it writes 
     the modified output to standard output              */   

#define BUFSIZE   256

main(int argc,char *argv[])

{
  FILE *fp;
  int i,j,l,last,nf,nfiles,nflag;
  int argind[256];          /* this imposes a 256 file limit */
  char buf[BUFSIZE];

  i = 1;
  nfiles = nflag = 0;
  while(i < argc){
	if(argv[i][0] == '-')goto doopt;  
	argind[nfiles++] = i;  /* save the argument number of filenames */
	i++;
	continue;

  doopt:
	if(argv[i][1] == 'n')nflag = 1;
	else printf("%s unknown option, ingnored.\n",argv[i]);
	i++;
	}    

  nf = 0;
  do{
	if(nfiles == 0)fp = stdin;
	else fp = fopen(argv[argind[nf]],"r");
	if(fp == NULL){
		printf("can't open %s\n",argv[argind[nf]]);
		continue; 
		}

	last = 0;
	while(fgets(buf,BUFSIZE,fp) != NULL){
		l = 0;
		      /* isspace macro in ctype.h */
		for(j=0;buf[j] != '\n';j++)if(!isspace(buf[j]))l = 1;

		if(l == 0){                /* blank line found */
			if(nflag)continue;        
			switch(last){
				case 0: 
	  			last = 1;
				break;  
				case 1:
				last++;
				continue;
				break;  
				default:
				continue;
				break; 
				}
			}
	        else last = 0;               /*  line wasn't blank */
	        printf("%s",buf); 
		}
    	 nf++;
    	}while(nf < nfiles);
 
 
}
