home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / ARCHIVERS / lha208src.lzh / LHA / SRC / lhlist.c < prev    next >
Text File  |  1994-02-14  |  9KB  |  343 lines

  1. /*----------------------------------------------------------------------*/
  2. /*        LHarc List Command                                            */
  3. /*        This is part of LHarc UNIX Archiver Driver                    */
  4. /*                                                                      */
  5. /*        Copyright(C) MCMLXXXIX  Yooichi.Tagawa                        */
  6. /*                                                                      */
  7. /*  V0.00  Original                               1988.05.23  Y.Tagawa  */
  8. /*  V1.00  Fixed                                  1989.09.22  Y.Tagawa  */
  9. /*  V1.01  Bug Fix for month name                 1989.12.25  Y.Tagawa  */
  10. /*----------------------------------------------------------------------*/
  11.  
  12. #include "lharc.h"
  13.  
  14. static long packed_size_total;
  15. static long original_size_total;
  16. static int list_files;
  17.  
  18. /*----------------------------------------------------------------------*/
  19. /*                Print Stuff                                           */
  20. /*----------------------------------------------------------------------*/
  21.  
  22. /* need 14 or 22 (when verbose_listing is TRUE) column spaces */
  23. static void
  24. print_size (packed_size, original_size)
  25. long packed_size, original_size;
  26. {
  27.     if (verbose_listing)
  28.         printf ("%7d ", packed_size);
  29.  
  30.     printf ("%7d ", original_size);
  31.  
  32.     if (original_size == 0L)
  33.         printf ("******");
  34.     else
  35.         printf ("%3d.%1d%%",
  36.     (int)((packed_size * 100L) / original_size),
  37.     (int)((packed_size * 1000L) / original_size) % 10);
  38. }
  39.  
  40. /* need 12 or 17 (when verbose_listing is TRUE) column spaces */
  41. static void
  42. print_stamp (t)
  43. time_t t;
  44. {
  45.     static boolean got_now = FALSE;
  46.     static time_t now;
  47.     static unsigned int threshold;
  48.     static char t_month[12*3+1] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  49.     struct tm *p;
  50.  
  51.     if (t == 0)
  52.     {
  53.         printf ("            "); /* 12 spaces */
  54.         return;
  55.     }
  56.  
  57.     if (!got_now)
  58.     {
  59.         now = time ((time_t*)0);
  60.         p = localtime (&now);
  61.         threshold = p->tm_year * 12 + p->tm_mon - 6;
  62.         got_now = TRUE;
  63.     }
  64.  
  65.     p = localtime (&t);
  66.  
  67.     if (p->tm_year * 12 + p->tm_mon > threshold)
  68.         printf ("%.3s %2d %02d:%02d",
  69.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_hour, p->tm_min);
  70.     else
  71.         printf ("%.3s %2d  %04d",
  72.     &t_month[p->tm_mon * 3], p->tm_mday, p->tm_year + 1900);
  73. }
  74.  
  75. static void
  76. print_bar ()
  77. {
  78.     char *p, *q;
  79.     /* 17+1+(0 or 7+1)+7+1+6+1+(0 or 1+4)+(12 or 17)+1+20 */
  80.     /*       12345678901234567_  1234567_123456  _123456789012   1234 */
  81.  
  82.     if (verbose_listing)
  83.     {
  84.         p = "- ------ ---------- ";
  85.         q = " -------------";
  86.     }
  87.     else
  88.     {
  89.         p = " ";
  90.         q = " --------------------";
  91.     }
  92.  
  93.     if (verbose)
  94.         q = "";
  95.  
  96.     printf ("----------------- ------- ------%s------------%s\n", p, q);
  97. }
  98.  
  99.  
  100. /*----------------------------------------------------------------------*/
  101. /*                                    */
  102. /*----------------------------------------------------------------------*/
  103.  
  104. static void
  105. list_header ()
  106. {
  107.     char *p, *q;
  108.  
  109.     if (verbose_listing)
  110.     {
  111.         p = "PACKED    SIZE  RATIO        CRC   ";
  112.         q = "       NAME";
  113.     }
  114.     else
  115.     {
  116.         p = "  SIZE  RATIO";
  117.         q = "    NAME";
  118.     }
  119.  
  120.     if (verbose)
  121.         q = "";
  122.  
  123.     printf (" PERMSSN  UID GID  %s     STAMP%s\n", p, q);
  124. #if 0
  125.     printf (" PERMSSN  UID GID %s   SIZE  RATIO%s %s    STAMP%s%s\n",
  126.     verbose_listing ? " PACKED " : "",    /* 8,0 */
  127.     verbose_listing ? "  CRC" : "",    /* 5,0 */
  128.     verbose_listing ? "  " : "",        /* 2,0 */
  129.     verbose_listing ? "      " : "   ",        /* 6,3 */
  130.     verbose ? "" : " NAME");
  131. #endif
  132.     print_bar ();
  133. }
  134.  
  135. static void
  136. list_one (hdr)
  137. register LzHeader *hdr;
  138. {
  139.     register int mode;
  140.     register char *p;
  141.     char method[6];
  142.  
  143.     if (verbose)
  144.         printf ("%s\n", hdr->name);
  145.  
  146.     strncpy(method,hdr->method,5);
  147.     method[5]='\0';
  148.  
  149.     switch ( mode=hdr->extend_type )
  150.     {
  151. #ifndef OSK
  152.     case EXTEND_UNIX:
  153.         mode=hdr->unix_mode;
  154.         printf ("%c%c%c%c%c%c%c%c%c%4d/%-4d",
  155.         ((mode & UNIX_OWNER_READ_PERM)  ? 'r' : '-'),
  156.         ((mode & UNIX_OWNER_WRITE_PERM) ? 'w' : '-'),
  157.         ((mode & UNIX_OWNER_EXEC_PERM)  ? 'x' : '-'),
  158.         ((mode & UNIX_GROUP_READ_PERM)  ? 'r' : '-'),
  159.         ((mode & UNIX_GROUP_WRITE_PERM) ? 'w' : '-'),
  160.         ((mode & UNIX_GROUP_EXEC_PERM)  ? 'x' : '-'),
  161.         ((mode & UNIX_OTHER_READ_PERM)  ? 'r' : '-'),
  162.         ((mode & UNIX_OTHER_WRITE_PERM) ? 'w' : '-'),
  163.         ((mode & UNIX_OTHER_EXEC_PERM)  ? 'x' : '-'),
  164.         hdr->unix_uid, hdr->unix_gid);
  165.         break;
  166. #else
  167.     case EXTEND_OS68K:
  168. /**/    case EXTEND_XOSK: /**/
  169.         mode=hdr->unix_mode;
  170.         printf ("%c%c%c%c%c%c%c%c %4d/%-4d",
  171.         ((mode & OSK_DIRECTORY_PERM)    ?    'd' : '-'),
  172.         ((mode & OSK_SHARED_PERM)       ?    's' : '-'),
  173.         ((mode & OSK_OTHER_EXEC_PERM)   ?    'e' : '-'),
  174.         ((mode & OSK_OTHER_WRITE_PERM)  ?    'w' : '-'),
  175.         ((mode & OSK_OTHER_READ_PERM)   ?    'r' : '-'),
  176.         ((mode & OSK_OWNER_EXEC_PERM)   ?    'e' : '-'),
  177.         ((mode & OSK_OWNER_WRITE_PERM)  ?    'w' : '-'),
  178.         ((mode & OSK_OWNER_READ_PERM)   ?    'r' : '-'),
  179.         hdr->unix_uid, hdr->unix_gid);
  180.         break;
  181. #endif /* OSK */
  182.     default:
  183.         switch (hdr->extend_type)
  184.         {            /* max 18 characters */
  185.         case EXTEND_GENERIC:
  186.             p = "[generic]"; 
  187.             break;
  188.         case EXTEND_UNIX:
  189.             p = "[UNIX]"; 
  190.             break;
  191.         case EXTEND_CPM:    
  192.             p = "[CP/M]"; 
  193.             break;
  194.         case EXTEND_FLEX:    
  195.             p = "[FLEX]"; 
  196.             break;
  197.         case EXTEND_OS9:    
  198.             p = "[OS-9]"; 
  199.             break;
  200.         case EXTEND_OS68K:    
  201.             p = "[OS-9/68K]"; 
  202.             break;
  203.         case EXTEND_MSDOS:    
  204.             p = "[MS-DOS]"; 
  205.             break;
  206.         case EXTEND_MACOS:    
  207.             p = "[Mac OS]"; 
  208.             break;
  209.         case EXTEND_OS2:    
  210.             p = "[OS/2]"; 
  211.             break;
  212.         case EXTEND_HUMAN:    
  213.             p = "[Human68K]"; 
  214.             break;
  215.         case EXTEND_OS386:    
  216.             p = "[OS-386]"; 
  217.             break;
  218.         case EXTEND_RUNSER:    
  219.             p = "[Runser]"; 
  220.             break;
  221. #ifdef EXTEND_TOWNSOS
  222.             /* This ID isn't fixed */
  223.         case EXTEND_TOWNSOS:    
  224.             p = "[TownsOS]"; 
  225.             break;
  226. #endif
  227.             /* Ouch!  Please customize it's ID.  */
  228.         default:         
  229.             p = "[unknown]"; 
  230.             break;
  231.         }
  232.         printf ("%-18.18s", p);
  233.         break;
  234.     }
  235.  
  236.     print_size (hdr->packed_size, hdr->original_size);
  237.  
  238.     if (verbose_listing)
  239.         if (hdr->has_crc)
  240.             printf (" %s %04x", method,hdr->crc);
  241.     else
  242.         printf (" %s ****",method);
  243.  
  244.     printf (" ");
  245.     print_stamp (hdr->unix_last_modified_stamp);
  246.  
  247.     if (!verbose)
  248.         printf (" %s", hdr->name);
  249.  
  250.     printf ("\n");
  251. }
  252.  
  253. static void
  254. list_tailer ()
  255. {
  256.     STAT stbuf;
  257.     time_t arct;
  258.  
  259.     print_bar ();
  260.  
  261.     printf (" Total %4d file%c ",
  262.     list_files, (list_files == 1) ? ' ' : 's');
  263.     print_size (packed_size_total, original_size_total);
  264.     printf (" ");
  265.  
  266.     if (verbose_listing)
  267.         printf ("           ");
  268.  
  269.     if (stat (archive_name, &stbuf) < 0)
  270.         print_stamp ((time_t)0);
  271.     else
  272. #ifdef OSK
  273.     {
  274.         arct = lm_to_time_t(stbuf.st_mtime);
  275.         print_stamp (arct);
  276.     }
  277. #else
  278.         print_stamp (stbuf.st_mtime);
  279. #endif
  280.  
  281.     printf ("\n");
  282. }
  283.  
  284. /*----------------------------------------------------------------------*/
  285. /*        LIST COMMAND MAIN                                             */
  286. /*----------------------------------------------------------------------*/
  287.  
  288. void
  289. cmd_list ()
  290. {
  291.     FILE *afp;
  292.     LzHeader hdr;
  293.     int i;
  294.  
  295.     /* initialize total count */
  296.     packed_size_total = 0L;
  297.     original_size_total = 0L;
  298.     list_files = 0;
  299.  
  300.     /* open archive file */
  301.     if ((afp = open_old_archive ()) == NULL)
  302.         fatal_error (archive_name);
  303.     if (archive_is_msdos_sfx1 (archive_name))
  304.         skip_msdos_sfx1_code (afp);
  305.  
  306.     /* print header message */
  307.     if (!quiet)
  308.         list_header ();
  309.  
  310.     /* print each file information */
  311.     while (get_header (afp, &hdr))
  312.     {
  313.         if (need_file (hdr.name))
  314.         {
  315.             list_one (&hdr);
  316.             list_files ++;
  317.             packed_size_total += hdr.packed_size;
  318.             original_size_total += hdr.original_size;
  319.         }
  320.  
  321.         if (afp != stdin)
  322.             fseek (afp, hdr.packed_size, SEEK_CUR);
  323.         else
  324.             {
  325.             i = hdr.packed_size;
  326.             while(i--) fgetc(afp);
  327.         }
  328.     }
  329.  
  330.     /* close archive file */
  331.     fclose (afp);
  332.  
  333.     /* print tailer message */
  334.     if (!quiet)
  335.         list_tailer ();
  336.  
  337.     return;
  338. }
  339.  
  340.  
  341.  
  342.  
  343.