[net.sources] ARC for BSD4.2

turner@imagen.UUCP (08/05/86)

This is my hacked version of the arc program that was posted to the net, I
have modified it to run under Unix BSD4.2. if you want it to run under 
SVR2 you probably will have to change the opendir, scandir, and closedir
calls, I don't really know System 5. IMPORTANT: this is a BETA test version
i doubt severly that there are no bugs, please report any that you find to me.

1) I don't have a PC handy so i have no idea if it still works on the PC; to
	compile it for the PC edit arc.h and arcs.c and change the #defines,
	the define ST is for the Atari 520ST and with luck that will be done
	shortly. If you compile it for the PC you will need the routines for 
	opendir etc. that are in shar part 5, otherwise you can ignore that 
	shar file.
2) the Makefile is configured to compile for debugging (-g) (shows ya what 
	kind of faith i have in my code (:-)
3) everything seems to work for both arc files created on the PC and on BSD4.2
	with the exception of the t (test) option which gives strange results,
	i suspect the problem is in the code not the archive
4) there is one bug that i know of, if you archive a 0 length file, arc will
	blow up (floating point exception) when you try to extract it; the
	question is should the check happen when you add the file, extract
	it, or should if create a 0 length file ? you decide.
5) send flames etc. to me, because of the way postnews works here my signature
	will probably be at the end of each shar file, if not its:
----
Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL
----

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	arc.h
#	arcm.h
#	arcs.h
#	arc.c
#	arcadd.c
#	arccode.c
#	arccvt.c
#	arcdel.c
#	arcdir.c
#	arcdos.c
#	Makefile
# This archive created: Mon Aug  4 14:42:58 1986
# By:	D'arc Angel (The Houses of the Holy)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'arc.h'" '(2153 characters)'
if test -f 'arc.h'
then
	echo shar: "will not over-write existing file 'arc.h'"
else
sed 's/^	X//' << \SHAR_EOF > 'arc.h'
	X/*
	X * $Header: arc.h,v 1.2 86/07/15 07:52:34 turner Exp $
	X */
	X
	X/*
	X * $Log:	arc.h,v $
	X * Revision 1.2  86/07/15  07:52:34  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:01:23  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X#define	ST	0	/* Atari 520ST or 1040 		 */
	X#define BSD	1	/* BSD4.2 on a vax		 */
	X#define MSDOS	0	/* MSDOS on an IBM PC or Wannabe */
	X
	X#if ST
	X#define EXTERN
	X#define INT short
	X#endif
	X
	X#if BSD
	X#include <ctype.h>	/* for isupper etc.                  */
	X#define EXTERN
	X#define INT short
	X#define envfind getenv
	X#endif
	X
	X#if MSDOS
	X#define EXTERN extern
	X#define INT int
	X#endif
	X
	X/*
	X * added macro def's in C format 6/26/86 jmt 
	X */
	X#include "arcm.h"
	X
	X/*  ARC - Archive utility - ARC Header
	X
	X    Version 2.14, created on 02/03/86 at 22:48:29
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description: 
	X         This is the header file for the ARC archive utility.  It defines
	X         global parameters and the references to the external data.
	X
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X */
	X
	X#include "arcs.h"
	X
	XEXTERN INT keepbak;             /* true if saving the old archive */
	XEXTERN INT warn;                /* true to print warnings */
	XEXTERN INT note;                /* true to print comments */
	XEXTERN INT bose;                /* true to be verbose */
	XEXTERN INT nocomp;              /* true to suppress compression */
	XEXTERN INT kludge;              /* kludge flag */
	XEXTERN char *arctemp;        /* arc temp file prefix */
	XEXTERN char *password;       /* encryption password pointer */
	XEXTERN INT nerrs;               /* number of errors encountered */
	X
	XEXTERN char hdrver;                      /* header version */
	X
	XEXTERN FILE *arc;                        /* the old archive */
	XEXTERN FILE *new;                        /* the new archive */
	X
	XEXTERN char arcname[100];           /* storage for archive name */
	XEXTERN char bakname[100];           /* storage for backup copy name */
	XEXTERN char newname[100];           /* storage for new archive name */
	XEXTERN unsigned INT arcdate;    /* archive date stamp */
	XEXTERN unsigned INT arctime;    /* archive time stamp */
SHAR_EOF
if test 2153 -ne "`wc -c < 'arc.h'`"
then
	echo shar: "error transmitting 'arc.h'" '(should have been 2153 characters)'
fi
fi
echo shar: "extracting 'arcm.h'" '(695 characters)'
if test -f 'arcm.h'
then
	echo shar: "will not over-write existing file 'arcm.h'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcm.h'
	X/*
	X * $Header: arcm.h,v 1.2 86/07/15 07:53:40 turner Exp $
	X */
	X
	X/*
	X * $Log:	arcm.h,v $
	X * Revision 1.2  86/07/15  07:53:40  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:01:25  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*
	X *
	X * ARC archive utility - standard MACRO insert file.
	X *
	X * parameters:
	X *
	X */
	X
	X#define ARCMARK 26 /*                   special archive marker        */
	X#define ARCVER 8   /*                   archive header version code   */
	X#define STRLEN 100 /*                   system standard string length */
	X#define FNLEN 13   /*                   file name length              */
	X#define MAXARG 25  /*                   maximum number of arguments   */
	X
	X#define ARC 1
	X#define XARC 0
SHAR_EOF
if test 695 -ne "`wc -c < 'arcm.h'`"
then
	echo shar: "error transmitting 'arcm.h'" '(should have been 695 characters)'
fi
fi
echo shar: "extracting 'arcs.h'" '(1965 characters)'
if test -f 'arcs.h'
then
	echo shar: "will not over-write existing file 'arcs.h'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcs.h'
	X/*
	X * $Header: arcs.h,v 1.2 86/07/15 07:54:02 turner Exp $
	X */
	X
	X/*
	X * $Log:	arcs.h,v $
	X * Revision 1.2  86/07/15  07:54:02  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:01:27  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - Archive file header format
	X
	X    Version 2.12, created on 12/17/85 at 14:40:26
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file defines the format of an archive file header, excluding
	X         the archive marker and the header version number.
	X
	X         Each entry in an archive begins with a one byte archive marker,
	X         which is set to 26.  The marker is followed by a one byte
	X         header type code, from zero to 7.
	X
	X         If the header type code is zero, then it is an end marker, and
	X         no more data should be read from the archive.
	X
	X         If the header type code is in the range 2 to 7, then it is
	X         followed by a standard archive header, which is defined below.
	X
	X         If the header type code is one, then it is followed by an older
	X         format archive header.  The older format header does not contain
	X         the true length.  A header should be read for a length of
	X         sizeof(struct heads)-sizeof(long).  Then set length equal to size
	X         and change the header version to 2.
	X
	X    Programming note:
	X         The crc value given in the header is based on the unpacked data.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X
	Xstruct heads                           /* archive entry header format */
	X{   char name[13];                 /* file name */
	X    long size;                         /* size of file, in bytes */
	X    unsigned INT date;                 /* creation date */
	X    unsigned INT time;                 /* creation time */
	X INT crc;                           /* cyclic redundancy check */
	X    long length;                       /* true file length */
	X}   ;
SHAR_EOF
if test 1965 -ne "`wc -c < 'arcs.h'`"
then
	echo shar: "error transmitting 'arcs.h'" '(should have been 1965 characters)'
fi
fi
echo shar: "extracting 'arc.c'" '(9644 characters)'
if test -f 'arc.c'
then
	echo shar: "will not over-write existing file 'arc.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arc.c'
	Xstatic char *RCSid = "$Header: arc.c,v 1.2 86/07/15 07:52:04 turner Exp $";
	X
	X/*
	X * $Log:	arc.c,v $
	X * Revision 1.2  86/07/15  07:52:04  turner
	X * first working version for the vax
	X * 
	X * Revision 1.1  86/06/26  14:59:15  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =5.12), created on $tag(
	XTED_DATE DB =02/05/86) at $tag(
	XTED_TIME DB =22:22:01))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This program is a general archive utility, and is used to maintain
	X         an archive of files.  An "archive" is a single file that combines
	X         many files, reducing storage space and allowing multiple files to
	X         be handled as one.
	X
	X    Instructions:
	X         Run this program with no arguments for complete instructions.
	X
	X    Programming notes:
	X         ARC Version 2 differs from version 1 in that archive entries
	X         are automatically compressed when they are added to the archive,
	X         making a separate compression step unecessary.  The nature of the
	X         compression is indicated by the header version number placed in
	X         each archive entry, as follows:
	X
	X         1 = Old style, no compression
	X         2 = New style, no compression
	X         3 = Compression of repeated characters only
	X         4 = Compression of repeated characters plus Huffman SQueezing
	X         5 = Lempel-Zev packing of repeated strings (old style)
	X         6 = Lempel-Zev packing of repeated strings (new style)
	X         7 = Lempel-Zev Williams packing with improved has function
	X         8 = Dynamic Lempel-Zev packing with adaptive reset
	X
	X         Type 5, Lempel-Zev packing, was added as of version 4.0
	X
	X         Type 6 is Lempel-Zev packing where runs of repeated characters
	X         have been collapsed, and was added as of version 4.1
	X
	X         Type 7 is a variation of Lempel-Zev using a different hash
	X         function which yields speed improvements of 20-25%, and was
	X         added as of version 4.6
	X
	X         Type 8 is a different implementation of Lempel-Zev, using a
	X         variable code size and an adaptive block reset, and was added
	X         as of version 5.0
	X
	X         Verion 4.3 introduced a temporary file for holding the result
	X         of the first crunch pass, thus speeding up crunching.
	X
	X         Version 4.4 introduced the ARCTEMP environment string, so that
	X         the temporary crunch file may be placed on a ramdisk.  Also
	X         added was the distinction bewteen Adding a file in all cases,
	X         and Updating a file only if the disk file is newer than the
	X         corresponding archive entry.
	X
	X         The compression method to use is determined when the file is
	X         added, based on whichever method yields the smallest result.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xmain(num,arg)                          /* system entry point */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	X{
	X    char opt = 0;                      /* selected action */
	X    char *a;                           /* option pointer */
	X    char *makefnam();                  /* filename fixup routine */
	X    char *upper();                     /* case conversion routine */
	X    char *index();                     /* string index utility */
	X    char *envfind();                   /* environment searcher */
	X int n;                             /* argument index */
	X
	X    if(num<3)
	X    {    printf("ARC - Archive utility, $version\n");
	X         printf("(C) COPYRIGHT 1985,86 by System Enhancement Associates;");
	X         printf(" ALL RIGHTS RESERVED\n\n");
	X         printf("Please refer all inquiries to:\n\n");
	X         printf("       System Enhancement Associates\n");
	X         printf("       21 New Street, Wayne NJ 07470\n\n");
	X         printf("You may copy and distribute this program freely,");
	X         printf(" provided that:\n");
	X         printf("    1)   No fee is charged for such copying and");
	X         printf(" distribution, and\n");
	X         printf("    2)   It is distributed ONLY in its original,");
	X         printf(" unmodified state.\n\n");
	X         printf("If you like this program, and find it of use, then your");
	X         printf(" contribution will\n");
	X         printf("be appreciated.  You may not use this product in a");
	X         printf(" commercial environment\n");
	X         printf("or a governmental organization without paying a license");
	X         printf(" fee of $35.  Site\n");
	X         printf("licenses and commercial distribution licenses are");
	X         printf(" available.  A program\n");
	X         printf("disk and printed documentation are available for $50.\n");
	X         printf("\nIf you fail to abide by the terms of this license, ");
	X         printf(" then your conscience\n");
	X         printf("will haunt you for the rest of your life.\n\n");
	X         printf("Usage: ARC {amufdxerplvtc}[bswn][g<password>]");
	X         printf(" <archive> [<filename> . . .]\n");
	X         printf("Where:   a   = add files to archive\n");
	X         printf("         m   = move files to archive\n");
	X         printf("         u   = update files in archive\n");
	X         printf("         f   = freshen files in archive\n");
	X         printf("         d   = delete files from archive\n");
	X         printf("         x,e = extract files from archive\n");
	X         printf("         r   = run files from archive\n");
	X         printf("         p   = copy files from archive to");
	X         printf(" standard output\n");
	X         printf("         l   = list files in archive\n");
	X         printf("         v   = verbose listing of files in archive\n");
	X         printf("         t   = test archive integrity\n");
	X         printf("         c   = convert entry to new packing method\n");
	X         printf("         b   = retain backup copy of archive\n");
	X         printf("         s   = suppress compression (store only)\n");
	X         printf("         w   = suppress warning messages\n");
	X         printf("         n   = suppress notes and comments\n");
	X         printf("         g   = Encrypt/decrypt archive entry\n");
	X         printf("\nPlease refer to the program documentation for");
	X         printf(" complete instructions.\n");
	X         return 1;
	X    }
	X
	X    /* see where temp files go */
	X
	X    if(!(arctemp = envfind("ARCTEMP")))
	X         arctemp = envfind("TEMP");
	X
	X#if MSDOS 
	X    /* avoid any case problems with arguments */
	X
	X    for(n=1; n<num; n++)               /* for each argument */
	X         upper(arg[n]);                /* convert it to uppercase */
	X#endif
	X#if BSD | ST
	X    /* avoid any case problems with command options */
	X
	X         upper(arg[1]);                /* convert it to uppercase */
	X#endif
	X
	X    /* create archive names, supplying defaults */
	X
	X#if MSDOS
	X    makefnam(arg[2],".ARC",arcname);
	X#endif
	X#if BSD | ST
	X    makefnam(arg[2],".arc",arcname);
	X#endif
	X    makefnam(".$$$$",arcname,newname);
	X    makefnam(".BAK",arcname,bakname);
	X
	X    /* now scan the command and see what we are to do */
	X
	X    for(a=arg[1]; *a; a++)             /* scan the option flags */
	X    {    if(index("AMUFDXEPLVTCR",*a)) /* if a known command */
	X         {    if(opt)                  /* do we have one yet? */
	X                   abort("Cannot mix %c and %c",opt,*a);
	X              opt = *a;                /* else remember it */
	X         }
	X
	X         else if(*a=='B')              /* retain backup copy */
	X              keepbak = 1;
	X
	X         else if(*a=='W')              /* suppress warnings */
	X              warn = 0;
	X
	X         else if(*a=='N')              /* suppress notes and comments */
	X              note = 0;
	X
	X         else if(*a=='G')              /* garble */
	X         {    password = a+1;
	X              while(*a)
	X                   a++;
	X              a--;
	X         }
	X
	X         else if(*a=='S')              /* storage kludge */
	X              nocomp = 1;
	X
	X         else if(*a=='K')              /* special kludge */
	X              kludge = 1;
	X
	X         else if(*a=='-' || *a=='/')   /* UNIX and PC-DOS option markers */
	X              ;
	X
	X         else abort("%c is an unknown command",*a);
	X    }
	X
	X    if(!opt)
	X         abort("I have nothing to do!");
	X
	X    /* act on whatever action command was given */
	X
	X    switch(opt)                        /* action depends on command */
	X    {
	X    case 'A':                          /* Add */
	X    case 'M':                          /* Move */
	X    case 'U':                          /* Update */
	X    case 'F':                          /* Freshen */
	X         addarc(num-3,&arg[3],(opt=='M'),(opt=='U'),(opt=='F'));
	X         break;
	X
	X    case 'D':                          /* Delete */
	X         delarc(num-3,&arg[3]);
	X         break;
	X
	X    case 'E':                          /* Extract */
	X    case 'X':                          /* eXtract */
	X    case 'P':                          /* Print */
	X         extarc(num-3,&arg[3],(opt=='P'));
	X         break;
	X
	X    case 'V':                          /* Verbose list */
	X         bose = 1;
	X    case 'L':                          /* List */
	X         lstarc(num-3,&arg[3]);
	X         break;
	X
	X    case 'T':                          /* Test */
	X         tstarc();
	X         break;
	X
	X    case 'C':                          /* Convert */
	X         cvtarc(num-3,&arg[3]);
	X         break;
	X
	X    case 'R':                          /* Run */
	X         runarc(num-3,&arg[3]);
	X         break;
	X
	X    default:
	X         abort("I don't know how to do %c yet!",opt);
	X    }
	X
	X    return nerrs;
	X}
SHAR_EOF
if test 9644 -ne "`wc -c < 'arc.c'`"
then
	echo shar: "error transmitting 'arc.c'" '(should have been 9644 characters)'
fi
fi
echo shar: "extracting 'arcadd.c'" '(10835 characters)'
if test -f 'arcadd.c'
then
	echo shar: "will not over-write existing file 'arcadd.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcadd.c'
	Xstatic char *RCSid = "$Header: arcadd.c,v 1.2 86/07/15 07:52:37 turner Exp $";
	X
	X/*
	X * $Log:	arcadd.c,v $
	X * Revision 1.2  86/07/15  07:52:37  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  14:59:37  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCADD
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =3.39), created on $tag(
	XTED_DATE DB =02/05/86) at $tag(
	XTED_TIME DB =22:21:53))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to add files to an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xaddarc(num,arg,move,update,fresh)      /* add files to archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	Xint move;                              /* true if moving file */
	Xint update;                            /* true if updating */
	Xint fresh;                             /* true if freshening */
	X{
	X    char *d, *dir();                   /* directory junk */
	X    char buf[STRLEN];                 /* pathname buffer */
	X    char **path = NULL;                /* pointer to pointers to paths */
	X    char **name = NULL;                /* pointer to pointers to names */
	X int nfiles = 0;                    /* number of files in lists */
	X int notemp;                        /* true until a template works */
	X int nowork = 1;                    /* true until files are added */
	X    char *i, *rindex();                /* string indexing junk */
	X    char *malloc(), *realloc();         /* memory allocators */
	X int m, n;                          /* indices */
	X    unsigned int coreleft();           /* remaining memory reporter */
	X
	X    if(num<1)                          /* if no files named */
	X    {    num = 1;                      /* then fake one */
	X         arg[0] = "*.*";               /* add everything */
	X    }
	X
	X    for(n=0; n<num; n++)               /* for each template supplied */
	X    {    strcpy(buf,arg[n]);           /* get ready to fix path */
	X         if(!(i=rindex(buf,'\\')))
	X              if(!(i=rindex(buf,'/')))
	X                   if(!(i=rindex(buf,':')))
	X                        i = buf-1;
	X         i++;                          /* pointer to where name goes */
	X
	X         notemp = 1;                   /* reset files flag */
	X         for(d=dir(arg[n],0); *d; d=dir(NULL,0))
	X         {    notemp = 0;              /* template is giving results */
	X              nfiles++;                /* add each matching file */
	X              path = (char **)realloc(path,nfiles*sizeof(char **));
	X              name = (char **)realloc(name,nfiles*sizeof(char **));
	X              strcpy(i,d);             /* put name in path */
	X              path[nfiles-1] = malloc(strlen(buf)+1);
	X              strcpy(path[nfiles-1],buf);
	X              name[nfiles-1] = d;      /* save name */
	X
	X#if MSDOS
	X              if(coreleft()<5120)
	X              {    nfiles = addbunch(nfiles,path,name,move,update,fresh);
	X                   nowork = nowork && !nfiles;
	X                   while(nfiles)
	X                   {    free(path[--nfiles]);
	X                        free(name[nfiles]);
	X                   }
	X                   free(path); free(name);
	X                   path = name = NULL;
	X              }
	X#endif
	X         }
	X         if(notemp && warn)
	X              printf("No files match: %s\n",arg[n]);
	X    }
	X
	X    if(nfiles)
	X    {    nfiles = addbunch(nfiles,path,name,move,update,fresh);
	X         nowork = nowork && !nfiles;
	X         while(nfiles)
	X         {    free(path[--nfiles]);
	X              free(name[nfiles]);
	X         }
	X         free(path); free(name);
	X    }
	X
	X    if(nowork && warn)
	X         printf("No files were added.\n");
	X}
	X
	Xint addbunch(nfiles,path,name,move,update,fresh) /* add a bunch of files */
	Xint nfiles;                            /* number of files to add */
	Xchar **path;                           /* pointers to pathnames */
	Xchar **name;                           /* pointers to filenames */
	Xint move;                              /* true if moving file */
	Xint update;                            /* true if updating */
	Xint fresh;                             /* true if freshening */
	X{
	X    char buf[STRLEN];                 /* pathname buffer */
	X int m, n;                          /* indices */
	X    char *d;                           /* swap pointer */
	X    struct heads hdr;                  /* file header data storage */
	X
	X    for(n=0; n<nfiles-1; n++)          /* sort the list of names */
	X    {    for(m=n+1; m<nfiles; m++)
	X         {    if(strcmp(name[n],name[m])>0)
	X              {    d = path[n];
	X                   path[n] = path[m];
	X                   path[m] = d;
	X                   d = name[n];
	X                   name[n] = name[m];
	X                   name[m] = d;
	X              }
	X         }
	X    }
	X
	X    for(n=0; n<nfiles-1; )             /* consolidate the list of names */
	X    {    if(!strcmp(path[n],path[n+1]) /* if duplicate names */
	X         || !strcmp(path[n],arcname)   /* or this archive */
	X         || !strcmp(path[n],newname)   /* or the new version */
	X         || !strcmp(path[n],bakname))  /* or its backup */
	X         {    free(path[n]);           /* then forget the file */
	X              free(name[n]);
	X              for(m=n; m<nfiles-1; m++)
	X              {    path[m] = path[m+1];
	X                   name[m] = name[m+1];
	X              }
	X              nfiles--;
	X         }
	X         else n++;                     /* else test the next one */
	X    }
	X
	X    if(!strcmp(path[n],arcname)        /* special check for last file */
	X    || !strcmp(path[n],newname)        /* courtesy of Rick Moore */
	X    || !strcmp(path[n],bakname))
	X    {    free(path[n]);
	X         free(name[n]);
	X         nfiles--;
	X    }
	X
	X    if(!nfiles)                        /* make sure we got some */
	X         return 0;
	X
	X    for(n=0; n<nfiles-1; n++)          /* watch out for duplicate names */
	X         if(!strcmp(name[n],name[n+1]))
	X              abort("Duplicate filenames:\n  %s\n  %s",path[n],path[n+1]);
	X
	X    openarc(1);                        /* open archive for changes */
	X
	X    for(n=0; n<nfiles; n++)            /* add each file in the list */
	X         addfile(path[n],name[n],update,fresh);
	X
	X    /* now we must copy over all files that follow our additions */
	X
	X    while(readhdr(&hdr,arc))           /* while more entries to copy */
	X    {    writehdr(&hdr,new);
	X         filecopy(arc,new,hdr.size);
	X    }
	X    hdrver = 0;                        /* archive EOF type */
	X    writehdr(&hdr,new);                /* write out our end marker */
	X    closearc(1);                       /* close archive after changes */
	X
	X    if(move)                           /* if this was a move */
	X    {    for(n=0; n<nfiles; n++)       /* then delete each file added */
	X         {    if(unlink(path[n]) && warn)
	X              {    printf("Cannot unsave %s\n",path[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X
	X    return nfiles;                     /* say how many were added */
	X}
	X
	Xstatic addfile(path,name,update,fresh) /* add named file to archive */
	Xchar *path;                            /* path name of file to add */
	Xchar *name;                            /* name of file to add */
	Xint update;                            /* true if updating */
	Xint fresh;                             /* true if freshening */
	X{
	X    struct heads nhdr;                 /* data regarding the new file */
	X    struct heads ohdr;                 /* data regarding an old file */
	X    FILE *f, *fopen();                 /* file to add, opener */
	X    long starts, ftell();              /* file locations */
	X int c;                             /* one char of file */
	X int upd = 0;                       /* true if replacing an entry */
	X
	X    if(!(f=fopen(path,"rb")))
	X    {    if(warn)
	X         {    printf("Cannot read file: %s\n",path);
	X              nerrs++;
	X         }
	X         return;
	X    }
	X
	X    strcpy(nhdr.name,name);            /* save name */
	X    nhdr.size = 0;                     /* clear out size storage */
	X    nhdr.crc = 0;                      /* clear out CRC check storage */
	X    getstamp(f,&nhdr.date,&nhdr.time);
	X
	X    /* position archive to spot for new file */
	X
	X    if(arc)                            /* if adding to existing archive */
	X    {    starts = ftell(arc);          /* where are we? */
	X         while(readhdr(&ohdr,arc))     /* while more files to check */
	X         {    if(!strcmp(ohdr.name,nhdr.name))
	X              {    upd = 1;            /* replace existing entry */
	X                   if(update || fresh) /* if updating or freshening */
	X                   {    if(nhdr.date<ohdr.date
	X                        || (nhdr.date==ohdr.date && nhdr.time<=ohdr.time))
	X                        {    fseek(arc,starts,0);
	X                             fclose(f);
	X                             return;   /* skip if not newer */
	X                        }
	X                   }
	X              }
	X
	X              if(strcmp(ohdr.name,nhdr.name)>=0)
	X                   break;              /* found our spot */
	X
	X              writehdr(&ohdr,new);     /* entry preceeds update; keep it */
	X              filecopy(arc,new,ohdr.size);
	X              starts = ftell(arc);     /* now where are we? */
	X         }
	X
	X         if(upd)                       /* if an update */
	X         {    if(note)
	X                   printf("Updating file: %-12s  ",name);
	X              fseek(arc,ohdr.size,1);
	X         }
	X         else if(fresh)                /* else if freshening */
	X         {    fseek(arc,starts,0);     /* then do not add files */
	X              fclose(f);
	X              return;
	X         }
	X         else                          /* else adding a new file */
	X         {    if(note)
	X                   printf("Adding file:   %-12s  ",name);
	X              fseek(arc,starts,0);     /* reset for next time */
	X         }
	X    }
	X
	X    else                               /* no existing archive */
	X    {    if(fresh)                     /* cannot freshen nothing */
	X         {    fclose(f);
	X              return;
	X         }
	X         else if(note)                 /* else adding a file */
	X              printf("Adding file:   %-12s  ",name);
	X    }
	X
	X    starts = ftell(new);               /* note where header goes */
	X    hdrver = ARCVER;                  /* anything but end marker */
	X    writehdr(&nhdr,new);               /* write out header skeleton */
	X    pack(f,new,&nhdr);                 /* pack file into archive */
	X    fseek(new,starts,0);               /* move back to header skeleton */
	X    writehdr(&nhdr,new);               /* write out real header */
	X    fseek(new,nhdr.size,1);            /* skip over data to next header */
	X    fclose(f);                         /* all done with the file */
	X}
SHAR_EOF
if test 10835 -ne "`wc -c < 'arcadd.c'`"
then
	echo shar: "error transmitting 'arcadd.c'" '(should have been 10835 characters)'
fi
fi
echo shar: "extracting 'arccode.c'" '(1724 characters)'
if test -f 'arccode.c'
then
	echo shar: "will not over-write existing file 'arccode.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arccode.c'
	Xstatic char *RCSid = "$Header: arccode.c,v 1.1 86/06/26 14:59:53 turner Exp $";
	X
	X/*
	X * $Log:	arccode.c,v $
	X * Revision 1.1  86/06/26  14:59:53  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCCODE
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =1.02), created on $tag(
	XTED_DATE DB =01/20/86) at $tag(
	XTED_TIME DB =13:33:35))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to encrypt and decrypt
	X         data in an archive.  The encryption method is nothing fancy,
	X         being just a routine XOR, but it is used on the packed data,
	X         and uses a variable length key.  The end result is something
	X         that is in theory crackable, but I'd hate to try it.  It should
	X         be more than sufficient for casual use.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xstatic char *p;                        /* password pointer */
	X
	Xsetcode()                              /* get set for encoding/decoding */
	X{
	X    p = password;                      /* reset password pointer */
	X}
	X
	Xint code(c)                            /* encode some character */
	Xint c;                                 /* character to encode */
	X{
	X    if(p)                              /* if password is in use */
	X    {    if(!*p)                       /* if we reached the end */
	X              p = password;            /* then wrap back to the start */
	X         return c^*p++;                /* very simple here */
	X    }
	X    else return c;                     /* else no encryption */
	X}
SHAR_EOF
if test 1724 -ne "`wc -c < 'arccode.c'`"
then
	echo shar: "error transmitting 'arccode.c'" '(should have been 1724 characters)'
fi
fi
echo shar: "extracting 'arccvt.c'" '(4450 characters)'
if test -f 'arccvt.c'
then
	echo shar: "will not over-write existing file 'arccvt.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arccvt.c'
	Xstatic char *RCSid = "$Header: arccvt.c,v 1.2 86/07/15 07:52:46 turner Exp $";
	X
	X/*
	X * $Log:	arccvt.c,v $
	X * Revision 1.2  86/07/15  07:52:46  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  14:59:56  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCCVT
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =1.16), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:53:02))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to convert archives to use
	X         newer file storage methods.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xstatic char tempname[STRLEN];         /* temp file name */
	X
	Xcvtarc(num,arg)                        /* convert archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	X{
	X    struct heads hdr;                  /* file header */
	X int cvt;                           /* true to convert current file */
	X int did[MAXARG];                  /* true when argument was used */
	X int n;                             /* index */
	X    char *makefnam();                  /* filename fixer */
	X    FILE *fopen();                     /* file opener */
	X
	X    if(arctemp)                   /* use temp area if specified */
	X         sprintf(tempname,"%s$ARCTEMP.CVT",arctemp);
	X    else makefnam("$ARCTEMP.CVT",arcname,tempname);
	X
	X    openarc(1);                        /* open archive for changes */
	X
	X    for(n=0; n<num; n++)               /* for each argument */
	X         did[n] = 0;                   /* reset usage flag */
	X    rempath(num,arg);                  /* strip off paths */
	X
	X    if(num)                            /* if files were named */
	X    {    while(readhdr(&hdr,arc))      /* while more files to check */
	X         {    cvt = 0;                 /* reset convert flag */
	X              for(n=0; n<num; n++)     /* for each template given */
	X              {    if(match(hdr.name,arg[n]))
	X                   {    cvt = 1;       /* turn on convert flag */
	X                        did[n] = 1;    /* turn on usage flag */
	X                        break;         /* stop looking */
	X                   }
	X              }
	X
	X              if(cvt)                  /* if converting this one */
	X                   cvtfile(&hdr);      /* then do it */
	X              else                     /* else just copy it */
	X              {    writehdr(&hdr,new);
	X                   filecopy(arc,new,hdr.size);
	X              }
	X         }
	X    }
	X
	X    else while(readhdr(&hdr,arc))      /* else convert all files */
	X         cvtfile(&hdr);
	X
	X    hdrver = 0;                        /* archive EOF type */
	X    writehdr(&hdr,new);                /* write out our end marker */
	X    closearc(1);                       /* close archive after changes */
	X
	X    if(note)
	X    {    for(n=0; n<num; n++)          /* report unused args */
	X         {    if(!did[n])
	X              {    printf("File not found: %s\n",arg[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X}
	X
	Xstatic cvtfile(hdr)                    /* convert a file */
	Xstruct heads *hdr;                     /* pointer to header data */
	X{
	X    long starts, ftell();              /* where the file goes */
	X    FILE *tmp, *fopen();               /* temporary file */
	X
	X    if(!(tmp=fopen(tempname,"wrb")))
	X         abort("Unable to create temporary file %s",tempname);
	X
	X    if(note)
	X         printf("Converting file: %-12s   reading, ",hdr->name);
	X
	X    unpack(arc,tmp,hdr);               /* unpack the entry */
	X    fseek(tmp,0L,0);                   /* reset temp for reading */
	X
	X    starts = ftell(new);               /* note where header goes */
	X    hdrver = ARCVER;                  /* anything but end marker */
	X    writehdr(hdr,new);                 /* write out header skeleton */
	X    pack(tmp,new,hdr);                 /* pack file into archive */
	X    fseek(new,starts,0);               /* move back to header skeleton */
	X    writehdr(hdr,new);                 /* write out real header */
	X    fseek(new,hdr->size,1);            /* skip over data to next header */
	X    fclose(tmp);                       /* all done with the file */
	X    if(unlink(tempname) && warn)
	X    {    printf("Cannot unsave %s\n",tempname);
	X         nerrs++;
	X    }
	X}
SHAR_EOF
if test 4450 -ne "`wc -c < 'arccvt.c'`"
then
	echo shar: "error transmitting 'arccvt.c'" '(should have been 4450 characters)'
fi
fi
echo shar: "extracting 'arcdel.c'" '(2914 characters)'
if test -f 'arcdel.c'
then
	echo shar: "will not over-write existing file 'arcdel.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcdel.c'
	Xstatic char *RCSid = "$Header: arcdel.c,v 1.2 86/07/15 07:52:53 turner Exp $";
	X
	X/*
	X * $Log:	arcdel.c,v $
	X * Revision 1.2  86/07/15  07:52:53  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:04  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCDEL
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.09), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:53:27))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to delete entries
	X         in an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xdelarc(num,arg)                        /* remove files from archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	X{
	X    struct heads hdr;                  /* header data */
	X int del;                           /* true to delete a file */
	X int did[MAXARG];                  /* true when argument used */
	X int n;                             /* index */
	X
	X    if(!num)                           /* she must specify which */
	X         abort("You must tell me which files to delete!");
	X
	X    for(n=0; n<num; n++)               /* for each argument */
	X         did[n] = 0;                   /* reset usage flag */
	X    rempath(num,arg);                  /* strip off paths */
	X
	X    openarc(1);                        /* open archive for changes */
	X
	X    while(readhdr(&hdr,arc))           /* while more entries in archive */
	X    {    del = 0;                      /* reset delete flag */
	X         for(n=0; n<num; n++)          /* for each template given */
	X         {    if(match(hdr.name,arg[n]))
	X              {    del = 1;            /* turn on delete flag */
	X                   did[n] = 1;         /* turn on usage flag */
	X                   break;              /* stop looking */
	X              }
	X         }
	X
	X         if(del)                       /* skip over unwanted files */
	X         {    fseek(arc,hdr.size,1);
	X              if(note)
	X                   printf("Deleting file: %s\n",hdr.name);
	X         }
	X         else                          /* else copy over file data */
	X         {    writehdr(&hdr,new);      /* write out header and file */
	X              filecopy(arc,new,hdr.size);
	X         }
	X    }
	X
	X    hdrver = 0;                        /* special end of archive type */
	X    writehdr(&hdr,new);                /* write out archive end marker */
	X    closearc(1);                       /* close archive after changes */
	X
	X    if(note)
	X    {    for(n=0; n<num; n++)          /* report unused arguments */
	X         {    if(!did[n])
	X              {    printf("File not found: %s\n",arg[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X}
SHAR_EOF
if test 2914 -ne "`wc -c < 'arcdel.c'`"
then
	echo shar: "error transmitting 'arcdel.c'" '(should have been 2914 characters)'
fi
fi
echo shar: "extracting 'arcdir.c'" '(8244 characters)'
if test -f 'arcdir.c'
then
	echo shar: "will not over-write existing file 'arcdir.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcdir.c'
	Xstatic char *RCSid = "$Header: arcdir.c,v 1.2 86/07/15 07:52:56 turner Exp $";
	X
	X/*
	X * $Log:	arcdir.c,v $
	X * Revision 1.2  86/07/15  07:52:56  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:12  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCDIR
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	X#if MSDOS
	X#include <dir.h>
	X#endif
	X
	X#if BSD
	X#include <sys/types.h>
	X#include <sys/dir.h>
	X#endif
	X
	Xchar *pattern;			      /* global so that fmatch can use them */
	Xint filemode;
	X
	Xchar *dir(filename,mode)               /* get files, one by one */
	Xchar *filename;                        /* template, or NULL */
	Xint mode;                              /* search mode bits */
	X{
	X	struct direct *(*namelist[]);
	X	int alphasort(), fmatch();
	X	static int num = 0,ii;
	X	char *result;
	X
	X	pattern = filename;
	X	filemode = mode;		/* set up globals for fmatch */
	X	if(num == 0) { /* first call */
	X		num = scandir(".", namelist, fmatch, alphasort);
	X		ii = 0;
	X	}
	X	if(ii >= num) { /* all out of files */
	X		if(num) /* there were some files found */
	X		    freedir(namelist);
	X		num = 0;
	X		return(NULL);
	X	}
	X	else {
	X		result = (char *)malloc(strlen((*namelist)[ii]->d_name)+1);
	X		strcpy(result,(*namelist)[ii++]->d_name);
	X		return(&result[0]);
	X	}
	X}
	X
	X
	X#define ASTERISK '*'		/* The '*' metacharacter */
	X#define QUESTION '?'		/* The '?' metacharacter */
	X#define LEFT_BRACKET '['	/* The '[' metacharacter */
	X#define RIGHT_BRACKET ']'	/* The ']' metacharacter */
	X
	X#define IS_OCTAL(ch) (ch >= '0' && ch <= '7')
	X
	Xtypedef int BOOLEAN;
	X#define VOID int
	X#define TRUE 1
	X#define FALSE 0
	X#define EOS '\000'
	X
	Xstatic BOOLEAN do_list ();
	Xstatic char nextch ();
	Xstatic VOID list_parse ();
	X
	X
	X/*
	X *  FUNCTION
	X *
	X *	match   test string for wildcard match
	X *
	X *  SYNOPSIS
	X *
	X *	BOOLEAN match (string, pattern)
	X *	register char *string;
	X *	register char *pattern;
	X *
	X *  DESCRIPTION
	X *
	X *	Test string for match using pattern.  The pattern may
	X *	contain the normal shell metacharacters for pattern
	X *	matching.  The '*' character matches any string,
	X *	including the null string.  The '?' character matches
	X *	any single character.  A list of characters enclosed
	X *	in '[' and ']' matches any character in the list.
	X *	If the first character following the beginning '['
	X *	is a '!' then any character not in the list is matched.
	X *
	X */
	X
	X
	X/*
	X *  PSEUDO CODE
	X *
	X *	Begin match
	X *	    Switch on type of pattern character
	X *		Case ASTERISK:
	X *		    Attempt to match asterisk
	X *		    Break
	X *		Case QUESTION MARK:
	X *		    Attempt to match question mark
	X *		    Break
	X *		Case EOS:
	X *		    Match is result of EOS on string test
	X *		    Break
	X *		Case default:
	X *		    If explicit match then
	X *			Match is result of submatch
	X *		    Else
	X *			Match is FALSE
	X *		    End if
	X *		    Break
	X *	    End switch
	X *	    Return result of match test
	X *	End match
	X *
	X */
	X
	Xstatic BOOLEAN match (string, pattern)
	Xregister char *string;
	Xregister char *pattern;
	X{
	X    register BOOLEAN ismatch;
	X
	X    ismatch = FALSE;
	X    switch (*pattern) {
	X	case ASTERISK:
	X	    pattern++;
	X	    do {
	X		ismatch = match (string, pattern);
	X	    } while (!ismatch && *string++ != EOS);
	X	    break;
	X	case QUESTION:
	X	    if (*string != EOS) {
	X		ismatch = match (++string, ++pattern);
	X	    }
	X	    break;
	X	case EOS:
	X	    if (*string == EOS) {
	X		ismatch = TRUE;
	X	    }
	X	    break;
	X	case LEFT_BRACKET:
	X	    if (*string != EOS) {
	X		ismatch = do_list (string, pattern);
	X	    }
	X	    break;
	X	default:
	X	    if (tolower(*string) == tolower(*pattern))
	X	    {
	X	    	string++;
	X	    	pattern++;
	X		ismatch = match (string, pattern);
	X	    } else {
	X		ismatch = FALSE;
	X	    }
	X	    break;
	X    }
	X    return (ismatch);
	X}
	X
	X
	X/*
	X *  FUNCTION
	X *
	X *	do_list    process a list and following substring
	X *
	X *  SYNOPSIS
	X *
	X *	static BOOLEAN do_list (string, pattern)
	X *	register char *string;
	X *	register char *pattern;
	X *
	X *  DESCRIPTION
	X *
	X *	Called when a list is found in the pattern.  Returns
	X *	TRUE if the current character matches the list and
	X *	the remaining substring matches the remaining pattern.
	X *
	X *	Returns FALSE if either the current character fails to
	X *	match the list or the list matches but the remaining
	X *	substring and subpattern's don't.
	X *
	X *  RESTRICTIONS
	X *
	X *	The mechanism used to match characters in an inclusive
	X *	pair (I.E. [a-d]) may not be portable to machines
	X *	in which the native character set is not ASCII.
	X *
	X *	The rules implemented here are:
	X *
	X *		(1)	The backslash character may be
	X *			used to quote any special character.
	X *			I.E.  "\]" and "\-" anywhere in list,
	X *			or "\!" at start of list.
	X *
	X *		(2)	The sequence \nnn becomes the character
	X *			given by nnn (in octal).
	X *
	X *		(3)	Any non-escaped ']' marks the end of list.
	X *
	X *		(4)	A list beginning with the special character
	X *			'!' matches any character NOT in list.
	X *			The '!' character is only special if it
	X *			is the first character in the list.
	X *
	X */
	X
	X
	X/*
	X *  PSEUDO CODE
	X *
	X *	Begin do_list
	X *	    Default result is no match
	X *	    Skip over the opening left bracket
	X *	    If the next pattern character is a '!' then
	X *		List match gives FALSE
	X *		Skip over the '!' character
	X *	    Else
	X *		List match gives TRUE
	X *	    End if
	X *	    While not at closing bracket or EOS
	X *		Get lower and upper bounds
	X *		If character in bounds then
	X *		    Result is same as sense flag.
	X *		    Skip over rest of list
	X *		End if
	X *	    End while
	X *	    If match found then
	X *		If not at end of pattern then
	X *		    Call match with rest of pattern
	X *		End if
	X *	    End if
	X *	    Return match result
	X *	End do_list
	X *
	X */
	X
	Xstatic BOOLEAN do_list (string, pattern)
	Xregister char *string;
	Xchar *pattern;
	X{
	X    register BOOLEAN ismatch;
	X    register BOOLEAN if_found;
	X    register BOOLEAN if_not_found;
	X    auto char lower;
	X    auto char upper;
	X
	X    pattern++;
	X    if (*pattern == '!') {
	X	if_found = FALSE;
	X	if_not_found = TRUE;
	X	pattern++;
	X    } else {
	X	if_found = TRUE;
	X	if_not_found = FALSE;
	X    }
	X    ismatch = if_not_found;
	X    while (*pattern != ']' && *pattern != EOS) {
	X	list_parse (&pattern, &lower, &upper);
	X	if (*string >= lower && *string <= upper) {
	X	    ismatch = if_found;
	X	    while (*pattern != ']' && *pattern != EOS) {pattern++;}
	X	}
	X    }
	X    if (*pattern++ != ']') {
	X	fprintf (stderr, "warning - character class error\n");
	X    } else {
	X	if (ismatch) {
	X	    ismatch = match (++string, pattern);
	X	}
	X    }
	X    return (ismatch);
	X}
	X
	X
	X/*
	X *  FUNCTION
	X *
	X *	list_parse    parse part of list into lower and upper bounds
	X *
	X *  SYNOPSIS
	X *
	X *	static VOID list_parse (patp, lowp, highp)
	X *	char **patp;
	X *	char *lowp;
	X *	char *highp;
	X *
	X *  DESCRIPTION
	X *
	X *	Given pointer to a pattern pointer (patp), pointer to
	X *	a place to store lower bound (lowp), and pointer to a
	X *	place to store upper bound (highp), parses part of
	X *	the list, updating the pattern pointer in the process.
	X *
	X *	For list characters which are not part of a range,
	X *	the lower and upper bounds are set to that character.
	X *
	X */
	X
	Xstatic VOID list_parse (patp, lowp, highp)
	Xchar **patp;
	Xchar *lowp;
	Xchar *highp;
	X{
	X    *lowp = nextch (patp);
	X    if (**patp == '-') {
	X	(*patp)++;
	X	*highp = nextch (patp);
	X    } else {
	X	*highp = *lowp;
	X    }
	X}
	X
	X
	X/*
	X *  FUNCTION
	X *
	X *	nextch    determine next character in a pattern
	X *
	X *  SYNOPSIS
	X *
	X *	static char nextch (patp)
	X *	char **patp;
	X *
	X *  DESCRIPTION
	X *
	X *	Given pointer to a pointer to a pattern, uses the pattern
	X *	pointer to determine the next character in the pattern,
	X *	subject to translation of backslash-char and backslash-octal
	X *	sequences.
	X *
	X *	The character pointer is updated to point at the next pattern
	X *	character to be processed.
	X *
	X */
	X
	Xstatic char nextch (patp)
	Xchar **patp;
	X{
	X    register char ch;
	X    register char chsum;
	X    register int count;
	X
	X    ch = *(*patp)++;
	X    if (ch == '\\') {
	X	ch = *(*patp)++;
	X	if (IS_OCTAL (ch)) {
	X	    chsum = 0;
	X	    for (count = 0; count < 3 && IS_OCTAL (ch); count++) {
	X		chsum *= 8;
	X		chsum += ch - '0';
	X		ch = *(*patp)++;
	X	    }
	X	    (*patp)--;
	X	    ch = chsum;
	X	}
	X    }
	X    return (ch);
	X}
	X
	X/*
	X *	Filename match - here, *.* matches everything
	X */
	X
	XBOOLEAN fmatch (direntry)
	Xstruct direct *direntry;
	X{
	X    char *ptr,*string;
	X
	X    string = direntry->d_name;
	X
	X    if(!strcmp(pattern, "") || !strcmp(pattern, "*.*"))
	X    	return(1);
	X    return(match(string, pattern));
	X}
SHAR_EOF
if test 8244 -ne "`wc -c < 'arcdir.c'`"
then
	echo shar: "error transmitting 'arcdir.c'" '(should have been 8244 characters)'
fi
fi
echo shar: "extracting 'arcdos.c'" '(3211 characters)'
if test -f 'arcdos.c'
then
	echo shar: "will not over-write existing file 'arcdos.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcdos.c'
	Xstatic char *RCSid = "$Header: arcdos.c,v 1.2 86/07/15 07:53:02 turner Exp $";
	X
	X/*
	X * $Log:	arcdos.c,v $
	X * Revision 1.2  86/07/15  07:53:02  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:15  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCDOS
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =1.43), created on $tag(
	XTED_DATE DB =11/09/85) at $tag(
	XTED_TIME DB =22:24:44))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains certain DOS level routines that assist
	X         in doing fancy things with an archive, primarily reading and
	X         setting the date and time last modified.
	X
	X         These are, by nature, system dependant functions.  But they are
	X         also, by nature, very expendable.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X#if MSDOS
	X#include "fileio2.h"                   /* needed for filehand */
	X#endif
	X#if BSD
	X#include <sys/types.h>
	X#include <sys/stat.h>
	X#endif
	X
	Xgetstamp(f,date,time)                  /* get a file's date/time stamp */
	XFILE *f;                               /* file to get stamp from */
	Xunsigned int *date, *time;             /* storage for the stamp */
	X{
	X#if MSDOS
	X    struct {int ax,bx,cx,dx,si,di,ds,es;} reg;
	X
	X    reg.ax = 0x5700;                   /* get date/time */
	X    reg.bx = filehand(f);              /* file handle */
	X    if(sysint21(&reg,&reg)&1)          /* DOS call */
	X         printf("Get timestamp fail (%d)\n",reg.ax);
	X
	X    *date = reg.dx;                    /* save date/time */
	X    *time = reg.cx;
	X#endif
	X#if BSD
	X    struct stat *buf;
	X    int day,hr,min,sec,yr,imon;
	X    static char mon[4],*mons[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
	X					"Aug","Sep","Oct","Nov","Dec"};
	X
	X    buf = (struct stat *)malloc(sizeof(struct stat));
	X    fstat(f->_file,buf); 
	X/*
	X * assume the UGLY ibm format for date and time
	X */
	X    sscanf(ctime(&(buf->st_atime)),"%*4s%3s%d%d:%d:%d%d"
	X	,mon,&day,&hr,&min,&sec,&yr);
	X    for(imon = 0; imon < 12 && strcmp(mon,mons[imon]); imon++);
	X
	X    *date = (unsigned int)(((yr-1980)<<9)+((imon+1)<<5)+day);
	X    *time = (unsigned int)((hr<<11)+(min<<5)+sec/2);
	X#endif
	X}
	X
	Xsetstamp(f,date,time)                  /* set a file's date/time stamp */
	XFILE *f;                               /* file to set stamp on */
	Xunsigned int date, time;               /* desired date, time */
	X{
	X#if MSDOS
	X    struct {int ax,bx,cx,dx,si,di,ds,es;} reg;
	X
	X    fflush(f);                         /* force any pending output */
	X
	X    reg.ax = 0x5701;                   /* set date/time */
	X    reg.bx = filehand(f);              /* file handle */
	X    reg.cx = time;                     /* desired time */
	X    reg.dx = date;                     /* desired date */
	X    if(sysint21(&reg,&reg)&1)          /* DOS call */
	X         printf("Set timestamp fail (%d)\n",reg.ax);
	X#endif
	X}
	X
	Xstatic int filehand(stream)            /* find handle on a file */
	Xstruct bufstr *stream;                 /* file to grab onto */
	X{
	X#if MSDOS
	X    return stream->bufhand;            /* return DOS 2.0 file handle */
	X#endif
	X}
SHAR_EOF
if test 3211 -ne "`wc -c < 'arcdos.c'`"
then
	echo shar: "error transmitting 'arcdos.c'" '(should have been 3211 characters)'
fi
fi
echo shar: "extracting 'Makefile'" '(479 characters)'
if test -f 'Makefile'
then
	echo shar: "will not over-write existing file 'Makefile'"
else
sed 's/^	X//' << \SHAR_EOF > 'Makefile'
	XCFLAGS = -g
	X#CFLAGS = -O
	X
	XOBJS = arc.o arcadd.o arccode.o arccvt.o arcdel.o arcdir.o \
	Xarcdos.o arcext.o arcio.o arclst.o arclzw.o arcmatch.o arcpack.o arcrun.o \
	Xarcs.o arcsq.o arcsvc.o arctst.o arcunp.o arcusq.o arcvax.o arcmisc.o
	X
	X
	XSRCS = arc.c arcadd.c arccode.c arccvt.c arcdel.c arcdir.c \
	Xarcdos.c arcext.c arcio.c arclst.c arclzw.c arcmatch.c arcpack.c arcrun.c \
	Xarcs.c arcsq.c arcsvc.c arctst.c arcunp.c arcusq.c arcvax.c arcmisc.c
	X
	Xarc:	${OBJS}
	X	cc  -g -o arc ${OBJS}
SHAR_EOF
if test 479 -ne "`wc -c < 'Makefile'`"
then
	echo shar: "error transmitting 'Makefile'" '(should have been 479 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
----
	"I ain't gay, but there are sure times when i wish i could say
		that i wasn't straight"

Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL

turner@imagen.UUCP (08/05/86)

This is my hacked version of the arc program that was posted to the net, I
have modified it to run under Unix BSD4.2. if you want it to run under 
SVR2 you probably will have to change the opendir, scandir, and closedir
calls, I don't really know System 5. IMPORTANT: this is a BETA test version
i doubt severly that there are no bugs, please report any that you find to me.

1) I don't have a PC handy so i have no idea if it still works on the PC; to
	compile it for the PC edit arc.h and arcs.c and change the #defines,
	the define ST is for the Atari 520ST and with luck that will be done
	shortly. If you compile it for the PC you will need the routines for 
	opendir etc. that are in shar part 5, otherwise you can ignore that 
	shar file.
2) the Makefile is configured to compile for debugging (-g) (shows ya what 
	kind of faith i have in my code (:-)
3) everything seems to work for both arc files created on the PC and on BSD4.2
	with the exception of the t (test) option which gives strange results,
	i suspect the problem is in the code not the archive
4) there is one bug that i know of, if you archive a 0 length file, arc will
	blow up (floating point exception) when you try to extract it; the
	question is should the check happen when you add the file, extract
	it, or should if create a 0 length file ? you decide.
5) send flames etc. to me, because of the way postnews works here my signature
	will probably be at the end of each shar file, if not its:
----
Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL
----

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	arcext.c
#	arcio.c
#	arclst.c
#	arclzw.c
#	arcmatch.c
#	arcmisc.c
#	arconvt.c
# This archive created: Mon Aug  4 14:44:49 1986
# By:	D'arc Angel (The Houses of the Holy)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'arcext.c'" '(6059 characters)'
if test -f 'arcext.c'
then
	echo shar: "will not over-write existing file 'arcext.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcext.c'
	X/*
	X$define(arc,$ifdef(xarc,off,on))#      macro switch for ARC only code
	X$define(xarc,$ifdef(xarc,on,off))#     macro switch for XARC only code
	X */
	X/*  ARC - Archive utility - ARCEXT
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.18), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:55:19))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to extract files from
	X         an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X#if ARC					/* $emit($arc)# */
	Xextarc(num,arg,prt)                    /* extract files from archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	Xint prt;                               /* true if printing */
	X#endif					/* $emit($xarc)# */
	X#if XARC
	Xextarc()                               /* extract files from archive */
	X#endif					/* $emit(on)# */
	X{
	X    struct heads hdr;                  /* file header */
	X#if ARC					/* $emit($arc)# */
	X int save;                          /* true to save current file */
	X int did[MAXARG];                  /* true when argument was used */
	X    char *i, *rindex();                /* string index */
	X    char **name, *malloc();             /* name pointer list, allocator */
	X int n;                             /* index */
	X
	X#if MSDOS
	X    name = malloc(num*sizeof(char *));  /* get storage for name pointers */
	X#endif
	X#if BSD
	X    name = (char **)malloc(num*sizeof(char *));  /* get storage for name pointers */
	X#endif
	X
	X    for(n=0; n<num; n++)               /* for each argument */
	X    {    did[n] = 0;                   /* reset usage flag */
	X         if(!(i=rindex(arg[n],'\\')))  /* find start of name */
	X              if(!(i=rindex(arg[n],'/')))
	X                   if(!(i=rindex(arg[n],':')))
	X                        i = arg[n]-1;
	X         name[n] = i+1;
	X    }
	X
	X#endif					/* $emit(on)# */
	X
	X    openarc(0);                        /* open archive for reading */
	X
	X#if ARC					/* $emit($arc)# */
	X    if(num)                            /* if files were named */
	X    {    while(readhdr(&hdr,arc))      /* while more files to check */
	X         {    save = 0;                /* reset save flag */
	X              for(n=0; n<num; n++)     /* for each template given */
	X              {    if(match(hdr.name,name[n]))
	X                   {    save = 1;      /* turn on save flag */
	X                        did[n] = 1;    /* turn on usage flag */
	X                        break;         /* stop looking */
	X                   }
	X              }
	X
	X              if(save)                 /* extract if desired, else skip */
	X                   extfile(&hdr,arg[n],prt);
	X              else fseek(arc,hdr.size,1);
	X         }
	X    }
	X
	X    else while(readhdr(&hdr,arc))      /* else extract all files */
	X         extfile(&hdr,"",prt);
	X#endif					/* $emit($xarc)# */
	X#if XARC
	X    while(readhdr(&hdr,arc))           /* extract all files */
	X         extfile(&hdr);
	X#endif					/* $emit(on)# */
	X
	X    closearc(0);                       /* close archive after reading */
	X#if ARC					/* $emit($arc)# */
	X
	X    if(note)
	X    {    for(n=0; n<num; n++)          /* report unused args */
	X         {    if(!did[n])
	X              {    printf("File not found: %s\n",arg[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X
	X    free(name);
	X#endif					/* $emit(on)# */
	X}
	X
	X#if ARC					/* $emit($arc)# */
	Xstatic extfile(hdr,path,prt)           /* extract a file */
	Xstruct heads *hdr;                     /* pointer to header data */
	Xchar *path;                            /* pointer to path name */
	Xint prt;                               /* true if printing */
	X#endif					/* $emit($xarc)# */
	X#if XARC
	Xstatic extfile(hdr)                    /* extract a file */
	X#endif					/* $emit(on)# */
	X			/* $define(use,$ife($arc,on,fix,hdr->name))# */
	X#if ARC
	X#define USE fix
	X#else
	X#define USE hdr->name
	X#endif
	X
	X{
	X    FILE *f, *fopen();                 /* extracted file, opener */
	X    char buf[STRLEN];                 /* input buffer */
	X#if ARC					/* $emit($arc)# */
	X    char fix[STRLEN];                 /* fixed name buffer */
	X    char *i, *rindex();                /* string index */
	X
	X    if(prt)                            /* printing is much easier */
	X    {    unpack(arc,stdout,hdr);       /* unpack file from archive */
	X         printf("\f");                 /* eject the form */
	X         return;                       /* see? I told you! */
	X    }
	X
	X    strcpy(fix,path);                  /* note path name template */
	X    if(!(i=rindex(fix,'\\')))          /* find start of name */
	X         if(!(i=rindex(fix,'/')))
	X              if(!(i=rindex(fix,':')))
	X                   i = fix-1;
	X    strcpy(i+1,hdr->name);             /* replace template with name */
	X#endif					/* $emit(on)# */
	X
	X    if(note)
	X         printf("Extracting file: %s\n",USE);
	X
	X    if(warn)
	X    {    if(f=fopen(USE,"rb"))        /* see if it exists */
	X         {    fclose(f);
	X              printf("WARNING: File %s already exists!",USE);
	X              while(1)
	X              {    printf("  Overwrite it (y/n)? ");
	X                   fgets(buf,STRLEN,stdin);
	X                   *buf = toupper(*buf);
	X                   if(*buf=='Y' || *buf=='N')
	X                        break;
	X              }
	X              if(*buf=='N')
	X              {    printf("%s not extracted.\n",USE);
	X                   fseek(arc,hdr->size,1);
	X                   return;
	X              }
	X         }
	X    }
	X
	X    if(!(f=fopen(USE,"wb")))
	X    {    if(warn)
	X         {    printf("Cannot create %s\n",USE);
	X              nerrs++;
	X         }
	X         fseek(arc,hdr->size,1);
	X         return;
	X    }
	X
	X    /* now unpack the file */
	X
	X    unpack(arc,f,hdr);                 /* unpack file from archive */
	X    setstamp(f,hdr->date,hdr->time);   /* set the proper date/time stamp */
	X    fclose(f);                         /* all done writing to file */
	X}
SHAR_EOF
if test 6059 -ne "`wc -c < 'arcext.c'`"
then
	echo shar: "error transmitting 'arcext.c'" '(should have been 6059 characters)'
fi
fi
echo shar: "extracting 'arcio.c'" '(5382 characters)'
if test -f 'arcio.c'
then
	echo shar: "will not over-write existing file 'arcio.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcio.c'
	Xstatic char *RCSid = "$Header: arcio.c,v 1.2 86/07/15 07:53:11 turner Exp $";
	X
	X/*
	X * $Log:	arcio.c,v $
	X * Revision 1.2  86/07/15  07:53:11  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:21  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCIO
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.30), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:56:00))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the file I/O routines used to manipulate
	X         an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xint readhdr(hdr,f)                     /* read a header from an archive */
	Xstruct heads *hdr;                     /* storage for header */
	XFILE *f;                               /* archive to read header from */
	X{
	X#if BSD | ST
	X        unsigned char dummy[28];
	X	int i,j,k;
	X#endif
	X    char name[FNLEN];                  /* filename buffer */
	X    int try = 0;                       /* retry counter */
	X    static int first = 1;              /* true only on first read */
	X
	X    if(!f)                             /* if archive didn't open */
	X         return 0;                     /* then pretend it's the end */
	X    if(feof(f))                        /* if no more data */
	X         return 0;                     /* then signal end of archive */
	X
	X    if(fgetc(f)!=ARCMARK)             /* check archive validity */
	X    {    if(warn)
	X         {    printf("An entry in %s has a bad header.",arcname);
	X              nerrs++;
	X         }
	X
	X         while(!feof(f))
	X         {    try++;
	X              if(fgetc(f)==ARCMARK)
	X              {    ungetc(hdrver=fgetc(f),f);
	X                   if(hdrver>=0 && hdrver<=ARCVER)
	X                        break;
	X              }
	X         }
	X
	X         if(feof(f) && first)
	X              abort("%s is not an archive",arcname);
	X
	X         if(warn)
	X              printf("  %d bytes skipped.\n",try);
	X
	X         if(feof(f))
	X              return 0;
	X    }
	X
	X    hdrver = fgetc(f);                 /* get header version */
	X    if(hdrver<0)
	X         abort("Invalid header in archive %s",arcname);
	X    if(hdrver==0)
	X         return 0;                     /* note our end of archive marker */
	X    if(hdrver>ARCVER)
	X    {    fread(name,sizeof(char),FNLEN,f);
	X         printf("I don't know how to handle file %s in archive %s\n",
	X              name,arcname);
	X         printf("I think you need a newer version of ARC.\n");
	X         exit(1);
	X    }
	X
	X    /* amount to read depends on header type */
	X
	X    if(hdrver==1)                      /* old style is shorter */
	X    {    fread(hdr,sizeof(struct heads)-sizeof(long int),1,f);
	X         hdrver = 2;                   /* convert header to new format */
	X         hdr->length = hdr->size;      /* size is same when not packed */
	X    }
	X    else {
	X#if MSDOS
	X	fread(hdr,sizeof(struct heads),1,f);
	X#endif
	X#if BSD | ST
	X	fread(dummy,27,1,f);
	X
	X	for(i=0;i<13;hdr->name[i]=dummy[i],i++);
	X	hdr->size = (long)((dummy[16]<<24) + (dummy[15]<<16) + (dummy[14]<<8)
	X	    + dummy[13]);
	X	hdr->date = (short)((dummy[18]<<8) + dummy[17]);
	X	hdr->time = (short)((dummy[20]<<8) + dummy[19]);
	X	hdr->crc  = (short)((dummy[22]<<8) + dummy[21]);
	X	hdr->length = (long)((dummy[26]<<24) + (dummy[25]<<16)
	X	    + (dummy[24]<<8) + dummy[23]);
	X#endif
	X    }
	X
	X    first = 0; return 1;               /* we read something */
	X}
	X
	Xwritehdr(hdr,f)                        /* write a header to an archive */
	Xstruct heads *hdr;                     /* header to write */
	XFILE *f;                               /* archive to write to */
	X{
	X    unsigned char dummy[28];
	X
	X    fputc(ARCMARK,f);                 /* write out the mark of ARC */
	X    fputc(hdrver,f);                   /* write out the header version */
	X    if(!hdrver)                        /* if that's the end */
	X         return;                       /* then write no more */
	X#if MSDOS
	X    fwrite(hdr,sizeof(struct heads),1,f);
	X#endif
	X#if BSD | ST
	X/*
	X * put out the hdr in the brain damaged unaligned half back *sswards
	X * way HAL does it
	X */
	X    fwrite(hdr->name,1,13,f);
	X    fwrite(&hdr->size,sizeof(long),1,f);
	X    fwrite(&hdr->date,sizeof(INT),1,f);
	X    fwrite(&hdr->time,sizeof(INT),1,f);
	X    fwrite(&hdr->crc ,sizeof(INT),1,f);
	X    fwrite(&hdr->length,sizeof(long),1,f);
	X#endif
	X
	X    /* note the newest file for updating the archive timestamp */
	X
	X    if(hdr->date>arcdate
	X    ||(hdr->date==arcdate && hdr->time>arctime))
	X    {    arcdate = hdr->date;
	X         arctime = hdr->time;
	X    }
	X}
	X
	Xfilecopy(f,t,size)                     /* bulk file copier */
	XFILE *f, *t;                           /* from, to */
	Xlong size;                             /* number of bytes */
	X{
	X int len;                           /* length of a given copy */
	X
	X    while(size--)                      /* while more bytes to move */
	X         putc_tst(fgetc(f),t);
	X}
	X
	Xputc_tst(c,t)                          /* put a character, with tests */
	Xchar c;                                /* character to output */
	XFILE *t;                               /* file to write to */
	X{
	X    if(t)
	X#if MSODS | ST
	X         if(fputc(c,t)==EOF)
	X              abort("Write fail (disk full?)");
	X#endif
	X#if BSD
	X/*
	X * for reasons beyond me BSD unix returns EOF 
	X */
	X	fputc(c,t);
	X#endif
	X}
SHAR_EOF
if test 5382 -ne "`wc -c < 'arcio.c'`"
then
	echo shar: "error transmitting 'arcio.c'" '(should have been 5382 characters)'
fi
fi
echo shar: "extracting 'arclst.c'" '(5517 characters)'
if test -f 'arclst.c'
then
	echo shar: "will not over-write existing file 'arclst.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arclst.c'
	Xstatic char *RCSid = "$Header: arclst.c,v 1.2 86/07/15 07:53:15 turner Exp $";
	X
	X/*
	X * $Log:	arclst.c,v $
	X * Revision 1.2  86/07/15  07:53:15  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:23  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCLST
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.34), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:56:57))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to list the contents
	X         of an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xlstarc(num,arg)                        /* list files in archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	X{
	X    struct heads hdr;                  /* header data */
	X int list;                          /* true to list a file */
	X int did[MAXARG];                  /* true when argument was used */
	X    long tnum, tlen, tsize;            /* totals */
	X int n;                             /* index */
	X
	X    tnum = tlen = tsize = 0;           /* reset totals */
	X
	X    for(n=0; n<num; n++)               /* for each argument */
	X         did[n] = 0;                   /* reset usage flag */
	X    rempath(num,arg);                  /* strip off paths */
	X
	X    if(!kludge)
	X    {    printf("Name          Length  ");
	X         if(bose)
	X              printf("  Stowage    SF   Size now");
	X         printf("  Date     ");
	X         if(bose)
	X              printf("  Time    CRC");
	X         printf("\n");
	X
	X         printf("============  ========");
	X         if(bose)
	X              printf("  ========  ====  ========");
	X         printf("  =========");
	X         if(bose)
	X              printf("  ======  ====");
	X         printf("\n");
	X    }
	X
	X    openarc(0);                        /* open archive for reading */
	X
	X    if(num)                            /* if files were named */
	X    {    while(readhdr(&hdr,arc))      /* process all archive files */
	X         {    list = 0;                /* reset list flag */
	X              for(n=0; n<num; n++)     /* for each template given */
	X              {    if(match(hdr.name,arg[n]))
	X                   {    list = 1;      /* turn on list flag */
	X                        did[n] = 1;    /* turn on usage flag */
	X                        break;         /* stop looking */
	X                   }
	X              }
	X
	X              if(list)                 /* if this file is wanted */
	X              {    if(!kludge)
	X                        lstfile(&hdr); /* then tell about it */
	X                   tnum++;             /* update totals */
	X                   tlen += hdr.length;
	X                   tsize += hdr.size;
	X              }
	X
	X              fseek(arc,hdr.size,1);   /* move to next header */
	X         }
	X    }
	X
	X    else while(readhdr(&hdr,arc))      /* else report on all files */
	X    {    if(!kludge)
	X              lstfile(&hdr);
	X         tnum++;                       /* update totals */
	X         tlen += hdr.length;
	X         tsize += hdr.size;
	X         fseek(arc,hdr.size,1);        /* skip to next header */
	X    }
	X
	X    closearc(0);                       /* close archive after reading */
	X
	X    if(!kludge)
	X    {    printf("        ====  ========");
	X         if(bose)
	X              printf("            ====  ========");
	X         printf("\n");
	X    }
	X
	X    printf("Total %6ld  %8ld  ",tnum,tlen);
	X    if(bose)
	X         printf("          %3ld%%  %8ld  ",100L - (100L*tsize)/tlen,tsize);
	X    printf("\n");
	X
	X    if(note)
	X    {    for(n=0; n<num; n++)          /* report unused args */
	X         {    if(!did[n])
	X              {    printf("File not found: %s\n",arg[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X}
	X
	Xstatic lstfile(hdr)                    /* tell about a file */
	Xstruct heads *hdr;                     /* pointer to header data */
	X{
	X int yr, mo, dy;                    /* parts of a date */
	X int hh, mm, ss;                    /* parts of a time */
	X
	X    static char *mon[] =               /* month abbreviations */
	X    {    "Jan",    "Feb",    "Mar",    "Apr",
	X         "May",    "Jun",    "Jul",    "Aug",
	X         "Sep",    "Oct",    "Nov",    "Dec"
	X    };
	X
	X    yr = (hdr->date >> 9) & 0x7f;      /* dissect the date */
	X    mo = (hdr->date >> 5) & 0x0f;
	X    dy = hdr->date & 0x1f;
	X
	X    hh = (hdr->time >> 11) & 0x1f;     /* dissect the time */
	X    mm = (hdr->time >> 5) & 0x3f;
	X    ss = (hdr->time & 0x1f) * 2;
	X
	X    printf("%-12s  %8ld  ",hdr->name,hdr->length);
	X
	X    if(bose)
	X    {    switch(hdrver)
	X         {
	X         case 1:
	X         case 2:
	X              printf("   --   ");
	X              break;
	X         case 3:
	X              printf(" Packed ");
	X              break;
	X         case 4:
	X              printf("Squeezed");
	X              break;
	X         case 5:
	X         case 6:
	X         case 7:
	X              printf("crunched");
	X              break;
	X         case 8:
	X              printf("Crunched");
	X              break;
	X         default:
	X              printf("Unknown!");
	X         }
	X
	X         printf("  %3d%%",100L - (100L*hdr->size)/hdr->length);
	X         printf("  %8ld  ",hdr->size);
	X    }
	X
	X    printf("%2d %3s %02d", dy, mon[mo-1], (yr+80)%100);
	X
	X    if(bose)
	X         printf("  %2d:%02d%c  %04x",
	X              (hh>12?hh-12:hh), mm, (hh>12?'p':'a'),
	X              hdr->crc);
	X
	X    printf("\n");
	X}
SHAR_EOF
if test 5517 -ne "`wc -c < 'arclst.c'`"
then
	echo shar: "error transmitting 'arclst.c'" '(should have been 5517 characters)'
fi
fi
echo shar: "extracting 'arclzw.c'" '(27190 characters)'
if test -f 'arclzw.c'
then
	echo shar: "will not over-write existing file 'arclzw.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arclzw.c'
	Xstatic char *RCSid = "$Header: arclzw.c,v 1.2 86/07/15 07:53:20 turner Exp $";
	X
	X/*
	X * $Log:	arclzw.c,v $
	X * Revision 1.2  86/07/15  07:53:20  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:26  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCLZW
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =1.88), created on $tag(
	XTED_DATE DB =01/20/86) at $tag(
	XTED_TIME DB =16:47:04))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to implement Lempel-Zev
	X         data compression, which calls for building a coding table on
	X         the fly.  This form of compression is especially good for encoding
	X         files which contain repeated strings, and can often give dramatic
	X         improvements over traditional Huffman SQueezing.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X
	X    Programming notes:
	X         In this section I am drawing heavily on the COMPRESS program
	X         from UNIX.  The basic method is taken from "A Technique for High
	X         Performance Data Compression", Terry A. Welch, IEEE Computer
	X         Vol 17, No 6 (June 1984), pp 8-19.  Also see "Knuth's Fundamental
	X         Algorithms", Donald Knuth, Vol 3, Section 6.4.
	X
	X         As best as I can tell, this method works by tracing down a hash
	X         table of code strings where each entry has the property:
	X
	X              if <string> <char> is in the table
	X              then <string> is in the table.
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	X/* definitions for older style crunching */
	X
	X#define FALSE    0
	X#define TRUE     !FALSE
	X#define TABSIZE  4096
	X#define NO_PRED  0xFFFF
	X#define EMPTY    0xFFFF
	X#define NOT_FND  0xFFFF
	X
	Xstatic unsigned int inbuf;             /* partial input code storage */
	Xstatic int sp;                         /* current stack pointer */
	X
	Xstatic struct entry                    /* string table entry format */
	X{   char used;                         /* true when this entry is in use */
	X    unsigned int next;                 /* ptr to next in collision list */
	X    unsigned int predecessor;          /* code for preceeding string */
	X    unsigned char follower;            /* char following string */
	X}   string_tab[TABSIZE];               /* the code string table */
	X
	X
	X/* definitions for the new dynamic Lempel-Zev crunching */
	X
	X#define BITS   12                      /* maximum bits per code */
	X#define HSIZE  5003                    /* 80% occupancy */
	X#define INIT_BITS 9                    /* initial number of bits/code */
	X
	Xstatic int n_bits;                     /* number of bits/code */
	Xstatic int maxcode;                    /* maximum code, given n_bits */
	X#define MAXCODE(n)      ((1<<(n)) - 1) /* maximum code calculation */
	Xstatic int maxcodemax =  1 << BITS;    /* largest possible code (+1) */
	X
	Xstatic unsigned char buf[BITS];        /* input/output buffer */
	X
	Xstatic unsigned char lmask[9] =        /* left side masks */
	X{   0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
	Xstatic unsigned char rmask[9] =        /* right side masks */
	X{   0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
	X
	Xstatic int offset;                     /* byte offset for code output */
	Xstatic long in_count;                  /* length of input */
	Xstatic long bytes_out;                 /* length of compressed output */
	Xstatic unsigned int ent;
	X
	X/* To save much memory (which we badly need at this point), we overlay
	X * the table used by the previous version of Lempel-Zev with those used
	X * by the new version.  Since no two of these routines will be used
	X * together, we can safely do this.  Note that the tables used for Huffman
	X * squeezing may NOT overlay these, since squeezing and crunching are done
	X * in parallel.
	X */
	X
	X#if MSODS
	Xstatic long *htab = string_tab;        /* hash code table   (crunch) */
	X#endif
	X#if BSD | ST
	Xstatic long htab[HSIZE];               /* hash code table   (crunch) */
	X#endif
	Xstatic unsigned int codetab[HSIZE];    /* string code table (crunch) */
	X
	Xstatic unsigned int *prefix = codetab; /* prefix code table (uncrunch) */
	X
	X#if MSDOS
	Xstatic unsigned char *suffix = string_tab;  /* suffix table (uncrunch) */
	X#endif
	X#if BSD | ST
	Xstatic unsigned char suffix[HSIZE];    /* suffix table (uncrunch) */
	X#endif
	Xstatic int free_ent;                   /* first unused entry */
	Xstatic int firstcmp;                   /* true at start of compression */
	Xstatic unsigned char stack[HSIZE];     /* local push/pop stack */
	X
	X/*
	X * block compression parameters -- after all codes are used up,
	X * and compression rate changes, start over.
	X */
	X
	Xstatic int clear_flg;
	Xstatic long ratio;
	X#define CHECK_GAP 10000                /* ratio check interval */
	Xstatic long checkpoint;
	X
	X/*
	X * the next two codes should not be changed lightly, as they must not
	X * lie within the contiguous general code space.
	X */
	X#define FIRST   257                    /* first free entry */
	X#define CLEAR   256                    /* table clear output code */
	X
	Xstatic cl_block(t)                     /* table clear for block compress */
	XFILE *t;                               /* our output file */
	X{
	X    long int rat;
	X
	X    checkpoint = in_count + CHECK_GAP;
	X
	X    if(in_count > 0x007fffff)          /* shift will overflow */
	X    {    rat = bytes_out >> 8;
	X         if(rat == 0)                  /* Don't divide by zero */
	X              rat = 0x7fffffff;
	X         else rat = in_count / rat;
	X    }
	X    else rat = (in_count<<8)/bytes_out;/* 8 fractional bits */
	X
	X    if(rat > ratio)
	X         ratio = rat;
	X    else
	X    {    ratio = 0;
	X         setmem	(htab,HSIZE*sizeof(long),0xff);
	X         free_ent = FIRST;
	X         clear_flg = 1;
	X         putcode(CLEAR,t);
	X    }
	X}
	X
	X/*****************************************************************
	X *
	X * Output a given code.
	X * Inputs:
	X *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
	X *              that n_bits =< (long)wordsize - 1.
	X * Outputs:
	X *      Outputs code to the file.
	X * Assumptions:
	X *      Chars are 8 bits long.
	X * Algorithm:
	X *      Maintain a BITS character long buffer (so that 8 codes will
	X * fit in it exactly).  When the buffer fills up empty it and start over.
	X */
	X
	Xstatic putcode(code,t)                 /* output a code */
	Xint code;                              /* code to output */
	XFILE *t;                               /* where to put it */
	X{
	X int r_off = offset;                /* right offset */
	X int bits = n_bits;                 /* bits to go */
	X unsigned char *bp = buf;           /* buffer pointer */
	X int n;                             /* index */
	X
	X    if(code >= 0)                      /* if a real code */
	X    {    /*
	X          * Get to the first byte.
	X          */
	X         bp += (r_off >> 3);
	X         r_off &= 7;
	X
	X         /*
	X          * Since code is always >= 8 bits, only need to mask the first
	X          * hunk on the left.
	X          */
	X         *bp = (*bp&rmask[r_off]) | (code<<r_off) & lmask[r_off];
	X         bp++;
	X         bits -= (8 - r_off);
	X         code >>= (8 - r_off);
	X
	X         /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
	X         if(bits >= 8)
	X         {    *bp++ = code;
	X              code >>= 8;
	X              bits -= 8;
	X         }
	X
	X         /* Last bits. */
	X         if(bits)
	X              *bp = code;
	X
	X         offset += n_bits;
	X
	X         if(offset == (n_bits << 3))
	X         {    bp = buf;
	X              bits = n_bits;
	X              bytes_out += bits;
	X              do
	X                   putc_pak(*bp++,t);
	X              while(--bits);
	X              offset = 0;
	X         }
	X
	X         /*
	X          * If the next entry is going to be too big for the code size,
	X          * then increase it, if possible.
	X          */
	X         if(free_ent>maxcode || clear_flg>0)
	X         {    /*
	X               * Write the whole buffer, because the input side won't
	X               * discover the size increase until after it has read it.
	X               */
	X              if(offset > 0)
	X              {    bp = buf;           /* reset pointer for writing */
	X                   bytes_out += n = n_bits;
	X                   while(n--)
	X                        putc_pak(*bp++,t);
	X              }
	X              offset = 0;
	X
	X              if(clear_flg)            /* reset if clearing */
	X              {    maxcode = MAXCODE(n_bits = INIT_BITS);
	X                   clear_flg = 0;
	X              }
	X              else                     /* else use more bits */
	X              {    n_bits++;
	X                   if(n_bits == BITS)
	X                        maxcode = maxcodemax;
	X                   else
	X                        maxcode = MAXCODE(n_bits);
	X              }
	X         }
	X    }
	X
	X    else                               /* dump the buffer on EOF */
	X    {    bytes_out += n = (offset+7) / 8;
	X
	X         if(offset > 0)
	X              while(n--)
	X                   putc_pak(*bp++,t);
	X         offset = 0;
	X    }
	X}
	X
	X/*****************************************************************
	X *
	X * Read one code from the standard input.  If EOF, return -1.
	X * Inputs:
	X *      cmpin
	X * Outputs:
	X *      code or -1 is returned.
	X */
	X
	Xstatic int getcode(f)                  /* get a code */
	XFILE *f;                               /* file to get from */
	X{
	X int code;
	X    static int offset = 0, size = 0;
	X int r_off, bits;
	X    unsigned char *bp = buf;
	X
	X    if(clear_flg > 0 || offset >= size || free_ent > maxcode)
	X    {    /*
	X          * If the next entry will be too big for the current code
	X          * size, then we must increase the size.  This implies reading
	X          * a new buffer full, too.
	X          */
	X         if(free_ent > maxcode)
	X         {    n_bits++;
	X              if(n_bits == BITS)
	X                   maxcode = maxcodemax;    /* won't get any bigger now */
	X              else maxcode = MAXCODE(n_bits);
	X         }
	X         if(clear_flg > 0)
	X         {    maxcode = MAXCODE(n_bits = INIT_BITS);
	X              clear_flg = 0;
	X         }
	X
	X         for(size=0; size<n_bits; size++)
	X         {    if((code=getc_unp(f))==EOF)
	X                   break;
	X              else buf[size] = code;
	X         }
	X         if(size <= 0)
	X              return -1;               /* end of file */
	X
	X         offset = 0;
	X         /* Round size down to integral number of codes */
	X         size = (size << 3)-(n_bits - 1);
	X    }
	X    r_off = offset;
	X    bits = n_bits;
	X
	X    /*
	X     * Get to the first byte.
	X     */
	X    bp +=(r_off >> 3);
	X    r_off &= 7;
	X
	X    /* Get first part (low order bits) */
	X    code = (*bp++ >> r_off);
	X    bits -= 8 - r_off;
	X    r_off = 8 - r_off;                 /* now, offset into code word */
	X
	X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
	X    if(bits >= 8)
	X    {    code |= *bp++ << r_off;
	X         r_off += 8;
	X         bits -= 8;
	X    }
	X    /* high order bits. */
	X    code |= (*bp & rmask[bits]) << r_off;
	X    offset += n_bits;
	X
	X    return code;
	X}
	X
	X/*
	X * compress a file
	X *
	X * Algorithm:  use open addressing double hashing (no chaining) on the
	X * prefix code / next character combination.  We do a variant of Knuth's
	X * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
	X * secondary probe.  Here, the modular division first probe is gives way
	X * to a faster exclusive-or manipulation.  Also do block compression with
	X * an adaptive reset, where the code table is cleared when the compression
	X * ratio decreases, but after the table fills.  The variable-length output
	X * codes are re-sized at this point, and a special CLEAR code is generated
	X * for the decompressor.
	X */
	X
	Xinit_cm(f,t)                           /* initialize for compression */
	XFILE *f;                               /* file we will be compressing */
	XFILE *t;                               /* where we will put it */
	X{
	X    offset = 0;
	X    bytes_out = 1;
	X    clear_flg = 0;
	X    ratio = 0;
	X    in_count = 1;
	X    checkpoint = CHECK_GAP;
	X    maxcode = MAXCODE(n_bits = INIT_BITS);
	X    free_ent = FIRST;
	X    setmem(htab,HSIZE*sizeof(long),0xff);
	X    n_bits = INIT_BITS;                /* set starting code size */
	X
	X    putc_pak(BITS,t);                  /* note our max code length */
	X
	X    firstcmp = 1;                      /* next byte will be first */
	X}
	X
	Xputc_cm(c,t)                           /* compress a character */
	Xunsigned char c;                       /* character to compress */
	XFILE *t;                               /* where to put it */
	X{
	X    static long fcode;
	X    static int hshift;
	X int i;
	X int disp;
	X
	X    if(firstcmp)                       /* special case for first byte */
	X    {    ent = c;                      /* remember first byte */
	X
	X         hshift = 0;
	X         for(fcode=(long)HSIZE;  fcode<65536L; fcode*=2L)
	X              hshift++;
	X         hshift = 8 - hshift;          /* set hash code range bund */
	X
	X         firstcmp = 0;                 /* no longer first */
	X         return;
	X    }
	X
	X    in_count++;
	X    fcode =(long)(((long)c << BITS)+ent);
	X    i = (c<<hshift)^ent;               /* xor hashing */
	X
	X    if(htab[i]==fcode)
	X    {    ent = codetab[i];
	X         return;
	X    }
	X    else if(htab[i]<0)                 /* empty slot */
	X         goto nomatch;
	X    disp = HSIZE - i;                  /* secondary hash (after G.Knott) */
	X    if(i == 0)
	X         disp = 1;
	X
	Xprobe:
	X    if((i -= disp) < 0)
	X         i += HSIZE;
	X
	X    if(htab[i] == fcode)
	X    {    ent = codetab[i];
	X         return;
	X    }
	X    if(htab[i] > 0)
	X         goto probe;
	X
	Xnomatch:
	X    putcode(ent,t);
	X    ent = c;
	X    if(free_ent < maxcodemax)
	X    {    codetab[i] = free_ent++;      /* code -> hashtable */
	X         htab[i] = fcode;
	X    }
	X    else if((long int)in_count >= checkpoint)
	X         cl_block(t);
	X}
	X
	Xlong pred_cm(t)                        /* finish compressing a file */
	XFILE *t;                               /* where to put it */
	X{
	X    putcode(ent,t);                    /* put out the final code */
	X    putcode(-1,t);                     /* tell output we are done */
	X
	X    return bytes_out;                  /* say how big it got */
	X}
	X
	X/*
	X * Decompress a file.  This routine adapts to the codes in the file
	X * building the string table on-the-fly; requiring no table to be stored
	X * in the compressed file.  The tables used herein are shared with those of
	X * the compress() routine.  See the definitions above.
	X */
	X
	Xdecomp(f,t)                            /* decompress a file */
	XFILE *f;                               /* file to read codes from */
	XFILE *t;                               /* file to write text to */
	X{
	X    unsigned char *stackp;
	X int finchar;
	X int code, oldcode, incode;
	X
	X    if((code=getc_unp(f))!=BITS)
	X         abort("File packed with %d bits, I can only handle %d",code,BITS);
	X
	X    n_bits = INIT_BITS;                /* set starting code size */
	X    clear_flg = 0;
	X
	X    /*
	X     * As above, initialize the first 256 entries in the table.
	X     */
	X    maxcode = MAXCODE(n_bits=INIT_BITS);
	X    for(code = 255; code >= 0; code--)
	X    {    prefix[code] = 0;
	X         suffix[code] = (unsigned char)code;
	X    }
	X    free_ent = FIRST;
	X
	X    finchar = oldcode = getcode(f);
	X    if(oldcode == -1)                  /* EOF already? */
	X         return;                       /* Get out of here */
	X    putc_ncr((char)finchar,t);         /* first code must be 8 bits=char */
	X    stackp = stack;
	X
	X    while((code = getcode(f))> -1)
	X    {    if(code==CLEAR)
	X         {    for(code = 255; code >= 0; code--)
	X                   prefix[code] = 0;
	X              clear_flg = 1;
	X              free_ent = FIRST - 1;
	X              if((code=getcode(f))==-1)/* O, untimely death! */
	X                   break;
	X         }
	X         incode = code;
	X         /*
	X          * Special case for KwKwK string.
	X          */
	X         if(code >= free_ent)
	X         {    *stackp++ = finchar;
	X              code = oldcode;
	X         }
	X
	X         /*
	X          * Generate output characters in reverse order
	X          */
	X         while(code >= 256)
	X         {    *stackp++ = suffix[code];
	X              code = prefix[code];
	X         }
	X         *stackp++ = finchar = suffix[code];
	X
	X         /*
	X          * And put them out in forward order
	X          */
	X         do
	X              putc_ncr(*--stackp,t);
	X         while(stackp > stack);
	X
	X         /*
	X          * Generate the new entry.
	X          */
	X         if((code=free_ent) < maxcodemax)
	X         {    prefix[code] = (unsigned short)oldcode;
	X              suffix[code] = finchar;
	X              free_ent = code+1;
	X         }
	X         /*
	X          * Remember previous code.
	X          */
	X         oldcode = incode;
	X    }
	X}
	X
	X
	X/*************************************************************************
	X * Please note how much trouble it can be to maintain upwards            *
	X * compatibility.  All that follows is for the sole purpose of unpacking *
	X * files which were packed using an older method.                        *
	X *************************************************************************/
	X
	X
	X/*  The h() pointer points to the routine to use for calculating a hash
	X    value.  It is set in the init routines to point to either of oldh()
	X    or newh().
	X
	X    oldh() calculates a hash value by taking the middle twelve bits
	X    of the square of the key.
	X
	X    newh() works somewhat differently, and was tried because it makes
	X    ARC about 23% faster.  This approach was abandoned because dynamic
	X    Lempel-Zev (above) works as well, and packs smaller also.  However,
	X    inadvertent release of a developmental copy forces us to leave this in.
	X*/
	X
	Xstatic unsigned (*h)();                /* pointer to hash function */
	X
	Xstatic unsigned oldh(pred,foll)        /* old hash function */
	Xunsigned int pred;                     /* code for preceeding string */
	Xunsigned char foll;                    /* value of following char */
	X{
	X    long local;                        /* local hash value */
	X
	X    local = (pred + foll) | 0x0800;    /* create the hash key */
	X    local *= local;                    /* square it */
	X    return (local >> 6) & 0x0FFF;      /* return the middle 12 bits */
	X}
	X
	Xstatic unsigned newh(pred,foll)        /* new hash function */
	Xunsigned int pred;                     /* code for preceeding string */
	Xunsigned char foll;                    /* value of following char */
	X{
	X    return ((pred+foll)*15073)&0xFFF;  /* faster hash */
	X}
	X
	X/*  The eolist() function is used to trace down a list of entries with
	X    duplicate keys until the last duplicate is found.
	X*/
	X
	Xstatic unsigned eolist(index)          /* find last duplicate */
	Xunsigned int index;
	X{
	X int temp;
	X
	X    while(temp=string_tab[index].next) /* while more duplicates */
	X         index = temp;
	X
	X    return index;
	X}
	X
	X/*  The hash() routine is used to find a spot in the hash table for a new
	X    entry.  It performs a "hash and linear probe" lookup, using h() to
	X    calculate the starting hash value and eolist() to perform the linear
	X    probe.  This routine DOES NOT detect a table full condition.  That
	X    MUST be checked for elsewhere.
	X*/
	X
	Xstatic unsigned hash(pred,foll)        /* find spot in the string table */
	Xunsigned int pred;                     /* code for preceeding string */
	Xunsigned char foll;                    /* char following string */
	X{
	X    unsigned int local, tempnext;      /* scratch storage */
	X    struct entry *ep;                  /* allows faster table handling */
	X
	X    local = (*h)(pred,foll);           /* get initial hash value */
	X
	X    if(!string_tab[local].used)        /* if that spot is free */
	X         return local;                 /* then that's all we need */
	X
	X    else                               /* else a collision has occured */
	X    {    local = eolist(local);        /* move to last duplicate */
	X
	X         /*   We must find an empty spot. We start looking 101 places
	X              down the table from the last duplicate.
	X         */
	X
	X         tempnext = (local+101) & 0x0FFF;
	X         ep = &string_tab[tempnext];   /* initialize pointer */
	X
	X         while(ep->used)               /* while empty spot not found */
	X         {    if(++tempnext==TABSIZE)  /* if we are at the end */
	X              {    tempnext = 0;       /* wrap to beginning of table*/
	X                   ep = string_tab;
	X              }
	X              else ++ep;               /* point to next element in table */
	X         }
	X
	X         /*   local still has the pointer to the last duplicate, while
	X              tempnext has the pointer to the spot we found.  We use
	X              this to maintain the chain of pointers to duplicates.
	X         */
	X
	X         string_tab[local].next = tempnext;
	X
	X         return tempnext;
	X    }
	X}
	X
	X/*  The unhash() function is used to search the hash table for a given key.
	X    Like hash(), it performs a hash and linear probe search.  It returns
	X    either the number of the entry (if found) or NOT_FND (if not found).
	X*/
	X
	Xstatic unsigned unhash(pred,foll)      /* search string table for a key */
	Xunsigned int pred;                     /* code of preceeding string */
	Xunsigned char foll;                    /* character following string */
	X{
	X    unsigned int local, offset;        /* scratch storage */
	X    struct entry *ep;                  /* this speeds up access */
	X
	X    local = (*h)(pred,foll);           /* initial hash */
	X
	X    while(1)
	X    {    ep = &string_tab[local];      /* speed up table access */
	X
	X         if((ep->predecessor==pred) && (ep->follower==foll))
	X              return local;            /* we have a match */
	X
	X         if(!ep->next)                 /* if no more duplicates */
	X              return NOT_FND;          /* then key is not listed */
	X
	X         local = ep->next;             /* move on to next duplicate */
	X    }
	X}
	X
	X/*  The init_tab() routine is used to initialize our hash table.
	X    You realize, of course, that "initialize" is a complete misnomer.
	X*/
	X
	Xstatic init_tab()                      /* set ground state in hash table */
	X{
	X    unsigned int i;                    /* table index */
	X
	X    setmem((char *)string_tab,sizeof(string_tab),0);
	X
	X    for(i=0; i<256; i++)               /* list all single byte strings */
	X         upd_tab(NO_PRED,i);
	X
	X    inbuf = EMPTY;                     /* nothing is in our buffer */
	X}
	X
	X/*  The upd_tab routine is used to add a new entry to the string table.
	X    As previously stated, no checks are made to ensure that the table
	X    has any room.  This must be done elsewhere.
	X*/
	X
	Xupd_tab(pred,foll)                     /* add an entry to the table */
	Xunsigned int pred;                     /* code for preceeding string */
	Xunsigned int foll;                     /* character which follows string */
	X{
	X    struct entry *ep;                  /* pointer to current entry */
	X
	X    /* calculate offset just once */
	X
	X    ep = &string_tab[hash(pred,foll)];
	X
	X    ep->used = TRUE;                   /* this spot is now in use */
	X    ep->next = 0;                      /* no duplicates after this yet */
	X    ep->predecessor = pred;            /* note code of preceeding string */
	X    ep->follower = foll;               /* note char after string */
	X}
	X
	X/*  This algorithm encoded a file into twelve bit strings (three nybbles).
	X    The gocode() routine is used to read these strings a byte (or two)
	X    at a time.
	X*/
	X
	Xstatic gocode(fd)                      /* read in a twelve bit code */
	XFILE *fd;                              /* file to get code from */
	X{
	X    unsigned int localbuf, returnval;
	X
	X    if(inbuf==EMPTY)                   /* if on a code boundary */
	X    {    if((localbuf=getc_unp(fd))==EOF)   /* get start of next code */
	X              return EOF;              /* pass back end of file status */
	X         localbuf &= 0xFF;             /* mask down to true byte value */
	X         if((inbuf=getc_unp(fd))==EOF) /* get end of code, start of next */
	X              return EOF;              /* this should never happen */
	X         inbuf &= 0xFF;                /* mask down to true byte value */
	X
	X         returnval = ((localbuf<<4)&0xFF0) + ((inbuf>>4)&0x00F);
	X         inbuf &= 0x000F;              /* leave partial code pending */
	X    }
	X
	X    else                               /* buffer contains first nybble */
	X    {    if((localbuf=getc_unp(fd))==EOF)
	X              return EOF;
	X         localbuf &= 0xFF;
	X
	X         returnval = localbuf + ((inbuf<<8)&0xF00);
	X         inbuf = EMPTY;                /* note no hanging nybbles */
	X    }
	X    return returnval;                  /* pass back assembled code */
	X}
	X
	Xstatic push(c)                         /* push char onto stack */
	Xint c;                                 /* character to push */
	X{
	X    stack[sp] = ((char) c);            /* coerce integer into a char */
	X
	X    if(++sp >= TABSIZE)
	X         abort("Stack overflow\n");
	X}
	X
	Xstatic int pop()                       /* pop character from stack */
	X{
	X    if(sp>0)
	X         return ((int) stack[--sp]);   /* leave ptr at next empty slot */
	X
	X    else return EMPTY;
	X}
	X
	X/***** LEMPEL-ZEV DECOMPRESSION *****/
	X
	Xstatic int code_count;                 /* needed to detect table full */
	Xstatic unsigned code;                  /* where we are so far */
	Xstatic int firstc;                     /* true only on first character */
	X
	Xinit_ucr(new)                          /* get set for uncrunching */
	Xint new;                               /* true to use new hash function */
	X{
	X    if(new)                            /* set proper hash function */
	X         h = newh;
	X    else h = oldh;
	X
	X    sp = 0;                            /* clear out the stack */
	X    init_tab();                        /* set up atomic code definitions */
	X    code_count = TABSIZE - 256;        /* note space left in table */
	X    firstc = 1;                        /* true only on first code */
	X}
	X
	Xint getc_ucr(f)                        /* get next uncrunched byte */
	XFILE *f;                               /* file containing crunched data */
	X{
	X    unsigned int c;                    /* a character of input */
	X int code, newcode;
	X    static int oldcode, finchar;
	X    struct entry *ep;                  /* allows faster table handling */
	X
	X    if(firstc)                         /* first code is always known */
	X    {    firstc = FALSE;               /* but next will not be first */
	X         oldcode = gocode(f);
	X         return finchar = string_tab[oldcode].follower;
	X    }
	X
	X    if(!sp)                            /* if stack is empty */
	X    {    if((code=newcode=gocode(f))==EOF)
	X              return EOF;
	X
	X         ep = &string_tab[code];       /* initialize pointer */
	X
	X         if(!ep->used)                 /* if code isn't known */
	X         {    code = oldcode;
	X              ep = &string_tab[code];  /* re-initialize pointer */
	X              push(finchar);
	X         }
	X
	X         while(ep->predecessor!=NO_PRED)
	X         {    push(ep->follower);      /* decode string backwards */
	X              code = ep->predecessor;
	X              ep = &string_tab[code];
	X         }
	X
	X         push(finchar=ep->follower);   /* save first character also */
	X
	X         /*   The above loop will terminate, one way or another,
	X              with string_tab[code].follower equal to the first
	X              character in the string.
	X         */
	X
	X         if(code_count)                /* if room left in string table */
	X         {    upd_tab(oldcode,finchar);
	X              --code_count;
	X         }
	X
	X         oldcode = newcode;
	X    }
	X
	X    return pop();                      /* return saved character */
	X}
SHAR_EOF
if test 27190 -ne "`wc -c < 'arclzw.c'`"
then
	echo shar: "error transmitting 'arclzw.c'" '(should have been 27190 characters)'
fi
fi
echo shar: "extracting 'arcmatch.c'" '(3092 characters)'
if test -f 'arcmatch.c'
then
	echo shar: "will not over-write existing file 'arcmatch.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcmatch.c'
	Xstatic char *RCSid = "$Header: arcmatch.c,v 1.2 86/07/15 07:53:42 turner Exp $";
	X
	X/*
	X * $Log:	arcmatch.c,v $
	X * Revision 1.2  86/07/15  07:53:42  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:34  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCMATCH
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.17), created on $tag(
	XTED_DATE DB =12/17/85) at $tag(
	XTED_TIME DB =20:32:18))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains service routines needed to maintain an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xint match(n,t)                         /* test name against template */
	Xchar *n;                               /* name to test */
	Xchar *t;                               /* template to test against */
	X{
	X
	X#if MSDOS
	X    upper(n); upper(t);                /* avoid case problems */
	X#endif
	X
	X    /* first match name part */
	X
	X    while((*n && *n!='.') || (*t && *t!='.'))
	X    {    if(*n!=*t && *t!='?')         /* match fail? */
	X         {    if(*t!='*')              /* wildcard fail? */
	X                   return 0;           /* then no match */
	X              else                     /* else jump over wildcard */
	X              {    while(*n && *n!='.')
	X                        n++;
	X                   while(*t && *t!='.')
	X                        t++;
	X                   break;              /* name part matches wildcard */
	X              }
	X         }
	X         else                          /* match good for this char */
	X         {    n++;                     /* advance to next char */
	X              t++;
	X         }
	X    }
	X
	X    if(*n && *n=='.') n++;             /* skip extension delimiters */
	X    if(*t && *t=='.') t++;
	X
	X    /* now match name part */
	X
	X    while(*n || *t)
	X    {    if(*n!=*t && *t!='?')         /* match fail? */
	X         {    if(*t!='*')              /* wildcard fail? */
	X                   return 0;           /* then no match */
	X              else return 1;           /* else good enough */
	X         }
	X         else                          /* match good for this char */
	X         {    n++;                     /* advance to next char */
	X              t++;
	X         }
	X    }
	X
	X    return 1;                          /* match worked */
	X}
	X
	Xrempath(nargs,arg)                     /* remove paths from filenames */
	Xint nargs;                             /* number of names */
	Xchar *arg[];                           /* pointers to names */
	X{
	X    char *i, *rindex();                /* string index, reverse indexer */
	X int n;                             /* index */
	X
	X    for(n=0; n<nargs; n++)             /* for each supplied name */
	X    {    if(!(i=rindex(arg[n],'\\')))  /* search for end of path */
	X              if(!(i=rindex(arg[n],'/')))
	X                   i=rindex(arg[n],':');
	X         if(i)                         /* if path was found */
	X              arg[n] = i+1;            /* then skip it */
	X    }
	X}
SHAR_EOF
if test 3092 -ne "`wc -c < 'arcmatch.c'`"
then
	echo shar: "error transmitting 'arcmatch.c'" '(should have been 3092 characters)'
fi
fi
echo shar: "extracting 'arcmisc.c'" '(1348 characters)'
if test -f 'arcmisc.c'
then
	echo shar: "will not over-write existing file 'arcmisc.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcmisc.c'
	X#include <stdio.h>
	X#include "arc.h"
	X
	X/*	split up a file name (subroutine for makefnam)
	X*/
	X
	Xstatic _makefn(source,dest)
	Xunsigned char *source;
	Xunsigned char *dest;
	X{
	X int j;
	X
	X    setmem (dest, 17, 0);	/* clear result field */
	X    if (strlen (source) > 1 && source[1] == ':')
	X	for (j = 0; j < 2;)
	X	    dest[j++] = *source++;
	X    for (j = 3; *source && *source != '.'; ++source)
	X	if (j < 11)
	X	    dest[j++] = *source;
	X    for (j = 12; *source; ++source)
	X	if (j < 16)
	X	    dest[j++] = *source;
	X}
	X/*	make a file name using a template
	X*/
	X
	Xchar *makefnam(rawfn,template,result)
	Xunsigned char *rawfn;			/* the original file name */
	Xunsigned char *template;		/* the template data */
	Xunsigned char *result;			/* where to place the result */
	X{
	X  unsigned char et[17],er[17];
	X
	X  _makefn(template,et);
	X  _makefn(rawfn,er);
	X  *result=0;			/* assure no data */
	X  strcat(result,er[0]?er:et);
	X  strcat(result,er[3]?er+3:et+3);
	X  strcat(result,er[12]?er+12:et+12);
	X  return (&result[0]);
	X}
	X
	Xfreedir(dirs)
	Xregister struct direct **dirs;
	X{
	X	register int ii;
	X
	X	if(dirs == (struct direct **)0)
	X		return(-1);
	X	for(ii = 0; dirs[ii] != (struct direct *)0; ii++)
	X		free(dirs[ii]);
	X	free(dirs);
	X	return(0);
	X}
	X
	X#if MSDOS
	X#include <dir.h>
	X
	Xint alphasort(dirptr1, dirptr2)
	Xstruct direct **dirptr1, **dirptr2;
	X{
	X	return(strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
	X}
	X
	X#endif
SHAR_EOF
if test 1348 -ne "`wc -c < 'arcmisc.c'`"
then
	echo shar: "error transmitting 'arcmisc.c'" '(should have been 1348 characters)'
fi
fi
echo shar: "extracting 'arconvt.c'" '(4350 characters)'
if test -f 'arconvt.c'
then
	echo shar: "will not over-write existing file 'arconvt.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arconvt.c'
	X#define BSD
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xmain(argc,argv)
	Xint argc;
	Xchar *argv[];
	X{
	X    int i,j,k;
	X    FILE *infile,*outfile,*fopen();
	X    char *inbuf;
	X    struct heads inhdr,outhdr;
	X
	X    if(argc < 3) {
	X	fprintf(stderr,"Usage is: arconvt infile outfile\n");
	X	exit(1);
	X    }
	X
	X    infile = fopen(argv[1],"r");
	X    if(infile == NULL) {
	X	fprintf(stderr,"Cannot open input file '%s'\n",argv[0]);
	X	exit(1);
	X    }
	X
	X    outfile = fopen(argv[2],"w");
	X    if(outfile == NULL) {
	X	fprintf(stderr,"Cannot open output file '%s'\n",argv[1]);
	X	exit(1);
	X    }
	X
	X    inbuf = NULL;
	X
	X    while(readhdr(&inhdr,infile)) {
	X	strcpy(outhdr.name,inhdr.name);
	X	outhdr.size = inhdr.size;
	X	outhdr.date = inhdr.date;
	X	outhdr.time = inhdr.time;
	X	outhdr.crc  = inhdr.crc;
	X	outhdr.length = inhdr.length;
	X	writehdr(&outhdr,outfile);
	X	if(inbuf)
	X	    free(inbuf);
	X	inbuf=(char *)malloc(outhdr.size);
	X	fread(inbuf,sizeof(char),outhdr.size,infile);
	X	fwrite(inbuf,sizeof(char),outhdr.size,outfile);
	X    }
	X}
	Xwritehdr(hdr,f)                        /* write a header to an archive */
	Xstruct heads *hdr;                     /* header to write */
	XFILE *f;                               /* archive to write to */
	X{
	X    fputc(ARCMARK,f);                 /* write out the mark of ARC */
	X    fputc(hdrver,f);                   /* write out the header version */
	X    if(!hdrver)                        /* if that's the end */
	X         return;                       /* then write no more */
	X    fwrite(hdr,sizeof(struct heads),1,f);
	X
	X    /* note the newest file for updating the archive timestamp */
	X
	X    if(hdr->date>arcdate
	X    ||(hdr->date==arcdate && hdr->time>arctime))
	X    {    arcdate = hdr->date;
	X         arctime = hdr->time;
	X    }
	X}
	Xint readhdr(hdr,f)                     /* read a header from an archive */
	Xstruct heads *hdr;                     /* storage for header */
	XFILE *f;                               /* archive to read header from */
	X{
	X    char name[FNLEN];                 /* filename buffer */
	X    int try = 0;                       /* retry counter */
	X    static int first = 1;              /* true only on first read */
	X    char dummy[28];
	X    int i,j,k;
	X
	X    if(!f)                             /* if archive didn't open */
	X         return 0;                     /* then pretend it's the end */
	X    if(feof(f))                        /* if no more data */
	X         return 0;                     /* then signal end of archive */
	X
	X    if(fgetc(f)!=ARCMARK)             /* check archive validity */
	X    {    if(warn)
	X         {    printf("An entry in %s has a bad header.",arcname);
	X              nerrs++;
	X         }
	X
	X         while(!feof(f))
	X         {    try++;
	X              if(fgetc(f)==ARCMARK)
	X              {    ungetc(hdrver=fgetc(f),f);
	X                   if(hdrver>=0 && hdrver<=ARCVER)
	X                        break;
	X              }
	X         }
	X
	X         if(feof(f) && first)
	X              abort("%s is not an archive",arcname);
	X
	X         if(warn)
	X              printf("  %d bytes skipped.\n",try);
	X
	X         if(feof(f))
	X              return 0;
	X    }
	X
	X    hdrver = fgetc(f);                 /* get header version */
	X    if(hdrver<0)
	X         abort("Invalid header in archive %s",arcname);
	X    if(hdrver==0)
	X         return 0;                     /* note our end of archive marker */
	X    if(hdrver>ARCVER)
	X    {    fread(name,sizeof(char),FNLEN,f);
	X         printf("I don't know how to handle file %s in archive %s\n",
	X              name,arcname);
	X         printf("I think you need a newer version of ARC.\n");
	X         exit(1);
	X    }
	X
	X    /* amount to read depends on header type */
	X
	X    if(hdrver==1)                      /* old style is shorter */
	X    {    fread(hdr,sizeof(struct heads)-sizeof(long int),1,f);
	X         hdrver = 2;                   /* convert header to new format */
	X         hdr->length = hdr->size;      /* size is same when not packed */
	X    }
	X    else { 
	X	fread(dummy,27,1,f);
	X	for(i=0;i<13;hdr->name[i]=dummy[i],i++);
	X	hdr->size = (long)((dummy[16]<<24) + (dummy[15]<<16) + (dummy[14]<<8)
	X	    + dummy[13]);
	X	hdr->date = (unsigned short)((dummy[18]<<8) + dummy[17]);
	X	hdr->time = (unsigned short)((dummy[20]<<8) + dummy[19]);
	X	hdr->crc  = (short)((dummy[22]<<8) + dummy[21]);
	X	hdr->length = (long)((dummy[26]<<24) + (dummy[25]<<16)
	X	    + (dummy[24]<<8) + dummy[23]);
	X	}
	X
	X    first = 0; return 1;               /* we read something */
	X}
SHAR_EOF
if test 4350 -ne "`wc -c < 'arconvt.c'`"
then
	echo shar: "error transmitting 'arconvt.c'" '(should have been 4350 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
----
	"I ain't gay, but there are sure times when i wish i could say
		that i wasn't straight"

Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL

turner@imagen.UUCP (08/05/86)

This is my hacked version of the arc program that was posted to the net, I
have modified it to run under Unix BSD4.2. if you want it to run under 
SVR2 you probably will have to change the opendir, scandir, and closedir
calls, I don't really know System 5. IMPORTANT: this is a BETA test version
i doubt severly that there are no bugs, please report any that you find to me.

1) I don't have a PC handy so i have no idea if it still works on the PC; to
	compile it for the PC edit arc.h and arcs.c and change the #defines,
	the define ST is for the Atari 520ST and with luck that will be done
	shortly. If you compile it for the PC you will need the routines for 
	opendir etc. that are in shar part 5, otherwise you can ignore that 
	shar file.
2) the Makefile is configured to compile for debugging (-g) (shows ya what 
	kind of faith i have in my code (:-)
3) everything seems to work for both arc files created on the PC and on BSD4.2
	with the exception of the t (test) option which gives strange results,
	i suspect the problem is in the code not the archive
4) there is one bug that i know of, if you archive a 0 length file, arc will
	blow up (floating point exception) when you try to extract it; the
	question is should the check happen when you add the file, extract
	it, or should if create a 0 length file ? you decide.
5) send flames etc. to me, because of the way postnews works here my signature
	will probably be at the end of each shar file, if not its:
----
Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL
----
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	arcpack.c
#	arcrun.c
#	arcs.c
#	arcsq.c
#	arcsvc.c
# This archive created: Mon Aug  4 14:46:18 1986
# By:	D'arc Angel (The Houses of the Holy)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'arcpack.c'" '(9695 characters)'
if test -f 'arcpack.c'
then
	echo shar: "will not over-write existing file 'arcpack.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcpack.c'
	Xstatic char *RCSid = "$Header: arcpack.c,v 1.2 86/07/15 07:53:48 turner Exp $";
	X
	X/*
	X * $Log:	arcpack.c,v $
	X * Revision 1.2  86/07/15  07:53:48  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:37  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCPACK
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =3.37), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:58:01))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to compress a file
	X         when placing it in an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	X/* stuff for non-repeat packing */
	X
	X#define DLE 0x90                       /* repeat sequence marker */
	X
	Xstatic unsigned char state;            /* current packing state */
	X
	X/* non-repeat packing states */
	X
	X#define NOHIST  0                      /* don't consider previous input*/
	X#define SENTCHAR 1                     /* lastchar set, no lookahead yet */
	X#define SENDNEWC 2                     /* run over, send new char next */
	X#define SENDCNT 3                      /* newchar set, send count next */
	X
	X/* packing results */
	X
	Xstatic long stdlen;                    /* length for standard packing */
	Xstatic int crcval;                     /* CRC check value */
	X
	Xpack(f,t,hdr)                          /* pack file into an archive */
	XFILE *f, *t;                           /* source, destination */
	Xstruct heads *hdr;                     /* pointer to header data */
	X{
	X int c;                             /* one character of stream */
	X    long ncrlen;                       /* length after packing */
	X    long huflen;                       /* length after squeezing */
	X    long lzwlen;                       /* length after crunching */
	X    long pred_sq(), file_sq();         /* stuff for squeezing */
	X    long pred_cm();                    /* dynamic crunching cleanup */
	X    char tnam[STRLEN];                /* temporary name buffer */
	X    char *makefnam();                  /* filename fixer upper */
	X    FILE *crn = NULL;                  /* temporary crunch file */
	X
	X    /* first pass - see which method is best */
	X
	X    if(!nocomp)                        /* if storage kludge not active */
	X    {    if(note)
	X              printf(" analyzing, ");
	X
	X         if(arctemp)                   /* use temp area if specified */
	X              sprintf(tnam,"%s$ARCTEMP.CRN",arctemp);
	X         else makefnam("$ARCTEMP.CRN",arcname,tnam);
	X#if MSDOS
	X         crn = fopen(tnam,"wrb");
	X#endif
	X#if BSD | ST
	X         crn = fopen(tnam,"w+");
	X#endif
	X         state = NOHIST;               /* initialize ncr packing */
	X         stdlen =  ncrlen = 0;         /* reset size counters */
	X         crcval = 0;                   /* initialize CRC check value */
	X         setcode();                    /* initialize encryption */
	X
	X         init_cm(f,crn);               /* initialize for crunching */
	X         init_sq();                    /* initialize for squeeze scan */
	X
	X         while((c=getc_ncr(f))!=EOF)   /* for each byte of file */
	X         {    ncrlen++;                /* one more packed byte */
	X              scan_sq(c);              /* see what squeezing can do */
	X              putc_cm(c,crn);          /* see what crunching can do */
	X         }
	X         huflen = pred_sq();           /* finish up after squeezing */
	X         lzwlen = pred_cm(crn);        /* finish up after crunching */
	X    }
	X    else                               /* else kludge the method */
	X    {    stdlen = 0;                   /* make standard look best */
	X         ncrlen = huflen = lzwlen = 1;
	X    }
	X
	X    /* standard set-ups common to all methods */
	X
	X    fseek(f,0L,0);                     /* rewind input */
	X    hdr->crc = crcval;                 /* note CRC check value */
	X    hdr->length = stdlen;              /* set actual file length */
	X    state = NOHIST;                    /* reinitialize ncr packing */
	X    setcode();                         /* reinitialize encryption */
	X
	X    /* choose and use the shortest method */
	X
	X    if(stdlen<=ncrlen && stdlen<=huflen && stdlen<=lzwlen)
	X    {    if(kludge)                    /*DEBUG*/
	X              printf("(%ld) ",lzwlen-stdlen);
	X         if(note)
	X              printf("storing, ");     /* store without compression */
	X         hdrver = 2;                   /* note packing method */
	X         stdlen = crcval = 0;          /* recalc these for kludge */
	X         while((c=getch(f))!=EOF)      /* store it straight */
	X              putc_pak(c,t);
	X         hdr->crc = crcval;
	X         hdr->length = hdr->size = stdlen;
	X    }
	X
	X    else if(ncrlen<huflen && ncrlen<lzwlen)
	X    {    if(kludge)                    /*DEBUG*/
	X              printf("(%ld) ",lzwlen-ncrlen);
	X         if(note)
	X              printf("packing, ");     /* pack with repeat suppression */
	X         hdrver = 3;                   /* note packing method */
	X         hdr->size = ncrlen;           /* set data length */
	X         while((c=getc_ncr(f))!=EOF)
	X              putc_pak(c,t);
	X    }
	X
	X    else if(huflen<lzwlen)
	X    {    if(kludge)                    /*DEBUG*/
	X              printf("(%ld) ",lzwlen-huflen);
	X         if(note)
	X              printf("squeezing, ");
	X         hdrver = 4;                   /* note packing method */
	X         hdr->size = file_sq(f,t);     /* note final size */
	X    }
	X
	X    else
	X    {    if(kludge)                    /*DEBUG*/
	X              printf("(%ld) ",huflen-lzwlen);
	X         if(note)
	X              printf("crunching, ");
	X         hdrver = 8;
	X         hdr->size = lzwlen;           /* size should not change */
	X         if(crn)                       /* if temp was created */
	X         {    fseek(crn,0L,0);         /* then copy over crunched temp */
	X              while((c=fgetc(crn))!=EOF)
	X                   putc_tst(c,t);
	X         }
	X         else                          /* else re-crunch */
	X         {    init_cm(f,t);
	X              while((c=getc_ncr(f))!=EOF)
	X                   putc_cm(c,t);
	X              pred_cm(t);              /* finish up after crunching */
	X         }
	X    }
	X
	X    /* standard cleanups common to all methods */
	X
	X    if(crn)                            /* get rid of crunch temporary */
	X    {    fclose(crn);
	X         if(unlink(tnam) && warn)
	X         {    printf("Cannot delete temporary file %s\n",tnam);
	X              nerrs++;
	X         }
	X    }
	X    if(note)
	X         printf("done.\n");
	X}
	X
	X/*  Non-repeat compression - text is passed through normally, except that
	X    a run of more than two is encoded as:
	X
	X         <char> <DLE> <count>
	X
	X    Special case: a count of zero indicates that the DLE is really a DLE,
	X    not a repeat marker.
	X*/
	X
	Xint getc_ncr(f)                        /* get bytes with collapsed runs */
	XFILE *f;                               /* file to get from */
	X{
	X    static int lastc;                  /* value returned on last call */
	X    static int repcnt;                 /* repetition counter */
	X    static int c;                      /* latest value seen */
	X
	X    switch(state)                      /* depends on our state */
	X    {
	X    case NOHIST:                       /* no relevant history */
	X         state = SENTCHAR;
	X         return lastc = getch(f);      /* remember the value next time */
	X
	X    case SENTCHAR:                     /* char was sent. look ahead */
	X         switch(lastc)                 /* action depends on char */
	X         {
	X         case DLE:                     /* if we sent a real DLE */
	X              state = NOHIST;          /* then start over again */
	X              return 0;                /* but note that the DLE was real */
	X
	X         case EOF:                     /* EOF is always a special case */
	X              return EOF;
	X
	X         default:                      /* else test for a repeat */
	X              for(repcnt=1; (c=getch(f))==lastc && repcnt<255; repcnt++)
	X                   ;                   /* find end of run */
	X
	X              switch(repcnt)           /* action depends on run size */
	X              {
	X              case 1:                  /* not a repeat */
	X                   return lastc = c;   /* but remember value next time */
	X
	X              case 2:                  /* a repeat, but too short */
	X                   state = SENDNEWC;   /* send the second one next time */
	X                   return lastc;
	X
	X              default:                 /* a run - compress it */
	X                   state = SENDCNT;    /* send repeat count next time */
	X                   return DLE;         /* send repeat marker this time */
	X              }
	X         }
	X
	X    case SENDNEWC:                     /* send second char of short run */
	X         state = SENTCHAR;
	X         return lastc = c;
	X
	X    case SENDCNT:                      /* sent DLE, now send count */
	X         state = SENDNEWC;
	X         return repcnt;
	X
	X    default:
	X         abort("Bug - bad ncr state\n");
	X    }
	X}
	X
	Xstatic int getch(f)                    /* special get char for packing */
	XFILE *f;                               /* file to get from */
	X{
	X int c;                             /* a char from the file */
	X
	X    if((c=fgetc(f))!=EOF)              /* if not the end of file */
	X    {    crcval = addcrc(crcval,c);    /* then update CRC check value */
	X         stdlen++;                     /* and bump length counter */
	X    }
	X
	X    return c;
	X}
	X
	Xputc_pak(c,f)                          /* put a packed byte into archive */
	Xchar c;                                /* byte to put */
	XFILE *f;                               /* archive to put it in */
	X{
	X    putc_tst(code(c),f);               /* put encoded byte, with checks */
	X}
SHAR_EOF
if test 9695 -ne "`wc -c < 'arcpack.c'`"
then
	echo shar: "error transmitting 'arcpack.c'" '(should have been 9695 characters)'
fi
fi
echo shar: "extracting 'arcrun.c'" '(4623 characters)'
if test -f 'arcrun.c'
then
	echo shar: "will not over-write existing file 'arcrun.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcrun.c'
	Xstatic char *RCSid = "$Header: arcrun.c,v 1.2 86/07/15 07:53:55 turner Exp $";
	X
	X/*
	X * $Log:	arcrun.c,v $
	X * Revision 1.2  86/07/15  07:53:55  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:40  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCRUN
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =1.17), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =22:59:06))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to "run" a file
	X         which is stored in an archive.  At present, all we really do
	X         is (a) extract a temporary file, (b) give its name as a system
	X         command, and then (c) delete the file.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xrunarc(num,arg)                        /* run file from archive */
	Xint num;                               /* number of arguments */
	Xchar *arg[];                           /* pointers to arguments */
	X{
	X    struct heads hdr;                  /* file header */
	X int run;                           /* true to run current file */
	X int did[MAXARG];                  /* true when argument was used */
	X int n;                             /* index */
	X    char *makefnam();                  /* filename fixer */
	X    char buf[STRLEN];                 /* filename buffer */
	X    FILE *fopen();                     /* file opener */
	X
	X    for(n=0; n<num; n++)               /* for each argument */
	X         did[n] = 0;                   /* reset usage flag */
	X    rempath(num,arg);                  /* strip off paths */
	X
	X    openarc(0);                        /* open archive for reading */
	X
	X    if(num)                            /* if files were named */
	X    {    while(readhdr(&hdr,arc))      /* while more files to check */
	X         {    run = 0;                 /* reset run flag */
	X              for(n=0; n<num; n++)     /* for each template given */
	X              {    if(match(hdr.name,makefnam(arg[n],".*",buf)))
	X                   {    run = 1;       /* turn on run flag */
	X                        did[n] = 1;    /* turn on usage flag */
	X                        break;         /* stop looking */
	X                   }
	X              }
	X
	X              if(run)                  /* if running this one */
	X                   runfile(&hdr);      /* then do it */
	X              else                     /* else just skip it */
	X                   fseek(arc,hdr.size,1);
	X         }
	X    }
	X
	X    else while(readhdr(&hdr,arc))      /* else run all files */
	X         runfile(&hdr);
	X
	X    closearc(0);                       /* close archive after changes */
	X
	X    if(note)
	X    {    for(n=0; n<num; n++)          /* report unused args */
	X         {    if(!did[n])
	X              {    printf("File not found: %s\n",arg[n]);
	X                   nerrs++;
	X              }
	X         }
	X    }
	X}
	X
	Xstatic runfile(hdr)                    /* run a file */
	Xstruct heads *hdr;                     /* pointer to header data */
	X{
	X    FILE *tmp, *fopen();               /* temporary file */
	X    char buf[STRLEN], *makefnam();    /* temp file name, fixer */
	X    char sys[STRLEN];                 /* invocation command buffer */
	X    char *dir, *gcdir();               /* directory stuff */
	X
	X    makefnam("$ARCTEMP",hdr->name,buf);
	X
	X    if(!strcmp(buf,"$ARCTEMP.BAS"))
	X         strcpy(sys,"BASICA $ARCTEMP");
	X
	X    else if(!strcmp(buf,"$ARCTEMP.BAT")
	X         || !strcmp(buf,"$ARCTEMP.COM")
	X         || !strcmp(buf,"$ARCTEMP.EXE"))
	X         strcpy(sys,"$ARCTEMP");
	X
	X    else
	X    {    if(warn)
	X         {    printf("File %s is not a .BAS, .BAT, .COM, or .EXE\n",
	X                   hdr->name);
	X              nerrs++;
	X         }
	X         fseek(arc,hdr->size,1);  /* skip this file */
	X         return;
	X    }
	X
	X    if(warn)
	X         if(tmp=fopen(buf,"rb"))
	X              abort("Temporary file %s already exists",buf);
	X    if(!(tmp=fopen(makefnam("$ARCTEMP",hdr->name,buf),"wrb")))
	X         abort("Unable to create temporary file %s",buf);
	X
	X    if(note)
	X         printf("Invoking file: %s\n",hdr->name);
	X
	X    dir = gcdir("");                   /* see where we are */
	X    unpack(arc,tmp,hdr);               /* unpack the entry */
	X    fclose(tmp);                       /* release the file */
	X    system(sys);                       /* try to invoke it */
	X    chdir(dir); free(dir);             /* return to whence we started */
	X    if(unlink(buf) && warn)
	X    {    printf("Cannot unsave temporary file %s\n",buf);
	X         nerrs++;
	X    }
	X}
SHAR_EOF
if test 4623 -ne "`wc -c < 'arcrun.c'`"
then
	echo shar: "error transmitting 'arcrun.c'" '(should have been 4623 characters)'
fi
fi
echo shar: "extracting 'arcs.c'" '(2286 characters)'
if test -f 'arcs.c'
then
	echo shar: "will not over-write existing file 'arcs.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcs.c'
	Xstatic char *RCSid = "$Header: arcs.c,v 1.2 86/07/15 07:53:59 turner Exp $";
	X
	X/*
	X * $Log:	arcs.c,v $
	X * Revision 1.2  86/07/15  07:53:59  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:43  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - Archive file header format
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.13), created on $tag(
	XTED_DATE DB =01/07/86) at $tag(
	XTED_TIME DB =17:17:32))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file defines the format of an archive file header, excluding
	X         the archive marker and the header version number.
	X
	X         Each entry in an archive begins with a one byte archive marker,
	X         which is set to ARCMARK.  The marker is followed by a one byte
	X         header type code, from zero to ARCVER.
	X
	X         If the header type code is zero, then it is an end marker, and
	X         no more data should be read from the archive.
	X
	X         If the header type code is in the range 2 to ARCVER, then it is
	X         followed by a standard archive header, which is defined below.
	X
	X         If the header type code is one, then it is followed by an older
	X         format archive header.  The older format header does not contain
	X         the true length.  A header should be read for a length of
	X         sizeof(struct heads)-sizeof(long).  Then set length equal to size
	X         and change the header version to 2.
	X
	X    Programming note:
	X         The crc value given in the header is based on the unpacked data.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#define BSD   1
	X#define ST    0
	X#define MSDOS 0
	X#if BSD | ST
	X#define INT short
	X#endif
	X
	X#if MSDOS
	X#define INT int
	X#endif
	X
	X#include "arcm.h"
	X
	Xstruct heads                           /* archive entry header format */
	X{   char name[FNLEN];                 /* file name */
	X    long size;                         /* size of file, in bytes */
	X    unsigned INT date;                 /* creation date */
	X    unsigned INT time;                 /* creation time */
	X INT crc;                           /* cyclic redundancy check */
	X    long length;                       /* true file length */
	X}   ;
SHAR_EOF
if test 2286 -ne "`wc -c < 'arcs.c'`"
then
	echo shar: "error transmitting 'arcs.c'" '(should have been 2286 characters)'
fi
fi
echo shar: "extracting 'arcsq.c'" '(16594 characters)'
if test -f 'arcsq.c'
then
	echo shar: "will not over-write existing file 'arcsq.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcsq.c'
	Xstatic char *RCSid = "$Header: arcsq.c,v 1.2 86/07/15 07:54:05 turner Exp $";
	X
	X/*
	X * $Log:	arcsq.c,v $
	X * Revision 1.2  86/07/15  07:54:05  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:48  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCSQ
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =3.10), created on $tag(
	XTED_DATE DB =01/30/86) at $tag(
	XTED_TIME DB =20:10:46))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to squeeze a file
	X         when placing it in an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X
	X    Programming notes:
	X         Most of the routines used for the Huffman squeezing algorithm
	X         were lifted from the SQ program by Dick Greenlaw, as adapted
	X         to CI-C86 by Robert J. Beilstein.
	X*/
	X#include <stdio.h>
	X
	X/* stuff for Huffman squeezing */
	X
	X#define TRUE 1
	X#define FALSE 0
	X#define ERROR (-1)
	X#define SPEOF 256                      /* special endfile token */
	X#define NOCHILD -1                     /* marks end of path through tree */
	X#define NUMVALS 257                    /* 256 data values plus SPEOF*/
	X#define NUMNODES (NUMVALS+NUMVALS-1)   /* number of nodes */
	X#define MAXCOUNT (unsigned) 65535      /* biggest unsigned integer */
	X
	X/* The following array of structures are the nodes of the
	X   binary trees. The first NUMVALS nodes become the leaves of the
	X   final tree and represent the values of the data bytes being
	X   encoded and the special endfile, SPEOF.
	X   The remaining nodes become the internal nodes of the final tree.
	X*/
	X
	Xstruct nd                              /* shared by unsqueezer */
	X{   unsigned weight;                   /* number of appearances */
	X int tdepth;                        /* length on longest path in tree */
	X int lchild, rchild;                /* indices to next level */
	X}   node[NUMNODES];                    /* use large buffer */
	X
	Xstatic int dctreehd;                   /* index to head of final tree */
	X
	X/* This is the encoding table:
	X   The bit strings have first bit in low bit.
	X   Note that counts were scaled so code fits unsigned integer.
	X*/
	X
	Xstatic int codelen[NUMVALS];           /* number of bits in code */
	Xstatic unsigned code[NUMVALS];         /* code itself, right adjusted */
	Xstatic unsigned tcode;                 /* temporary code value */
	Xstatic long valcount[NUMVALS];         /* actual count of times seen */
	X
	X/* Variables used by encoding process */
	X
	Xstatic int curin;                      /* value currently being encoded */
	Xstatic int cbitsrem;                   /* # of code string bits left */
	Xstatic unsigned ccode;                 /* current code right justified */
	X
	Xinit_sq()                              /* prepare for scanning pass */
	X{
	X int i;                             /* node index */
	X
	X    /* Initialize all nodes to single element binary trees
	X       with zero weight and depth.
	X    */
	X
	X    for(i=0; i<NUMNODES; ++i)
	X    {    node[i].weight = 0;
	X         node[i].tdepth = 0;
	X         node[i].lchild = NOCHILD;
	X         node[i].rchild = NOCHILD;
	X    }
	X
	X    for(i=0; i<NUMVALS; i++)
	X         valcount[i] = 0;
	X}
	X
	Xscan_sq(c)                             /* add a byte to the tables */
	Xint c;                                 /* byte to add */
	X{
	X    unsigned *wp;                      /* speeds up weight counting */
	X
	X    /* Build frequency info in tree */
	X
	X    if(c == EOF)                       /* it's traditional */
	X         c = SPEOF;                    /* dumb, but traditional */
	X
	X    if(*(wp = &node[c].weight) !=  MAXCOUNT)
	X         ++(*wp);                      /* bump weight counter */
	X
	X    valcount[c]++;                     /* bump byte counter */
	X}
	X
	Xlong pred_sq()                         /* predict size of squeezed file */
	X{
	X int i;
	X int btlist[NUMVALS];               /* list of intermediate b-trees */
	X int listlen;                       /* length of btlist */
	X    unsigned ceiling;                  /* limit for scaling */
	X    long size = 0;                     /* predicted size */
	X int numnodes;                      /* # of nodes in simplified tree */
	X
	X    scan_sq(EOF);                      /* signal end of input */
	X
	X    ceiling = MAXCOUNT;
	X
	X    /* Keep trying to scale and encode */
	X
	X    do
	X    {    scale(ceiling);
	X         ceiling /= 2;                 /* in case we rescale */
	X
	X         /* Build list of single node binary trees having
	X            leaves for the input values with non-zero counts
	X         */
	X
	X         for(i=listlen=0; i<NUMVALS; ++i)
	X         {    if(node[i].weight != 0)
	X              {    node[i].tdepth = 0;
	X                   btlist[listlen++] = i;
	X              }
	X         }
	X
	X         /* Arrange list of trees into a heap with the entry
	X            indexing the node with the least weight at the top.
	X         */
	X
	X         heap(btlist,listlen);
	X
	X         /* Convert the list of trees to a single decoding tree */
	X
	X         bld_tree(btlist,listlen);
	X
	X         /* Initialize the encoding table */
	X
	X         init_enc();
	X
	X         /* Try to build encoding table.
	X            Fail if any code is > 16 bits long.
	X         */
	X    }    while(buildenc(0,dctreehd) == ERROR);
	X
	X    /* Initialize encoding variables */
	X
	X    cbitsrem = 0;                      /* force initial read */
	X    curin = 0;                         /* anything but endfile */
	X
	X    for(i=0; i<NUMVALS; i++)           /* add bits for each code */
	X         size += valcount[i] * codelen[i];
	X
	X    size = (size+7)/8;                 /* reduce to number of bytes */
	X
	X    numnodes = dctreehd<NUMVALS ? 0 : dctreehd-(NUMVALS-1);
	X
	X    size += sizeof(int) + 2*numnodes*sizeof(int);
	X
	X    return size;
	X}
	X
	X/* The count of number of occurrances of each input value
	X   have already been prevented from exceeding MAXCOUNT.
	X   Now we must scale them so that their sum doesn't exceed
	X   ceiling and yet no non-zero count can become zero.
	X   This scaling prevents errors in the weights of the
	X   interior nodes of the Huffman tree and also ensures that
	X   the codes will fit in an unsigned integer. Rescaling is
	X   used if necessary to limit the code length.
	X*/
	X
	Xstatic scale(ceil)
	Xunsigned ceil;                         /* upper limit on total weight */
	X{
	X    register int i,c;
	X int ovflw, divisor;
	X    unsigned w, sum;
	X    unsigned char increased;           /* flag */
	X
	X    do
	X    {    for(i=sum=ovflw=0; i<NUMVALS; ++i)
	X         {    if(node[i].weight > (ceil-sum))
	X                   ++ovflw;
	X              sum += node[i].weight;
	X         }
	X
	X         divisor = ovflw + 1;
	X
	X         /* Ensure no non-zero values are lost */
	X
	X         increased = FALSE;
	X         for(i=0; i<NUMVALS; ++i)
	X         {    w = node[i].weight;
	X              if(w<divisor && w!=0)
	X              {    /* Don't fail to provide a code if it's used at all */
	X
	X                   node[i].weight = divisor;
	X                   increased = TRUE;
	X              }
	X         }
	X    }    while(increased);
	X
	X    /* Scaling factor choosen, now scale */
	X
	X    if(divisor>1)
	X         for(i=0; i<NUMVALS; ++i)
	X              node[i].weight /= divisor;
	X}
	X
	X/* heap() and adjust() maintain a list of binary trees as a
	X   heap with the top indexing the binary tree on the list
	X   which has the least weight or, in case of equal weights,
	X   least depth in its longest path. The depth part is not
	X   strictly necessary, but tends to avoid long codes which
	X   might provoke rescaling.
	X*/
	X
	Xstatic heap(list,length)
	Xint list[], length;
	X{
	X    register int i;
	X
	X    for(i=(length-2)/2; i>=0; --i)
	X         adjust(list,i,length-1);
	X}
	X
	X/* Make a heap from a heap with a new top */
	X
	Xstatic adjust(list,top,bottom)
	Xint list[], top, bottom;
	X{
	X    register int k, temp;
	X
	X    k = 2 * top + 1;                   /* left child of top */
	X    temp = list[top];                  /* remember root node of top tree */
	X
	X    if(k<=bottom)
	X    {    if(k<bottom && cmptrees(list[k],list[k+1]))
	X              ++k;
	X
	X         /* k indexes "smaller" child (in heap of trees) of top */
	X         /* now make top index "smaller" of old top and smallest child */
	X
	X         if(cmptrees(temp,list[k]))
	X         {    list[top] = list[k];
	X              list[k] = temp;
	X
	X              /* Make the changed list a heap */
	X
	X              adjust(list,k,bottom);   /* recursive */
	X         }
	X    }
	X}
	X
	X/* Compare two trees, if a > b return true, else return false.
	X   Note comparison rules in previous comments.
	X*/
	X
	Xstatic cmptrees(a,b)
	Xint a, b;                              /* root nodes of trees */
	X{
	X    if(node[a].weight > node[b].weight)
	X         return TRUE;
	X    if(node[a].weight == node[b].weight)
	X         if(node[a].tdepth > node[b].tdepth)
	X              return TRUE;
	X    return FALSE;
	X}
	X
	X/* HUFFMAN ALGORITHM: develops the single element trees
	X   into a single binary tree by forming subtrees rooted in
	X   interior nodes having weights equal to the sum of weights of all
	X   their descendents and having depth counts indicating the
	X   depth of their longest paths.
	X
	X   When all trees have been formed into a single tree satisfying
	X   the heap property (on weight, with depth as a tie breaker)
	X   then the binary code assigned to a leaf (value to be encoded)
	X   is then the series of left (0) and right (1)
	X   paths leading from the root to the leaf.
	X   Note that trees are removed from the heaped list by
	X   moving the last element over the top element and
	X   reheaping the shorter list.
	X*/
	X
	Xstatic bld_tree(list,len)
	Xint list[];
	Xint len;
	X{
	X    register int freenode;             /* next free node in tree */
	X    register struct nd *frnp;          /* free node pointer */
	X int lch, rch;                      /* temps for left, right children */
	X int i;
	X
	X    /* Initialize index to next available (non-leaf) node.
	X       Lower numbered nodes correspond to leaves (data values).
	X    */
	X
	X    freenode = NUMVALS;
	X
	X    while(len>1)
	X    {    /* Take from list two btrees with least weight
	X            and build an interior node pointing to them.
	X            This forms a new tree.
	X         */
	X
	X         lch = list[0];                /* This one will be left child */
	X
	X         /* delete top (least) tree from the list of trees */
	X
	X         list[0] = list[--len];
	X         adjust(list,0,len-1);
	X
	X         /* Take new top (least) tree. Reuse list slot later */
	X
	X         rch = list[0];                /* This one will be right child */
	X
	X         /* Form new tree from the two least trees using
	X            a free node as root. Put the new tree in the list.
	X         */
	X
	X         frnp = &node[freenode];       /* address of next free node */
	X         list[0] = freenode++;         /* put at top for now */
	X         frnp->lchild = lch;
	X         frnp->rchild = rch;
	X         frnp->weight = node[lch].weight + node[rch].weight;
	X         frnp->tdepth = 1 + maxchar(node[lch].tdepth, node[rch].tdepth);
	X
	X         /* reheap list  to get least tree at top */
	X
	X         adjust(list,0,len-1);
	X    }
	X    dctreehd = list[0];                /* head of final tree */
	X}
	X
	Xstatic maxchar(a,b)
	X{
	X    return a>b ? a : b;
	X}
	X
	Xstatic init_enc()
	X{
	X    register int i;
	X
	X    /* Initialize encoding table */
	X
	X    for(i=0; i<NUMVALS; ++i)
	X         codelen[i] = 0;
	X}
	X
	X/* Recursive routine to walk the indicated subtree and level
	X   and maintain the current path code in bstree. When a leaf
	X   is found the entire code string and length are put into
	X   the encoding table entry for the leaf's data value .
	X
	X   Returns ERROR if codes are too long.
	X*/
	X
	Xstatic int buildenc(level,root)
	Xint level;              /* level of tree being examined, from zero */
	Xint root;               /* root of subtree is also data value if leaf */
	X{
	X    register int l, r;
	X
	X    l = node[root].lchild;
	X    r = node[root].rchild;
	X
	X    if(l==NOCHILD && r==NOCHILD)
	X    {    /* Leaf. Previous path determines bit string
	X            code of length level (bits 0 to level - 1).
	X            Ensures unused code bits are zero.
	X         */
	X
	X         codelen[root] = level;
	X         code[root] = tcode & (((unsigned)~0) >> (16-level));
	X         return (level>16) ? ERROR : NULL;
	X    }
	X
	X    else
	X    {    if(l!=NOCHILD)
	X         {    /* Clear path bit and continue deeper */
	X
	X              tcode &= ~(1 << level);
	X              if(buildenc(level+1,l)==ERROR)
	X                   return ERROR;       /* pass back bad statuses */
	X         }
	X         if(r!=NOCHILD)
	X         {    /* Set path bit and continue deeper */
	X
	X              tcode |= 1 << level;
	X              if(buildenc(level+1,r)==ERROR)
	X                   return ERROR;       /* pass back bad statuses */
	X         }
	X    }
	X    return NULL;                       /* it worked if we reach here */
	X}
	X
	Xstatic put_int(n,f)                    /* output an integer */
	Xint n;                                 /* integer to output */
	XFILE *f;                               /* file to put it to */
	X{
	X    putc_pak(n&0xff,f);                /* first the low byte */
	X    putc_pak(n>>8,f);                  /* then the high byte */
	X}
	X
	X/* Write out the header of the compressed file */
	X
	Xstatic long wrt_head(ob)
	XFILE *ob;
	X{
	X    register int l,r;
	X int i, k;
	X int numnodes;                      /* # of nodes in simplified tree */
	X
	X    /* Write out a simplified decoding tree. Only the interior
	X       nodes are written. When a child is a leaf index
	X       (representing a data value) it is recoded as
	X       -(index + 1) to distinguish it from interior indexes
	X       which are recoded as positive indexes in the new tree.
	X
	X       Note that this tree will be empty for an empty file.
	X    */
	X
	X    numnodes = dctreehd<NUMVALS ? 0 : dctreehd-(NUMVALS-1);
	X    put_int(numnodes,ob);
	X
	X    for(k=0, i=dctreehd; k<numnodes; ++k, --i)
	X    {    l = node[i].lchild;
	X         r = node[i].rchild;
	X         l = l<NUMVALS ? -(l+1) : dctreehd-l;
	X         r = r<NUMVALS ? -(r+1) : dctreehd-r;
	X         put_int(l,ob);
	X         put_int(r,ob);
	X    }
	X
	X    return sizeof(int) + numnodes*2*sizeof(int);
	X}
	X
	X/* Get an encoded byte or EOF. Reads from specified stream AS NEEDED.
	X
	X   There are two unsynchronized bit-byte relationships here.
	X   The input stream bytes are converted to bit strings of
	X   various lengths via the static variables named c...
	X   These bit strings are concatenated without padding to
	X   become the stream of encoded result bytes, which this
	X   function returns one at a time. The EOF (end of file) is
	X   converted to SPEOF for convenience and encoded like any
	X   other input value. True EOF is returned after that.
	X*/
	X
	Xstatic int gethuff(ib)                 /* Returns bytes except for EOF */
	XFILE *ib;
	X{
	X int rbyte;                         /* Result byte value */
	X int need, take;                    /* numbers of bits */
	X
	X    rbyte = 0;
	X    need = 8;                          /* build one byte per call */
	X
	X    /* Loop to build a byte of encoded data.
	X       Initialization forces read the first time.
	X    */
	X
	Xloop:
	X    if(cbitsrem>=need)                 /* if current code is big enough */
	X    {    if(need==0)
	X              return rbyte;
	X
	X         rbyte |= ccode << (8-need);   /* take what we need */
	X         ccode >>= need;               /* and leave the rest */
	X         cbitsrem -= need;
	X         return rbyte & 0xff;
	X    }
	X
	X    /* We need more than current code */
	X
	X    if(cbitsrem>0)
	X    {    rbyte |= ccode << (8-need);   /* take what there is */
	X         need -= cbitsrem;
	X    }
	X
	X    /* No more bits in current code string */
	X
	X    if(curin==SPEOF)
	X    {    /* The end of file token has been encoded. If
	X            result byte has data return it and do EOF next time.
	X         */
	X
	X         cbitsrem = 0;
	X         return (need==8) ? EOF : rbyte + 0;
	X    }
	X
	X    /* Get an input byte */
	X
	X    if((curin=getc_ncr(ib)) == EOF)
	X         curin = SPEOF;                /* convenient for encoding */
	X
	X    ccode = code[curin];               /* get the new byte's code */
	X    cbitsrem = codelen[curin];
	X
	X    goto loop;
	X}
	X
	X/*  This routine is used to perform the actual squeeze operation.  It can
	X    only be called after the file has been scanned.  It returns the true
	X    length of the squeezed entry.
	X*/
	X
	Xlong file_sq(f,t)                      /* squeeze a file into an archive */
	XFILE *f;                               /* file to squeeze */
	XFILE *t;                               /* archive to receive file */
	X{
	X int c;                             /* one byte of squeezed data */
	X    long size;                         /* size after squeezing */
	X
	X    size = wrt_head(t);                /* write out the decode tree */
	X
	X    while((c=gethuff(f))!=EOF)
	X    {    putc_pak(c,t);
	X         size++;
	X    }
	X
	X    return size;                       /* report true size */
	X}
SHAR_EOF
if test 16594 -ne "`wc -c < 'arcsq.c'`"
then
	echo shar: "error transmitting 'arcsq.c'" '(should have been 16594 characters)'
fi
fi
echo shar: "extracting 'arcsvc.c'" '(5097 characters)'
if test -f 'arcsvc.c'
then
	echo shar: "will not over-write existing file 'arcsvc.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcsvc.c'
	Xstatic char *RCSid = "$Header: arcsvc.c,v 1.2 86/07/15 07:54:19 turner Exp $";
	X
	X/*
	X * $Log:	arcsvc.c,v $
	X * Revision 1.2  86/07/15  07:54:19  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:00:57  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCSVC
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.15), created on $tag(
	XTED_DATE DB =12/17/85) at $tag(
	XTED_TIME DB =15:17:16))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains service routines needed to maintain an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xopenarc(chg)                           /* open archive */
	Xint chg;                               /* true to open for changes */
	X{
	X    FILE *fopen();                     /* file opener */
	X
	X    if(!(arc=fopen(arcname,"rb")))
	X    {    if(chg)
	X              printf("Creating new archive: %s\n",arcname);
	X         else abort("Cannot read archive: %s",arcname);
	X    }
	X
	X    if(chg)                            /* if opening for changes */
	X         if(!(new=fopen(newname,"wb")))
	X              abort("Cannot create archive copy: %s",newname);
	X}
	X
	Xclosearc(chg)                          /* close an archive */
	Xint chg;                               /* true if archive was changed */
	X{
	X    if(arc)                            /* if we had an initial archive */
	X         fclose(arc);                  /* then close it */
	X
	X    if(chg)                            /* if things have changed */
	X    {    setstamp(new,arcdate,arctime);/* archive matches newest file */
	X         fclose(new);                  /* close the new copy */
	X
	X         if(arc)                       /* if we had an original archive */
	X         {    if(keepbak)              /* if a backup is wanted */
	X              {    unlink(bakname);    /* erase any old copies */
	X                   if(rename(arcname,bakname))
	X                        abort("Cannot rename %s to %s",arcname,bakname);
	X                   printf("Keeping backup archive: %s\n",bakname);
	X              }
	X              else if(unlink(arcname))
	X                   abort("Cannot delete old archive: %s",arcname);
	X         }
	X
	X         if(rename(newname,arcname))
	X              abort("Cannot rename %s to %s",newname,arcname);
	X    }
	X}
	X
	X/* CRC computation logic
	X
	X   The logic for this method of calculating the CRC 16 bit polynomial
	X   is taken from an article by David Schwaderer in the April 1985
	X   issue of PC Tech Journal.
	X*/
	X
	Xstatic int crctab[] =                  /* CRC lookup table */
	X{   0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
	X    0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
	X    0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
	X    0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
	X    0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
	X    0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
	X    0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
	X    0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
	X    0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
	X    0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
	X    0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
	X    0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
	X    0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
	X    0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
	X    0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
	X    0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
	X    0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
	X    0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
	X    0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
	X    0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
	X    0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
	X    0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
	X    0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
	X    0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
	X    0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
	X    0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
	X    0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
	X    0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
	X    0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
	X    0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
	X    0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
	X    0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
	X};
	Xint addcrc(crc,c)                      /* update a CRC check */
	Xint crc;                               /* running CRC value */
	Xunsigned char c;                       /* character to add */
	X{
	X    return ((crc>>8)&0x00ff) ^ crctab[(crc^c)&0x00ff];
	X}
SHAR_EOF
if test 5097 -ne "`wc -c < 'arcsvc.c'`"
then
	echo shar: "error transmitting 'arcsvc.c'" '(should have been 5097 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
----
	"I ain't gay, but there are sure times when i wish i could say
		that i wasn't straight"

Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL

turner@imagen.UUCP (08/05/86)

This is my hacked version of the arc program that was posted to the net, I
have modified it to run under Unix BSD4.2. if you want it to run under 
SVR2 you probably will have to change the opendir, scandir, and closedir
calls, I don't really know System 5. IMPORTANT: this is a BETA test version
i doubt severly that there are no bugs, please report any that you find to me.

1) I don't have a PC handy so i have no idea if it still works on the PC; to
	compile it for the PC edit arc.h and arcs.c and change the #defines,
	the define ST is for the Atari 520ST and with luck that will be done
	shortly. If you compile it for the PC you will need the routines for 
	opendir etc. that are in shar part 5, otherwise you can ignore that 
	shar file.
2) the Makefile is configured to compile for debugging (-g) (shows ya what 
	kind of faith i have in my code (:-)
3) everything seems to work for both arc files created on the PC and on BSD4.2
	with the exception of the t (test) option which gives strange results,
	i suspect the problem is in the code not the archive
4) there is one bug that i know of, if you archive a 0 length file, arc will
	blow up (floating point exception) when you try to extract it; the
	question is should the check happen when you add the file, extract
	it, or should if create a 0 length file ? you decide.
5) send flames etc. to me, because of the way postnews works here my signature
	will probably be at the end of each shar file, if not its:
----
Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL
----

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	arctst.c
#	arcunp.c
#	arcusq.c
#	arcvax.c
# This archive created: Mon Aug  4 14:48:03 1986
# By:	D'arc Angel (The Houses of the Holy)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'arctst.c'" '(1724 characters)'
if test -f 'arctst.c'
then
	echo shar: "will not over-write existing file 'arctst.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arctst.c'
	Xstatic char *RCSid = "$Header: arctst.c,v 1.1 86/06/26 15:01:02 turner Exp $";
	X
	X/*
	X * $Log:	arctst.c,v $
	X * Revision 1.1  86/06/26  15:01:02  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCTST
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =2.12), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =23:00:40))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to test archive integrity.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	Xtstarc()                               /* test integrity of an archive */
	X{
	X    struct heads hdr;                  /* file header */
	X    long arcsize, ftell();             /* archive size */
	X
	X    openarc(0);                        /* open archive for reading */
	X    fseek(arc,0L,2);                   /* move to end of archive */
	X    arcsize = ftell(arc);              /* see how big it is */
	X    fseek(arc,0L,0);                   /* return to top of archive */
	X
	X    while(readhdr(&hdr,arc))
	X    {    if(ftell(arc)+hdr.size>arcsize)
	X         {    printf("Archive truncated in file %s\n",hdr.name);
	X              nerrs++;
	X              break;
	X         }
	X
	X         else
	X         {    printf("Testing file: %-12s  ",hdr.name);
	X              if(unpack(arc,NULL,&hdr))
	X                   nerrs++;
	X              else printf("okay\n");
	X         }
	X    }
	X
	X    if(nerrs<1)
	X         printf("No errors detected\n");
	X    else if(nerrs==1)
	X         printf("One error detected\n");
	X    else printf("%d errors detected\n",nerrs);
	X}
SHAR_EOF
if test 1724 -ne "`wc -c < 'arctst.c'`"
then
	echo shar: "error transmitting 'arctst.c'" '(should have been 1724 characters)'
fi
fi
echo shar: "extracting 'arcunp.c'" '(6288 characters)'
if test -f 'arcunp.c'
then
	echo shar: "will not over-write existing file 'arcunp.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcunp.c'
	Xstatic char *RCSid = "$Header: arcunp.c,v 1.2 86/07/15 07:54:25 turner Exp $";
	X
	X/*
	X * $Log:	arcunp.c,v $
	X * Revision 1.2  86/07/15  07:54:25  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:01:04  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCUNP
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =3.16), created on $tag(
	XTED_DATE DB =02/03/86) at $tag(
	XTED_TIME DB =23:01:16))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to expand a file
	X         when taking it out of an archive.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	X/* stuff for repeat unpacking */
	X
	X#define DLE 0x90                       /* repeat byte flag */
	X
	Xstatic int state;                      /* repeat unpacking state */
	X
	X/* repeat unpacking states */
	X
	X#define NOHIST 0                       /* no relevant history */
	X#define INREP 1                        /* sending a repeated value */
	X
	Xstatic int crcval;                     /* CRC check value */
	Xstatic long size;                      /* bytes to read */
	X
	Xint unpack(f,t,hdr)                    /* unpack an archive entry */
	XFILE *f, *t;                           /* source, destination */
	Xstruct heads *hdr;                     /* pointer to file header data */
	X{
	X int c;                             /* one char of stream */
	X
	X    /* setups common to all methods */
	X
	X    crcval = 0;                        /* reset CRC check value */
	X    size = hdr->size;                  /* set input byte counter */
	X    state = NOHIST;                    /* initial repeat unpacking state */
	X    setcode();                         /* set up for decoding */
	X
	X    /* use whatever method is appropriate */
	X
	X    switch(hdrver)                     /* choose proper unpack method */
	X    {
	X    case 1:                            /* standard packing */
	X    case 2:
	X         while((c=getc_unp(f))!=EOF)
	X              putc_unp(c,t);
	X         break;
	X
	X    case 3:                            /* non-repeat packing */
	X         while((c=getc_unp(f))!=EOF)
	X              putc_ncr(c,t);
	X         break;
	X
	X    case 4:                            /* Huffman squeezing */
	X         init_usq(f);
	X         while((c=getc_usq(f))!=EOF)
	X              putc_ncr(c,t);
	X         break;
	X
	X    case 5:                            /* Lempel-Zev compression */
	X         init_ucr(0);
	X         while((c=getc_ucr(f))!=EOF)
	X              putc_unp(c,t);
	X         break;
	X
	X    case 6:                            /* Lempel-Zev plus non-repeat */
	X         init_ucr(0);
	X         while((c=getc_ucr(f))!=EOF)
	X              putc_ncr(c,t);
	X         break;
	X
	X    case 7:                            /* L-Z plus ncr with new hash */
	X         init_ucr(1);
	X         while((c=getc_ucr(f))!=EOF)
	X              putc_ncr(c,t);
	X         break;
	X
	X    case 8:                            /* dynamic Lempel-Zev */
	X         decomp(f,t);
	X         break;
	X
	X    default:                           /* unknown method */
	X         if(warn)
	X         {    printf("I don't know how to unpack file %s\n",hdr->name);
	X              printf("I think you need a newer version of ARC\n");
	X              nerrs++;
	X         }
	X         fseek(f,hdr->size,1);         /* skip over bad file */
	X         return 1;                     /* note defective file */
	X    }
	X
	X    /* cleanups common to all methods */
	X
	X    if(crcval!=hdr->crc)
	X    {    if(warn)
	X         {    printf("WARNING: File %s fails CRC check\n",hdr->name);
	X              nerrs++;
	X         }
	X         return 1;                     /* note defective file */
	X    }
	X    return 0;                          /* file is okay */
	X}
	X
	X/*  This routine is used to put bytes in the output file.  It also
	X    performs various housekeeping functions, such as maintaining the
	X    CRC check value.
	X*/
	X
	Xstatic putc_unp(c,t)                   /* output an unpacked byte */
	Xchar c;                                /* byte to output */
	XFILE *t;                               /* file to output to */
	X{
	X    crcval = addcrc(crcval,c);         /* update the CRC check value */
	X    putc_tst(c,t);
	X}
	X
	X/*  This routine is used to decode non-repeat compression.  Bytes are
	X    passed one at a time in coded format, and are written out uncoded.
	X    The data is stored normally, except that runs of more than two
	X    characters are represented as:
	X
	X         <char> <DLE> <count>
	X
	X    With a special case that a count of zero indicates a DLE as data,
	X    not as a repeat marker.
	X*/
	X
	Xputc_ncr(c,t)                          /* put NCR coded bytes */
	Xunsigned char c;                       /* next byte of stream */
	XFILE *t;                               /* file to receive data */
	X{
	X    static int lastc;                  /* last character seen */
	X
	X    switch(state)                      /* action depends on our state */
	X    {
	X    case NOHIST:                       /* no previous history */
	X         if(c==DLE)                    /* if starting a series */
	X              state = INREP;           /* then remember it next time */
	X         else putc_unp(lastc=c,t);     /* else nothing unusual */
	X         return;
	X
	X    case INREP:                        /* in a repeat */
	X         if(c)                         /* if count is nonzero */
	X              while(--c)               /* then repeatedly ... */
	X                   putc_unp(lastc,t);  /* ... output the byte */
	X         else putc_unp(DLE,t);         /* else output DLE as data */
	X         state = NOHIST;               /* back to no history */
	X         return;
	X
	X    default:
	X         abort("Bad NCR unpacking state (%d)",state);
	X    }
	X}
	X
	X/*  This routine provides low-level byte input from an archive.  This
	X    routine MUST be used, as end-of-file is simulated at the end of
	X    the archive entry.
	X*/
	X
	Xint getc_unp(f)                        /* get a byte from an archive */
	XFILE *f;                               /* archive file to read */
	X{
	X    if(!size)                          /* if no data left */
	X         return EOF;                   /* then pretend end of file */
	X
	X    size--;                            /* deduct from input counter */
	X    return code(fgetc(f));             /* and return next decoded byte */
	X}
SHAR_EOF
if test 6288 -ne "`wc -c < 'arcunp.c'`"
then
	echo shar: "error transmitting 'arcunp.c'" '(should have been 6288 characters)'
fi
fi
echo shar: "extracting 'arcusq.c'" '(3170 characters)'
if test -f 'arcusq.c'
then
	echo shar: "will not over-write existing file 'arcusq.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcusq.c'
	Xstatic char *RCSid = "$Header: arcusq.c,v 1.2 86/07/15 07:54:30 turner Exp $";
	X
	X/*
	X * $Log:	arcusq.c,v $
	X * Revision 1.2  86/07/15  07:54:30  turner
	X * 
	X * 
	X * Revision 1.1  86/06/26  15:01:07  turner
	X * initial version
	X * 
	X * 
	X */
	X
	X/*  ARC - Archive utility - ARCUSQ
	X
	X$define(tag,$$segment(@1,$$index(@1,=)+1))#
	X$define(version,Version $tag(
	XTED_VERSION DB =3.13), created on $tag(
	XTED_DATE DB =01/30/86) at $tag(
	XTED_TIME DB =20:11:42))#
	X$undefine(tag)#
	X    $version
	X
	X(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
	X
	X    By:  Thom Henderson
	X
	X    Description:
	X         This file contains the routines used to expand a file
	X         which was packed using Huffman squeezing.
	X
	X         Most of this code is taken from an USQ program by Richard
	X         Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
	X
	X    Language:
	X         Computer Innovations Optimizing C86
	X*/
	X#include <stdio.h>
	X#include "arc.h"
	X
	X/* stuff for Huffman unsqueezing */
	X
	X#define ERROR (-1)
	X
	X#define SPEOF 256                      /* special endfile token */
	X#define NUMVALS 257                    /* 256 data values plus SPEOF */
	X
	Xextern struct nd                       /* decoding tree */
	X{ int child[2];                      /* left, right */
	X}   node[NUMVALS];                     /* use large buffer */
	X
	Xstatic int bpos;                       /* last bit position read */
	Xstatic int curin;                      /* last byte value read */
	Xstatic int numnodes;                   /* number of nodes in decode tree */
	X
	Xstatic int get_int(f)                  /* get an integer */
	XFILE *f;                               /* file to get it from */
	X{
	X    return getc_unp(f) | (getc_unp(f)<<8);
	X}
	X
	Xinit_usq(f)                            /* initialize Huffman unsqueezing */
	XFILE *f;                               /* file containing squeezed data */
	X{
	X int i;                             /* node index */
	X
	X    bpos = 99;                         /* force initial read */
	X
	X    numnodes = get_int(f);
	X
	X    if(numnodes<0 || numnodes>=NUMVALS)
	X         abort("File has an invalid decode tree");
	X
	X    /* initialize for possible empty tree (SPEOF only) */
	X
	X    node[0].child[0] = -(SPEOF + 1);
	X    node[0].child[1] = -(SPEOF + 1);
	X
	X    for(i=0; i<numnodes; ++i)          /* get decoding tree from file */
	X    {    node[i].child[0] = get_int(f);
	X         node[i].child[1] = get_int(f);
	X    }
	X}
	X
	Xint getc_usq(f)                        /* get byte from squeezed file */
	XFILE *f;                               /* file containing squeezed data */
	X{
	X int i;                             /* tree index */
	X
	X    /* follow bit stream in tree to a leaf */
	X
	X    for(i=0; i>=0; )                   /* work down(up?) from root */
	X    {    if(++bpos>7)
	X         {    if((curin=getc_unp(f)) == ERROR)
	X                   return(ERROR);
	X              bpos = 0;
	X
	X              /* move a level deeper in tree */
	X              i = node[i].child[1&curin];
	X         }
	X         else i = node[i].child[1 & (curin >>= 1)];
	X    }
	X
	X    /* decode fake node index to original data value */
	X
	X    i = -(i + 1);
	X
	X    /* decode special endfile token to normal EOF */
	X
	X    i = (i==SPEOF) ? EOF : i;
	X    return i;
	X}
SHAR_EOF
if test 3170 -ne "`wc -c < 'arcusq.c'`"
then
	echo shar: "error transmitting 'arcusq.c'" '(should have been 3170 characters)'
fi
fi
echo shar: "extracting 'arcvax.c'" '(671 characters)'
if test -f 'arcvax.c'
then
	echo shar: "will not over-write existing file 'arcvax.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'arcvax.c'
	X#include <stdio.h>
	X#include <ctype.h>
	X
	X/*
	X * Misc routines to emulate IBM MSDOS functions under BSD
	X */
	Xupper(string)
	Xchar *string;
	X{
	X    char *p;
	X
	X    for(p = string; *p != NULL; p++)
	X	if(islower(*p))
	X	    *p = toupper(*p);
	X}
	Xchar *setmem(dest,size,c)
	Xchar *dest,c;
	Xint size;
	X{
	X int i;
	X
	X    for(i = 0; i < size; dest[i] = c, i++);
	X    return(&dest[0]);
	X}
	Xchar *gcdir(dirname)
	Xchar *dirname;
	X
	X{
	X	if(dirname == NULL || strlen(dirname) == 0)
	X		dirname = (char *)malloc(1024);
	X
	X	getwd(dirname);
	X}
	Xabort(s,arg1,arg2,arg3)
	Xchar *s;
	X{
	X    fprintf(stderr,"ARC: ");
	X    fprintf(stderr,s,arg1,arg2,arg3);
	X    fprintf(stderr,"\n");
	X#if BSD
	X    perror("BSD");
	X#endif
	X    exit(1);
	X}
SHAR_EOF
if test 671 -ne "`wc -c < 'arcvax.c'`"
then
	echo shar: "error transmitting 'arcvax.c'" '(should have been 671 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
----
	"I ain't gay, but there are sure times when i wish i could say
		that i wasn't straight"

Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL

turner@imagen.UUCP (08/05/86)

This is my hacked version of the arc program that was posted to the net, I
have modified it to run under Unix BSD4.2. if you want it to run under 
SVR2 you probably will have to change the opendir, scandir, and closedir
calls, I don't really know System 5. IMPORTANT: this is a BETA test version
i doubt severly that there are no bugs, please report any that you find to me.

1) I don't have a PC handy so i have no idea if it still works on the PC; to
	compile it for the PC edit arc.h and arcs.c and change the #defines,
	the define ST is for the Atari 520ST and with luck that will be done
	shortly. If you compile it for the PC you will need the routines for 
	opendir etc. that are in shar part 5, otherwise you can ignore that 
	shar file.
2) the Makefile is configured to compile for debugging (-g) (shows ya what 
	kind of faith i have in my code (:-)
3) everything seems to work for both arc files created on the PC and on BSD4.2
	with the exception of the t (test) option which gives strange results,
	i suspect the problem is in the code not the archive
4) there is one bug that i know of, if you archive a 0 length file, arc will
	blow up (floating point exception) when you try to extract it; the
	question is should the check happen when you add the file, extract
	it, or should if create a 0 length file ? you decide.
5) send flames etc. to me, because of the way postnews works here my signature
	will probably be at the end of each shar file, if not its:
----
Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL
----

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	dir.h
#	dir.c
#	fmatch.c
#	scandir.c
# This archive created: Mon Aug  4 14:48:52 1986
# By:	D'arc Angel (The Houses of the Holy)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'dir.h'
then
	echo shar: "will not over-write existing file 'dir.h'"
else
cat << \SHAR_EOF > 'dir.h'
/*
 * $Header: dir.h,v 1.1 86/07/04 08:31:37 turner Exp $
 */

/*
 * $Log:	dir.h,v $
 * Revision 1.1  86/07/04  08:31:37  turner
 * Initial revision
 * 
 * 
 */

#ifndef	DIRSIZ
#define	DIRSIZ	13
#endif

/*
 *	Structure returned by search calls
 */
struct	direct
{
	char	rsvd[21];		/* reserved for dos */
	char	d_attr;			/* file attribute */
	long	d_time;			/* modified time */
	long	d_size;			/* file size */
	char	d_name[DIRSIZ];		/* directory entry name */
};

typedef struct _dirdesc
{
	int	d_magic;		/* magic number 1234 */
	int	d_length;		/* number of directory entries */
	int	d_pos;			/* current position */
	struct direct **namelist;	/* list of directory entries */
} DIR;

#define DMAGIC 0x1234

DIR *opendir();
struct direct *readdir();
long telldir();
SHAR_EOF
fi
if test -f 'dir.c'
then
	echo shar: "will not over-write existing file 'dir.c'"
else
cat << \SHAR_EOF > 'dir.c'
static char *RCSid = "$Header: dir.c,v 1.1 86/07/04 08:31:25 turner Exp $";

/*
 * $Log:	dir.c,v $
 * Revision 1.1  86/07/04  08:31:25  turner
 * Initial revision
 * 
 * 
 */

/*
 *	dir.c - contains routines which behave (hopefully) like the
 *		similar versions on 4.2 BSD.
 *
 */
#include <errno.h>
#include "dir.h"

extern int errno;
extern char *calloc();

/*
 *	opendir - opens given directory and reads its contents into memory.
 *	Other functions use this data or the DIR structure to do their work.
 */
DIR *
opendir(dirname)
char *dirname;
{
	struct direct **namelist;
	DIR *dirptr;

	dirptr = (DIR *)calloc(1, sizeof(DIR));
	if(dirptr == (DIR *)0)
	{
		errno = ENOMEM;
		return((DIR *)0);
	}
	dirptr->d_magic = DMAGIC;
	dirptr->d_pos = 0;
	dirptr->d_length = scandir(dirname, &(dirptr->namelist),
		(int (*)())0, (int (*)())0);
	if(dirptr->d_length < 0)
	{
		free((char *)dirptr);
		return((DIR *)0);
	}
	return(dirptr);
}

/*
 *	readdir - returns the next directory structure from the list and
 *	updates values in the DIR structure.
 */
struct direct *
readdir(dirptr)
DIR *dirptr;
{
	if(dirptr->d_magic != DMAGIC)
	{
		errno = ENOTDIR;
		return((struct direct *)0);
	}
	if(dirptr->d_pos >= dirptr->d_length)
	{
		errno = ENFILE;
		return((struct direct *)0);
	}
	return(dirptr->namelist[dirptr->d_pos++]);
}

/*
 *	telldir - return the current position of the directory.
 */
long
telldir(dirptr)
DIR *dirptr;
{
	if(dirptr->d_magic != DMAGIC)
	{
		errno = ENOTDIR;
		return(-1L);
	}
	return((long)dirptr->d_pos);
}

/*
 *	seekdir - position the given DIR stream to position given.
 */
seekdir(dirptr, loc)
DIR *dirptr;
long loc;
{
	if(dirptr->d_magic != DMAGIC)
	{
		errno = ENOTDIR;
		return(-1);
	}
	if(loc > (long)dirptr->d_length)
	{
		errno = EINVAL;
		return(-1);
	}
	dirptr->d_pos = (int)loc;
	return(0);
}

/*
 *	rewinddir - rewind given DIR to beginning
 */
rewinddir(dirptr)
DIR *dirptr;
{
	if(dirptr->d_magic != DMAGIC)
	{
		errno = ENOTDIR;
		return(-1);
	}
	dirptr->d_pos = 0;
	return(0);
}

/*
 *	closedir - close given directory. destroy given DIR struct so we
 *	know it is closed.
 */
closedir(dirptr)
DIR *dirptr;
{
	if(dirptr->d_magic != DMAGIC)
	{
		errno = ENOTDIR;
		return(-1);
	}
	dirptr->d_magic = ~DMAGIC;	/* mess it up a little */
	freedir(dirptr->namelist);
	free(dirptr);
	return(0);
}
SHAR_EOF
fi
if test -f 'fmatch.c'
then
	echo shar: "will not over-write existing file 'fmatch.c'"
else
cat << \SHAR_EOF > 'fmatch.c'
static char *RCSid = "$Header: fmatch.c,v 1.1 86/07/04 08:31:47 turner Exp $";

/*
 * $Log:	fmatch.c,v $
 * Revision 1.1  86/07/04  08:31:47  turner
 * Initial revision
 * 
 * 
 */

#include <stdio.h>

#define ASTERISK '*'		/* The '*' metacharacter */
#define QUESTION '?'		/* The '?' metacharacter */
#define LEFT_BRACKET '['	/* The '[' metacharacter */
#define RIGHT_BRACKET ']'	/* The ']' metacharacter */

#define IS_OCTAL(ch) (ch >= '0' && ch <= '7')

typedef int BOOLEAN;
#define VOID int
#define TRUE 1
#define FALSE 0
#define EOS '\000'

static BOOLEAN do_list ();
static char nextch ();
static VOID list_parse ();


/*
 *  FUNCTION
 *
 *	match   test string for wildcard match
 *
 *  SYNOPSIS
 *
 *	BOOLEAN match (string, pattern)
 *	register char *string;
 *	register char *pattern;
 *
 *  DESCRIPTION
 *
 *	Test string for match using pattern.  The pattern may
 *	contain the normal shell metacharacters for pattern
 *	matching.  The '*' character matches any string,
 *	including the null string.  The '?' character matches
 *	any single character.  A list of characters enclosed
 *	in '[' and ']' matches any character in the list.
 *	If the first character following the beginning '['
 *	is a '!' then any character not in the list is matched.
 *
 */


/*
 *  PSEUDO CODE
 *
 *	Begin match
 *	    Switch on type of pattern character
 *		Case ASTERISK:
 *		    Attempt to match asterisk
 *		    Break
 *		Case QUESTION MARK:
 *		    Attempt to match question mark
 *		    Break
 *		Case EOS:
 *		    Match is result of EOS on string test
 *		    Break
 *		Case default:
 *		    If explicit match then
 *			Match is result of submatch
 *		    Else
 *			Match is FALSE
 *		    End if
 *		    Break
 *	    End switch
 *	    Return result of match test
 *	End match
 *
 */

static BOOLEAN match (string, pattern)
register char *string;
register char *pattern;
{
    register BOOLEAN ismatch;

    ismatch = FALSE;
    switch (*pattern) {
	case ASTERISK:
	    pattern++;
	    do {
		ismatch = match (string, pattern);
	    } while (!ismatch && *string++ != EOS);
	    break;
	case QUESTION:
	    if (*string != EOS) {
		ismatch = match (++string, ++pattern);
	    }
	    break;
	case EOS:
	    if (*string == EOS) {
		ismatch = TRUE;
	    }
	    break;
	case LEFT_BRACKET:
	    if (*string != EOS) {
		ismatch = do_list (string, pattern);
	    }
	    break;
	default:
	    if (tolower(*string) == tolower(*pattern))
	    {
	    	string++;
	    	pattern++;
		ismatch = match (string, pattern);
	    } else {
		ismatch = FALSE;
	    }
	    break;
    }
    return (ismatch);
}


/*
 *  FUNCTION
 *
 *	do_list    process a list and following substring
 *
 *  SYNOPSIS
 *
 *	static BOOLEAN do_list (string, pattern)
 *	register char *string;
 *	register char *pattern;
 *
 *  DESCRIPTION
 *
 *	Called when a list is found in the pattern.  Returns
 *	TRUE if the current character matches the list and
 *	the remaining substring matches the remaining pattern.
 *
 *	Returns FALSE if either the current character fails to
 *	match the list or the list matches but the remaining
 *	substring and subpattern's don't.
 *
 *  RESTRICTIONS
 *
 *	The mechanism used to match characters in an inclusive
 *	pair (I.E. [a-d]) may not be portable to machines
 *	in which the native character set is not ASCII.
 *
 *	The rules implemented here are:
 *
 *		(1)	The backslash character may be
 *			used to quote any special character.
 *			I.E.  "\]" and "\-" anywhere in list,
 *			or "\!" at start of list.
 *
 *		(2)	The sequence \nnn becomes the character
 *			given by nnn (in octal).
 *
 *		(3)	Any non-escaped ']' marks the end of list.
 *
 *		(4)	A list beginning with the special character
 *			'!' matches any character NOT in list.
 *			The '!' character is only special if it
 *			is the first character in the list.
 *
 */


/*
 *  PSEUDO CODE
 *
 *	Begin do_list
 *	    Default result is no match
 *	    Skip over the opening left bracket
 *	    If the next pattern character is a '!' then
 *		List match gives FALSE
 *		Skip over the '!' character
 *	    Else
 *		List match gives TRUE
 *	    End if
 *	    While not at closing bracket or EOS
 *		Get lower and upper bounds
 *		If character in bounds then
 *		    Result is same as sense flag.
 *		    Skip over rest of list
 *		End if
 *	    End while
 *	    If match found then
 *		If not at end of pattern then
 *		    Call match with rest of pattern
 *		End if
 *	    End if
 *	    Return match result
 *	End do_list
 *
 */

static BOOLEAN do_list (string, pattern)
register char *string;
char *pattern;
{
    register BOOLEAN ismatch;
    register BOOLEAN if_found;
    register BOOLEAN if_not_found;
    auto char lower;
    auto char upper;

    pattern++;
    if (*pattern == '!') {
	if_found = FALSE;
	if_not_found = TRUE;
	pattern++;
    } else {
	if_found = TRUE;
	if_not_found = FALSE;
    }
    ismatch = if_not_found;
    while (*pattern != ']' && *pattern != EOS) {
	list_parse (&pattern, &lower, &upper);
	if (*string >= lower && *string <= upper) {
	    ismatch = if_found;
	    while (*pattern != ']' && *pattern != EOS) {pattern++;}
	}
    }
    if (*pattern++ != ']') {
	fprintf (stderr, "warning - character class error\n");
    } else {
	if (ismatch) {
	    ismatch = match (++string, pattern);
	}
    }
    return (ismatch);
}


/*
 *  FUNCTION
 *
 *	list_parse    parse part of list into lower and upper bounds
 *
 *  SYNOPSIS
 *
 *	static VOID list_parse (patp, lowp, highp)
 *	char **patp;
 *	char *lowp;
 *	char *highp;
 *
 *  DESCRIPTION
 *
 *	Given pointer to a pattern pointer (patp), pointer to
 *	a place to store lower bound (lowp), and pointer to a
 *	place to store upper bound (highp), parses part of
 *	the list, updating the pattern pointer in the process.
 *
 *	For list characters which are not part of a range,
 *	the lower and upper bounds are set to that character.
 *
 */

static VOID list_parse (patp, lowp, highp)
char **patp;
char *lowp;
char *highp;
{
    *lowp = nextch (patp);
    if (**patp == '-') {
	(*patp)++;
	*highp = nextch (patp);
    } else {
	*highp = *lowp;
    }
}


/*
 *  FUNCTION
 *
 *	nextch    determine next character in a pattern
 *
 *  SYNOPSIS
 *
 *	static char nextch (patp)
 *	char **patp;
 *
 *  DESCRIPTION
 *
 *	Given pointer to a pointer to a pattern, uses the pattern
 *	pointer to determine the next character in the pattern,
 *	subject to translation of backslash-char and backslash-octal
 *	sequences.
 *
 *	The character pointer is updated to point at the next pattern
 *	character to be processed.
 *
 */

static char nextch (patp)
char **patp;
{
    register char ch;
    register char chsum;
    register int count;

    ch = *(*patp)++;
    if (ch == '\\') {
	ch = *(*patp)++;
	if (IS_OCTAL (ch)) {
	    chsum = 0;
	    for (count = 0; count < 3 && IS_OCTAL (ch); count++) {
		chsum *= 8;
		chsum += ch - '0';
		ch = *(*patp)++;
	    }
	    (*patp)--;
	    ch = chsum;
	}
    }
    return (ch);
}

/*
 *	Filename match - here, *.* matches everything
 */

BOOLEAN fmatch (string, pattern)
char *string;
char *pattern;
{
    char *ptr;

    if(*string && !strcmp(pattern, "*.*"))
    	return(TRUE);
    return(match(string, pattern));
}
SHAR_EOF
fi
if test -f 'scandir.c'
then
	echo shar: "will not over-write existing file 'scandir.c'"
else
cat << \SHAR_EOF > 'scandir.c'
static char *RCSid = "$Header: scandir.c,v 1.1 86/07/04 08:31:49 turner Exp $";

/*
 * $Log:	scandir.c,v $
 * Revision 1.1  86/07/04  08:31:49  turner
 * Initial revision
 * 
 * 
 */

#include <dos.h>
#include <errno.h>
#include "dir.h"
#define NULL (char *)0

static struct direct buffer;
extern int errno;

extern char *calloc(), *malloc(), *realloc();

scandir(dirname, namelist, select, compar)
char *dirname;
struct direct *(*namelist[]);
int (*select)();
int (*compar)();
{
	register struct direct **names;
	register int dirno;
	union REGS sregs, oregs;
	struct SREGS segregs;
	char *ptr, *paths;

	names = (struct direct **)calloc(1, sizeof(struct direct *));
	paths = calloc(128, 1);
	if(names == (struct direct **)0 || paths == NULL)
	{
		errno = ENOMEM;
		return(-1);
	}
	strcpy(paths, dirname);
	ptr = &paths[strlen(paths) - 1];
	if(*ptr == '/' || *ptr == '\\')
		*ptr = '\0';
	strcat(paths, "/*.*");

	segread(&segregs);		/* set up segment registers */
	ptr = (char *)&buffer;
	sregs.h.ah = 0x1a;		/* set DTA to buffer */
	sregs.x.dx = FP_OFF(ptr);	/* offset */
#ifdef	M_I86LM
	segregs.ds = FP_SEG(ptr);	/* pointer in large model */
#endif	M_I86LM
	intdosx(&sregs, &oregs, &segregs);
	sregs.x.ax = 0x4e00;		/* search for first */
	sregs.x.cx = 0x1f;		/* include all attributes */
	sregs.x.dx = FP_OFF(paths);	/* offset to path */
#ifdef	M_I86LM
	segregs.ds = FP_SEG(paths);	/* segment for large model */
#endif	M_I86LM
	intdosx(&sregs, &oregs, &segregs);
	if(oregs.x.cflag)
	{
		errno = ENOTDIR;
		return(-1);
	}
	sregs.x.ax = 0x4f00;		/* search for next */
	for(dirno = 0; oregs.x.cflag == 0; intdosx(&sregs, &oregs, &segregs))
	{
		for(ptr = buffer.d_name; *ptr; ptr++)
			*ptr = tolower(*ptr);
		if(select == (int (*)())0 || (*select)(&buffer))
		{
			names = (struct direct **)realloc((char *)names,
				(dirno + 2)*sizeof(struct direct *));
			if(names == (struct direct **)0)
			{
				errno = ENOMEM;
				return(-1);
			}
			names[dirno] = (struct direct *)calloc(1,
				sizeof(struct direct));
			if(names[dirno] == (struct direct *)0)
			{
				errno = ENOMEM;
				return(-1);
			}
			*names[dirno] = buffer;
			names[++dirno] = (struct direct *)0;
		}
	}

	if(compar != (int (*)())0)
		qsort((char *)names, dirno, sizeof(char *), compar);

	*namelist = names;
	free(paths);			/* free temp space */
	return(dirno);
}

freedir(dirs)
register struct direct **dirs;
{
	register int ii;

	if(dirs == (struct direct **)0)
		return(-1);
	for(ii = 0; dirs[ii] != (struct direct *)0; ii++)
		free(dirs[ii]);
	free(dirs);
	return(0);
}

alphasort(dirptr1, dirptr2)
struct direct **dirptr1, **dirptr2;
{
	return(strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
}
SHAR_EOF
fi
exit 0
#	End of shell archive
-- 
----
	"I ain't gay, but there are sure times when i wish i could say
		that i wasn't straight"

Name:	James M. Turner
Mail:	Imagen Corp. 2650 San Tomas Expressway, P.O. Box 58101
        Santa Clara, CA 95052-8101
AT&T:	(408) 986-9400
UUCP:	...{decvax,ucbvax}!decwrl!imagen!turner
CompuServe: 76327,1575
GEnie     : D-ARCANGEL