home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / useful / dist / disk / cdrom / amicdrom / main.c < prev    next >
C/C++ Source or Header  |  1994-02-20  |  30KB  |  1,023 lines

  1. /* main.c:
  2.  *
  3.  * Interactive test program for the ISO- and Rock-Ridge-support
  4.  * routines.
  5.  *
  6.  * ----------------------------------------------------------------------
  7.  * This code is (C) Copyright 1993,1994 by Frank Munkert.
  8.  * All rights reserved.
  9.  * This software may be freely distributed and redistributed for
  10.  * non-commercial purposes, provided this notice is included.
  11.  * ----------------------------------------------------------------------
  12.  * History:
  13.  * 
  14.  * 17-Feb-94   fmu   Fixed typo.
  15.  * 28-Nov-93   fmu   Improved "cdrom d" command.
  16.  * 12-Oct-93   fmu   "Show path table" function removed.
  17.  * 09-Oct-93   fmu   Open utility.library.
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. #include <dos/var.h>
  25. #include <clib/dos_protos.h>
  26. #include <clib/exec_protos.h>
  27.  
  28. #include "cdrom.h"
  29. #include "iso9660.h"
  30. #include "rock.h"
  31. #include "hfs.h"
  32.  
  33. #ifdef AZTEC_C
  34. #include <pragmas/dos_lib.h>
  35. #include <pragmas/exec_lib.h>
  36. #endif
  37.  
  38. #ifdef LATTICE
  39. #include <pragmas/dos_pragmas.h>
  40. #include <pragmas/exec_pragmas.h>
  41. extern struct Library *SysBase, *DOSBase;
  42. #endif
  43.  
  44. #if defined(_DCC) && defined(REGISTERED)
  45. #include <pragmas/dos_pragmas.h>
  46. #include <pragmas/exec_pragmas.h>
  47. extern struct Library *SysBase, *DOSBase;
  48. #endif
  49.  
  50. #define STD_BUFFERS 10
  51. #define FILE_BUFFERS 5
  52.  
  53. #ifdef DEBUG_SECTORS
  54. void dbprintf (char *p_dummy, ...)
  55. {
  56. }
  57. #endif
  58.  
  59. CDROM *cd = NULL;
  60. char g_the_device[80];
  61. int g_the_unit;
  62. int g_trackdisk = 0;
  63. t_ulong g_memory_type = MEMF_CHIP;
  64.  
  65. struct Library *UtilityBase;
  66.  
  67. void Cleanup (void)
  68. {
  69.   if (cd)
  70.     Cleanup_CDROM (cd);
  71.  
  72.   if (UtilityBase)
  73.     CloseLibrary (UtilityBase);
  74. }
  75.  
  76. void Usage (void)
  77. {
  78.   fprintf (stderr,
  79.     "Usage: cdrom command [parameters]\n"
  80.     "Commands:\n"
  81.     "  a                Show information on CDROM drive\n"
  82.     "  b                Read table of contents\n"
  83.     "  c name           Show contents of file 'name'\n"
  84.     "  d[rl] dir        Show contents of directory 'dir' (use ISO names)\n"
  85.     "                   r=also show subdirectories, l=show additional information\n"
  86.     "  e[r[l|L]] dir    Show contents of directory 'dir' (use Rock Ridge names)\n"
  87.     "                   r=also show subdirectories, l=show system use field names\n"
  88.     "                   L=show names and contents of system use fields\n"
  89.     "  f dir name       Change to directory 'dir' and try to find object 'name'\n"
  90.     "  i                Check which protocol is used\n"
  91.     "  j [01]           0=start audio, 1=stop motor\n"
  92.     "  m num            Read catalog node 'num' (MacHFS only)\n"
  93.     "  o name           Try to open object 'name'\n"
  94.     "  r                Read contents of root directory\n"
  95.     "  s num [cnt]      Read 'cnt' sectors, starting at sector 'num'\n"
  96.     "  t name           Try to open parent of object 'name'\n"
  97.     "  v                Read primary volume descriptor\n"
  98.     "  x dens [length]  Select mode: dens=density code,\n"
  99.     "                   length=block length (default: 2048)\n"
  100.     "  z                Send test unit ready command\n"
  101.     "Use \":\" as the name of the root directory\n"
  102.     );
  103.   exit (1);
  104. }
  105.  
  106. char *MKSTR (char *p_in, int p_length, char *p_out)
  107. {
  108.   char *res = p_out;
  109.   int len = p_length;
  110.   int i;
  111.   
  112.   while (len && p_in[len-1] == ' ')
  113.     len--;
  114.  
  115.   for (i=0; i<len; i++)
  116.     *p_out++ = *p_in++;
  117.     
  118.   *p_out = 0;
  119.   
  120.   return res;
  121. }
  122.  
  123. void Show_Flags (unsigned char p_flags)
  124. {
  125.   if (p_flags & 1)
  126.     printf ("existence ");
  127.   if (p_flags & 2)
  128.     printf ("directory ");
  129.   if (p_flags & 4)
  130.     printf ("associated ");
  131.   if (p_flags & 8)
  132.     printf ("record ");
  133.   if (p_flags & 16)
  134.     printf ("protection ");
  135.   if (p_flags & 128)
  136.     printf ("multi-extent ");
  137. }
  138.  
  139. void Show_Directory_Record (directory_record *p_dir)
  140. {
  141.   char buf[256];
  142.  
  143.   printf ("Extended Attr Record Length: %d\n", (int) p_dir->ext_attr_length);
  144.   printf ("Location of Extent:          %lu\n", p_dir->extent_loc_m);
  145.   printf ("Data Length:                 %lu\n", p_dir->data_length_m);
  146.   printf ("Recording Date and Time:     %02d.%02d.19%02d %02d:%02d:%02d %+d\n",
  147.         (int) p_dir->day, (int) p_dir->month, (int) p_dir->year,
  148.       (int) p_dir->hour, (int) p_dir->minute, (int) p_dir->second,
  149.       (int) p_dir->tz);
  150.   printf ("Flags:                       %d (", (int) p_dir->flags);
  151.   Show_Flags (p_dir->flags);
  152.   printf (")\n");
  153.   printf ("File Unit Size:              %d\n", (int) p_dir->file_unit_size);
  154.   printf ("Gap Size:                    %d\n", (int) p_dir->gap_size);
  155.   printf ("Volume Sequence Number:      %hu\n", p_dir->sequence_m);
  156.   printf ("File Identifier:             ");
  157.   if (p_dir->file_id[0] == 0)
  158.     printf ("(00)\n");
  159.   else if (p_dir->file_id[0] == 1)
  160.     printf ("(01)\n");
  161.   else
  162.     printf ("%s\n", MKSTR (p_dir->file_id, p_dir->file_id_length, buf));
  163. }
  164.  
  165. void Find_Block_Starting_With (CDROM *p_cd, int p_val)
  166. {
  167.   unsigned long sec = 0;
  168.   int cmp;
  169.   int i;
  170.   
  171.   for (;;) {
  172.     if (!Read_Sector (p_cd, sec)) {
  173.       fprintf (stderr, "cannot read sector 16\n");
  174.       exit (1);
  175.     }
  176.     for (i=0; i<4; i++) {
  177.       cmp = p_cd->buffer[i<<9] * 256 + p_cd->buffer[(i<<9)+1];
  178.       if (cmp == p_val)
  179.         printf ("sector %lu, block %d\n", sec, i);
  180.     }
  181.     sec++;
  182.   }
  183. }
  184.  
  185. void Show_Primary_Volume_Descriptor (CDROM *p_cd)
  186. {
  187.   prim_vol_desc *pvd;
  188.   char buf[129];
  189.   int blk;
  190.   t_mdb mdb;
  191.   t_hdr_node hdr;
  192.   int skip;
  193.   t_ulong offset;
  194.   int protocol;
  195.   t_bool hfs;
  196.   
  197.   hfs = Uses_HFS_Protocol (p_cd, &skip);
  198.   protocol = Which_Protocol (p_cd, TRUE, &skip, &offset);
  199.   
  200.   if (protocol == PRO_UNKNOWN) {
  201.     printf ("Unknown protocol\n");
  202.     return;
  203.   }
  204.   
  205.   if (protocol == PRO_HIGH_SIERRA) {
  206.     printf ("High sierra protocol (not supported)\n");
  207.   }
  208.   
  209.   if ((protocol == PRO_ROCK || protocol == PRO_ISO) && hfs)
  210.     printf ("Multi-platform disk: HFS & ISO\n");
  211.  
  212.   if (protocol == PRO_ROCK)
  213.     printf ("Rock Ridge extensions available, skip size = %d\n", skip);
  214.   
  215.   if (protocol == PRO_ISO)
  216.     printf ("Data track offset = %lu\n", offset);
  217.  
  218.   if (protocol == PRO_ISO || protocol == PRO_ROCK) {
  219.  
  220.     if (!Read_Sector (p_cd, 16 + offset)) {
  221.       fprintf (stderr, "cannot read sector %lu\n", 16 + offset);
  222.       exit (1);
  223.     }
  224.  
  225.     pvd = (prim_vol_desc *) p_cd->buffer;
  226.     printf ("--- ISO-9660: ---\n");
  227.     printf ("Volume Descriptor Type:          %d\n", (int) pvd->type);
  228.     printf ("Standard Identifier:             %s\n", MKSTR (pvd->id,5,buf));
  229.     printf ("Volume Descriptor Version:       %d\n", (int) pvd->version);
  230.     printf ("System Identifier:               %s\n", MKSTR (pvd->system_id,32,buf));
  231.     printf ("Volume Identifier:               %s\n", MKSTR (pvd->volume_id,32,buf));
  232.     printf ("Volume Space Size:               %lu\n", pvd->space_size_m);
  233.     printf ("Volume Set Size:                 %hu\n", pvd->set_size_m);
  234.     printf ("Volume Sequence Number:          %hu\n", pvd->sequence_m);
  235.     printf ("Logical Block Size:              %hu\n", pvd->block_size_m);
  236.     printf ("Path Table Size:                 %lu\n", pvd->path_size_m);
  237.     printf ("Location of Occ of M Path Table: %lu\n", pvd->m_table); 
  238.     printf ("Location of Occ of Opt M Path T: %lu\n", pvd->opt_m_table);
  239.     printf ("Volume Set Identifier:           %s\n",
  240.                       MKSTR (pvd->volume_set_id,128,buf));  
  241.     printf ("Publisher Identifier:            %s\n",
  242.                       MKSTR (pvd->publisher_id,128,buf)); 
  243.     printf ("Data Preparer Identifier:        %s\n",
  244.                       MKSTR (pvd->data_preparer,128,buf));
  245.     printf ("Application Identifier:          %s\n",
  246.                       MKSTR (pvd->application_id,128,buf));
  247.     printf ("Copyright File Identifier:       %s\n",
  248.                       MKSTR (pvd->copyright,37,buf));
  249.     printf ("Abstract File Identifier:        %s\n",
  250.                       MKSTR (pvd->abstract_file_id,37,buf));
  251.     printf ("Bibliographic File Identifier:   %s\n",
  252.                       MKSTR (pvd->bibliographic_id,37,buf));
  253.     printf ("File Structure Version:          %d\n",
  254.                       (int) pvd->file_structure_version);
  255.     printf ("ROOT DIRECTORY RECORD:\n");
  256.     Show_Directory_Record (&pvd->root);
  257.   }
  258.   
  259.   if (hfs) {
  260.     if ((blk = HFS_Find_Master_Directory_Block (p_cd, &mdb)) < 0) {
  261.       printf ("No master directory block found\n");
  262.       exit (1);
  263.     }
  264.     printf ("--- MacHFS: ---\n");
  265.     printf ("Master directory