home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / compresp.tch < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  7.8 KB

  1. From decwrl!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery Fri Mar 24 22:25:54 PST 1989
  2. Article 833 of comp.sources.misc:
  3. Path: decwrl!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i066: compress 4.0 bug fixes
  7. Keywords: compress, zcat, bug-fix
  8. Message-ID: <51317@uunet.UU.NET>
  9. Date: 21 Mar 89 01:43:58 GMT
  10. Sender: allbery@uunet.UU.NET
  11. Reply-To: res@cbnews.ATT.COM (Robert E. Stampfli)
  12. Distribution: na
  13. Organization: AT&T Bell Laboratories
  14. Lines: 258
  15. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  16.  
  17. Posting-number: Volume 6, Issue 66
  18. Submitted-by: res@cbnews.ATT.COM (Robert E. Stampfli)
  19. Archive-name: compress-patch
  20.  
  21. [These are NOT official patches.  Make sure you save your original compress 4.0
  22. sources, so you have them if/when official patches come out.
  23.  
  24. In the meantime, you're crazy if you don't apply them (or, alternatively, only
  25. use compress/uncompress as filters as I do).  I've been bit by these a few times
  26. myself.  ++bsa]
  27.  
  28. I use compress 4.0 (and its variants uncompress and zcat) daily and have
  29. come to find them invaluable.  One pesky problem, though, is that they
  30. occasionally go overboard while trying to cleanup after themselves and
  31. remove valuable files.  In an effort to break them of these nasty
  32. habits, I sat down the other night and studied the code.  I think I have
  33. isolated and resolved several problems:
  34.  
  35. 1. I had just finished porting a file, gcc.33-34diffZ, or some such 14
  36.    character gobbledegook, from osu-cis and typed "zcat gcc.33-34diffZ".
  37.    Not realizing it did not end in ".Z", everything appeared to be
  38.    uncompressing fine.  It was a large file, so I broke out of it, only
  39.    to discover to my horror that the file was no longer among the living:
  40.    zcat had dutifully removed it, thinking it was the object of the aborted
  41.    uncompress.
  42.  
  43. 2. Recently, I typed "compress xxx", to which the program responded with the
  44.    fact that a file xxx.Z already existed and asked if I wanted to overwrite
  45.    it.  Thankful for this degree of error checking, I hit DELETE to abort
  46.    the compress.  In this case, DELETE was very aptly named, much to my
  47.    chagrin.
  48.  
  49. 3. I have found that there are small windows of vulnerability when
  50.    (de)compressing a number of files, where an ill-timed DELETE will
  51.    remove all references to a file which has just finished being
  52.    successfully (de)compressed.
  53.  
  54. Actually, the changes are fairly minimal.  I inserted a few sanity checks
  55. and created a variable that indicates to the various interrupt handlers
  56. when it is proper to remove a object, and when it is not.  The diffs against
  57. the official version 4.0 compress.c file are included below.  Of course,
  58. no warrantee, expressed or implied.  Happy compressing.
  59.  
  60. Rob Stampfli
  61. att!cblpe!res (work)
  62. osu-cis!n8emr!kd8wk!rees (home)
  63.  
  64. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  65. The original 4.0 compress.c, upon which this diff is based, sums to 47475 78
  66. (2nd number may vary according to your blocksize) and wc stats it as
  67. 1486 6313 39614.  It is 19141 bytes long, compressed of course.
  68. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  69.  
  70. *** ocompress.c    Tue Mar  7 00:31:20 1989
  71. --- compress.c    Tue Mar  7 11:27:37 1989
  72. ***************
  73. *** 356,361
  74.   #define    CLEAR    256    /* table clear output code */
  75.   
  76.   int force = 0;
  77.   char ofname [100];
  78.   #ifdef DEBUG
  79.   int verbose = 0;
  80.  
  81. --- 356,362 -----
  82.   #define    CLEAR    256    /* table clear output code */
  83.   
  84.   int force = 0;
  85. + int valid = 0;        /* set when signal can remove ofname -- added by res */
  86.   char ofname [100];
  87.   #ifdef DEBUG
  88.   int verbose = 0;
  89. ***************
  90. *** 414,420
  91.   
  92.       if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  93.       signal ( SIGINT, onintr );
  94. -     signal ( SIGSEGV, oops );
  95.       }
  96.   
  97.   #ifdef COMPATIBLE
  98.  
  99. --- 415,420 -----
  100.   
  101.       if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  102.       signal ( SIGINT, onintr );
  103.       }
  104.       signal ( SIGSEGV, oops );
  105.   
  106. ***************
  107. *** 416,421
  108.       signal ( SIGINT, onintr );
  109.       signal ( SIGSEGV, oops );
  110.       }
  111.   
  112.   #ifdef COMPATIBLE
  113.       nomagic = 1;    /* Original didn't have a magic number */
  114.  
  115. --- 416,422 -----
  116.       if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  117.       signal ( SIGINT, onintr );
  118.       }
  119. +     signal ( SIGSEGV, oops );
  120.   
  121.   #ifdef COMPATIBLE
  122.       nomagic = 1;    /* Original didn't have a magic number */
  123. ***************
  124. *** 529,534
  125.           /* Check for .Z suffix */
  126.           if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
  127.               /* No .Z: tack one on */
  128.               strcpy(tempname, *fileptr);
  129.               strcat(tempname, ".Z");
  130.               *fileptr = tempname;
  131.  
  132. --- 530,543 -----
  133.           /* Check for .Z suffix */
  134.           if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
  135.               /* No .Z: tack one on */
  136. + #ifndef BSD4_2        /* Short filenames */
  137. +             if ((cp=rindex(*fileptr,'/')) != NULL)    cp++;
  138. +             else                    cp = *fileptr;
  139. +             if (strlen(cp) > 12) {
  140. +             fprintf(stderr,"%s.Z: No such file or directory\n",cp);
  141. +             continue;
  142. +             }
  143. + #endif  /* BSD4_2    Long filenames allowed */
  144.               strcpy(tempname, *fileptr);
  145.               strcat(tempname, ".Z");
  146.               *fileptr = tempname;
  147. ***************
  148. *** 607,614
  149.               response[0] = 'n';
  150.               fprintf(stderr, "%s already exists;", ofname);
  151.               if (foreground()) {
  152. !             fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
  153. !             ofname);
  154.               fflush(stderr);
  155.               read(2, response, 2);
  156.               while (response[1] != '\n') {
  157.  
  158. --- 616,622 -----
  159.               response[0] = 'n';
  160.               fprintf(stderr, "%s already exists;", ofname);
  161.               if (foreground()) {
  162. !             fprintf(stderr," OK to overwrite (y or n)? ");
  163.               fflush(stderr);
  164.               read(2, response, 2);
  165.               while (response[1] != '\n') {
  166. ***************
  167. *** 624,629
  168.           }
  169.           }
  170.           if(zcat_flg == 0) {        /* Open output file */
  171.           if (freopen(ofname, "w", stdout) == NULL) {
  172.               perror(ofname);
  173.               continue;
  174.  
  175. --- 632,638 -----
  176.           }
  177.           }
  178.           if(zcat_flg == 0) {        /* Open output file */
  179. +         valid = 1;    /* added by res */
  180.           if (freopen(ofname, "w", stdout) == NULL) {
  181.               perror(ofname);
  182.               continue;
  183. ***************
  184. *** 633,639
  185.           }
  186.   
  187.           /* Actually do the compression/decompression */
  188. !         if (do_decomp == 0)    compress();
  189.   #ifndef DEBUG
  190.           else            decompress();
  191.   #else
  192.  
  193. --- 642,648 -----
  194.           }
  195.   
  196.           /* Actually do the compression/decompression */
  197. !         if (do_decomp == 0)        compress();
  198.   #ifndef DEBUG
  199.           else            decompress();
  200.   #else
  201. ***************
  202. *** 1264,1270
  203.   writeerr()
  204.   {
  205.       perror ( ofname );
  206. !     unlink ( ofname );
  207.       exit ( 1 );
  208.   }
  209.   
  210.  
  211. --- 1273,1280 -----
  212.   writeerr()
  213.   {
  214.       perror ( ofname );
  215. !     if( valid )
  216. !     unlink ( ofname );
  217.       exit ( 1 );
  218.   }
  219.   
  220. ***************
  221. *** 1303,1308
  222.       timep[0] = statbuf.st_atime;
  223.       timep[1] = statbuf.st_mtime;
  224.       utime(ofname, timep);    /* Update last accessed and modified times */
  225.       if (unlink(ifname))    /* Remove input file */
  226.           perror(ifname);
  227.       if(!quiet)
  228.  
  229. --- 1313,1319 -----
  230.       timep[0] = statbuf.st_atime;
  231.       timep[1] = statbuf.st_mtime;
  232.       utime(ofname, timep);    /* Update last accessed and modified times */
  233. +     valid = 0;    /* added by res -- prevents latent ofname removal */
  234.       if (unlink(ifname))    /* Remove input file */
  235.           perror(ifname);
  236.       if(!quiet)
  237. ***************
  238. *** 1333,1339
  239.   
  240.   onintr ( )
  241.   {
  242. !     unlink ( ofname );
  243.       exit ( 1 );
  244.   }
  245.   
  246.  
  247. --- 1344,1351 -----
  248.   
  249.   onintr ( )
  250.   {
  251. !     if( valid )
  252. !     unlink ( ofname );
  253.       exit ( 1 );
  254.   }
  255.   
  256. ***************
  257. *** 1341,1347
  258.   {
  259.       if ( do_decomp == 1 ) 
  260.           fprintf ( stderr, "uncompress: corrupt input\n" );
  261. !     unlink ( ofname );
  262.       exit ( 1 );
  263.   }
  264.   
  265.  
  266. --- 1353,1360 -----
  267.   {
  268.       if ( do_decomp == 1 ) 
  269.           fprintf ( stderr, "uncompress: corrupt input\n" );
  270. !     if( valid )
  271. !     unlink ( ofname );
  272.       exit ( 1 );
  273.   }
  274.   
  275.  
  276.  
  277.