/*     manxarcv.c -- unpack a Manx format archive
*
*     History:  8/2/85     Winkler     Created.
*
*/

#include <stdio.h>

#define MAXNAMELEN 100
#define CNTRLL 0x0c
#define CNTRLZ 0x1a

main(argc,argv)
       int argc; char *argv[];
{
       int c; FILE * arc;

       if (argc != 2)
       {
               fprintf(stderr, "Usage: manxarcv archivename\n");
               exit(1);
       }

       if ((arc = fopen(argv[1], "r")) == (FILE *) NULL)
       {
               fprintf(stderr, "Can't open %s\n", argv[1]);
               exit(1);
       }

       while ( (c = getc(arc)) != EOF )
       {
               switch( c )
               {
                       case CNTRLL : UnPackOneFile(arc) ; break ;
                       case CNTRLZ : (void) fclose(arc) ; exit(0) ;
                       default : fprintf(stderr, "Ignoring %c\n", c) ;
                               /* default case is only reached if archive
                                * is not in valid format
                                */
               }
       }
}

UnPackOneFile( arc )
       FILE * arc ;
{
       FILE * newFile ; char name[ MAXNAMELEN ] ; int c ;

       /* first read the file name */
               (void) fscanf( arc, "%s\n", name ) ;
               name[ strlen(name) ] = 0 ; /* throw away newline */
               printf("Extracting %s...", name) ;

       /* open the new file */
               if ( (newFile = fopen(name, "w")) == (FILE *) NULL )
               {
                       fprintf(stderr, "Can't write file %s\n", name) ;
                       exit(1) ;
               }

       /* copy everything up to the next ^L or ^Z into the new file */
               while ( (c = getc(arc)) != EOF )
               {
                       if ( c == CNTRLL || c == CNTRLZ )
                       {
                               (void) ungetc( c, arc ) ;
                               break ;
                       }
                       else (void) putc( c, newFile ) ;
               }

       /* close the new file */
               printf("Done.\n", name) ;
               (void) fclose( newFile ) ;
}