/**********************************************************************
 * file.c -- file manipulation routines for lucy
 *
 * $Source$
 * $Author$
 * $Header$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 * For copying and distribution information, see the file
 * "mit-copyright.h".
 **********************************************************************/
#include "mit-copyright.h"

#ifndef lint
static char rcsid_file_c[] = "$Header$";
#endif /* lint */

#include <stdio.h>
#include <sys/param.h>
#include "lucy/lucy.h"
tfile unix_tfile();		/* XXX should be in discuss .h file */

/* lucy deals with small files, so we copy in DEV_BSIZE chunks instead
 * of BLKDEV_IOSIZE, thus saving stack space.
 */

long
lucy_append_file(file, appendixf)
     char *file, *appendixf;
{
  long code;
  FILE *f1, *f2;
  char buf[DEV_BSIZE];
  int nbytes;
  tfile tf;
  lucy_qident i;
  name_blk *nb;
  lucyqlist l;
  extern int errno;

  if (!(f2 = fopen(appendixf, "r"))) return((long) errno);
  if (!(f1 = fopen(file, "a"))) return((long) errno);

  do {
    if (nbytes = fread(buf, sizeof(char), DEV_BSIZE, f2)) {
      if (!fwrite(buf, sizeof(char), nbytes, f1)) {
	code = (long) errno;
	(void) fclose(f2);
	(void) fclose(f1);
	return(code);
      }
    }
  } while (nbytes == DEV_BSIZE);
  (void) fclose(f2);
  if (fclose(f1) == EOF) return((long) errno);
  return(0);
}

long
lucy_append_string(file, appendixs)
     char *file, *appendixs;
{
  long code = 0L;
  FILE *f1;
  extern int errno;

  if (!(f1 = fopen(file, "a"))) return((long) errno);
  if (!fwrite(appendixs, sizeof(char), strlen(appendixs), f1)) {
    code = (long) errno;
    (void) fclose(f1);
    return(code);
  }
  if (fclose(f1) == EOF) code = (long) errno;
  return(code);
}

lucy_chain_to_file(n, fp, delflag)
     int n;
     FILE *fp;
     int delflag;
{
  long code;
  lucy_qident i;
  lucyqlist l, l1;
  name_blk *nb;
  tfile tf;

  code = lucy_qnb(&nb);
  if (code) {
    return(code);
  }

  tf = unix_tfile(fp->_file);
  code = lucy_trn_info(n, &l);
  if (code) {
    LUCY_DEBUG((stderr, "Error from lucy_trn_info(%d)\n", n));
    return(code);
  }
  for(i = l->info.fref ; i <= l->info.lref && i ; i = l1->info.nref) {
    if (code = lucy_trn_info(i, &l1)) {
      return(code);
    }
    fprintf(fp, "--[%04d]----------------------------------------------------------------\n", i);
    fprintf(fp, "From: %s\n", l1->info.author);
    fprintf(fp, "Subject:  %s\n", l1->info.subject);
    fprintf(fp, "Date: %s\n\n", ctime(&(l1->info.date_entered)));
    fflush(fp);			/* dsc_get_trn doesn't user buffer */
    dsc_get_trn(nb, i, tf, &code);
    if (code) {
      LUCY_DEBUG((stderr, "Error from dsc_get_trn(%d)\n", i));
      return(code);
    }
    /* XXX this should really only be done after everything's archived */
    if (delflag) dsc_delete_trn(nb, i, &code);
  }
  return(0L);
}

/*
========================================================================
--[0000]----------------------------------------------------------------
| money                                   Thu Mar 21 15:06:31 EST 1991 |
| FAPR  daemon@ATHENA.MIT.EDU            jdoe@eagle.mit.edu (john doe) |
------------------------------------------------------------------------
*/
