[alt.sources] useful utility when out of disk space

webber@athos.rutgers.edu (Bob Webber) (01/06/89)

/* INSERT is a program that takes two files as arguments and writes
   the contents of the first onto the second.  It can be built by
   putting this stuff in a file called INSERT.c and then typing
   make INSERT.  If this doesn't work you are on your own. */


#include <stdio.h>
#include <errno.h>
#include <fcntl.h> 
#include <sys/types.h>
#include <sys/file.h>
#include <sys/param.h>

main(argc,argv)
     int argc;
     char *argv[];
{
  int fdin, fdout;
  int ReadExit;
  int WriteExit;
  int SeekExit;
  int ReadSize;
  
  char chs[MAXBSIZE];

  if (argc != 3) {
    fprintf(stderr,"Error 1 [%d]: Read the source before using\n",argc);
    exit(1);
  }
  if ((fdin = open(argv[1],O_RDONLY)) == -1) {
    perror("Error 2 : Read the source before using:");
    exit(2);
  }
  if ((SeekExit = lseek(fdin,0,L_SET)) != 0) {
    perror("Error 3 : Read the source before using:");
    exit(3);
  }
  if ((fdout = open(argv[2],O_WRONLY)) == -1) {
    perror("Error 4 : Read the source before using:");
    exit(4);
  }
  if ((SeekExit = lseek(fdout,0,L_SET)) != 0) {
    perror("Error 5 : Read the source before using:");
    exit(5);
  }
  ReadSize = 0;
  while ((ReadExit = read(fdin,chs,MAXBSIZE)) >0) {
    ReadSize += ReadExit;
    if ((WriteExit = write(fdout,chs,ReadExit)) != ReadExit) {
      perror("Error 6 : Read the source before using:");
      exit(6);
    }
  }
  if (ReadExit == 0) { /* reached end of input file */
    if (ftruncate(fdout,ReadSize) != 0) {
      perror("Error 7 : Read the source before using:");
      exit(7);
    }
  } else {
    perror("Error 8 : Read the source before using:");
    exit(8);
  }
  exit(0);
}