home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 189_01 / merge.c < prev    next >
Text File  |  1985-08-21  |  2KB  |  100 lines

  1. /* merge.c:        merge input files to standard output
  2.  *
  3.  *         usage:  merge file1 file2 [file3 ...]
  4.  *
  5.  *              ..a file argument of "-" refers to standard input
  6.  */
  7.  
  8. #include <stdio.h>
  9. #define MAXLINE 256
  10.  
  11. main(argc,argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.     FILE *f, *inf, *outf, *mustopen();
  16.     register i;
  17.     static char line[MAXLINE], *t, *inp = "temp1.$$$", *outp = "temp2.$$$";
  18.  
  19.     /* ..process files.. */
  20.     if (argc < 2)
  21.     {
  22.         fprintf(stderr,"usage: merge f1 f2 [f3 ...]\n");
  23.         exit(1);
  24.     }
  25.  
  26.     /* ..Set up first file as current input; outp as current output.. */
  27.     inf = (argv[1][0] == '-') ? stdin : mustopen(argv[1],"r");
  28.     outf = mustopen(outp,"w");
  29.  
  30.     for (i = 2; i < argc; ++i)
  31.     {
  32.         f = (argv[i][0] == '-') ? stdin : mustopen(argv[i],"r");
  33.         fprintf(stderr,"merging %s...\n",
  34.           (argv[i][0] == '-') ? "stdin" : argv[i]);
  35.         merge(f,inf,outf); 
  36.         fclose(f); fclose(inf); fclose(outf);
  37.         t = inp; inp = outp; outp = t;  /* ..swap filenames.. */
  38.         inf = mustopen(inp,"r");
  39.         outf = mustopen(outp,"w");
  40.     }
  41.     fclose(inf); fclose(outf);
  42.  
  43.     /* ..answer is in inp.. */
  44.     f = mustopen(inp,"r");
  45.     while (fgets(line,MAXLINE-1,f))
  46.         fputs(line,stdout);
  47.     fclose(f);
  48.     unlink(inp); unlink(outp);
  49. }
  50.  
  51. FILE *mustopen(s,mode)
  52. char *s, *mode;
  53. {
  54.     FILE *f;
  55.  
  56.     /* ..open file or die.. */
  57.     if ((f = fopen(s,mode)) == NULL)
  58.     {
  59.         fprintf(stderr,"---> ERROR: can't open %s\n",s);
  60.         exit(1);
  61.     }
  62.  
  63.     return(f);
  64. }
  65.  
  66. merge(f1,f2,outf)
  67. FILE *f1, *f2, *outf;
  68. {
  69.     char s1[MAXLINE], s2[MAXLINE];
  70.  
  71.     fgets(s1,MAXLINE-1,f1);
  72.     fgets(s2,MAXLINE-1,f2);
  73.  
  74.     /* ..merge while both open.. */
  75.     while (!feof(f1) && !feof(f2))
  76.         if (strcmp(s1,s2) < 0)
  77.         {
  78.             fputs(s1,outf);
  79.             fgets(s1,MAXLINE-1,f1);
  80.         }
  81.         else
  82.         {
  83.             fputs(s2,outf);
  84.             fgets(s2,MAXLINE-1,f2);
  85.         }
  86.  
  87.     /* ..dump remaining file.. */
  88.     while (!feof(f1))
  89.     {
  90.         fputs(s1,outf);
  91.         fgets(s1,MAXLINE-1,f1);
  92.     }
  93.     while (!feof(f2))
  94.     {
  95.         fputs(s2,outf);
  96.         fgets(s2,MAXLINE-1,f2);
  97.     }
  98. }
  99. while both open.. */
  100.     while