[net.sources] ARC for System V R2

aeusemrs@csun.UUCP (02/03/87)

Please read the README file below.

#! /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 the files:
#	README
#	Makefile
#	arc.c
#	arc.h
#	arcadd.c
#	arccode.c
#	arccvt.c
#	arcdel.c
#	arcdir.c
#	arcdos.c
#	arcext.c
#	arcio.c
#	arclst.c
#	arcm.h
# This archive created: Mon Feb  2 18:01:48 1987
export PATH; PATH=/bin:$PATH
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
cat << \SHAR_EOF > 'README'
ARC  -  For Unix System V

This version is based upon a version that was posted to net.sources for
BSD.  This program DOES NOT suffer from core dumps!  After looking at
the code for BSD I understand why there were core dumps in the first
place.  The BSD version is based upon IBM PC ARC 5.12.  I tried to
maintain backwards compatability with the original PC ARC, but, the
BSD version I got, only made a half-hearted attempt at it, commenting
out entire sections, and deleting sections of the MSDOS version.  After
looking at the BSD version, I decided in favor of NOT attemping to
remain backwards compatable with it.  Maybe some can clean up the code
and make it work under both systems, and send diffs, or the program back
to me (Mike Stump) at:

uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs
SHAR_EOF
fi # end of overwriting check
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
#    Makefile for ARC
CFLAGS = -g
#  CFLAGS = -O

OBJS = arc.o arcadd.o arccode.o arccvt.o arcdel.o arcdir.o \
arcdos.o arcext.o arcio.o arclst.o arclzw.o arcmatch.o arcpack.o \
arcsq.o arcsvc.o arctst.o arcunp.o arcusq.o arcmisc.o


SRCS = arc.c arcadd.c arccode.c arccvt.c arcdel.c arcdir.c \
arcdos.c arcext.c arcio.c arclst.c arclzw.c arcmatch.c arcpack.c \
arcs.c arcsq.c arcsvc.c arctst.c arcunp.c arcusq.c arcmisc.c

arc:            ${OBJS}
	cc  ${CFLAGS} -o arc ${OBJS}
${OBJS}:        arc.h
arc.h:          arcm.h arcs.h
	touch arc.h
SHAR_EOF
fi # end of overwriting check
if test -f 'arc.c'
then
	echo shar: will not over-write existing file "'arc.c'"
else
cat << \SHAR_EOF > 'arc.c'
/*  ARC - Archive utility

System V Version 1.0 based upon:
    Version 5.12, created on 02/05/86 at 22:22:01

Port to System V, by Mike Stump
Please send all bug fixes, comments, etc...  to:
uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs

Machines known to work on:
    3B5 System V, Version 2 Release 2.0

(C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This program is a general archive utility, and is used to maintain
         an archive of files.  An "archive" is a single file that combines
         many files, reducing storage space and allowing multiple files to
         be handled as one.

    Instructions:
         Run this program with no arguments for complete instructions.

    Programming notes:
         ARC Version 2 differs from version 1 in that archive entries
         are automatically compressed when they are added to the archive,
         making a separate compression step unecessary.  The nature of the
         compression is indicated by the header version number placed in
         each archive entry, as follows:

         1 = Old style, no compression
         2 = New style, no compression
         3 = Compression of repeated characters only
         4 = Compression of repeated characters plus Huffman SQueezing
         5 = Lempel-Zev packing of repeated strings (old style)
         6 = Lempel-Zev packing of repeated strings (new style)
         7 = Lempel-Zev Williams packing with improved has function
         8 = Dynamic Lempel-Zev packing with adaptive reset

         Type 5, Lempel-Zev packing, was added as of version 4.0

         Type 6 is Lempel-Zev packing where runs of repeated characters
         have been collapsed, and was added as of version 4.1

         Type 7 is a variation of Lempel-Zev using a different hash
         function which yields speed improvements of 20-25%, and was
         added as of version 4.6

         Type 8 is a different implementation of Lempel-Zev, using a
         variable code size and an adaptive block reset, and was added
         as of version 5.0

         Verion 4.3 introduced a temporary file for holding the result
         of the first crunch pass, thus speeding up crunching.

         Version 4.4 introduced the ARCTEMP environment string, so that
         the temporary crunch file may be placed on a ramdisk.  Also
         added was the distinction bewteen Adding a file in all cases,
         and Updating a file only if the disk file is newer than the
         corresponding archive entry.

         The compression method to use is determined when the file is
         added, based on whichever method yields the smallest result.

*/
#include "arc.h"

main(num,arg)                          /* system entry point */
int num;                               /* number of arguments */
char **arg;                            /* pointers to arguments */
{
    char opt = 0;                      /* selected action */
    char *a;                           /* option pointer */
    char *makefnam();                  /* filename fixup routine */
    char *upper();                     /* case conversion routine */
    char *index();                     /* string index utility */
    char *envfind();                   /* environment searcher */
    INT n;                             /* argument index */
    char *arctemp2;
    long getpid();

    warn = 1;
    note = 1;
    if(num<3)
    {    printf("arc - Archive Utility, 5.12\n");
         printf("Usage: arc {amufdxeplvtc}[bswn][g<password>]");
         printf(" <archive> [<filename> . . .]\n");
         printf("Where:   a   = add files to archive\n");
         printf("         m   = move files to archive\n");
         printf("         u   = update files in archive\n");
         printf("         f   = freshen files in archive\n");
         printf("         d   = delete files from archive\n");
         printf("         x,e = extract files from archive\n");
         printf("         p   = copy files from archive to");
         printf(" standard output\n");
         printf("         l   = list files in archive\n");
         printf("         v   = verbose listing of files in archive\n");
         printf("         t   = test archive integrity\n");
         printf("         c   = convert entry to new packing method\n");
         printf("         b   = retain backup copy of archive\n");
         printf("         s   = suppress compression (store only)\n");
         printf("         w   = suppress warning messages\n");
         printf("         n   = suppress notes and comments\n");
         printf("         g   = Encrypt/decrypt archive entry\n\n");
         return 1;
    }

    /* see where temp files go */
    /* use process id to "enhance uniquity" of temp filenames */
    /* (avoids multi-user or background foolishness) */

    if(!(arctemp2 = envfind("ARCTEMP")))
         arctemp2 = envfind("TEMP");
    if (arctemp2) sprintf(arctemp,"%s.Arc%ld",arctemp2,getpid());
    else sprintf(arctemp,".Arc%ld",getpid());

    /* avoid any case problems with command options */
         upper(arg[1]);                /* convert it to uppercase */

    /* create archive names, supplying defaults */

    makefnam(arg[2],".arc",arcname);
    sprintf(newname,"%s.arc",arctemp);
    makefnam(".bak",arcname,bakname);

    /* now scan the command and see what we are to do */

    for(a=arg[1]; *a; a++)             /* scan the option flags */
    {    if(index("AMUFDXEPLVTC",*a)) /* if a known command */
         {    if(opt)                  /* do we have one yet? */
                   abort("Cannot mix %c and %c",opt,*a);
              opt = *a;                /* else remember it */
         }

         else if(*a=='B')              /* retain backup copy */
              keepbak = 1;

         else if(*a=='W')              /* suppress warnings */
              warn = 0;

         else if(*a=='N')              /* suppress notes and comments */
              note = 0;

         else if(*a=='G')              /* garble */
         {    password = a+1;
              while(*a)
                   a++;
              a--;
         }

         else if(*a=='S')              /* storage kludge */
              nocomp = 1;

         else if (*a=='-')   /* UNIX option marker */
              ;

         else abort("%c is an unknown command",*a);
    }

    if(!opt)
         abort("I have nothing to do!");

    /* act on whatever action command was given */

    switch(opt)                        /* action depends on command */
    {
    case 'A':                          /* Add */
    case 'M':                          /* Move */
    case 'U':                          /* Update */
    case 'F':                          /* Freshen */
         addarc(num-3,&arg[3],(opt=='M'),(opt=='U'),(opt=='F'));
         break;

    case 'D':                          /* Delete */
         delarc(num-3,&arg[3]);
         break;

    case 'E':                          /* Extract */
    case 'X':                          /* eXtract */
    case 'P':                          /* Print */
         extarc(num-3,&arg[3],(opt=='P'));
         break;

    case 'V':                          /* Verbose list */
         bose = 1;
    case 'L':                          /* List */
         lstarc(num-3,&arg[3]);
         break;

    case 'T':                          /* Test */
         tstarc();
         break;

    case 'C':                          /* Convert */
         cvtarc(num-3,&arg[3]);
         break;

    default:
         abort("I don't know how to do %c yet!",opt);
    }

    return nerrs;
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arc.h'
then
	echo shar: will not over-write existing file "'arc.h'"
else
cat << \SHAR_EOF > 'arc.h'
/*
System V Version 1.0.
*/
#include <stdio.h>

#include <ctype.h>	/* for isupper etc.                  */
#define EXTERN
#define INT short
#define envfind getenv
#define index strchr
#define rindex strrchr

#include "arcm.h"

/*  ARC - Archive utility - ARC Header

    Version 2.14, created on 02/03/86 at 22:48:29

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description: 
         This is the header file for the ARC archive utility.  It defines
         global parameters and the references to the external data.

 */

#include "arcs.h"

EXTERN INT keepbak;             /* true if saving the old archive */
EXTERN INT warn;                /* true to print warnings */
EXTERN INT note;                /* true to print comments */
EXTERN INT bose;                /* true to be verbose */
EXTERN INT nocomp;              /* true to suppress compression */
EXTERN char arctemp[STRLEN];    /* arc temp file prefix */
EXTERN char *password;          /* encryption password pointer */
EXTERN INT nerrs;               /* number of errors encountered */

EXTERN char hdrver;             /* header version */

EXTERN FILE *arc;               /* the old archive */
EXTERN FILE *new;               /* the new archive */

EXTERN char arcname[STRLEN];    /* storage for archive name */
EXTERN char bakname[STRLEN];    /* storage for backup copy name */
EXTERN char newname[STRLEN];    /* storage for new archive name */
EXTERN unsigned INT arcdate;    /* archive date stamp */
EXTERN unsigned INT arctime;    /* archive time stamp */
SHAR_EOF
fi # end of overwriting check
if test -f 'arcadd.c'
then
	echo shar: will not over-write existing file "'arcadd.c'"
else
cat << \SHAR_EOF > 'arcadd.c'
/*  ARC - Archive utility - ARCADD

System V Version 1.0 based upon:
    Version 3.39, created on 02/05/86 at 22:21:53

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to add files to an archive.
*/
#include "arc.h"

INT addarc(num,arg,move,update,fresh)  /* add files to archive */
INT num;                               /* number of arguments */
char *arg[];                           /* pointers to arguments */
INT move;                              /* true if moving file */
INT update;                            /* true if updating */
INT fresh;                             /* true if freshening */
{
    char *buf;                         /* pathname buffer */
    char *i, *rindex();                /* string indexing junk */
    INT n;                             /* indices */
    struct heads hdr;                  /* file header data storage */
    INT addfile();

    openarc(1); /* open archive for changes */

    for (n=0; n<num; ++n) {
        if (i=rindex(buf=arg[n], '/')) buf=i+1;
        addfile(arg[n],buf,update,fresh);
    }

    /* now we must copy over all files that follow our additions */

    while (readhdr(&hdr,arc))           /* while more entries to copy */
    {    writehdr(&hdr,new);
         filecopy(arc,new,hdr.size);
    }
    hdrver = 0;                        /* archive EOF type */
    writehdr(&hdr,new);                /* write out our end marker */
    closearc(1);                       /* close archive after changes */

    if (move) for (n=0; n<num; ++n)    /* if this was a move */
        if (unlink(arg[n]) && warn) {
            printf("Cannot unsave %s\n",arg[n]);
            ++nerrs;
         }
}

static INT addfile(path,name,update,fresh) /* add named file to archive */
char *path;                                /* path name of file to add */
char *name;                                /* name of file to add */
INT update;                                /* true if updating */
INT fresh;                                 /* true if freshening */
{
    struct heads nhdr;                 /* data regarding the new file */
    struct heads ohdr;                 /* data regarding an old file */
    FILE *f, *fopen();                 /* file to add, opener */
    long starts, ftell();              /* file locations */
    INT c;                             /* one char of file */
    INT upd = 0;                       /* true if replacing an entry */

    if (!(f=fopen(path,"r"))) {
        if (warn)
         {    printf("Cannot read file: %s\n",path);
              nerrs++;
         }
    }

    strcpy(nhdr.name,name);            /* save name */
    nhdr.size = 0;                     /* clear out size storage */
    nhdr.crc = 0;                      /* clear out CRC check storage */
    getstamp(f,&nhdr.date,&nhdr.time);

    /* position archive to spot for new file */

    if (arc)                           /* if adding to existing archive */
    {    starts = ftell(arc);          /* where are we? */
         while (readhdr(&ohdr,arc))    /* while more files to check */
         {    if (!strcmp(ohdr.name,nhdr.name))
              {    upd = 1;            /* replace existing entry */
                   if (update || fresh) /* if updating or freshening */
                   {    if (nhdr.date<ohdr.date
                        || (nhdr.date==ohdr.date && nhdr.time<=ohdr.time))
                        {    fseek(arc,starts,0);
                             fclose(f);
                             return;   /* skip if not newer */
                        }
                   }
              }

              if (strcmp(ohdr.name,nhdr.name)>=0)
                   break;              /* found our spot */

              writehdr(&ohdr,new);     /* entry preceeds update; keep it */
              filecopy(arc,new,ohdr.size);
              starts = ftell(arc);     /* now where are we? */
         }

         if (upd)                      /* if an update */
         {    if (note)
                 { printf("Updating file: %-12s  ",name); fflush(stdout);}
              fseek(arc,ohdr.size,1);
         }
         else if (fresh)               /* else if freshening */
         {    fseek(arc,starts,0);     /* then do not add files */
              fclose(f);
              return;
         }
         else                          /* else adding a new file */
         {    if (note)
                 { printf("Adding file:   %-12s  ",name); fflush(stdout);}
              fseek(arc,starts,0);     /* reset for next time */
         }
    }

    else                               /* no existing archive */
    {    if (fresh)                    /* cannot freshen nothing */
         {    fclose(f);
              return;
         }
         else if (note)                 /* else adding a file */
            { printf("Adding file:   %-12s  ",name); fflush(stdout);}
    }

    starts = ftell(new);               /* note where header goes */
    hdrver = ARCVER;                   /* anything but end marker */
    writehdr(&nhdr,new);               /* write out header skeleton */
    pack(f,new,&nhdr);                 /* pack file into archive */
    fseek(new,starts,0);               /* move back to header skeleton */
    writehdr(&nhdr,new);               /* write out real header */
    fseek(new,nhdr.size,1);            /* skip over data to next header */
    fclose(f);                         /* all done with the file */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arccode.c'
then
	echo shar: will not over-write existing file "'arccode.c'"
else
cat << \SHAR_EOF > 'arccode.c'
/*  ARC - Archive utility - ARCCODE

System V Version 1.0 based upon:
    Version 1.02, created on 01/20/86 at 13:33:35

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to encrypt and decrypt
         data in an archive.  The encryption method is nothing fancy,
         being just a routine XOR, but it is used on the packed data,
         and uses a variable length key.  The end result is something
         that is in theory crackable, but I'd hate to try it.  It should
         be more than sufficient for casual use.
*/
#include "arc.h"

static char *p;                        /* password pointer */

INT setcode()                          /* get set for encoding/decoding */
{
    p = password;                      /* reset password pointer */
}

INT code(c)                            /* encode some character */
INT c;                                 /* character to encode */
{
    if(p)                              /* if password is in use */
    {    if(!*p)                       /* if we reached the end */
              p = password;            /* then wrap back to the start */
         return c^*p++;                /* very simple here */
    }
    else return c;                     /* else no encryption */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arccvt.c'
then
	echo shar: will not over-write existing file "'arccvt.c'"
else
cat << \SHAR_EOF > 'arccvt.c'
/*  ARC - Archive utility - ARCCVT

System V Version 1.0 based upon:
    Version 1.16, created on 02/03/86 at 22:53:02

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to convert archives to use
         newer file storage methods.
*/
#include "arc.h"

static char tempname[STRLEN];          /* temp file name */

INT cvtarc(num,arg)                    /* convert archive */
INT num;                               /* number of arguments */
char *arg[];                           /* pointers to arguments */
{
    struct heads hdr;                  /* file header */
    INT cvt;                           /* true to convert current file */
    INT did[MAXARG];                   /* true when argument was used */
    INT n;                             /* index */
    char *makefnam();                  /* filename fixer */
    FILE *fopen();                     /* file opener */
    INT cvtfile();

    if(arctemp)                   /* use temp area if specified */
         sprintf(tempname,"%s.cvt",arctemp);
    else makefnam("$ARCTEMP.cvt",arcname,tempname);

    openarc(1);                        /* open archive for changes */

    for(n=0; n<num; n++)               /* for each argument */
         did[n] = 0;                   /* reset usage flag */
    rempath(num,arg);                  /* strip off paths */

    if(num)                            /* if files were named */
    {    while(readhdr(&hdr,arc))      /* while more files to check */
         {    cvt = 0;                 /* reset convert flag */
              for(n=0; n<num; n++)     /* for each template given */
              {    if(match(hdr.name,arg[n]))
                   {    cvt = 1;       /* turn on convert flag */
                        did[n] = 1;    /* turn on usage flag */
                        break;         /* stop looking */
                   }
              }

              if(cvt)                  /* if converting this one */
                   cvtfile(&hdr);      /* then do it */
              else                     /* else just copy it */
              {    writehdr(&hdr,new);
                   filecopy(arc,new,hdr.size);
              }
         }
    }

    else while(readhdr(&hdr,arc))      /* else convert all files */
         cvtfile(&hdr);

    hdrver = 0;                        /* archive EOF type */
    writehdr(&hdr,new);                /* write out our end marker */
    closearc(1);                       /* close archive after changes */

    if(note)
    {    for(n=0; n<num; n++)          /* report unused args */
         {    if(!did[n])
              {    printf("File not found: %s\n",arg[n]);
                   nerrs++;
              }
         }
    }
}

static INT cvtfile(hdr)                /* convert a file */
struct heads *hdr;                     /* pointer to header data */
{
    long starts, ftell();              /* where the file goes */
    FILE *tmp, *fopen();               /* temporary file */

    if(!(tmp=fopen(tempname,"w+")))
         abort("Unable to create temporary file %s",tempname);

    if(note)
     { printf("Converting file: %-12s   reading, ",hdr->name); fflush(stdout);}

    unpack(arc,tmp,hdr);               /* unpack the entry */
    fseek(tmp,0L,0);                   /* reset temp for reading */
    starts = ftell(new);               /* note where header goes */
    hdrver = ARCVER;                   /* anything but end marker */
    writehdr(hdr,new);                 /* write out header skeleton */
    pack(tmp,new,hdr);                 /* pack file into archive */
    fseek(new,starts,0);               /* move back to header skeleton */
    writehdr(hdr,new);                 /* write out real header */
    fseek(new,hdr->size,1);            /* skip over data to next header */
    fclose(tmp);                       /* all done with the file */
    if(unlink(tempname) && warn)
    {    printf("Cannot unsave %s\n",tempname);
         nerrs++;
    }
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcdel.c'
then
	echo shar: will not over-write existing file "'arcdel.c'"
else
cat << \SHAR_EOF > 'arcdel.c'
/*  ARC - Archive utility - ARCDEL

System V Version 1.0 based upon:
    Version 2.09, created on 02/03/86 at 22:53:27

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to delete entries
         in an archive.
*/
#include "arc.h"

INT delarc(num,arg)                    /* remove files from archive */
INT num;                               /* number of arguments */
char *arg[];                           /* pointers to arguments */
{
    struct heads hdr;                  /* header data */
    INT del;                           /* true to delete a file */
    INT did[MAXARG];                   /* true when argument used */
    INT n;                             /* index */

    if(!num)                           /* she must specify which */
         abort("You must tell me which files to delete!");

    for(n=0; n<num; n++)               /* for each argument */
         did[n] = 0;                   /* reset usage flag */
    rempath(num,arg);                  /* strip off paths */

    openarc(1);                        /* open archive for changes */

    while(readhdr(&hdr,arc))           /* while more entries in archive */
    {    del = 0;                      /* reset delete flag */
         for(n=0; n<num; n++)          /* for each template given */
         {    if(match(hdr.name,arg[n]))
              {    del = 1;            /* turn on delete flag */
                   did[n] = 1;         /* turn on usage flag */
                   break;              /* stop looking */
              }
         }

         if(del)                       /* skip over unwanted files */
         {    fseek(arc,hdr.size,1);
              if(note)
                   printf("Deleting file: %s\n",hdr.name);
         }
         else                          /* else copy over file data */
         {    writehdr(&hdr,new);      /* write out header and file */
              filecopy(arc,new,hdr.size);
         }
    }

    hdrver = 0;                        /* special end of archive type */
    writehdr(&hdr,new);                /* write out archive end marker */
    closearc(1);                       /* close archive after changes */

    if(note)
    {    for(n=0; n<num; n++)          /* report unused arguments */
         {    if(!did[n])
              {    printf("File not found: %s\n",arg[n]);
                   nerrs++;
              }
         }
    }
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcdir.c'
then
	echo shar: will not over-write existing file "'arcdir.c'"
else
cat << \SHAR_EOF > 'arcdir.c'
/*  ARC - Archive utility - ARCDIR

System V Version 1.0 based upon:
    Version ?.??, created on ??/??/?? at ??:??:??

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contians ...
*/
#include "arc.h"

#include <sys/types.h>
#include <sys/dir.h>

char *pattern;			      /* global so that fmatch can use them */
INT filemode;

char *dir(filename,mode,NameList)      /* get files, one by one */
char *filename;                        /* template, or NULL */
INT mode;                              /* search mode bits */
char *(*NameList[]);
{
    return NULL;
}


#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 short
#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 (direntry)
struct direct *direntry;
{
    char *ptr,*string;

    string = direntry->d_name;

    if(!strcmp(pattern, "") || !strcmp(pattern, "*.*"))
    	return(1);
    return(match(string, pattern));
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcdos.c'
then
	echo shar: will not over-write existing file "'arcdos.c'"
else
cat << \SHAR_EOF > 'arcdos.c'
/*  ARC - Archive utility - ARCDOS

System V Version 1.0 based upon:
    Version 1.43, created on 11/09/85 at 22:24:44

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains certain DOS level routines that assist
         in doing fancy things with an archive, primarily reading and
         setting the date and time last modified.

         These are, by nature, system dependant functions.  But they are
         also, by nature, very expendable.
*/
#include "arc.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

INT getstamp(f,date,time)              /* get a file's date/time stamp */
FILE *f;                               /* file to get stamp from */
unsigned INT *date, *time;             /* storage for the stamp */
{
    struct stat buf;
    struct tm *tmbuf;

    fstat(fileno(f),&buf);
    tmbuf=localtime(&buf.st_mtime);
    
    *date = ((tmbuf->tm_year-80)<<9) + ((tmbuf->tm_mon+1)<<5) + tmbuf->tm_mday;
    *time = (tmbuf->tm_hour<<11) + (tmbuf->tm_min<<5) + (tmbuf->tm_sec>>1);;
}

INT setstamp(f,date,time)              /* set a file's date/time stamp */
FILE *f;                               /* file to set stamp on */
unsigned INT date, time;               /* desired date, time */
{
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcext.c'
then
	echo shar: will not over-write existing file "'arcext.c'"
else
cat << \SHAR_EOF > 'arcext.c'
/*  ARC - Archive utility - ARCEXT

System V Version 1.0 based upon:
    Version 2.18, created on 02/03/86 at 22:55:19

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to extract files from
         an archive.
*/
#include "arc.h"
INT extarc(num,arg,prt)                /* extract files from archive */
INT num;                               /* number of arguments */
char *arg[];                           /* pointers to arguments */
INT prt;                               /* true if printing */
{
    struct heads hdr;                  /* file header */
    INT save;                          /* true to save current file */
    INT did[MAXARG];                   /* true when argument was used */
    char *i, *rindex();                /* string index */
    char **name, *malloc();            /* name pointer list, allocator */
    INT n;                             /* index */
    INT extfile();

    name = (char **)malloc(num*sizeof(char *));  /* get storage for name pointers */

    for(n=0; n<num; n++)               /* for each argument */
    {    did[n] = 0;                   /* reset usage flag */
         if(!(i=rindex(arg[n],'/')))  /* find start of name */
              i = arg[n]-1;
         name[n] = i+1;
    }

    openarc(0);                        /* open archive for reading */

    if(num)                            /* if files were named */
    {    while(readhdr(&hdr,arc))      /* while more files to check */
         {    save = 0;                /* reset save flag */
              for(n=0; n<num; n++)     /* for each template given */
              {    if(match(hdr.name,name[n]))
                   {    save = 1;      /* turn on save flag */
                        did[n] = 1;    /* turn on usage flag */
                        break;         /* stop looking */
                   }
              }

              if(save)                 /* extract if desired, else skip */
                   extfile(&hdr,arg[n],prt);
              else fseek(arc,hdr.size,1);
         }
    }

    else while(readhdr(&hdr,arc))      /* else extract all files */
         extfile(&hdr,"",prt);

    closearc(0);                       /* close archive after reading */

    if(note)
    {    for(n=0; n<num; n++)          /* report unused args */
         {    if(!did[n])
              {    printf("File not found: %s\n",arg[n]);
                   nerrs++;
              }
         }
    }

    free(name);
}

static INT extfile(hdr,path,prt)       /* extract a file */
struct heads *hdr;                     /* pointer to header data */
char *path;                            /* pointer to path name */
INT prt;                               /* true if printing */

{
    FILE *f, *fopen();                 /* extracted file, opener */
    char buf[STRLEN];                  /* input buffer */
    char fix[STRLEN];                  /* fixed name buffer */
    char *i, *rindex();                /* string index */

    if(prt)                            /* printing is much easier */
    {    unpack(arc,stdout,hdr);       /* unpack file from archive */
         printf("\f");                 /* eject the form */
         return;                       /* see? I told you! */
    }

    strcpy(fix,path);                  /* note path name template */
    if(!(i=rindex(fix,'/')))           /* find start of name */
        i = fix-1;
    strcpy(i+1,hdr->name);             /* replace template with name */

    if(note)
         printf("Extracting file: %s\n",fix);

    if(warn)
    {    if(f=fopen(fix,"r"))        /* see if it exists */
         {    fclose(f);
              printf("WARNING: File %s already exists!",fix);
              while(1)
              {    printf("  Overwrite it (y/n)? ");
                   fgets(buf,STRLEN,stdin);
                   *buf = toupper(*buf);
                   if(*buf=='Y' || *buf=='N')
                        break;
              }
              if(*buf=='N')
              {    printf("%s not extracted.\n",fix);
                   fseek(arc,hdr->size,1);
                   return;
              }
         }
    }

    if(!(f=fopen(fix,"w")))
    {    if(warn)
         {    printf("Cannot create %s\n",fix);
              nerrs++;
         }
         fseek(arc,hdr->size,1);
         return;
    }

    /* now unpack the file */

    unpack(arc,f,hdr);                 /* unpack file from archive */
    setstamp(f,hdr->date,hdr->time);   /* set the proper date/time stamp */
    fclose(f);                         /* all done writing to file */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcio.c'
then
	echo shar: will not over-write existing file "'arcio.c'"
else
cat << \SHAR_EOF > 'arcio.c'
/*  ARC - Archive utility - ARCIO

System V Version 1.0 based upon:
    Version 2.30, created on 02/03/86 at 22:56:00

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the file I/O routines used to manipulate
         an archive.
*/
#include "arc.h"

INT readhdr(hdr,f)                     /* read a header from an archive */
struct heads *hdr;                     /* storage for header */
FILE *f;                               /* archive to read header from */
{
    unsigned char dummy[28];
	INT i,j,k;
    char name[FNLEN];                  /* filename buffer */
    INT try = 0;                       /* retry counter */
    static INT first = 1;              /* true only on first read */

    if(!f)                             /* if archive didn't open */
         return 0;                     /* then pretend it's the end */
    if(feof(f))                        /* if no more data */
         return 0;                     /* then signal end of archive */

    if(fgetc(f)!=ARCMARK)              /* check archive validity */
    {    if(warn)
         {    printf("An entry in %s has a bad header.\n",arcname);
              nerrs++;
         }

         while(!feof(f))
         {    try++;
              if(fgetc(f)==ARCMARK)
              {    ungetc(hdrver=fgetc(f),f);
                   if(hdrver>=0 && hdrver<=ARCVER)
                        break;
              }
         }

         if(feof(f) && first)
              abort("%s is not an archive",arcname);

         if(warn)
              printf("  %d bytes skipped.\n",try);

         if(feof(f))
              return 0;
    }

    hdrver = fgetc(f);                 /* get header version */
    if(hdrver<0)
         abort("Invalid header in archive %s",arcname);
    if(hdrver==0)
         return 0;                     /* note our end of archive marker */
    if(hdrver>ARCVER)
    {    fread(name,sizeof(char),FNLEN,f);
         printf("I don't know how to handle file %s in archive %s\n",
              name,arcname);
         printf("I think you need a newer version of ARC.\n");
         exit(1);
    }

    /* amount to read depends on header type */

    if(hdrver==1)                      /* old style is shorter */
    {    fread(hdr,sizeof(struct heads)-sizeof(long),1,f);
         hdrver = 2;                   /* convert header to new format */
         hdr->length = hdr->size;      /* size is same when not packed */
    }
    else {
	fread(dummy,27,1,f);

	for(i=0;i<13;hdr->name[i]=dummy[i],i++);
	hdr->size = (long)((dummy[16]<<24) + (dummy[15]<<16) + (dummy[14]<<8)
	    + dummy[13]);
	hdr->date = (short)((dummy[18]<<8) + dummy[17]);
	hdr->time = (short)((dummy[20]<<8) + dummy[19]);
	hdr->crc  = (short)((dummy[22]<<8) + dummy[21]);
	hdr->length = (long)((dummy[26]<<24) + (dummy[25]<<16)
	    + (dummy[24]<<8) + dummy[23]);
    }

    first = 0; return 1;               /* we read something */
}

INT writehdr(hdr,f)                    /* write a header to an archive */
struct heads *hdr;                     /* header to write */
FILE *f;                               /* archive to write to */
{
    unsigned char dummy[27];

    fputc(ARCMARK,f);                  /* write out the mark of ARC */
    fputc(hdrver,f);                   /* write out the header version */
    if(!hdrver)                        /* if that's the end */
         return;                       /* then write no more */

    fwrite(hdr->name,1,13,f);
    fputc(hdr->size&255,f);         fputc((hdr->size>>8)&255,f);
    fputc((hdr->size>>16)&255,f);   fputc((hdr->size>>24)&255,f);
    fputc(hdr->date&255,f);         fputc((hdr->date>>8)&255,f);
    fputc(hdr->time&255,f);         fputc((hdr->time>>8)&255,f);
    fputc(hdr->crc&255,f);          fputc((hdr->crc>>8)&255,f);
    fputc(hdr->length&255,f);       fputc((hdr->length>>8)&255,f);
    fputc((hdr->length>>16)&255,f); fputc((hdr->length>>24)&255,f);

    /* note the newest file for updating the archive timestamp */

    if(hdr->date>arcdate
    ||(hdr->date==arcdate && hdr->time>arctime))
    {    arcdate = hdr->date;
         arctime = hdr->time;
    }
}

INT filecopy(f,t,size)                 /* bulk file copier */
FILE *f, *t;                           /* from, to */
long size;                             /* number of bytes */
{
    INT len;                           /* length of a given copy */
    INT putc_tst();

    while(size--)                      /* while more bytes to move */
         putc_tst(fgetc(f),t);
}

INT putc_tst(c,t)                      /* put a character, with tests */
char c;                                /* character to output */
FILE *t;                               /* file to write to */
{
    if(t)
         if(fputc(c,t)==EOF)
              abort("Write fail (disk full?)");
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arclst.c'
then
	echo shar: will not over-write existing file "'arclst.c'"
else
cat << \SHAR_EOF > 'arclst.c'
/*  ARC - Archive utility - ARCLST

System V Version 1.0 based upon:
    Version 2.34, created on 02/03/86 at 22:56:57

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to list the contents
         of an archive.
*/
#include "arc.h"

INT lstarc(num,arg)                    /* list files in archive */
INT num;                               /* number of arguments */
char *arg[];                           /* pointers to arguments */
{
    struct heads hdr;                  /* header data */
    INT list;                          /* true to list a file */
    INT did[MAXARG];                   /* true when argument was used */
    long tnum, tlen, tsize;            /* totals */
    INT n;                             /* index */
    INT lstfile();

    tnum = tlen = tsize = 0;           /* reset totals */

    for(n=0; n<num; n++)               /* for each argument */
         did[n] = 0;                   /* reset usage flag */
    rempath(num,arg);                  /* strip off paths */

    printf("Name          Length  ");
    if(bose)
        printf("  Stowage    SF   Size now");
    printf("  Date     ");
    if(bose)
        printf("  Time    CRC");
    printf("\n");

    printf("============  ========");
    if(bose)
        printf("  ========  ====  ========");
    printf("  =========");
    if(bose)
         printf("  ======  ====");
    printf("\n");

    openarc(0);                        /* open archive for reading */

    if(num)                            /* if files were named */
    {    while(readhdr(&hdr,arc))      /* process all archive files */
         {    list = 0;                /* reset list flag */
              for(n=0; n<num; n++)     /* for each template given */
              {    if(match(hdr.name,arg[n]))
                   {    list = 1;      /* turn on list flag */
                        did[n] = 1;    /* turn on usage flag */
                        break;         /* stop looking */
                   }
              }

              if(list)                 /* if this file is wanted */
              {    lstfile(&hdr);      /* then tell about it */
                   tnum++;             /* update totals */
                   tlen += hdr.length;
                   tsize += hdr.size;
              }

              fseek(arc,hdr.size,1);   /* move to next header */
         }
    }

    else while(readhdr(&hdr,arc))      /* else report on all files */
    {    lstfile(&hdr);
         tnum++;                       /* update totals */
         tlen += hdr.length;
         tsize += hdr.size;
         fseek(arc,hdr.size,1);        /* skip to next header */
    }

    closearc(0);                       /* close archive after reading */

    printf("        ====  ========");
    if (bose)
        printf("            ====  ========");
    printf("\n");
    printf("Total %6ld  %8ld  ",tnum,tlen);
    if (bose)
        printf("          %3ld%%  %8ld  ",100L - (100L*tsize)/tlen,tsize);
    printf("\n");

    if(note)
    {    for(n=0; n<num; n++)          /* report unused args */
         {    if(!did[n])
              {    printf("File not found: %s\n",arg[n]);
                   nerrs++;
              }
         }
    }
}

static INT lstfile(hdr)                /* tell about a file */
struct heads *hdr;                     /* pointer to header data */
{
    INT yr, mo, dy;                    /* parts of a date */
    INT hh, mm, ss;                    /* parts of a time */

    static char *mon[] =               /* month abbreviations */
    {    "Jan",    "Feb",    "Mar",    "Apr",
         "May",    "Jun",    "Jul",    "Aug",
         "Sep",    "Oct",    "Nov",    "Dec"
    };

    yr = (hdr->date >> 9) & 0x7f;      /* dissect the date */
    mo = (hdr->date >> 5) & 0x0f;
    dy = hdr->date & 0x1f;

    hh = (hdr->time >> 11) & 0x1f;     /* dissect the time */
    mm = (hdr->time >> 5) & 0x3f;
    ss = (hdr->time & 0x1f) * 2;

    printf("%-12s  %8ld  ",hdr->name,hdr->length);

    if(bose)
    {    switch(hdrver)
         {
         case 1:
         case 2:
              printf("   --   ");
              break;
         case 3:
              printf(" Packed ");
              break;
         case 4:
              printf("Squeezed");
              break;
         case 5:
         case 6:
         case 7:
              printf("crunched");
              break;
         case 8:
              printf("Crunched");
              break;
         default:
              printf("Unknown!");
         }

         printf("  %3d%%",100L - (100L*hdr->size)/hdr->length);
         printf("  %8ld  ",hdr->size);
    }

    printf("%2d %3s %02d", dy, mon[mo-1], (yr+80)%100);

    if(bose)
         printf("  %2d:%02d%c  %04x",
              (hh>12?hh-12:hh), mm, (hh>12?'p':'a'),
              (unsigned INT)hdr->crc);

    printf("\n");
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcm.h'
then
	echo shar: will not over-write existing file "'arcm.h'"
else
cat << \SHAR_EOF > 'arcm.h'
/*  ARC archive utility - standard MACRO insert file.

System V Version 1.0.
*/

#define ARCMARK 26 /*                   special archive marker        */
#define ARCVER 8   /*                   archive header version code   */
#define STRLEN 100 /*                   system standard string length */
#define FNLEN 13   /*                   file name length              */
#define MAXARG 25  /*                   maximum number of arguments   */
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
Mike Stump, Cal State Univ, Northridge Comp Sci Department
uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs

aeusemrs@csun.UUCP (02/03/87)

#! /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 the files:
#	arclzw.c
#	arcmatch.c
#	arcmisc.c
#	arcpack.c
# This archive created: Mon Feb  2 17:59:35 1987
export PATH; PATH=/bin:$PATH
if test -f 'arclzw.c'
then
	echo shar: will not over-write existing file "'arclzw.c'"
else
cat << \SHAR_EOF > 'arclzw.c'
/*  ARC - Archive utility - ARCLZW

System V Version 1.0 based upon:
    Version 1.88, created on 01/20/86 at 16:47:04

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to implement Lempel-Zev
         data compression, which calls for building a coding table on
         the fly.  This form of compression is especially good for encoding
         files which contain repeated strings, and can often give dramatic
         improvements over traditional Huffman SQueezing.

    Programming notes:
         In this section I am drawing heavily on the COMPRESS program
         from UNIX.  The basic method is taken from "A Technique for High
         Performance Data Compression", Terry A. Welch, IEEE Computer
         Vol 17, No 6 (June 1984), pp 8-19.  Also see "Knuth's Fundamental
         Algorithms", Donald Knuth, Vol 3, Section 6.4.

         As best as I can tell, this method works by tracing down a hash
         table of code strings where each entry has the property:

              if <string> <char> is in the table
              then <string> is in the table.
*/
#include "arc.h"

/* definitions for older style crunching */

#define FALSE    0
#define TRUE     !FALSE
#define TABSIZE  4096
#define NO_PRED  0xFFFF
#define EMPTY    0xFFFF
#define NOT_FND  0xFFFF

static unsigned INT inbuf;             /* partial input code storage */
static INT sp;                         /* current stack pointer */

static struct entry                    /* string table entry format */
{   char used;                         /* true when this entry is in use */
    unsigned INT next;                 /* ptr to next in collision list */
    unsigned INT predecessor;          /* code for preceeding string */
    unsigned char follower;            /* char following string */
}   string_tab[TABSIZE];               /* the code string table */


/* definitions for the new dynamic Lempel-Zev crunching */

#define BITS   12                      /* maximum bits per code */
#define HSIZE  5003                    /* 80% occupancy */
#define INIT_BITS 9                    /* initial number of bits/code */

static INT n_bits;                     /* number of bits/code */
static INT maxcode;                    /* maximum code, given n_bits */
#define MAXCODE(n)      ((1<<(n)) - 1) /* maximum code calculation */
static INT maxcodemax =  1 << BITS;    /* largest possible code (+1) */

static unsigned char buf[BITS];        /* input/output buffer */

static unsigned char lmask[9] =        /* left side masks */
{   0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
static unsigned char rmask[9] =        /* right side masks */
{   0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};

static INT offset;                     /* byte offset for code output */
static long in_count;                  /* length of input */
static long bytes_out;                 /* length of compressed output */
static unsigned INT ent;

/* To save much memory (which we badly need at this point), we overlay
 * the table used by the previous version of Lempel-Zev with those used
 * by the new version.  Since no two of these routines will be used
 * together, we can safely do this.  Note that the tables used for Huffman
 * squeezing may NOT overlay these, since squeezing and crunching are done
 * in parallel.
 */

static long htab[HSIZE];               /* hash code table   (crunch) */
static unsigned INT codetab[HSIZE];    /* string code table (crunch) */

static unsigned INT *prefix = codetab; /* prefix code table (uncrunch) */

static unsigned char suffix[HSIZE];    /* suffix table (uncrunch) */
static INT free_ent;                   /* first unused entry */
static INT firstcmp;                   /* true at start of compression */
static unsigned char stack[HSIZE];     /* local push/pop stack */

/*
 * block compression parameters -- after all codes are used up,
 * and compression rate changes, start over.
 */

static INT clear_flg;
static long ratio;
#define CHECK_GAP 10000                /* ratio check interval */
static long checkpoint;

/*
 * the next two codes should not be changed lightly, as they must not
 * lie within the contiguous general code space.
 */
#define FIRST   257                    /* first free entry */
#define CLEAR   256                    /* table clear output code */

static INT cl_block(t)                 /* table clear for block compress */
FILE *t;                               /* our output file */
{
    long rat;
    INT putcode();

    checkpoint = in_count + CHECK_GAP;

    if(in_count > 0x007fffff)          /* shift will overflow */
    {    rat = bytes_out >> 8;
         if(rat == 0)                  /* Don't divide by zero */
              rat = 0x7fffffff;
         else rat = in_count / rat;
    }
    else rat = (in_count<<8)/bytes_out;/* 8 fractional bits */

    if(rat > ratio)
         ratio = rat;
    else
    {    ratio = 0;
         setmem	(htab,HSIZE*sizeof(long),0xff);
         free_ent = FIRST;
         clear_flg = 1;
         putcode(CLEAR,t);
    }
}

/*****************************************************************
 *
 * Output a given code.
 * Inputs:
 *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
 *              that n_bits =< (long)wordsize - 1.
 * Outputs:
 *      Outputs code to the file.
 * Assumptions:
 *      Chars are 8 bits long.
 * Algorithm:
 *      Maintain a BITS character long buffer (so that 8 codes will
 * fit in it exactly).  When the buffer fills up empty it and start over.
 */

static INT putcode(code,t)             /* output a code */
INT code;                              /* code to output */
FILE *t;                               /* where to put it */
{
    INT r_off = offset;                /* right offset */
    INT bits = n_bits;                 /* bits to go */
    unsigned char *bp = buf;           /* buffer pointer */
    INT n;                             /* index */

    if(code >= 0)                      /* if a real code */
    {    /*
          * Get to the first byte.
          */
         bp += (r_off >> 3);
         r_off &= 7;

         /*
          * Since code is always >= 8 bits, only need to mask the first
          * hunk on the left.
          */
         *bp = (*bp&rmask[r_off]) | (code<<r_off) & lmask[r_off];
         bp++;
         bits -= (8 - r_off);
         code >>= (8 - r_off);

         /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
         if(bits >= 8)
         {    *bp++ = code;
              code >>= 8;
              bits -= 8;
         }

         /* Last bits. */
         if(bits)
              *bp = code;

         offset += n_bits;

         if(offset == (n_bits << 3))
         {    bp = buf;
              bits = n_bits;
              bytes_out += bits;
              do
                   putc_pak(*bp++,t);
              while(--bits);
              offset = 0;
         }

         /*
          * If the next entry is going to be too big for the code size,
          * then increase it, if possible.
          */
         if(free_ent>maxcode || clear_flg>0)
         {    /*
               * Write the whole buffer, because the input side won't
               * discover the size increase until after it has read it.
               */
              if(offset > 0)
              {    bp = buf;           /* reset pointer for writing */
                   bytes_out += n = n_bits;
                   while(n--)
                        putc_pak(*bp++,t);
              }
              offset = 0;

              if(clear_flg)            /* reset if clearing */
              {    maxcode = MAXCODE(n_bits = INIT_BITS);
                   clear_flg = 0;
              }
              else                     /* else use more bits */
              {    n_bits++;
                   if(n_bits == BITS)
                        maxcode = maxcodemax;
                   else
                        maxcode = MAXCODE(n_bits);
              }
         }
    }

    else                               /* dump the buffer on EOF */
    {    bytes_out += n = (offset+7) / 8;

         if(offset > 0)
              while(n--)
                   putc_pak(*bp++,t);
         offset = 0;
    }
}

/*****************************************************************
 *
 * Read one code from the standard input.  If EOF, return -1.
 * Inputs:
 *      cmpin
 * Outputs:
 *      code or -1 is returned.
 */

static INT getcode(f)                  /* get a code */
FILE *f;                               /* file to get from */
{
    INT code;
    static INT offset = 0, size = 0;
    INT r_off, bits;
    unsigned char *bp = buf;

    if(clear_flg > 0 || offset >= size || free_ent > maxcode)
    {    /*
          * If the next entry will be too big for the current code
          * size, then we must increase the size.  This implies reading
          * a new buffer full, too.
          */
         if(free_ent > maxcode)
         {    n_bits++;
              if(n_bits == BITS)
                   maxcode = maxcodemax;    /* won't get any bigger now */
              else maxcode = MAXCODE(n_bits);
         }
         if(clear_flg > 0)
         {    maxcode = MAXCODE(n_bits = INIT_BITS);
              clear_flg = 0;
         }

         for(size=0; size<n_bits; size++)
         {    if((code=getc_unp(f))==EOF)
                   break;
              else buf[size] = code;
         }
         if(size <= 0)
              return -1;               /* end of file */

         offset = 0;
         /* Round size down to integral number of codes */
         size = (size << 3)-(n_bits - 1);
    }
    r_off = offset;
    bits = n_bits;

    /*
     * Get to the first byte.
     */
    bp +=(r_off >> 3);
    r_off &= 7;

    /* Get first part (low order bits) */
    code = (*bp++ >> r_off);
    bits -= 8 - r_off;
    r_off = 8 - r_off;                 /* now, offset into code word */

    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
    if(bits >= 8)
    {    code |= *bp++ << r_off;
         r_off += 8;
         bits -= 8;
    }
    /* high order bits. */
    code |= (*bp & rmask[bits]) << r_off;
    offset += n_bits;

    return code;
}

/*
 * compress a file
 *
 * Algorithm:  use open addressing double hashing (no chaining) on the
 * prefix code / next character combination.  We do a variant of Knuth's
 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
 * secondary probe.  Here, the modular division first probe is gives way
 * to a faster exclusive-or manipulation.  Also do block compression with
 * an adaptive reset, where the code table is cleared when the compression
 * ratio decreases, but after the table fills.  The variable-length output
 * codes are re-sized at this point, and a special CLEAR code is generated
 * for the decompressor.
 */

INT init_cm(f,t)                       /* initialize for compression */
FILE *f;                               /* file we will be compressing */
FILE *t;                               /* where we will put it */
{
    offset = 0;
    bytes_out = 1;
    clear_flg = 0;
    ratio = 0;
    in_count = 1;
    checkpoint = CHECK_GAP;
    maxcode = MAXCODE(n_bits = INIT_BITS);
    free_ent = FIRST;
    setmem(htab,HSIZE*sizeof(long),0xff);
    n_bits = INIT_BITS;                /* set starting code size */

    putc_pak(BITS,t);                  /* note our max code length */

    firstcmp = 1;                      /* next byte will be first */
}

INT putc_cm(c,t)                       /* compress a character */
unsigned char c;                       /* character to compress */
FILE *t;                               /* where to put it */
{
    static long fcode;
    static INT hshift;
    INT i;
    INT disp;

    if(firstcmp)                       /* special case for first byte */
    {    ent = c;                      /* remember first byte */

         hshift = 0;
         for(fcode=(long)HSIZE;  fcode<65536L; fcode*=2L)
              hshift++;
         hshift = 8 - hshift;          /* set hash code range bund */

         firstcmp = 0;                 /* no longer first */
         return;
    }

    in_count++;
    fcode =(long)(((long)c << BITS)+ent);
    i = (c<<hshift)^ent;               /* xor hashing */

    if(htab[i]==fcode)
    {    ent = codetab[i];
         return;
    }
    else if(htab[i]<0)                 /* empty slot */
         goto nomatch;
    disp = HSIZE - i;                  /* secondary hash (after G.Knott) */
    if(i == 0)
         disp = 1;

probe:
    if((i -= disp) < 0)
         i += HSIZE;

    if(htab[i] == fcode)
    {    ent = codetab[i];
         return;
    }
    if(htab[i] > 0)
         goto probe;

nomatch:
    putcode(ent,t);
    ent = c;
    if(free_ent < maxcodemax)
    {    codetab[i] = free_ent++;      /* code -> hashtable */
         htab[i] = fcode;
    }
    else if((long)in_count >= checkpoint)
         cl_block(t);
}

long pred_cm(t)                        /* finish compressing a file */
FILE *t;                               /* where to put it */
{
    putcode(ent,t);                    /* put out the final code */
    putcode(-1,t);                     /* tell output we are done */

    return bytes_out;                  /* say how big it got */
}

/*
 * Decompress a file.  This routine adapts to the codes in the file
 * building the string table on-the-fly; requiring no table to be stored
 * in the compressed file.  The tables used herein are shared with those of
 * the compress() routine.  See the definitions above.
 */

INT decomp(f,t)                        /* decompress a file */
FILE *f;                               /* file to read codes from */
FILE *t;                               /* file to write text to */
{
    unsigned char *stackp;
    INT finchar;
    INT code, oldcode, incode;

    if((code=getc_unp(f))!=BITS)
         abort("File packed with %d bits, I can only handle %d",code,BITS);

    n_bits = INIT_BITS;                /* set starting code size */
    clear_flg = 0;

    /*
     * As above, initialize the first 256 entries in the table.
     */
    maxcode = MAXCODE(n_bits=INIT_BITS);
    for(code = 255; code >= 0; code--)
    {    prefix[code] = 0;
         suffix[code] = (unsigned char)code;
    }
    free_ent = FIRST;

    finchar = oldcode = getcode(f);
    if(oldcode == -1)                  /* EOF already? */
         return;                       /* Get out of here */
    putc_ncr((char)finchar,t);         /* first code must be 8 bits=char */
    stackp = stack;

    while((code = getcode(f))> -1)
    {    if(code==CLEAR)
         {    for(code = 255; code >= 0; code--)
                   prefix[code] = 0;
              clear_flg = 1;
              free_ent = FIRST - 1;
              if((code=getcode(f))==-1)/* O, untimely death! */
                   break;
         }
         incode = code;
         /*
          * Special case for KwKwK string.
          */
         if(code >= free_ent)
         {    *stackp++ = finchar;
              code = oldcode;
         }

         /*
          * Generate output characters in reverse order
          */
         while(code >= 256)
         {    *stackp++ = suffix[code];
              code = prefix[code];
         }
         *stackp++ = finchar = suffix[code];

         /*
          * And put them out in forward order
          */
         do
              putc_ncr(*--stackp,t);
         while(stackp > stack);

         /*
          * Generate the new entry.
          */
         if((code=free_ent) < maxcodemax)
         {    prefix[code] = (unsigned short)oldcode;
              suffix[code] = finchar;
              free_ent = code+1;
         }
         /*
          * Remember previous code.
          */
         oldcode = incode;
    }
}


/*************************************************************************
 * Please note how much trouble it can be to maintain upwards            *
 * compatibility.  All that follows is for the sole purpose of unpacking *
 * files which were packed using an older method.                        *
 *************************************************************************/


/*  The h() pointer points to the routine to use for calculating a hash
    value.  It is set in the init routines to point to either of oldh()
    or newh().

    oldh() calculates a hash value by taking the middle twelve bits
    of the square of the key.

    newh() works somewhat differently, and was tried because it makes
    ARC about 23% faster.  This approach was abandoned because dynamic
    Lempel-Zev (above) works as well, and packs smaller also.  However,
    inadvertent release of a developmental copy forces us to leave this in.
*/

static unsigned INT (*h)();            /* pointer to hash function */

static unsigned INT oldh(pred,foll)    /* old hash function */
unsigned INT pred;                     /* code for preceeding string */
unsigned char foll;                    /* value of following char */
{
    long local;                        /* local hash value */

    local = (pred + foll) | 0x0800;    /* create the hash key */
    local *= local;                    /* square it */
    return (local >> 6) & 0x0FFF;      /* return the middle 12 bits */
}

static unsigned INT newh(pred,foll)    /* new hash function */
unsigned INT pred;                     /* code for preceeding string */
unsigned char foll;                    /* value of following char */
{
    return ((pred+foll)*15073)&0xFFF;  /* faster hash */
}

/*  The eolist() function is used to trace down a list of entries with
    duplicate keys until the last duplicate is found.
*/

static unsigned INT eolist(index)      /* find last duplicate */
unsigned INT index;
{
    INT temp;

    while(temp=string_tab[index].next) /* while more duplicates */
         index = temp;

    return index;
}

/*  The hash() routine is used to find a spot in the hash table for a new
    entry.  It performs a "hash and linear probe" lookup, using h() to
    calculate the starting hash value and eolist() to perform the linear
    probe.  This routine DOES NOT detect a table full condition.  That
    MUST be checked for elsewhere.
*/

static unsigned INT hash(pred,foll)    /* find spot in the string table */
unsigned INT pred;                     /* code for preceeding string */
unsigned char foll;                    /* char following string */
{
    unsigned INT local, tempnext;      /* scratch storage */
    struct entry *ep;                  /* allows faster table handling */

    local = (*h)(pred,foll);           /* get initial hash value */

    if(!string_tab[local].used)        /* if that spot is free */
         return local;                 /* then that's all we need */

    else                               /* else a collision has occured */
    {    local = eolist(local);        /* move to last duplicate */

         /*   We must find an empty spot. We start looking 101 places
              down the table from the last duplicate.
         */

         tempnext = (local+101) & 0x0FFF;
         ep = &string_tab[tempnext];   /* initialize pointer */

         while(ep->used)               /* while empty spot not found */
         {    if(++tempnext==TABSIZE)  /* if we are at the end */
              {    tempnext = 0;       /* wrap to beginning of table*/
                   ep = string_tab;
              }
              else ++ep;               /* point to next element in table */
         }

         /*   local still has the pointer to the last duplicate, while
              tempnext has the pointer to the spot we found.  We use
              this to maintain the chain of pointers to duplicates.
         */

         string_tab[local].next = tempnext;

         return tempnext;
    }
}

/*  The unhash() function is used to search the hash table for a given key.
    Like hash(), it performs a hash and linear probe search.  It returns
    either the number of the entry (if found) or NOT_FND (if not found).
*/

static unsigned INT unhash(pred,foll)      /* search string table for a key */
unsigned INT pred;                     /* code of preceeding string */
unsigned char foll;                    /* character following string */
{
    unsigned INT local, offset;        /* scratch storage */
    struct entry *ep;                  /* this speeds up access */

    local = (*h)(pred,foll);           /* initial hash */

    while(1)
    {    ep = &string_tab[local];      /* speed up table access */

         if((ep->predecessor==pred) && (ep->follower==foll))
              return local;            /* we have a match */

         if(!ep->next)                 /* if no more duplicates */
              return NOT_FND;          /* then key is not listed */

         local = ep->next;             /* move on to next duplicate */
    }
}

/*  The init_tab() routine is used to initialize our hash table.
    You realize, of course, that "initialize" is a complete misnomer.
*/

static INT init_tab()                      /* set ground state in hash table */
{
    unsigned INT i;                    /* table index */
    INT upd_tab();

    setmem((char *)string_tab,sizeof(string_tab),0);

    for(i=0; i<256; i++)               /* list all single byte strings */
         upd_tab(NO_PRED,i);

    inbuf = EMPTY;                     /* nothing is in our buffer */
}

/*  The upd_tab routine is used to add a new entry to the string table.
    As previously stated, no checks are made to ensure that the table
    has any room.  This must be done elsewhere.
*/

INT upd_tab(pred,foll)                     /* add an entry to the table */
unsigned INT pred;                     /* code for preceeding string */
unsigned INT foll;                     /* character which follows string */
{
    struct entry *ep;                  /* pointer to current entry */

    /* calculate offset just once */

    ep = &string_tab[hash(pred,foll)];

    ep->used = TRUE;                   /* this spot is now in use */
    ep->next = 0;                      /* no duplicates after this yet */
    ep->predecessor = pred;            /* note code of preceeding string */
    ep->follower = foll;               /* note char after string */
}

/*  This algorithm encoded a file into twelve bit strings (three nybbles).
    The gocode() routine is used to read these strings a byte (or two)
    at a time.
*/

static INT gocode(fd)                      /* read in a twelve bit code */
FILE *fd;                              /* file to get code from */
{
    unsigned INT localbuf, returnval;

    if(inbuf==EMPTY)                   /* if on a code boundary */
    {    if((localbuf=getc_unp(fd))==EOF)   /* get start of next code */
              return EOF;              /* pass back end of file status */
         localbuf &= 0xFF;             /* mask down to true byte value */
         if((inbuf=getc_unp(fd))==EOF) /* get end of code, start of next */
              return EOF;              /* this should never happen */
         inbuf &= 0xFF;                /* mask down to true byte value */

         returnval = ((localbuf<<4)&0xFF0) + ((inbuf>>4)&0x00F);
         inbuf &= 0x000F;              /* leave partial code pending */
    }

    else                               /* buffer contains first nybble */
    {    if((localbuf=getc_unp(fd))==EOF)
              return EOF;
         localbuf &= 0xFF;

         returnval = localbuf + ((inbuf<<8)&0xF00);
         inbuf = EMPTY;                /* note no hanging nybbles */
    }
    return returnval;                  /* pass back assembled code */
}

static INT push(c)                         /* push char onto stack */
INT c;                                 /* character to push */
{
    stack[sp] = ((char) c);            /* coerce integer into a char */

    if(++sp >= TABSIZE)
         abort("Stack overflow\n");
}

static INT pop()                       /* pop character from stack */
{
    if(sp>0)
         return ((INT) stack[--sp]);   /* leave ptr at next empty slot */

    else return EMPTY;
}

/***** LEMPEL-ZEV DECOMPRESSION *****/

static INT code_count;                 /* needed to detect table full */
static unsigned INT code;                  /* where we are so far */
static INT firstc;                     /* true only on first character */

INT init_ucr(new)                          /* get set for uncrunching */
INT new;                               /* true to use new hash function */
{
    if(new)                            /* set proper hash function */
         h = newh;
    else h = oldh;

    sp = 0;                            /* clear out the stack */
    init_tab();                        /* set up atomic code definitions */
    code_count = TABSIZE - 256;        /* note space left in table */
    firstc = 1;                        /* true only on first code */
}

INT getc_ucr(f)                        /* get next uncrunched byte */
FILE *f;                               /* file containing crunched data */
{
    unsigned INT c;                    /* a character of input */
 INT code, newcode;
    static INT oldcode, finchar;
    struct entry *ep;                  /* allows faster table handling */

    if(firstc)                         /* first code is always known */
    {    firstc = FALSE;               /* but next will not be first */
         oldcode = gocode(f);
         return finchar = string_tab[oldcode].follower;
    }

    if(!sp)                            /* if stack is empty */
    {    if((code=newcode=gocode(f))==EOF)
              return EOF;

         ep = &string_tab[code];       /* initialize pointer */

         if(!ep->used)                 /* if code isn't known */
         {    code = oldcode;
              ep = &string_tab[code];  /* re-initialize pointer */
              push(finchar);
         }

         while(ep->predecessor!=NO_PRED)
         {    push(ep->follower);      /* decode string backwards */
              code = ep->predecessor;
              ep = &string_tab[code];
         }

         push(finchar=ep->follower);   /* save first character also */

         /*   The above loop will terminate, one way or another,
              with string_tab[code].follower equal to the first
              character in the string.
         */

         if(code_count)                /* if room left in string table */
         {    upd_tab(oldcode,finchar);
              --code_count;
         }

         oldcode = newcode;
    }

    return pop();                      /* return saved character */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcmatch.c'
then
	echo shar: will not over-write existing file "'arcmatch.c'"
else
cat << \SHAR_EOF > 'arcmatch.c'
/*  ARC - Archive utility - ARCMATCH

System V Version 1.0 based upon:
    Version 2.17, created on 12/17/85) at 20:32:18

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains service routines needed to maintain an archive.
*/
#include "arc.h"

INT match(n,t)                         /* test name against template */
char *n;                               /* name to test */
char *t;                               /* template to test against */
{
    /* first match name part */

    while((*n && *n!='.') || (*t && *t!='.'))
    {    if(*n!=*t && *t!='?')         /* match fail? */
         {    if(*t!='*')              /* wildcard fail? */
                   return 0;           /* then no match */
              else                     /* else jump over wildcard */
              {    while(*n && *n!='.')
                        n++;
                   while(*t && *t!='.')
                        t++;
                   break;              /* name part matches wildcard */
              }
         }
         else                          /* match good for this char */
         {    n++;                     /* advance to next char */
              t++;
         }
    }

    if(*n && *n=='.') n++;             /* skip extension delimiters */
    if(*t && *t=='.') t++;

    /* now match name part */

    while(*n || *t)
    {    if(*n!=*t && *t!='?')         /* match fail? */
         {    if(*t!='*')              /* wildcard fail? */
                   return 0;           /* then no match */
              else return 1;           /* else good enough */
         }
         else                          /* match good for this char */
         {    n++;                     /* advance to next char */
              t++;
         }
    }

    return 1;                          /* match worked */
}

INT rempath(nargs,arg)                 /* remove paths from filenames */
INT nargs;                             /* number of names */
char *arg[];                           /* pointers to names */
{
    char *i, *rindex();                /* string index, reverse indexer */
    INT n;                             /* index */

    for(n=0; n<nargs; n++)             /* for each supplied name */
    {    i=rindex(arg[n],'/');         /* search for end of path */
         if(i)                         /* if path was found */
              arg[n] = i+1;            /* then skip it */
    }
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcmisc.c'
then
	echo shar: will not over-write existing file "'arcmisc.c'"
else
cat << \SHAR_EOF > 'arcmisc.c'
/*  ARC - Archive utility - ARCMISC

System V Version 1.0.

*/
#include <ctype.h>
#include "arc.h"

static INT _makefn(source,dest)
unsigned char *source;
unsigned char *dest;
{
    int j;

    setmem(dest, 17, 0);             /* clear result field */
    if (strlen (source) > 1 && source[1] == ':')
    for (j = 0; j < 2;)
        dest[j++] = *source++;
    for (j = 3; *source && *source != '.'; ++source)
        if (j < 11)
            dest[j++] = *source;
    for (j = 12; *source; ++source)
        if (j < 16)
            dest[j++] = *source;
}

/*    make a file name using a template
*/
char *makefnam(rawfn,template,result)
unsigned char *rawfn;                /* the original file name */
unsigned char *template;             /* the template data */
unsigned char *result;               /* where to place the result */
{
    unsigned char et[17],er[17];

    _makefn(template,et);
    _makefn(rawfn,er);
    *result=0;                       /* assure no data */
    strcat(result,er[0]?er:et);
    strcat(result,er[3]?er+3:et+3);
    strcat(result,er[12]?er+12:et+12);
    return ((char *)&result[0]);
}

/*  convert a string to upper case  */
upper(s) char *s; {
    while (*s = toupper(*s)) ++s;
}

setmem(dest,size,c) char *dest,c; INT size; {
    int i;

    for (i = 0; i < size; dest[i] = c, i++);
}

abort(s,arg1,arg2,arg3) char *s; {
    fprintf(stderr,"arc: ");
    fprintf(stderr,s,arg1,arg2,arg3);
    fprintf(stderr,"\n");
    perror("arc system:");
    exit(1);
}

rename(o, n) char *o, *n; {
    return link(o, n) || unlink(o);
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcpack.c'
then
	echo shar: will not over-write existing file "'arcpack.c'"
else
cat << \SHAR_EOF > 'arcpack.c'
/*  ARC - Archive utility - ARCPACK

System V Version 1.0 based upon:
    Version 3.37, created on 02/03/86 at 22:58:01

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to compress a file
         when placing it in an archive.
*/
#include "arc.h"

/* stuff for non-repeat packing */

#define DLE 0x90                       /* repeat sequence marker */

static unsigned char state;            /* current packing state */

/* non-repeat packing states */

#define NOHIST  0                      /* don't consider previous input*/
#define SENTCHAR 1                     /* lastchar set, no lookahead yet */
#define SENDNEWC 2                     /* run over, send new char next */
#define SENDCNT 3                      /* newchar set, send count next */

/* packing results */

static long stdlen;                    /* length for standard packing */
static INT crcval;                     /* CRC check value */

INT pack(f,t,hdr)                          /* pack file into an archive */
FILE *f, *t;                           /* source, destination */
struct heads *hdr;                     /* pointer to header data */
{
 INT c;                             /* one character of stream */
    long ncrlen;                       /* length after packing */
    long huflen;                       /* length after squeezing */
    long lzwlen;                       /* length after crunching */
    long pred_sq(), file_sq();         /* stuff for squeezing */
    long pred_cm();                    /* dynamic crunching cleanup */
    char tnam[STRLEN];                /* temporary name buffer */
    char *makefnam();                  /* filename fixer upper */
    FILE *crn = NULL;                  /* temporary crunch file */
    INT getch();
    INT getc_ncr();
    INT putc_pak();

    /* first pass - see which method is best */

    if(!nocomp)                        /* if storage kludge not active */
    {    if(note)
            { printf(" analyzing, "); fflush(stdout);}

         if(arctemp)                   /* use temp area if specified */
              sprintf(tnam,"%s.crn",arctemp);
         else makefnam("$ARCTEMP.crn",arcname,tnam);
         crn = fopen(tnam,"w+");
         state = NOHIST;               /* initialize ncr packing */
         stdlen =  ncrlen = 0;         /* reset size counters */
         crcval = 0;                   /* initialize CRC check value */
         setcode();                    /* initialize encryption */

         init_cm(f,crn);               /* initialize for crunching */
         init_sq();                    /* initialize for squeeze scan */

         while((c=getc_ncr(f))!=EOF)   /* for each byte of file */
         {    ncrlen++;                /* one more packed byte */
              scan_sq(c);              /* see what squeezing can do */
              putc_cm(c,crn);          /* see what crunching can do */
         }
         huflen = pred_sq();           /* finish up after squeezing */
         lzwlen = pred_cm(crn);        /* finish up after crunching */
    }
    else                               /* else kludge the method */
    {    stdlen = 0;                   /* make standard look best */
         ncrlen = huflen = lzwlen = 1;
    }

    /* standard set-ups common to all methods */

    fseek(f,0L,0);                     /* rewind input */
    hdr->crc = crcval;                 /* note CRC check value */
    hdr->length = stdlen;              /* set actual file length */
    state = NOHIST;                    /* reinitialize ncr packing */
    setcode();                         /* reinitialize encryption */

    /* choose and use the shortest method */

    if(stdlen<=ncrlen && stdlen<=huflen && stdlen<=lzwlen)
    {    if(note)
          { printf("storing, "); fflush(stdout);}/* store w/out compression */
         hdrver = 2;                   /* note packing method */
         stdlen = crcval = 0;          /* recalc these for kludge */
         while((c=getch(f))!=EOF)      /* store it straight */
              putc_pak(c,t);
         hdr->crc = crcval;
         hdr->length = hdr->size = stdlen;
    }

    else if(ncrlen<huflen && ncrlen<lzwlen)
    {    if(note)
              { printf("packing, ");
              fflush(stdout);} /* pack w/repeat suppression */
         hdrver = 3;                   /* note packing method */
         hdr->size = ncrlen;           /* set data length */
         while((c=getc_ncr(f))!=EOF)
              putc_pak(c,t);
    }

    else if(huflen<lzwlen)
    {    if(note)
            { printf("squeezing, "); fflush(stdout);}
         hdrver = 4;                   /* note packing method */
         hdr->size = file_sq(f,t);     /* note final size */
    }

    else
    {    if(note)
            { printf("crunching, "); fflush(stdout);}
         hdrver = 8;
         hdr->size = lzwlen;           /* size should not change */
         if(crn)                       /* if temp was created */
         {    fseek(crn,0L,0);         /* then copy over crunched temp */
              while((c=fgetc(crn))!=EOF)
                   putc_tst(c,t);
         }
         else                          /* else re-crunch */
         {    init_cm(f,t);
              while((c=getc_ncr(f))!=EOF)
                   putc_cm(c,t);
              pred_cm(t);              /* finish up after crunching */
         }
    }

    /* standard cleanups common to all methods */

    if(crn)                            /* get rid of crunch temporary */
    {    fclose(crn);
         if(unlink(tnam) && warn)
         {    printf("Cannot delete temporary file %s\n",tnam);
              nerrs++;
         }
    }
    if(note)
         printf("done.\n");
}

/*  Non-repeat compression - text is passed through normally, except that
    a run of more than two is encoded as:

         <char> <DLE> <count>

    Special case: a count of zero indicates that the DLE is really a DLE,
    not a repeat marker.
*/

INT getc_ncr(f)                        /* get bytes with collapsed runs */
FILE *f;                               /* file to get from */
{
    static INT lastc;                  /* value returned on last call */
    static INT repcnt;                 /* repetition counter */
    static INT c;                      /* latest value seen */

    switch(state)                      /* depends on our state */
    {
    case NOHIST:                       /* no relevant history */
         state = SENTCHAR;
         return lastc = getch(f);      /* remember the value next time */

    case SENTCHAR:                     /* char was sent. look ahead */
         switch(lastc)                 /* action depends on char */
         {
         case DLE:                     /* if we sent a real DLE */
              state = NOHIST;          /* then start over again */
              return 0;                /* but note that the DLE was real */

         case EOF:                     /* EOF is always a special case */
              return EOF;

         default:                      /* else test for a repeat */
              for(repcnt=1; (c=getch(f))==lastc && repcnt<255; repcnt++)
                   ;                   /* find end of run */

              switch(repcnt)           /* action depends on run size */
              {
              case 1:                  /* not a repeat */
                   return lastc = c;   /* but remember value next time */

              case 2:                  /* a repeat, but too short */
                   state = SENDNEWC;   /* send the second one next time */
                   return lastc;

              default:                 /* a run - compress it */
                   state = SENDCNT;    /* send repeat count next time */
                   return DLE;         /* send repeat marker this time */
              }
         }

    case SENDNEWC:                     /* send second char of short run */
         state = SENTCHAR;
         return lastc = c;

    case SENDCNT:                      /* sent DLE, now send count */
         state = SENDNEWC;
         return repcnt;

    default:
         abort("Bug - bad ncr state\n");
    }
}

static INT getch(f)                    /* special get char for packing */
FILE *f;                               /* file to get from */
{
 INT c;                             /* a char from the file */

    if((c=fgetc(f))!=EOF)              /* if not the end of file */
    {    crcval = addcrc(crcval,c);    /* then update CRC check value */
         stdlen++;                     /* and bump length counter */
    }

    return c;
}

INT putc_pak(c,f)                          /* put a packed byte into archive */
char c;                                /* byte to put */
FILE *f;                               /* archive to put it in */
{
    putc_tst(code(c),f);               /* put encoded byte, with checks */
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
Mike Stump, Cal State Univ, Northridge Comp Sci Department
uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs

aeusemrs@csun.UUCP (02/03/87)

#! /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 the files:
#	arcs.h
#	arcsq.c
#	arcsvc.c
#	arctst.c
#	arcunp.c
#	arcusq.c
# This archive created: Mon Feb  2 18:00:36 1987
export PATH; PATH=/bin:$PATH
if test -f 'arcs.h'
then
	echo shar: will not over-write existing file "'arcs.h'"
else
cat << \SHAR_EOF > 'arcs.h'
/*  ARC - Archive utility - Archive file header format

System V Version 1.0 based upon:
    Version 2.12, created on 12/17/85 at 14:40:26

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file defines the format of an archive file header, excluding
         the archive marker and the header version number.

         Each entry in an archive begins with a one byte archive marker,
         which is set to 26.  The marker is followed by a one byte
         header type code, from zero to 7.

         If the header type code is zero, then it is an end marker, and
         no more data should be read from the archive.

         If the header type code is in the range 2 to 7, then it is
         followed by a standard archive header, which is defined below.

         If the header type code is one, then it is followed by an older
         format archive header.  The older format header does not contain
         the true length.  A header should be read for a length of
         sizeof(struct heads)-sizeof(long).  Then set length equal to size
         and change the header version to 2.

    Programming note:
         The crc value given in the header is based on the unpacked data.
*/

struct heads                           /* archive entry header format */
{   char name[FNLEN];                  /* file name */
    long size;                         /* size of file, in bytes */
    unsigned INT date;                 /* creation date */
    unsigned INT time;                 /* creation time */
    INT crc;                           /* cyclic redundancy check */
    long length;                       /* true file length */
}   ;
SHAR_EOF
fi # end of overwriting check
if test -f 'arcsq.c'
then
	echo shar: will not over-write existing file "'arcsq.c'"
else
cat << \SHAR_EOF > 'arcsq.c'
/*  ARC - Archive utility - ARCSQ

System V Version 1.0 based upon:
    Version 3.10, created on 01/30/86 at 20:10:46

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to squeeze a file
         when placing it in an archive.

    Programming notes:
         Most of the routines used for the Huffman squeezing algorithm
         were lifted from the SQ program by Dick Greenlaw, as adapted
         to CI-C86 by Robert J. Beilstein.
*/
#include "arc.h"

/* stuff for Huffman squeezing */

#define TRUE 1
#define FALSE 0
#define ERROR (-1)
#define SPEOF 256                      /* special endfile token */
#define NOCHILD (-1)                   /* marks end of path through tree */
#define NUMVALS 257                    /* 256 data values plus SPEOF*/
#define NUMNODES (NUMVALS+NUMVALS-1)   /* number of nodes */
#define MAXCOUNT (unsigned INT) 65535      /* biggest unsigned integer */

/* The following array of structures are the nodes of the
   binary trees. The first NUMVALS nodes become the leaves of the
   final tree and represent the values of the data bytes being
   encoded and the special endfile, SPEOF.
   The remaining nodes become the internal nodes of the final tree.
*/

struct nd                              /* shared by unsqueezer */
{   unsigned INT weight;                   /* number of appearances */
 INT tdepth;                        /* length on longest path in tree */
 INT lchild, rchild;                /* indices to next level */
}   node[NUMNODES];                    /* use large buffer */

static INT dctreehd;                   /* index to head of final tree */

/* This is the encoding table:
   The bit strings have first bit in low bit.
   Note that counts were scaled so code fits unsigned integer.
*/

static INT codelen[NUMVALS];           /* number of bits in code */
static unsigned INT code[NUMVALS];         /* code itself, right adjusted */
static unsigned INT tcode;                 /* temporary code value */
static long valcount[NUMVALS];         /* actual count of times seen */

/* Variables used by encoding process */

static INT curin;                      /* value currently being encoded */
static INT cbitsrem;                   /* # of code string bits left */
static unsigned INT ccode;                 /* current code right justified */

INT init_sq()                              /* prepare for scanning pass */
{
 INT i;                             /* node index */

    /* Initialize all nodes to single element binary trees
       with zero weight and depth.
    */

    for(i=0; i<NUMNODES; ++i)
    {    node[i].weight = 0;
         node[i].tdepth = 0;
         node[i].lchild = NOCHILD;
         node[i].rchild = NOCHILD;
    }

    for(i=0; i<NUMVALS; i++)
         valcount[i] = 0;
}

INT scan_sq(c)                             /* add a byte to the tables */
INT c;                                 /* byte to add */
{
    unsigned INT *wp;                      /* speeds up weight counting */

    /* Build frequency info in tree */

    if(c == EOF)                       /* it's traditional */
         c = SPEOF;                    /* dumb, but traditional */

    if(*(wp = &node[c].weight) !=  MAXCOUNT)
         ++(*wp);                      /* bump weight counter */

    valcount[c]++;                     /* bump byte counter */
}

long pred_sq()                         /* predict size of squeezed file */
{
 INT i;
 INT btlist[NUMVALS];               /* list of intermediate b-trees */
 INT listlen;                       /* length of btlist */
    unsigned INT ceiling;                  /* limit for scaling */
    long size = 0;                     /* predicted size */
 INT numnodes;                      /* # of nodes in simplified tree */
    INT scale();
    INT heap();
    INT bld_tree();
    INT buildenc();
    INT init_enc();

    scan_sq(EOF);                      /* signal end of input */

    ceiling = MAXCOUNT;

    /* Keep trying to scale and encode */

    do
    {    scale(ceiling);
         ceiling /= 2;                 /* in case we rescale */

         /* Build list of single node binary trees having
            leaves for the input values with non-zero counts
         */

         for(i=listlen=0; i<NUMVALS; ++i)
         {    if(node[i].weight != 0)
              {    node[i].tdepth = 0;
                   btlist[listlen++] = i;
              }
         }

         /* Arrange list of trees into a heap with the entry
            indexing the node with the least weight at the top.
         */

         heap(btlist,listlen);

         /* Convert the list of trees to a single decoding tree */

         bld_tree(btlist,listlen);

         /* Initialize the encoding table */

         init_enc();

         /* Try to build encoding table.
            Fail if any code is > 16 bits long.
         */
    }    while(buildenc(0,dctreehd) == ERROR);

    /* Initialize encoding variables */

    cbitsrem = 0;                      /* force initial read */
    curin = 0;                         /* anything but endfile */

    for(i=0; i<NUMVALS; i++)           /* add bits for each code */
         size += valcount[i] * codelen[i];

    size = (size+7)/8;                 /* reduce to number of bytes */

    numnodes = dctreehd<NUMVALS ? 0 : dctreehd-(NUMVALS-1);

    size += sizeof(INT) + 2*numnodes*sizeof(INT);

    return size;
}

/* The count of number of occurrances of each input value
   have already been prevented from exceeding MAXCOUNT.
   Now we must scale them so that their sum doesn't exceed
   ceiling and yet no non-zero count can become zero.
   This scaling prevents errors in the weights of the
   interior nodes of the Huffman tree and also ensures that
   the codes will fit in an unsigned integer. Rescaling is
   used if necessary to limit the code length.
*/

static INT scale(ceil)
unsigned INT ceil;                         /* upper limit on total weight */
{
    register INT i,c;
 INT ovflw, divisor;
    unsigned INT w, sum;
    unsigned char increased;           /* flag */

    do
    {    for(i=sum=ovflw=0; i<NUMVALS; ++i)
         {    if(node[i].weight > (ceil-sum))
                   ++ovflw;
              sum += node[i].weight;
         }

         divisor = ovflw + 1;

         /* Ensure no non-zero values are lost */

         increased = FALSE;
         for(i=0; i<NUMVALS; ++i)
         {    w = node[i].weight;
              if(w<divisor && w!=0)
              {    /* Don't fail to provide a code if it's used at all */

                   node[i].weight = divisor;
                   increased = TRUE;
              }
         }
    }    while(increased);

    /* Scaling factor choosen, now scale */

    if(divisor>1)
         for(i=0; i<NUMVALS; ++i)
              node[i].weight /= divisor;
}

/* heap() and adjust() maintain a list of binary trees as a
   heap with the top indexing the binary tree on the list
   which has the least weight or, in case of equal weights,
   least depth in its longest path. The depth part is not
   strictly necessary, but tends to avoid long codes which
   might provoke rescaling.
*/

static INT heap(list,length)
INT list[], length;
{
    register INT i;
    INT adjust();

    for(i=(length-2)/2; i>=0; --i)
         adjust(list,i,length-1);
}

/* Make a heap from a heap with a new top */

static INT adjust(list,top,bottom)
INT list[], top, bottom;
{
    register INT k, temp;
    INT cmptrees();

    k = 2 * top + 1;                   /* left child of top */
    temp = list[top];                  /* remember root node of top tree */

    if(k<=bottom)
    {    if(k<bottom && cmptrees(list[k],list[k+1]))
              ++k;

         /* k indexes "smaller" child (in heap of trees) of top */
         /* now make top index "smaller" of old top and smallest child */

         if(cmptrees(temp,list[k]))
         {    list[top] = list[k];
              list[k] = temp;

              /* Make the changed list a heap */

              adjust(list,k,bottom);   /* recursive */
         }
    }
}

/* Compare two trees, if a > b return true, else return false.
   Note comparison rules in previous comments.
*/

static INT cmptrees(a,b)
INT a, b;                              /* root nodes of trees */
{
    if(node[a].weight > node[b].weight)
         return TRUE;
    if(node[a].weight == node[b].weight)
         if(node[a].tdepth > node[b].tdepth)
              return TRUE;
    return FALSE;
}

/* HUFFMAN ALGORITHM: develops the single element trees
   into a single binary tree by forming subtrees rooted in
   interior nodes having weights equal to the sum of weights of all
   their descendents and having depth counts indicating the
   depth of their longest paths.

   When all trees have been formed into a single tree satisfying
   the heap property (on weight, with depth as a tie breaker)
   then the binary code assigned to a leaf (value to be encoded)
   is then the series of left (0) and right (1)
   paths leading from the root to the leaf.
   Note that trees are removed from the heaped list by
   moving the last element over the top element and
   reheaping the shorter list.
*/

static INT bld_tree(list,len)
INT list[];
INT len;
{
    register INT freenode;             /* next free node in tree */
    register struct nd *frnp;          /* free node pointer */
 INT lch, rch;                      /* temps for left, right children */
 INT i;
    INT maxchar();

    /* Initialize index to next available (non-leaf) node.
       Lower numbered nodes correspond to leaves (data values).
    */

    freenode = NUMVALS;

    while(len>1)
    {    /* Take from list two btrees with least weight
            and build an interior node pointing to them.
            This forms a new tree.
         */

         lch = list[0];                /* This one will be left child */

         /* delete top (least) tree from the list of trees */

         list[0] = list[--len];
         adjust(list,0,len-1);

         /* Take new top (least) tree. Reuse list slot later */

         rch = list[0];                /* This one will be right child */

         /* Form new tree from the two least trees using
            a free node as root. Put the new tree in the list.
         */

         frnp = &node[freenode];       /* address of next free node */
         list[0] = freenode++;         /* put at top for now */
         frnp->lchild = lch;
         frnp->rchild = rch;
         frnp->weight = node[lch].weight + node[rch].weight;
         frnp->tdepth = 1 + maxchar(node[lch].tdepth, node[rch].tdepth);

         /* reheap list  to get least tree at top */

         adjust(list,0,len-1);
    }
    dctreehd = list[0];                /* head of final tree */
}

static INT maxchar(a,b)
{
    return a>b ? a : b;
}

static INT init_enc()
{
    register INT i;

    /* Initialize encoding table */

    for(i=0; i<NUMVALS; ++i)
         codelen[i] = 0;
}

/* Recursive routine to walk the indicated subtree and level
   and maintain the current path code in bstree. When a leaf
   is found the entire code string and length are put into
   the encoding table entry for the leaf's data value .

   Returns ERROR if codes are too long.
*/

static INT buildenc(level,root)
INT level;              /* level of tree being examined, from zero */
INT root;               /* root of subtree is also data value if leaf */
{
    register INT l, r;

    l = node[root].lchild;
    r = node[root].rchild;

    if(l==NOCHILD && r==NOCHILD)
    {    /* Leaf. Previous path determines bit string
            code of length level (bits 0 to level - 1).
            Ensures unused code bits are zero.
         */

         codelen[root] = level;
         code[root] = tcode & (((unsigned INT)~0) >> (16-level));
         return (level>16) ? ERROR : NULL;
    }

    else
    {    if(l!=NOCHILD)
         {    /* Clear path bit and continue deeper */

              tcode &= ~(1 << level);
              if(buildenc(level+1,l)==ERROR)
                   return ERROR;       /* pass back bad statuses */
         }
         if(r!=NOCHILD)
         {    /* Set path bit and continue deeper */

              tcode |= 1 << level;
              if(buildenc(level+1,r)==ERROR)
                   return ERROR;       /* pass back bad statuses */
         }
    }
    return NULL;                       /* it worked if we reach here */
}

static INT put_int(n,f)                    /* output an integer */
INT n;                                 /* integer to output */
FILE *f;                               /* file to put it to */
{
    putc_pak(n&0xff,f);                /* first the low byte */
    putc_pak(n>>8,f);                  /* then the high byte */
}

/* Write out the header of the compressed file */

static long wrt_head(ob)
FILE *ob;
{
    register INT l,r;
 INT i, k;
 INT numnodes;                      /* # of nodes in simplified tree */

    /* Write out a simplified decoding tree. Only the interior
       nodes are written. When a child is a leaf index
       (representing a data value) it is recoded as
       -(index + 1) to distinguish it from interior indexes
       which are recoded as positive indexes in the new tree.

       Note that this tree will be empty for an empty file.
    */

    numnodes = dctreehd<NUMVALS ? 0 : dctreehd-(NUMVALS-1);
    put_int(numnodes,ob);

    for(k=0, i=dctreehd; k<numnodes; ++k, --i)
    {    l = node[i].lchild;
         r = node[i].rchild;
         l = l<NUMVALS ? -(l+1) : dctreehd-l;
         r = r<NUMVALS ? -(r+1) : dctreehd-r;
         put_int(l,ob);
         put_int(r,ob);
    }

    return sizeof(INT) + numnodes*2*sizeof(INT);
}

/* Get an encoded byte or EOF. Reads from specified stream AS NEEDED.

   There are two unsynchronized bit-byte relationships here.
   The input stream bytes are converted to bit strings of
   various lengths via the static variables named c...
   These bit strings are concatenated without padding to
   become the stream of encoded result bytes, which this
   function returns one at a time. The EOF (end of file) is
   converted to SPEOF for convenience and encoded like any
   other input value. True EOF is returned after that.
*/

static INT gethuff(ib)                 /* Returns bytes except for EOF */
FILE *ib;
{
 INT rbyte;                         /* Result byte value */
 INT need, take;                    /* numbers of bits */

    rbyte = 0;
    need = 8;                          /* build one byte per call */

    /* Loop to build a byte of encoded data.
       Initialization forces read the first time.
    */

loop:
    if(cbitsrem>=need)                 /* if current code is big enough */
    {    if(need==0)
              return rbyte;

         rbyte |= ccode << (8-need);   /* take what we need */
         ccode >>= need;               /* and leave the rest */
         cbitsrem -= need;
         return rbyte & 0xff;
    }

    /* We need more than current code */

    if(cbitsrem>0)
    {    rbyte |= ccode << (8-need);   /* take what there is */
         need -= cbitsrem;
    }

    /* No more bits in current code string */

    if(curin==SPEOF)
    {    /* The end of file token has been encoded. If
            result byte has data return it and do EOF next time.
         */

         cbitsrem = 0;
         return (need==8) ? EOF : rbyte + 0;
    }

    /* Get an input byte */

    if((curin=getc_ncr(ib)) == EOF)
         curin = SPEOF;                /* convenient for encoding */

    ccode = code[curin];               /* get the new byte's code */
    cbitsrem = codelen[curin];

    goto loop;
}

/*  This routine is used to perform the actual squeeze operation.  It can
    only be called after the file has been scanned.  It returns the true
    length of the squeezed entry.
*/

long file_sq(f,t)                      /* squeeze a file into an archive */
FILE *f;                               /* file to squeeze */
FILE *t;                               /* archive to receive file */
{
 INT c;                             /* one byte of squeezed data */
    long size;                         /* size after squeezing */

    size = wrt_head(t);                /* write out the decode tree */

    while((c=gethuff(f))!=EOF)
    {    putc_pak(c,t);
         size++;
    }

    return size;                       /* report true size */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcsvc.c'
then
	echo shar: will not over-write existing file "'arcsvc.c'"
else
cat << \SHAR_EOF > 'arcsvc.c'
/*  ARC - Archive utility - ARCSVC

System V Version 1.0 based upon:
    Version 2.15, created on 12/17/85 at 15:17:16

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains service routines needed to maintain an archive.
*/
#include "arc.h"

INT openarc(chg)                       /* open archive */
INT chg;                               /* true to open for changes */
{
    FILE *fopen();                     /* file opener */

    if(!(arc=fopen(arcname,"r")))
    {    if(chg)
              printf("Creating new archive: %s\n",arcname);
         else abort("Cannot read archive: %s",arcname);
    }

    if(chg)                            /* if opening for changes */
         if(!(new=fopen(newname,"w")))
              abort("Cannot create archive copy: %s",newname);
}

INT closearc(chg)                          /* close an archive */
INT chg;                               /* true if archive was changed */
{
    if(arc)                            /* if we had an initial archive */
         fclose(arc);                  /* then close it */

    if(chg)                            /* if things have changed */
    {    setstamp(new,arcdate,arctime);/* archive matches newest file */
         fclose(new);                  /* close the new copy */

         if(arc)                       /* if we had an original archive */
         {    if(keepbak)              /* if a backup is wanted */
              {    unlink(bakname);    /* erase any old copies */
                   if(rename(arcname,bakname))
                        abort("Cannot rename %s to %s",arcname,bakname);
                   printf("Keeping backup archive: %s\n",bakname);
              }
              else if(unlink(arcname))
                   abort("Cannot delete old archive: %s",arcname);
         }

         if(rename(newname,arcname))
              abort("Cannot rename %s to %s",newname,arcname);
    }
}

/* CRC computation logic

   The logic for this method of calculating the CRC 16 bit polynomial
   is taken from an article by David Schwaderer in the April 1985
   issue of PC Tech Journal.
*/

static INT crctab[] =                  /* CRC lookup table */
{   0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
    0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
    0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
    0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
    0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
    0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
    0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
    0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
    0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
    0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
    0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
    0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
    0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
    0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
    0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
    0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
    0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
    0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
    0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
    0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
    0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
    0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
    0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
    0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
    0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
    0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
    0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
    0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
    0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
    0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
    0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
    0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};
INT addcrc(crc,c)                      /* update a CRC check */
INT crc;                               /* running CRC value */
unsigned char c;                       /* character to add */
{
    return (((crc>>8)&0x00ff) ^ crctab[(crc^c)&0x00ff]) & 0x0000ffff;
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arctst.c'
then
	echo shar: will not over-write existing file "'arctst.c'"
else
cat << \SHAR_EOF > 'arctst.c'
/*  ARC - Archive utility - ARCTST

System V Version 1.0 based upon:
    Version 2.12, created on 02/03/86 at 23:00:40

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to test archive integrity.
*/
#include "arc.h"

INT tstarc()                               /* test integrity of an archive */
{
    struct heads hdr;                  /* file header */
    long arcsize, ftell();             /* archive size */

    openarc(0);                        /* open archive for reading */
    fseek(arc,0L,2);                   /* move to end of archive */
    arcsize = ftell(arc);              /* see how big it is */
    fseek(arc,0L,0);                   /* return to top of archive */

    printf ("Testing archive...\n");
    while(readhdr(&hdr,arc))
    {    if(ftell(arc)+hdr.size>arcsize)
         {    printf("Archive truncated in file %s\n",hdr.name);
              nerrs++;
              break;
         }

         else
         {    printf("    File: %-12s  ",hdr.name);
              fflush(stdout);
              if(unpack(arc,NULL,&hdr))
                   nerrs++;
              else printf("okay\n");
         }
    }

    if(nerrs<1)
         printf("No errors detected\n");
    else if(nerrs==1)
         printf("One error detected\n");
    else printf("%d errors detected\n",nerrs);
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcunp.c'
then
	echo shar: will not over-write existing file "'arcunp.c'"
else
cat << \SHAR_EOF > 'arcunp.c'
/*  ARC - Archive utility - ARCUNP

System V Version 1.0 based upon:
    Version 3.16, created on 02/03/86 at 23:01:16

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to expand a file
         when taking it out of an archive.
*/
#include "arc.h"

/* stuff for repeat unpacking */

#define DLE 0x90                       /* repeat byte flag */

static INT state;                      /* repeat unpacking state */

/* repeat unpacking states */

#define NOHIST 0                       /* no relevant history */
#define INREP 1                        /* sending a repeated value */

static INT crcval;                     /* CRC check value */
static long size;                      /* bytes to read */

INT unpack(f,t,hdr)                    /* unpack an archive entry */
FILE *f, *t;                           /* source, destination */
struct heads *hdr;                     /* pointer to file header data */
{
 INT c;                             /* one char of stream */
    INT putc_unp();
    INT putc_ncr();
    INT getc_unp();

    /* setups common to all methods */

    crcval = 0;                        /* reset CRC check value */
    size = hdr->size;                  /* set input byte counter */
    state = NOHIST;                    /* initial repeat unpacking state */
    setcode();                         /* set up for decoding */

    /* use whatever method is appropriate */

    switch(hdrver)                     /* choose proper unpack method */
    {
    case 1:                            /* standard packing */
    case 2:
         while((c=getc_unp(f))!=EOF)
              putc_unp(c,t);
         break;

    case 3:                            /* non-repeat packing */
         while((c=getc_unp(f))!=EOF)
              putc_ncr(c,t);
         break;

    case 4:                            /* Huffman squeezing */
         init_usq(f);
         while((c=getc_usq(f))!=EOF)
              putc_ncr(c,t);
         break;

    case 5:                            /* Lempel-Zev compression */
         init_ucr(0);
         while((c=getc_ucr(f))!=EOF)
              putc_unp(c,t);
         break;

    case 6:                            /* Lempel-Zev plus non-repeat */
         init_ucr(0);
         while((c=getc_ucr(f))!=EOF)
              putc_ncr(c,t);
         break;

    case 7:                            /* L-Z plus ncr with new hash */
         init_ucr(1);
         while((c=getc_ucr(f))!=EOF)
              putc_ncr(c,t);
         break;

    case 8:                            /* dynamic Lempel-Zev */
         decomp(f,t);
         break;

    default:                           /* unknown method */
         if(warn)
         {    printf("I don't know how to unpack file %s\n",hdr->name);
              printf("I think you need a newer version of ARC\n");
              nerrs++;
         }
         fseek(f,hdr->size,1);         /* skip over bad file */
         return 1;                     /* note defective file */
    }

    /* cleanups common to all methods */

    if((crcval&0xffff)!=(hdr->crc&0x0000ffff))
    {    if(warn)
         {    printf("WARNING: File %s fails CRC check\n",hdr->name);
              nerrs++;
         }
         return 1;                     /* note defective file */
    }
    return 0;                          /* file is okay */
}

/*  This routine is used to put bytes in the output file.  It also
    performs various housekeeping functions, such as maintaining the
    CRC check value.
*/

static INT putc_unp(c,t)                   /* output an unpacked byte */
char c;                                /* byte to output */
FILE *t;                               /* file to output to */
{
    crcval = addcrc(crcval,c);         /* update the CRC check value */
    putc_tst(c,t);
}

/*  This routine is used to decode non-repeat compression.  Bytes are
    passed one at a time in coded format, and are written out uncoded.
    The data is stored normally, except that runs of more than two
    characters are represented as:

         <char> <DLE> <count>

    With a special case that a count of zero indicates a DLE as data,
    not as a repeat marker.
*/

INT putc_ncr(c,t)                          /* put NCR coded bytes */
unsigned char c;                       /* next byte of stream */
FILE *t;                               /* file to receive data */
{
    static INT lastc;                  /* last character seen */

    switch(state)                      /* action depends on our state */
    {
    case NOHIST:                       /* no previous history */
         if(c==DLE)                    /* if starting a series */
              state = INREP;           /* then remember it next time */
         else putc_unp(lastc=c,t);     /* else nothing unusual */
         return;

    case INREP:                        /* in a repeat */
         if(c)                         /* if count is nonzero */
              while(--c)               /* then repeatedly ... */
                   putc_unp(lastc,t);  /* ... output the byte */
         else putc_unp(DLE,t);         /* else output DLE as data */
         state = NOHIST;               /* back to no history */
         return;

    default:
         abort("Bad NCR unpacking state (%d)",state);
    }
}

/*  This routine provides low-level byte input from an archive.  This
    routine MUST be used, as end-of-file is simulated at the end of
    the archive entry.
*/

INT getc_unp(f)                        /* get a byte from an archive */
FILE *f;                               /* archive file to read */
{
    if(!size)                          /* if no data left */
         return EOF;                   /* then pretend end of file */

    size--;                            /* deduct from input counter */
    return code(fgetc(f));             /* and return next decoded byte */
}
SHAR_EOF
fi # end of overwriting check
if test -f 'arcusq.c'
then
	echo shar: will not over-write existing file "'arcusq.c'"
else
cat << \SHAR_EOF > 'arcusq.c'
/*  ARC - Archive utility - ARCUSQ

System V Version 1.0 based upon:
    Version 3.13, created on 01/30/86 at 20:11:42

(C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED

    By:  Thom Henderson

    Description:
         This file contains the routines used to expand a file
         which was packed using Huffman squeezing.

         Most of this code is taken from an USQ program by Richard
         Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
*/
#include "arc.h"

/* stuff for Huffman unsqueezing */

#define ERROR (-1)

#define SPEOF 256                      /* special endfile token */
#define NUMVALS 257                    /* 256 data values plus SPEOF */

EXTERN struct nd                       /* decoding tree */
{ INT child[2];                      /* left, right */
}   node[NUMVALS];                     /* use large buffer */

static INT bpos;                       /* last bit position read */
static INT curin;                      /* last byte value read */
static INT numnodes;                   /* number of nodes in decode tree */

static INT get_int(f)                  /* get an integer */
FILE *f;                               /* file to get it from */
{
    INT i;

    i = getc_unp(f);
    return (short)(i | (getc_unp(f)<<8));
}

INT init_usq(f)                            /* initialize Huffman unsqueezing */
FILE *f;                               /* file containing squeezed data */
{
 INT i;                             /* node index */

    bpos = 99;                         /* force initial read */

    numnodes = get_int(f);

    if(numnodes<0 || numnodes>=NUMVALS)
         abort("File has an invalid decode tree");

    /* initialize for possible empty tree (SPEOF only) */

    node[0].child[0] = -(SPEOF + 1);
    node[0].child[1] = -(SPEOF + 1);

    for(i=0; i<numnodes; ++i)          /* get decoding tree from file */
    {    node[i].child[0] = get_int(f);
         node[i].child[1] = get_int(f);
    }
}

INT getc_usq(f)                        /* get byte from squeezed file */
FILE *f;                               /* file containing squeezed data */
{
 INT i;                             /* tree index */

    /* follow bit stream in tree to a leaf */

    for(i=0; i>=0; )                   /* work down(up?) from root */
    {    if(++bpos>7)
         {    if((curin=getc_unp(f)) == ERROR)
                   return(ERROR);
              bpos = 0;

              /* move a level deeper in tree */
              i = node[i].child[1&curin];
         }
         else i = node[i].child[1 & (curin >>= 1)];
    }

    /* decode fake node index to original data value */

    i = -(i + 1);

    /* decode special endfile token to normal EOF */

    i = (i==SPEOF) ? EOF : i;
    return i;
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
Mike Stump, Cal State Univ, Northridge Comp Sci Department
uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs