home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / shark.lzh / compusi.uni < prev    next >
Text File  |  1990-11-12  |  7KB  |  255 lines

  1. /*@H************************ < COMPRESS utility> ****************************
  2. *   $@(#) compusi.uni,v 4.3d 90/01/18 03:00:00 don Release ^                *
  3. *                                                                           *
  4. *   compress : compusi.uni <Unix/Xenix support functions>                   *
  5. *                                                                           *
  6. *   port by  : Donald J. Gloistein                                          *
  7. *                                                                           *
  8. *   Source, Documentation, Object Code:                                     *
  9. *   released to Public Domain. This code is ported from compress v4.0       *
  10. *   release joe.                                                            *
  11. *                                                                           *
  12. *---------------------------  Module Description  --------------------------*
  13. *   Unix system dependent routines. These are specific to either the        *
  14. *   unix file structure or some unix only functions.                        *
  15. *                                                                           *
  16. *   Separated out for ease of writing the main module.                      *
  17. *                                                                           *
  18. *--------------------------- Implementation Notes --------------------------*
  19. *                                                                           *
  20. *   compiled with : compress.h compress.fns                                 *
  21. *   linked with   : compress.o compapi.o                                    *
  22. *                                                                           *
  23. *   To use, copy or rename this file to compusi.c and recompile.            *
  24. *   Set the defines in compress.h to reflect the status of your compiler's  *
  25. *   runtime library, allocation type for malloc()'s, and memory models if   *
  26. *   applicable, and your library function call for malloc() and free().     *
  27. *                                                                           *
  28. *   problems: header has some hardcoded defines you may need to change      *
  29. *             for your compiler. Please read the header thoroughly.         *
  30. *                                                                           *
  31. *---------------------------      Author(s)        -------------------------*
  32. *     Initials ---- Name ---------------------------------                  *
  33. *      DjG          Donald J. Gloistein                                     *
  34. *                   Plus many others, see rev.hst file for full list        *
  35. *      LvR          Lyle V. Rains, thanks for the improved implementation   *
  36. *                   of the compression and decompression routines.          *
  37. *************************************************************************@H*/
  38.  
  39. #include <stdio.h>
  40.  
  41. #include "compress.h" /* contains the rest of the include file declarations */
  42.  
  43. /* For those who don't have it in libc.a */
  44.  
  45. #ifdef NO_STRRCHR
  46. char  *strrchr(s,c)
  47. char *s;
  48. int c;
  49. {
  50.     register int count;
  51.  
  52.     while (*s){
  53.         s++;
  54.         count++;
  55.     }
  56.     s--;
  57.     while (count--)
  58.         if (*s == (char)c)
  59.             return(s);
  60.         else
  61.             s--;
  62.     return(NULL);
  63. }
  64. #endif
  65.  
  66. char *get_program_name(ptr)
  67. char *ptr;
  68. {
  69.     char *cp;
  70.     if ((cp = strrchr(ptr, '/')) != NULL)
  71.         cp++;
  72.     else
  73.         cp = ptr;
  74.  
  75.     if(strcmp(cp,"uncompress") == 0) {
  76.         do_decomp = 1;
  77.     }
  78.     else
  79.         if(strcmp(cp, "zcat") == 0) {
  80.             keep = TRUE;
  81.             zcat_flg = do_decomp = 1;
  82.         }
  83.     return (cp);
  84. }
  85.  
  86.  
  87. char *name_index(ptr)
  88. char *ptr;
  89. {
  90.     char *p;
  91.  
  92.     p = strrchr(ptr,'/');
  93.     return ((p)? ++p: ptr);
  94. }
  95.  
  96. int is_z_name(ptr)       /* checks if it is already a z name */
  97. char *ptr;
  98. {
  99.     return (!(strcmp(ptr + strlen(ptr) -2,".Z")));
  100. }
  101.  
  102. int make_z_name(ptr)
  103. char *ptr;
  104. {
  105. #ifndef BSD4_2
  106.     if (strlen(name_index(ptr)) > 12 ) {
  107.         fprintf(stderr,"%s: filename too long to add .Z\n",name_index(ptr));
  108.         return FALSE;
  109.     }
  110. #endif
  111.     strcat(ptr,".Z");
  112.     return TRUE;
  113. }
  114. void unmake_z_name(ptr)
  115. char *ptr;
  116. {
  117.     register int len = strlen(ptr)-2;
  118.  
  119.     ptr[len] = '\0';
  120. }
  121.  
  122. #ifndef NOSIGNAL
  123. SIGTYPE onintr ( )
  124. {
  125.     if (!zcat_flg && !keep_error){
  126.         fclose(stdout);
  127.         unlink ( ofname );
  128.     }
  129.     exit ( ERROR );
  130. }
  131.  
  132. SIGTYPE oops ( )    /* wild pointer -- assume bad input */
  133. {
  134.     if ( do_decomp == 1 )
  135.         fprintf ( stderr, "%s: corrupt input: %s\n",prog_name,ifname);
  136.     if (!zcat_flg && !keep_error){
  137.         fclose(stdout);
  138.         unlink ( ofname );
  139.     }
  140.     exit ( ERROR );
  141. }
  142. #endif
  143.  
  144. int test_file(ifname)  /* test for a good file name and no links */
  145. char *ifname;          /* returns ERROR for bad file */
  146. {
  147.     struct stat statbuf;
  148.  
  149.     if (stat(ifname, &statbuf)) {       /* Get stat on input file */
  150.         perror(ifname);
  151.         return(ERROR);
  152.     }else if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
  153.         fprintf(stderr, "%s -- not a regular file: unchanged",ifname);
  154.         return(ERROR);
  155.     } else if (statbuf.st_nlink > 1) {
  156.         if(quiet)
  157.         fprintf(stderr, "%s -- has %d other links: unchanged",ifname,
  158.             statbuf.st_nlink - 1);
  159.         return(ERROR);
  160.     }
  161.    return (0);
  162. }
  163.  
  164. void copystat(ifname, ofname)
  165. char *ifname, *ofname;
  166. {
  167.     struct stat statbuf;
  168.     int mode;
  169.     time_t timep[2];
  170.  
  171.     fclose(stdout);
  172.     if (stat(ifname, &statbuf)) {       /* Get stat on input file */
  173.         perror(ifname);
  174.         return;
  175.     }
  176.     if (exit_stat == NOSAVING && (!force)) { /* No compression: remove file.Z */
  177.         if(!quiet)
  178.             fprintf(stderr, " -- no savings -- file unchanged");
  179.     }
  180.     else if (exit_stat == NOMEM){
  181.         if (!quiet)
  182.             fprintf(stderr, " -- file unchanged");
  183.         if (!do_decomp)
  184.             exit(ERROR);
  185.         else
  186.             return;     /* otherwise will unlink outfile */
  187.     }
  188.     else if (exit_stat == OK) {  /* ***** Successful Compression ***** */
  189.         mode = statbuf.st_mode & 07777;
  190.         if (chmod(ofname, mode))        /* Copy modes */
  191.             perror(ofname);
  192.         chown(ofname,statbuf.st_uid,statbuf.st_gid); /* Copy Ownership */
  193.         timep[0] = statbuf.st_atime;
  194.         timep[1] = statbuf.st_mtime;
  195.         utime(ofname,timep);   /* Update last accessed and modified times */
  196.         if (!keep){
  197.             fclose(stdin);
  198.             if (unlink(ifname)) /* Remove input file */
  199.                 perror(ifname);
  200.             if(!quiet)
  201.                 fprintf(stderr, " -- replaced with %s", ofname);
  202.         }
  203.         else{
  204.             if(!quiet)
  205.                 fprintf(stderr, " -- compressed to %s", ofname);
  206.         }
  207.         return;     /* Successful return */
  208.     }
  209.  
  210.     /* Unsuccessful return -- one of the tests failed */
  211.     fclose(stdout);
  212.     if (unlink(ofname))
  213.         perror(ofname);
  214. }
  215.  
  216. #ifndef COMPVER
  217. #ifdef XENIX
  218. #define COMPVER "Xenix"
  219. #else
  220. #define COMPVER "unix"
  221. #endif
  222. #endif
  223.  
  224. #ifdef COMP40
  225. #define RESET "Adaptive Reset"
  226. #else
  227. #define RESET "No Adaptive Reset"
  228. #endif
  229.  
  230. void version()
  231. {
  232. #ifdef DEBUG
  233.     fprintf(stderr, "%s\nOptions: %s %s DEBUG MAXBITS = %d\n",rcs_ident,COMPVER,
  234.         RESET,MAXBITS);
  235. #else
  236.     fprintf(stderr, "%s\nOptions: %s %s MAXBITS = %d\n",rcs_ident,COMPVER,
  237.     RESET,MAXBITS);
  238. #endif
  239. }
  240.  
  241. ALLOCTYPE FAR *emalloc(x,y)
  242. unsigned int x;
  243. int y;
  244. {
  245.     ALLOCTYPE FAR *p;
  246.     p = (ALLOCTYPE FAR *)ALLOCATE(x,y);
  247.     return(p);
  248. }
  249.  
  250. void efree(ptr)
  251. ALLOCTYPE FAR *ptr;
  252. {
  253.     FREEIT(ptr);
  254. }
  255.