/*
  Extract V1.00 11.06.1995
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define english 1
/* english 1 for english error messages, anything else for german */

const char ver[]  = "$VER: Extract 1.00 (11.06.1995)";


void usage()
{   printf("Usage: Extract <infile> <outfile> <from:bytes> <to:bytes>\n");
}

void fin_error()
#if english == 1
{   printf("Can't open input file.\n"); }
#else
{   printf("Kann Eingabedatei nicht öffnen.\n"); }
#endif

void fout_error()
#if english == 1
{   printf("Can't open output file.\n"); }
#else
{   printf("Kann Ausgabedatei nicht öffnen.\n"); }
#endif


void fout_exits()
#if english == 1
{   printf("output file exists!\n"); }
#else
{   printf("Ausgabedatei existiert schon!\n"); }
#endif

main(argc,argv)
int argc;
char *argv[];
{   int    i,a,b;
    FILE *in,*out;
    char ch;

    printf ("%s\n",ver+6);
    printf ("(C)1995 Christian Steigies (steigies@physik.uni-kiel.d400.de)\n");
    printf ("Freeware, NO commercial usage ;-)\n\n");

    if  (argc!=5)
    {   atexit(&usage);
        exit(EXIT_SUCCESS);
    }

    if  ((in=fopen(argv[1],"r"))==0) /* Eingabefile zum lesen öffnen */
    {   atexit(&usage);
        atexit(&fin_error);
        exit(EXIT_SUCCESS);
    }

    a=atoi(argv[3]);
    b=atoi(argv[4]);
    if  (a<1)

#if english == 1
    {   printf("<from> must be >= 1 !\n");
#else
    {   printf("<from> muss >= 1 sein!\n");
#endif
        atexit(&usage);
        exit(EXIT_SUCCESS);
    }
    else if  (b<a)

#if english == 1
    {   printf("<to> must be >= <from> !\n");
#else
    {   printf("<to> muss >= <from> sein!\n");
#endif
        atexit(&usage);
        exit(EXIT_SUCCESS);
    }

    if  ((out=fopen(argv[2],"r"))==0) /* Ausgabefile öffnen */
    {
        if  ((out=fopen(argv[2],"w"))==0)
        {   atexit(&usage); atexit(&fout_error); exit(EXIT_SUCCESS); }
    }
    else /* file existiert schon! */
    {   atexit(&usage); atexit(&fout_exits); exit(EXIT_SUCCESS); }

    printf("extracting %s from %i to %i into file %s\n",argv[1],a,b,argv[2]);

    i=0;
    do
    {   ch=getc(in);
        i++;
        if  ((i>=a) && (i<=b) && !feof(in)) fputc(ch,out);
    }   while   (!feof(in));
    fclose(in);
    printf("Ready.\n");
    exit(EXIT_SUCCESS);

}
