/**********************************************************************
 * luc_copy_file.c -- copy a file
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_copy_file.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_copy_file.c,v 1.2 91/09/24 14:33:39 brlewis Exp Locker: brlewis $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_luc_copy_file_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_copy_file.c,v 1.2 91/09/24 14:33:39 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <lucy/lucy.h>
#include <sys/errno.h>

/**********************************************************************
 * luc_copy_file(char *fname1, char *fname2)
 *
 * - copys file named fname1 to file named fname2
 * - returns error code
 **********************************************************************/

long
luc_copy_file(fname1, fname2)
     char *fname1, *fname2;
{
  long code;
  FILE *f1, *f2;
  char buf[BUFSIZ];
  int nbytes;

  if (!(f2 = fopen(fname1, "r"))) return((long) errno);
  if (!(f1 = fopen(fname2, "w"))) return((long) errno);

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

long
luc_insert_file(fp, fname)
     FILE *fp;
     char *fname;
{
  long code;
  FILE *f2;
  char buf[BUFSIZ];
  int nbytes;

  if (!(f2 = fopen(fname, "r"))) return((long) errno);

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