home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / compress-patch < prev    next >
Text File  |  1989-03-20  |  8KB  |  268 lines

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