/*  ocat.c --- concat overlapping files

 Copyright (C) 2004 by Walter C. Pelissero

 Author: Walter C. Pelissero <walter@pelissero.de>
 Project: catwlog
 $Id: ocat.c,v 1.2 2004/04/14 21:07:52 wcp Exp $

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

 Commentary:

 Concatenate a list of files considering that the end of a file may
 match the beginning of the next one.  This program skip the duplicate
 common part, thus avoiding repetition.  This is expecially useful to
 concatenate HTTP logs (either CLF or anything else).
*/

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#define until(x)	while (!(x))

static char RCSid[] = "$Id: ocat.c,v 1.2 2004/04/14 21:07:52 wcp Exp $";

static const char *myname;

/* Find in fd2 the end of the common part between fd1 and fd2.  Upon
   return fd2 position is at the end of the common part. */
static off_t
seek_end_of_common_part (FILE *fp1, FILE *fp2)
{
  char c1;
  off_t beginning = ftell(fp1);

  until ((c1 = getc(fp1)) == EOF)
    {
      char c2;

      if ((c2 = getc(fp2)) == EOF || c1 != c2)
	{
	  fseek(fp1, ++beginning, SEEK_SET);
	  fseek(fp2, 0, SEEK_SET);
	}
    }
  return ftell(fp2);
}

static void
copy_till_eof (FILE *input, FILE *output)
{
  char buf[1024];
  int l;

  until ((l = fread(buf, 1, sizeof(buf), input)) < 1)
    fwrite(buf, 1, l, output);
}

static FILE *
open_file (const char *name, const char *mode)
{
  FILE *fp = fopen(name, mode);

  if (fp)
    return fp;
  fprintf(stderr, "%s: unable to open file %s (%s)\n", myname, name, sys_errlist[errno]);
  exit(2);
}

static void
concat_files (FILE *fp1, const char **files, FILE *output_fp)
{
  if (*files)
    {
      FILE *fp2 = open_file(*files, "r");
      off_t pos = seek_end_of_common_part (fp1, fp2);
      fclose(fp1);
      copy_till_eof(fp2, output_fp);
      fseek(fp2, pos, SEEK_SET);
      concat_files (fp2, files + 1, output_fp);
    }
  else
    fclose(fp1);
}

static void
usage ()
{
  printf("%s\nusage: %s [-o] file ...\n", RCSid, myname);
  exit(1);
}

int
main (int argc, const char *argv[])
{
  int accumulate_mode = 0;

  myname = argv[0];

  if (argc > 1 && strcmp(argv[1], "-o") == 0)
    {
      accumulate_mode = 1;
      ++argv;
    }

  if (argv[1])
    {
      if (accumulate_mode)
	concat_files(open_file(argv[1], "r"), argv + 2, open_file(argv[1], "a"));
      else
	{
	  FILE *fp = open_file(argv[1], "r");

	  copy_till_eof(fp, stdout);
	  fseek(fp, 0, SEEK_SET);
	  concat_files(fp, argv + 2, stdout);
	}
    }
  else
    usage();
  return 0;
}
