home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / mrg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-12  |  2.0 KB  |  84 lines

  1. /*
  2. ** mrg.c -- compares two sorted text files
  3. ** Copyright 1982 J. E. Hendrix.  All rights reserved.
  4. */
  5. #include <stdio.h>
  6. #include "tools.h"
  7. #define NOCCARGC
  8. int cmpr, fd1, fd2, i;
  9. char *line1, *line2, column, arg[MAXFN];
  10. main(argc, argv) int argc, *argv; {
  11.   int error;
  12.   column='0';
  13.   fd1=fd2=stdin;
  14.   line1=malloc(MAXLINE+1);
  15.   line2=malloc(MAXLINE+1);
  16.   error=NO;
  17.   i=0;
  18.   while(getarg(++i, arg, MAXFN, argc, argv)!=EOF) {
  19.     if(arg[0]!='-') {
  20.       if(fd1==stdin) {
  21.         if((fd1=fopen(arg, "r"))==NULL) cant(arg);
  22.         }
  23.       else if(fd2==stdin) {
  24.         if((fd2=fopen(arg, "r"))==NULL) cant(arg);
  25.         }
  26.       else error=YES;
  27.       }
  28.     else if(((arg[1] > '0')&(arg[1] < '4'))
  29.             |(same(arg[1], 'f')))
  30.       column=arg[1];
  31.     else error=YES;
  32.     }
  33.   if((error)|(fd1==stdin)) {
  34.     fputs("usage: MRG file [file] [-1|-2|-3|-F]\n", stderr);
  35.     abort(7);
  36.     }
  37.   auxbuf(fd1, 4096);
  38.   auxbuf(fd2, 4096);
  39.   getline(line1, fd1);
  40.   getline(line2, fd2);
  41.   while(YES) {
  42.     poll(YES);
  43.     cmpr=lexcmp(line1, line2);
  44.     if(cmpr < 0) {
  45.       if(same(column, 'f')) fout("1) ", line1);
  46.       else if((column=='0')|(column=='1')) sout(line1, stdout);
  47.       getline(line1, fd1);
  48.       continue;
  49.       }
  50.     else if(cmpr > 0) {
  51.       if(same(column, 'f')) fout("  2) ", line2);
  52.       else if((column=='0')|(column=='2')) sout(line2, stdout);
  53.       getline(line2, fd2);
  54.       continue;
  55.       }
  56.     if(line1[0] == 127) break;
  57.     if(same(column, 'f'))   fout("    3) ", line1);
  58.     else if((column=='0')|(column=='3')) sout(line1, stdout);
  59.     getline(line1, fd1);
  60.     getline(line2, fd2);
  61.     }
  62.   fclose(stdout);
  63.   }
  64.  
  65. getline(line, fd) char *line; int fd; {
  66.   if(fgets(line, MAXLINE+1, fd)==NULL) {
  67.     line[0] = 127;
  68.     line[1] = NULL;
  69.     }
  70.   }
  71.  
  72. /*
  73. ** fout -- formatted output of a line
  74. */
  75. fout(header, data) char *header, *data; {
  76.   sout(header, stdout);
  77.   sout(data, stdout);
  78.   }
  79.  
  80. #include "out.c"
  81. #include "cant.c"
  82. #include "same.c"
  83.  
  84.