home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / tools / FileUtils / tidir.c < prev   
Encoding:
C/C++ Source or Header  |  2006-10-19  |  4.2 KB  |  255 lines

  1. #include <ctype.h>
  2. #include <memory.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #include <unistd.h>
  8. #include "fiad.h"
  9.  
  10. int generateoldv9t9filenames = 0,
  11.     fixupoldv9t9filenames = 0,
  12.     unknownfileistext = 0,
  13.     repairbadfiles = 0,
  14.     keepfileformat = 1,
  15.     newfileformat = F_TIFILES;
  16.  
  17. enum
  18. {
  19.     TOLIST_WIDE,
  20.     TOLIST_NORMAL,
  21.     TOLIST_LONG
  22. };
  23. int    tolist = TOLIST_NORMAL;
  24. int    sort_by = FIAD_CATALOG_SORT_BY_DISK;
  25. bool sort_ascending = true;
  26. fiad_catalog cat;
  27.  
  28. static int    
  29. get_catalog(char *name)
  30. {
  31.     OSError err;
  32.  
  33.     err = fiad_catalog_read_catalog(&cat, name);
  34.     if (err != OS_NOERR)
  35.     {
  36.         fprintf(stderr, "Could not read catalog for '%s' (%s)\n",
  37.                 name, OS_GetErrText(err));
  38.         return 0;
  39.     }
  40.     else
  41.         return 1;
  42. }
  43.  
  44. static void    
  45. display_header(void)
  46. {
  47.     int    x;
  48.  
  49.     switch (tolist)
  50.     {
  51.     case TOLIST_WIDE:    break;
  52.     case TOLIST_NORMAL:
  53. //        for (x=0; x<2; x++)
  54.         printf("%-10s  %-4s  %-11s  \t",
  55.                "Filename","Secs", "Type");
  56.         break;
  57.  
  58.     case TOLIST_LONG:
  59.         printf("%-10s  %-4s  %-11s  %-8s  %-5s  %s",
  60.                 "Filename","Secs","Type",
  61.                "Bytesize","#recs", "Host name");
  62.         break;
  63.     }
  64.     printf("\n");
  65. }
  66.  
  67. static void    
  68. display_entry(char *filename, fdrrec *fdr, int index)
  69. {
  70.     int    on_line;
  71.     int sec;
  72.  
  73.     switch (tolist)
  74.     {
  75.     case TOLIST_WIDE:
  76.         printf("%.10s ",fdr->filenam);
  77.         on_line = 7;
  78.         break;
  79.         
  80.     case TOLIST_NORMAL:
  81.         printf("%.10s  %4d  %-11s  ",
  82.                fdr->filenam,
  83.                fdr->secsused,
  84.                fiad_catalog_get_file_type_string(fdr));
  85.         on_line = 2;
  86.         break;
  87.  
  88.     case TOLIST_LONG:
  89.         printf("%.10s  %4d  %-11s  ",
  90.                fdr->filenam,
  91.                fdr->secsused,
  92.                fiad_catalog_get_file_type_string(fdr));
  93.  
  94.         printf("%8ld  ",FDR_FILESIZE(fdr));
  95.         if (!(fdr->flags&(ff_variable|ff_program)))
  96.             printf("%5d  ",fdr->numrecs);
  97.         else
  98.             printf("%5s  ","");
  99.  
  100.         printf("%s", filename);
  101.  
  102. /*
  103.         sec = cat->fdrsec[cat->index[index]];
  104.         if (sec)
  105.             printf("%5d",sec);
  106.         else
  107.             printf("%5s","");
  108. */
  109.         on_line = 1;
  110.         break;
  111.     }
  112.  
  113. //    if ((index + 1) % on_line == 0)
  114.         printf("\n");
  115. //    else 
  116. //        printf("\t");
  117. }
  118.  
  119. static int    
  120. display_catalog(void)
  121. {
  122.     int    index;
  123.     int    lines;
  124.  
  125. //    First, sort the list.
  126.  
  127.     fiad_catalog_sort_catalog(&cat, sort_by, sort_ascending);
  128.  
  129.     index = 0;
  130.     while (index < cat.entries)
  131.     {
  132.         display_entry(cat.filenames[cat.index[index]],
  133.                       &cat.fdrs[cat.index[index]], 
  134.                       index);
  135.         index++;
  136.     }
  137.     printf("\n\n");
  138.  
  139.     return 1;
  140. }
  141.  
  142. static int    
  143. free_catalog(void)
  144. {
  145.     fiad_catalog_free_catalog(&cat);
  146.     return 1;
  147. }
  148.  
  149. static void    
  150. help(void)
  151. {
  152.     printf("\n"
  153.            "TIDIR V9t9 Directory Lister v2.0\n"
  154.            "\n"
  155.            "Usage:   TIDIR [options] { <directory> }\n"
  156. //           "Usage:   TIDIR [options] { <file> | <directory> | <disk image:> }\n"
  157.            "\n"
  158.            "TIDIR will print the listing of files in emulated directories.\n"
  159. //           "or disk images.\n"
  160.            "\n"
  161.            "Options:\n"
  162.            "\t\t/L\t-- long format\n"
  163.            "\t\t/W\t-- wide format\n"
  164.            "\t\t/O[-]x\t-- sort by Disk order, Name (default), Size, or Type.\n"
  165.            "\t\t\t   \"-\" means descending.\n"
  166.            "\n"
  167.         );
  168. }
  169.  
  170. static    char    *legalsorts="DNST";
  171.  
  172. int    main(int argc, char **argv)
  173. {
  174.     char    *whichsort;
  175.     int        sortchar;
  176.     int        opt;
  177.     bool    printed_header;
  178.     int        bad;
  179.  
  180.     if (argc <= 1)
  181.     {
  182.         help();
  183.         return 0;
  184.     }
  185.  
  186.     tolist = TOLIST_NORMAL;
  187.     sort_by = FIAD_CATALOG_SORT_BY_NAME;
  188.     sort_ascending = true;
  189.  
  190.     while ((opt = getopt(argc, argv, "?HhWwLlO:o:")) != -1)
  191.     {
  192.         switch (opt)
  193.         {
  194.         case '?':
  195.         case 'H':
  196.         case 'h':    
  197.             help();
  198.             break;
  199.         case 'W':
  200.         case 'w':
  201.             tolist = TOLIST_WIDE;
  202.             break;
  203.         case 'L':    
  204.         case 'l':
  205.             tolist = TOLIST_LONG;
  206.             break;
  207.         case 'O':
  208.         case 'o':
  209.             if (optarg[0] == '-')
  210.                 sortchar = 1;
  211.             else
  212.                 sortchar = 0;
  213.  
  214.             if (optarg[sortchar + 1])
  215.             {
  216.                 fprintf(stderr, "Too many letters in /O parameter (%s).\n",
  217.                         optarg);
  218.                 return 1;
  219.             }
  220.             whichsort = strchr(legalsorts, toupper(optarg[sortchar]));
  221.             if (whichsort == NULL)
  222.             {
  223.                 fprintf(stderr, "Unknown sort option in /O parameter (%s).\n", 
  224.                        optarg);
  225.                 return 1;
  226.             }
  227.             sort_by = whichsort - legalsorts + FIAD_CATALOG_SORT_BY_DISK;
  228.             sort_ascending = (sortchar != 1);
  229.             break;
  230.         }
  231.     }
  232.  
  233.     printed_header = false;
  234.  
  235.     while (argv[optind])
  236.     {
  237.         if (get_catalog(argv[optind]))
  238.         {
  239.             if (!printed_header)
  240.             {
  241.                 display_header();
  242.                 printed_header = true;
  243.             }
  244.  
  245.             display_catalog();
  246.             free_catalog();
  247.         }
  248.  
  249.         optind++;
  250.     }
  251.  
  252.     return 0;
  253. }
  254.  
  255.