home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / textutils-1.11-src.lha / textutils-1.11 / src / comm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-13  |  6.1 KB  |  267 lines

  1. /* comm -- compare two sorted files line by line.
  2.    Copyright (C) 1986, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman and David MacKenzie. */
  19.  
  20. #include <config.h>
  21.  
  22. #include <stdio.h>
  23. #include <getopt.h>
  24. #include <sys/types.h>
  25. #include "system.h"
  26. #include "linebuffer.h"
  27. #include "version.h"
  28.  
  29. #define min(x, y) ((x) < (y) ? (x) : (y))
  30.  
  31. /* The name this program was run with. */
  32. char *program_name;
  33.  
  34. /* If nonzero, print lines that are found only in file 1. */
  35. static int only_file_1;
  36.  
  37. /* If nonzero, print lines that are found only in file 2. */
  38. static int only_file_2;
  39.  
  40. /* If nonzero, print lines that are found in both files. */
  41. static int both;
  42.  
  43. /* If non-zero, display usage information and exit.  */
  44. static int show_help;
  45.  
  46. /* If non-zero, print the version on standard output then exit.  */
  47. static int show_version;
  48.  
  49. static struct option const long_options[] =
  50. {
  51.   {"help", no_argument, &show_help, 1},
  52.   {"version", no_argument, &show_version, 1},
  53.   {0, 0, 0, 0}
  54. };
  55.  
  56. void error ();
  57. static int compare_files ();
  58. static void writeline ();
  59. static void usage ();
  60.  
  61. main (argc, argv)
  62.      int argc;
  63.      char *argv[];
  64. {
  65.   int c;
  66.  
  67.   program_name = argv[0];
  68.  
  69.   only_file_1 = 1;
  70.   only_file_2 = 1;
  71.   both = 1;
  72.  
  73.   while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
  74.     switch (c)
  75.       {
  76.       case 0:
  77.     break;
  78.  
  79.       case '1':
  80.     only_file_1 = 0;
  81.     break;
  82.  
  83.       case '2':
  84.     only_file_2 = 0;
  85.     break;
  86.  
  87.       case '3':
  88.     both = 0;
  89.     break;
  90.  
  91.       default:
  92.     usage (1);
  93.       }
  94.  
  95.   if (show_version)
  96.     {
  97.       printf ("comm - %s\n", version_string);
  98.       exit (0);
  99.     }
  100.  
  101.   if (show_help)
  102.     usage (0);
  103.  
  104.   if (optind + 2 != argc)
  105.     usage (1);
  106.  
  107.   exit (compare_files (argv + optind));
  108. }
  109.  
  110. /* Compare INFILES[0] and INFILES[1].
  111.    If either is "-", use the standard input for that file.
  112.    Assume that each input file is sorted;
  113.    merge them and output the result.
  114.    Return 0 if successful, 1 if any errors occur. */
  115.  
  116. static int
  117. compare_files (infiles)
  118.      char **infiles;
  119. {
  120.   /* For each file, we have one linebuffer in lb1.  */
  121.   struct linebuffer lb1[2];
  122.  
  123.   /* thisline[i] points to the linebuffer holding the next available line
  124.      in file i, or is NULL if there are no lines left in that file.  */
  125.   struct linebuffer *thisline[2];
  126.  
  127.   /* streams[i] holds the input stream for file i.  */
  128.   FILE *streams[2];
  129.  
  130.   int i, ret = 0;
  131.  
  132.   /* Initialize the storage. */
  133.   for (i = 0; i < 2; i++)
  134.     {
  135.       initbuffer (&lb1[i]);
  136.       thisline[i] = &lb1[i];
  137.       streams[i] = strcmp (infiles[i], "-")
  138.     ? fopen (infiles[i], "r") : stdin;
  139.       if (!streams[i])
  140.     {
  141.       error (0, errno, "%s", infiles[i]);
  142.       return 1;
  143.     }
  144.  
  145.       thisline[i] = readline (thisline[i], streams[i]);
  146.     }
  147.  
  148.   while (thisline[0] || thisline[1])
  149.     {
  150.       int order;
  151.  
  152.       /* Compare the next available lines of the two files.  */
  153.  
  154.       if (!thisline[0])
  155.     order = 1;
  156.       else if (!thisline[1])
  157.     order = -1;
  158.       else
  159.     {
  160.       /* Cannot use bcmp -- it only returns a boolean value. */
  161.       order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
  162.               min (thisline[0]->length, thisline[1]->length));
  163.       if (order == 0)
  164.         order = thisline[0]->length - thisline[1]->length;
  165.     }
  166.  
  167.       /* Output the line that is lesser. */
  168.       if (order == 0)
  169.     writeline (thisline[1], stdout, 3);
  170.       else if (order > 0)
  171.     writeline (thisline[1], stdout, 2);
  172.       else
  173.     writeline (thisline[0], stdout, 1);
  174.  
  175.       /* Step the file the line came from.
  176.      If the files match, step both files.  */
  177.       if (order >= 0)
  178.     thisline[1] = readline (thisline[1], streams[1]);
  179.       if (order <= 0)
  180.     thisline[0] = readline (thisline[0], streams[0]);
  181.     }
  182.  
  183.   /* Free all storage and close all input streams. */
  184.   for (i = 0; i < 2; i++)
  185.     {
  186.       free (lb1[i].buffer);
  187.       if (ferror (streams[i]) || fclose (streams[i]) == EOF)
  188.     {
  189.       error (0, errno, "%s", infiles[i]);
  190.       ret = 1;
  191.     }
  192.     }
  193.   if (ferror (stdout) || fclose (stdout) == EOF)
  194.     {
  195.       error (0, errno, "write error");
  196.       ret = 1;
  197.     }
  198.   return ret;
  199. }
  200.  
  201. /* Output the line in linebuffer LINE to stream STREAM
  202.    provided the switches say it should be output.
  203.    CLASS is 1 for a line found only in file 1,
  204.    2 for a line only in file 2, 3 for a line in both. */
  205.  
  206. static void
  207. writeline (line, stream, class)
  208.      struct linebuffer *line;
  209.      FILE *stream;
  210.      int class;
  211. {
  212.   switch (class)
  213.     {
  214.     case 1:
  215.       if (!only_file_1)
  216.     return;
  217.       break;
  218.  
  219.     case 2:
  220.       if (!only_file_2)
  221.     return;
  222.       /* Skip the tab stop for case 1, if we are printing case 1.  */
  223.       if (only_file_1)
  224.     putc ('\t', stream);
  225.       break;
  226.  
  227.     case 3:
  228.       if (!both)
  229.     return;
  230.       /* Skip the tab stop for case 1, if we are printing case 1.  */
  231.       if (only_file_1)
  232.     putc ('\t', stream);
  233.       /* Skip the tab stop for case 2, if we are printing case 2.  */
  234.       if (only_file_2)
  235.     putc ('\t', stream);
  236.       break;
  237.     }
  238.  
  239.   fwrite (line->buffer, sizeof (char), line->length, stream);
  240.   putc ('\n', stream);
  241. }
  242.  
  243. static void
  244. usage (status)
  245.      int status;
  246. {
  247.   if (status != 0)
  248.     fprintf (stderr, "Try `%s --help' for more information.\n",
  249.          program_name);
  250.   else
  251.     {
  252.       printf ("\
  253. Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
  254. ",
  255.           program_name);
  256.       printf ("\
  257. \n\
  258.   -1              suppress lines unique to left file\n\
  259.   -2              suppress lines unique to right file\n\
  260.   -3              suppress lines unique to both files\n\
  261.       --help      display this help and exit\n\
  262.       --version   output version information and exit\n\
  263. ");
  264.     }
  265.   exit (status);
  266. }
  267.