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