add -a flag
[unix-history] / usr / src / usr.bin / compress / compress.c
index 1666c10..296886e 100644 (file)
@@ -1,6 +1,33 @@
+/*
+ * Copyright (c) 1985, 1986 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * James A. Woods, derived from original work by Spencer Thomas
+ * and Joseph Orost.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1985, 1986 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)compress.c 5.6 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)compress.c 5.10 (Berkeley) %G%";
+#endif /* not lint */
 
 /* 
  * Compress - data compression program 
 
 /* 
  * Compress - data compression program 
@@ -135,7 +162,7 @@ char_type magic_header[] = { "\037\235" };  /* 1F 9D */
 /*
  * compress.c - File compression ala IEEE Computer, June 1984.
  *
 /*
  * compress.c - File compression ala IEEE Computer, June 1984.
  *
- * Authors:    Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
+ * Authors:    Spencer W. Thomas       (decvax!utah-cs!thomas)
  *             Jim McKie               (decvax!mcvax!jim)
  *             Steve Davies            (decvax!vax135!petsd!peora!srd)
  *             Ken Turkowski           (decvax!decwrl!turtlevax!ken)
  *             Jim McKie               (decvax!mcvax!jim)
  *             Steve Davies            (decvax!vax135!petsd!peora!srd)
  *             Ken Turkowski           (decvax!decwrl!turtlevax!ken)
@@ -261,6 +288,9 @@ static char rcs_ident[] = "$Header: compress.c,v 4.0 85/07/30 12:50:00 joe Relea
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifdef notdef
+#include <sys/ioctl.h>
+#endif
 
 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
 
 
 #define ARGVAL() (*++(*argv) || (--argc && *++argv))
 
@@ -299,8 +329,17 @@ unsigned short * codetab[5] = {
 #define codetabof(i)   (codetab[(i) >> 14][(i) & 0x3fff])
 
 #else  /* Normal machine */
 #define codetabof(i)   (codetab[(i) >> 14][(i) & 0x3fff])
 
 #else  /* Normal machine */
+
+#ifdef sel     /* gould base register braindamage */
+/*NOBASE*/
+count_int htab [HSIZE];
+unsigned short codetab [HSIZE];
+/*NOBASE*/
+#else
 count_int htab [HSIZE];
 unsigned short codetab [HSIZE];
 count_int htab [HSIZE];
 unsigned short codetab [HSIZE];
+#endif sel
+
 #define htabof(i)      htab[i]
 #define codetabof(i)   codetab[i]
 #endif /* XENIX_16 */
 #define htabof(i)      htab[i]
 #define codetabof(i)   codetab[i]
 #endif /* XENIX_16 */
@@ -326,7 +365,8 @@ count_int fsize;
 #endif /* XENIX_16 */
 
 code_int free_ent = 0;                 /* first unused entry */
 #endif /* XENIX_16 */
 
 code_int free_ent = 0;                 /* first unused entry */
-int exit_stat = 0;
+int exit_stat = 0;                     /* per-file status */
+int perm_stat = 0;                     /* permanent status */
 
 code_int getcode();
 
 
 code_int getcode();
 
@@ -336,11 +376,12 @@ fprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
 }
 int debug = 0;
 #else
 }
 int debug = 0;
 #else
-fprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
+fprintf(stderr,"Usage: compress [-fvc] [-b maxbits] [file ...]\n");
 }
 #endif /* DEBUG */
 int nomagic = 0;       /* Use a 3-byte magic number header, unless old file */
 int zcat_flg = 0;      /* Write output on stdout, suppress messages */
 }
 #endif /* DEBUG */
 int nomagic = 0;       /* Use a 3-byte magic number header, unless old file */
 int zcat_flg = 0;      /* Write output on stdout, suppress messages */
+int precious = 1;      /* Don't unlink output file on interrupt */
 int quiet = 1;         /* don't tell me about compression */
 
 /*
 int quiet = 1;         /* don't tell me about compression */
 
 /*
@@ -364,7 +405,8 @@ char ofname [100];
 #ifdef DEBUG
 int verbose = 0;
 #endif /* DEBUG */
 #ifdef DEBUG
 int verbose = 0;
 #endif /* DEBUG */
-int (*bgnd_flag)();
+int (*oldint)();
+int bgnd_flag;
 
 int do_decomp = 0;
 
 
 int do_decomp = 0;
 
@@ -415,12 +457,20 @@ register int argc; char **argv;
     struct stat statbuf;
     extern onintr(), oops();
 
     struct stat statbuf;
     extern onintr(), oops();
 
-
-    if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
+    /* This bg check only works for sh. */
+    if ( (oldint = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
        signal ( SIGINT, onintr );
        signal ( SIGSEGV, oops );
     }
        signal ( SIGINT, onintr );
        signal ( SIGSEGV, oops );
     }
-
+    bgnd_flag = oldint != SIG_DFL;
+#ifdef notdef     /* This works for csh but we don't want it. */
+    { int tgrp;
+    if (bgnd_flag == 0 && ioctl(2, TIOCGPGRP, (char *)&tgrp) == 0 &&
+      getpgrp(0) != tgrp)
+       bgnd_flag = 1;
+    }
+#endif
+    
 #ifdef COMPATIBLE
     nomagic = 1;       /* Original didn't have a magic number */
 #endif /* COMPATIBLE */
 #ifdef COMPATIBLE
     nomagic = 1;       /* Original didn't have a magic number */
 #endif /* COMPATIBLE */
@@ -529,7 +579,7 @@ register int argc; char **argv;
     if (*filelist != NULL) {
        for (fileptr = filelist; *fileptr; fileptr++) {
            exit_stat = 0;
     if (*filelist != NULL) {
        for (fileptr = filelist; *fileptr; fileptr++) {
            exit_stat = 0;
-           if (do_decomp != 0) {                       /* DECOMPRESSION */
+           if (do_decomp) {                    /* DECOMPRESSION */
                /* Check for .Z suffix */
                if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
                    /* No .Z: tack one on */
                /* Check for .Z suffix */
                if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
                    /* No .Z: tack one on */
@@ -539,7 +589,9 @@ register int argc; char **argv;
                }
                /* Open input file */
                if ((freopen(*fileptr, "r", stdin)) == NULL) {
                }
                /* Open input file */
                if ((freopen(*fileptr, "r", stdin)) == NULL) {
-                       perror(*fileptr); continue;
+                   perror(*fileptr);
+                   perm_stat = 1;
+                   continue;
                }
                /* Check the magic number */
                if (nomagic == 0) {
                }
                /* Check the magic number */
                if (nomagic == 0) {
@@ -571,7 +623,9 @@ register int argc; char **argv;
                }
                /* Open input file */
                if ((freopen(*fileptr, "r", stdin)) == NULL) {
                }
                /* Open input file */
                if ((freopen(*fileptr, "r", stdin)) == NULL) {
-                   perror(*fileptr); continue;
+                   perror(*fileptr);
+                   perm_stat = 1;
+                   continue;
                }
                stat ( *fileptr, &statbuf );
                fsize = (long) statbuf.st_size;
                }
                stat ( *fileptr, &statbuf );
                fsize = (long) statbuf.st_size;
@@ -610,7 +664,7 @@ register int argc; char **argv;
                    char response[2];
                    response[0] = 'n';
                    fprintf(stderr, "%s already exists;", ofname);
                    char response[2];
                    response[0] = 'n';
                    fprintf(stderr, "%s already exists;", ofname);
-                   if (foreground()) {
+                   if (bgnd_flag == 0 && isatty(2)) {
                        fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
                        ofname);
                        fflush(stderr);
                        fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
                        ofname);
                        fflush(stderr);
@@ -630,8 +684,10 @@ register int argc; char **argv;
            if(zcat_flg == 0) {         /* Open output file */
                if (freopen(ofname, "w", stdout) == NULL) {
                    perror(ofname);
            if(zcat_flg == 0) {         /* Open output file */
                if (freopen(ofname, "w", stdout) == NULL) {
                    perror(ofname);
+                   perm_stat = 1;
                    continue;
                }
                    continue;
                }
+               precious = 0;
                if(!quiet)
                        fprintf(stderr, "%s: ", *fileptr);
            }
                if(!quiet)
                        fprintf(stderr, "%s: ", *fileptr);
            }
@@ -647,6 +703,7 @@ register int argc; char **argv;
 #endif /* DEBUG */
            if(zcat_flg == 0) {
                copystat(*fileptr, ofname);     /* Copy stats */
 #endif /* DEBUG */
            if(zcat_flg == 0) {
                copystat(*fileptr, ofname);     /* Copy stats */
+               precious = 1;
                if((exit_stat == 1) || (!quiet))
                        putc('\n', stderr);
            }
                if((exit_stat == 1) || (!quiet))
                        putc('\n', stderr);
            }
@@ -688,7 +745,7 @@ register int argc; char **argv;
 #endif /* DEBUG */
        }
     }
 #endif /* DEBUG */
        }
     }
-    exit(exit_stat);
+    exit(perm_stat ? perm_stat : exit_stat);
 }
 
 static int offset;
 }
 
 static int offset;
@@ -1289,12 +1346,14 @@ char *ifname, *ofname;
                fprintf(stderr, "%s: ", ifname);
        fprintf(stderr, " -- not a regular file: unchanged");
        exit_stat = 1;
                fprintf(stderr, "%s: ", ifname);
        fprintf(stderr, " -- not a regular file: unchanged");
        exit_stat = 1;
+       perm_stat = 1;
     } else if (statbuf.st_nlink > 1) {
        if(quiet)
                fprintf(stderr, "%s: ", ifname);
        fprintf(stderr, " -- has %d other links: unchanged",
                statbuf.st_nlink - 1);
        exit_stat = 1;
     } else if (statbuf.st_nlink > 1) {
        if(quiet)
                fprintf(stderr, "%s: ", ifname);
        fprintf(stderr, " -- has %d other links: unchanged",
                statbuf.st_nlink - 1);
        exit_stat = 1;
+       perm_stat = 1;
     } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
        if(!quiet)
                fprintf(stderr, " -- file unchanged");
     } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
        if(!quiet)
                fprintf(stderr, " -- file unchanged");
@@ -1318,33 +1377,17 @@ char *ifname, *ofname;
     if (unlink(ofname))
        perror(ofname);
 }
     if (unlink(ofname))
        perror(ofname);
 }
-/*
- * This routine returns 1 if we are running in the foreground and stderr
- * is a tty.
- */
-foreground()
-{
-       if(bgnd_flag) { /* background? */
-               return(0);
-       } else {                        /* foreground */
-               if(isatty(2)) {         /* and stderr is a tty */
-                       return(1);
-               } else {
-                       return(0);
-               }
-       }
-}
 
 onintr ( )
 {
 
 onintr ( )
 {
-    if (!zcat_flg)
+    if (!precious)
        unlink ( ofname );
     exit ( 1 );
 }
 
 oops ( )       /* wild pointer -- assume bad input */
 {
        unlink ( ofname );
     exit ( 1 );
 }
 
 oops ( )       /* wild pointer -- assume bad input */
 {
-    if ( do_decomp == 1 
+    if ( do_decomp ) 
        fprintf ( stderr, "uncompress: corrupt input\n" );
     unlink ( ofname );
     exit ( 1 );
        fprintf ( stderr, "uncompress: corrupt input\n" );
     unlink ( ofname );
     exit ( 1 );
@@ -1464,7 +1507,7 @@ long int num, den;
 
 version()
 {
 
 version()
 {
-       fprintf(stderr, "%s\n", rcs_ident);
+       fprintf(stderr, "%s, Berkeley 5.10 %G%\n", rcs_ident);
        fprintf(stderr, "Options: ");
 #ifdef vax
        fprintf(stderr, "vax, ");
        fprintf(stderr, "Options: ");
 #ifdef vax
        fprintf(stderr, "vax, ");