home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / textutils / textutils-1.9-amiga / src / comm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  6.4 KB  |  276 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. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <getopt.h>
  33. #include <sys/types.h>
  34. #include "system.h"
  35. #include "linebuffer.h"
  36. #include "version.h"
  37.  
  38. #define min(x, y) ((x) < (y) ? (x) : (y))
  39.  
  40. /* The name this program was run with. */
  41. char *program_name;
  42.  
  43. /* If nonzero, print lines that are found only in file 1. */
  44. static int only_file_1;
  45.  
  46. /* If nonzero, print lines that are found only in file 2. */
  47. static int only_file_2;
  48.  
  49. /* If nonzero, print lines that are found in both files. */
  50. static int both;
  51.  
  52. /* If non-zero, display usage information and exit.  */
  53. static int show_help;
  54.  
  55. /* If non-zero, print the version on standard output then exit.  */
  56. static int show_version;
  57.  
  58. static struct option const long_options[] =
  59. {
  60.   {"help", no_argument, &show_help, 1},
  61.   {"version", no_argument, &show_version, 1},
  62.   {0, 0, 0, 0}
  63. };
  64.  
  65. void error ();
  66. static int compare_files ();
  67. static void writeline ();
  68. static void usage ();
  69.  
  70. main (argc, argv)
  71.      int argc;
  72.      char *argv[];
  73. {
  74.   int c;
  75.  
  76.   program_name = argv[0];
  77.  
  78.   only_file_1 = 1;
  79.   only_file_2 = 1;
  80.   both = 1;
  81.  
  82.   while ((c = getopt_long (argc, argv, "123", long_options, (int *) 0)) != EOF)
  83.     switch (c)
  84.       {
  85.       case 0:
  86.     break;
  87.  
  88.       case '1':
  89.     only_file_1 = 0;
  90.     break;
  91.  
  92.       case '2':
  93.     only_file_2 = 0;
  94.     break;
  95.  
  96.       case '3':
  97.     both = 0;
  98.     break;
  99.  
  100.       default:
  101.     usage (1);
  102.       }
  103.  
  104.   if (show_version)
  105.     {
  106.       printf ("%s\n", version_string);
  107.       exit (0);
  108.     }
  109.  
  110.   if (show_help)
  111.     usage (0);
  112.  
  113.   if (optind + 2 != argc)
  114.     usage (1);
  115.  
  116.   exit (compare_files (argv + optind));
  117. }
  118.  
  119. /* Compare INFILES[0] and INFILES[1].
  120.    If either is "-", use the standard input for that file.
  121.    Assume that each input file is sorted;
  122.    merge them and output the result.
  123.    Return 0 if successful, 1 if any errors occur. */
  124.  
  125. static int
  126. compare_files (infiles)
  127.      char **infiles;
  128. {
  129.   /* For each file, we have one linebuffer in lb1.  */
  130.   struct linebuffer lb1[2];
  131.  
  132.   /* thisline[i] points to the linebuffer holding the next available line
  133.      in file i, or is NULL if there are no lines left in that file.  */
  134.   struct linebuffer *thisline[2];
  135.  
  136.   /* streams[i] holds the input stream for file i.  */
  137.   FILE *streams[2];
  138.  
  139.   int i, ret = 0;
  140.  
  141.   /* Initialize the storage. */
  142.   for (i = 0; i < 2; i++)
  143.     {
  144.       initbuffer (&lb1[i]);
  145.       thisline[i] = &lb1[i];
  146.       streams[i] = strcmp (infiles[i], "-")
  147.     ? fopen (infiles[i], "r") : stdin;
  148.       if (!streams[i])
  149.     {
  150.       error (0, errno, "%s", infiles[i]);
  151.       return 1;
  152.     }
  153.  
  154.       thisline[i] = readline (thisline[i], streams[i]);
  155.     }
  156.  
  157.   while (thisline[0] || thisline[1])
  158.     {
  159.       int order;
  160.  
  161.       /* Compare the next available lines of the two files.  */
  162.  
  163.       if (!thisline[0])
  164.     order = 1;
  165.       else if (!thisline[1])
  166.     order = -1;
  167.       else
  168.     {
  169.       /* Cannot use bcmp -- it only returns a boolean value. */
  170.       order = memcmp (thisline[0]->buffer, thisline[1]->buffer,
  171.               min (thisline[0]->length, thisline[1]->length));
  172.       if (order == 0)
  173.         order = thisline[0]->length - thisline[1]->length;
  174.     }
  175.  
  176.       /* Output the line that is lesser. */
  177.       if (order == 0)
  178.     writeline (thisline[1], stdout, 3);
  179.       else if (order > 0)
  180.     writeline (thisline[1], stdout, 2);
  181.       else
  182.     writeline (thisline[0], stdout, 1);
  183.  
  184.       /* Step the file the line came from.
  185.      If the files match, step both files.  */
  186.       if (order >= 0)
  187.     thisline[1] = readline (thisline[1], streams[1]);
  188.       if (order <= 0)
  189.     thisline[0] = readline (thisline[0], streams[0]);
  190.     }
  191.  
  192.   /* Free all storage and close all input streams. */
  193.   for (i = 0; i < 2; i++)
  194.     {
  195.       free (lb1[i].buffer);
  196.       if (ferror (streams[i]) || fclose (streams[i]) == EOF)
  197.     {
  198.       error (0, errno, "%s", infiles[i]);
  199.       ret = 1;
  200.     }
  201.     }
  202.   if (ferror (stdout) || fclose (stdout) == EOF)
  203.     {
  204.       error (0, errno, "write error");
  205.       ret = 1;
  206.     }
  207.   return ret;
  208. }
  209.  
  210. /* Output the line in linebuffer LINE to stream STREAM
  211.    provided the switches say it should be output.
  212.    CLASS is 1 for a line found only in file 1,
  213.    2 for a line only in file 2, 3 for a line in both. */
  214.  
  215. static void
  216. writeline (line, stream, class)
  217.      struct linebuffer *line;
  218.      FILE *stream;
  219.      int class;
  220. {
  221.   switch (class)
  222.     {
  223.     case 1:
  224.       if (!only_file_1)
  225.     return;
  226.       break;
  227.  
  228.     case 2:
  229.       if (!only_file_2)
  230.     return;
  231.       /* Skip the tab stop for case 1, if we are printing case 1.  */
  232.       if (only_file_1)
  233.     putc ('\t', stream);
  234.       break;
  235.  
  236.     case 3:
  237.       if (!both)
  238.     return;
  239.       /* Skip the tab stop for case 1, if we are printing case 1.  */
  240.       if (only_file_1)
  241.     putc ('\t', stream);
  242.       /* Skip the tab stop for case 2, if we are printing case 2.  */
  243.       if (only_file_2)
  244.     putc ('\t', stream);
  245.       break;
  246.     }
  247.  
  248.   fwrite (line->buffer, sizeof (char), line->length, stream);
  249.   putc ('\n', stream);
  250. }
  251.  
  252. static void
  253. usage (status)
  254.      int status;
  255. {
  256.   if (status != 0)
  257.     fprintf (stderr, "Try `%s --help' for more information.\n",
  258.          program_name);
  259.   else
  260.     {
  261.       printf ("\
  262. Usage: %s [OPTION]... LEFT_FILE RIGHT_FILE\n\
  263. ",
  264.           program_name);
  265.       printf ("\
  266. \n\
  267.   -1              suppress lines unique to left file\n\
  268.   -2              suppress lines unique to right file\n\
  269.   -3              suppress lines unique to both files\n\
  270.       --help      display this help and exit\n\
  271.       --version   output version information and exit\n\
  272. ");
  273.     }
  274.   exit (status);
  275. }
  276.