home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 282.dms / 282.adf / SourceCode / Cat.c < prev    next >
C/C++ Source or Header  |  2004-11-28  |  9KB  |  275 lines

  1. #include <devices/trackdisk.h>
  2. #include <libraries/dosextens.h>
  3. #include <exec/memory.h>
  4.  
  5. /* Allgemein verwendete Extern-Variablen */
  6. struct MsgPort  *dport,*CreatePort();
  7. struct IOStdReq *dmsg,*CreateStdIO();
  8. ULONG root[130],buffer[130],store[130];/* Store NUR fuer ReadBlock()*/
  9. UBYTE cache[100][520];
  10. ULONG bcount=0l,wb[84],parent=880l,pfnd=1l;
  11. ULONG filezahl=0l,bytezahl=0l,GetHash(),GetParent();
  12. struct FileInfoBlock *block,*AllocMem();
  13. struct FileLock *Lock(),*lock;
  14. struct CommandLineInterface *cli;
  15. struct Process *proc,*FindTask();
  16. UBYTE dpath[2][300];
  17.  
  18. /* main liest die Parameter und verzweigt in die jeweiligen Unterprogramme*/
  19. main (argc,argv)
  20. SHORT argc;
  21. UBYTE *argv[];
  22. {
  23.  ULONG loop=10l,block;
  24.  char proz='%';
  25.  strcpy (dpath[0],argv[0]); strcpy(dpath[1],argv[1]);
  26.  if (argc==1) { FindPath(); argc=2; }
  27.  if ((argc>1l)&&(toupper(dpath[1][0])=='D')&&(toupper(dpath[1][1])=='F')&&
  28.      (dpath[1][3]==':')&&(dpath[1][2]-48<4))
  29.  loop=(long)(dpath[1][2])-48l;   /* Fall 1:Laufwerk [+Pfad] */
  30.  
  31.  if ((argc>1)&&(dpath[1][0]=='#')) 
  32.  { loop=GetParent (dpath[1]); argc=0; } /* Fall 1.1:Laufwerk [+Block] */
  33.  
  34.  if ((argc>1)&&(dpath[1][0]=='?')) catinfo(); /* Fall 1.2 :Anleitung */
  35.  
  36.  if ((loop==10l)&&(argc>1))    /* Fall 2:Device   [+Pfad] */
  37.  { 
  38.   if (!(lock=Lock (dpath[1],ACCESS_READ))) { printf ("No Lock\n"); exit();} 
  39.   exdir(); 
  40.  }
  41.  if (!(dport=CreatePort(0l,0l))) exit (FALSE);
  42.  if (!(dmsg=CreateStdIO(dport))) exit (FALSE);
  43.  if (OpenDevice("trackdisk.device",loop,dmsg,0l))
  44.  {printf ("Laufwerk nicht vorhanden!\n"); exit (FALSE);}
  45.  if (argc>1) ChDir (&(dpath[1][4])); pfnd=0l; /* flag fuer DIR gefunden */
  46.  ReadBlock (dmsg,parent,root);
  47.  if ((*root!=2l)||((root[127]!=2l)&&(root[127]!=1l)))
  48.  { printf ("Block %lu ist kein Directoryblock\n",parent); finish(); }
  49.  for (loop=6l;loop<=77l;loop++)
  50.  {
  51.   if (root[loop]==0l) continue;
  52.   else block=root[loop];
  53.   do
  54.   {
  55.    ReadBlock (dmsg,block,buffer);
  56.    HandleBlock (buffer);
  57.    block=buffer[124];
  58.   } while (block);
  59.  }
  60.  printf ("%lu Eintraege - %lu bytes\n",filezahl,bytezahl);
  61.  printf ("%lu%c des Caches wurden gebraucht.\n",bcount,proz);
  62.  finish();
  63. }
  64. /**************************************************************************
  65.  *  Dieses Unterprogramm liest einen gewuenschten Block ein.              *
  66.  *  Zu parent gehoerige Header-Bloecke werden in einen Cache geschrieben  *
  67.  *  Die Speicher cache und store duerfen sonst nicht benutzt werden,da    *
  68.  *  sonst Verwirrungen entstehen.                                         *
  69.  **************************************************************************/
  70. ReadBlock (d,block,addr)
  71. struct IOStdReq *d;
  72. LONG block,*addr;
  73. {
  74.  ULONG start,end,loop;
  75.  for (loop=0l;loop<bcount;loop++) 
  76.  {
  77.   if (wb[loop]==block) 
  78.   { CopyMem (cache[loop],addr,512l); return (); }
  79.  }
  80.  d->io_Command=CMD_READ;
  81.  d->io_Offset=512l*block;
  82.  d->io_Length=512l;
  83.  d->io_Data=(APTR) addr; 
  84.  DoIO (d); 
  85.  end=10l+(start=(block/11l)*11l);
  86.  for (loop=start;loop<=end;loop++)
  87.  {
  88.   d->io_Command=CMD_READ;
  89.   d->io_Offset=512l*loop;
  90.   d->io_Length=512l;
  91.   d->io_Data=(APTR) store; 
  92.   DoIO (d); 
  93.   if ((block!=loop)&&(store[0]==2l)&&((store[125]==parent)||(pfnd)))
  94.   { CopyMem (store,cache[bcount],512l); wb[bcount++]=loop; }
  95.  if (bcount>99l) { printf ("Cache voll\n"); finish(); }
  96.  }
  97. }
  98.  
  99. /* Ausgabe eines Header-Blockes */
  100. HandleBlock (buffer)
  101. ULONG *buffer;
  102. {
  103.  if (buffer[80]<<28) printf ("*");
  104.  else printf (" ");
  105.  bcprint (&(buffer[108]));  
  106.  printf ("\r\233\63\62\103");
  107.  if (buffer[127]==-3l)
  108.  printf ("%6lu ",buffer[81]);
  109.  else printf (" (dir) ");
  110.  if (!(buffer[126]))
  111.  printf ("BC:%2lu ",buffer[2]);
  112.  else printf ("BC:?? ");
  113.  printf ("HB:%4lu ",buffer[1]);
  114.  printf ("DS:%4lu ",buffer[4]);
  115.  bcprint(&(buffer[82]));
  116.  printf ("\n");
  117.  filezahl++;bytezahl+=buffer[81];
  118. }
  119.  
  120. ChDir (addr)
  121. UBYTE *addr;
  122. {
  123.  UBYTE path[50],*pointer;
  124.  ULONG loop,block;
  125.  if (!(*addr)) return();
  126.  for (loop=0l;(*addr!='/')&&(*addr);loop++)
  127.  path[loop]=(UBYTE) toupper(*addr++);
  128.  path[loop]=0;
  129.  ReadBlock (dmsg,parent,buffer);
  130.  block=GetHash(path);
  131.  if (buffer[block]==0l) { printf ("\nPfad nicht gefunden!\n"); finish(); }
  132.  block=buffer[block];
  133.  do
  134.  {
  135.   ReadBlock (dmsg,block,buffer);
  136.   if (buffer[0]!=2l) { printf ("\nProgramm Fehler 1!!!!\n"); finish(); }
  137.   pointer=(UBYTE *) &(buffer[108]);  
  138.   loop=(long)(*pointer++);
  139.   parent=block;
  140.   block=buffer[124];
  141.   pointer[loop]=0;
  142.   bigletter(pointer);
  143.  } while ((strcmp(path,pointer)!=0)&&(block));
  144.  loop=buffer[127];
  145.  if ((strcmp(path,pointer))&&(block==0l))
  146.  { printf ("\nPfad nicht gefunden.\n"); finish(); }
  147.  if (loop!=2l)
  148.  { printf ("\nDateien gehoeren nicht in den Pfad!\n"); finish(); }
  149.  ChDir(++addr);
  150. }
  151. /************************************************************************
  152.  * GetParent () setzt das aktuelle Unterverzeichnis auf den angegebenen *
  153.  * Wert.                                                                *
  154.  ************************************************************************/
  155. ULONG GetParent (addr)
  156. UBYTE *addr;
  157. {
  158.  ULONG drive=0l,mult=1l,block=0l;
  159.  while (*(++addr)=='#') drive++;
  160.  if (!(*addr)) return (drive);
  161.  turn (addr);
  162.  do
  163.  {
  164.   block+=mult*(ULONG)((*addr)-48);
  165.   mult*=10l;
  166.  } while ((mult<=1000l)&&(*(++addr)));
  167.  if (block>1759l){printf("%lu ist keine gueltige Blockzahl\n",block); exit();}
  168.  parent=block;
  169.  return(drive);
  170. }
  171. /* Diese Funktion stoppt den Laufwerksmotor und beendet das Programm */
  172. finish()
  173. {
  174.  dmsg->io_Command=TD_MOTOR;
  175.  dmsg->io_Length=0l;
  176.  DoIO (dmsg);
  177.  CloseDevice (dmsg);
  178.  DeleteStdIO (dmsg);
  179.  DeletePort (dport);
  180.  exit();
  181. }
  182. /* Diese langsamere Variante ist fuer Devices und pfadlose
  183.   Angaben zustaendig */
  184. exdir()
  185. {
  186.  if (!(block=AllocMem (300l,MEMF_CLEAR|MEMF_CHIP)))
  187.  { printf ("Kein Speicher (Gleich koennen Sie meditieren.\n"); exit();}
  188.  if (!(Examine (lock,block))) { printf ("Exa err\n"); exit(); }
  189.  while (ExNext (lock,block)) 
  190.  {
  191.   if ((block->fib_Protection)<<28) printf ("*");
  192.   else printf (" "); 
  193.   printf ("%s\266\r\233\63\62\103",block->fib_FileName);
  194.   if (block->fib_DirEntryType>=0l) printf ("  (dir)");
  195.   else printf (" %6lu",block->fib_Size);
  196.   printf (" BC:%4lu ",block->fib_NumBlocks);
  197.   printf (":%s\266\n",block->fib_Comment);
  198.   filezahl++; bytezahl+=block->fib_Size;
  199.  }
  200.  printf ("%lu Eintraege - %lu bytes\n",filezahl,bytezahl);
  201.  printf ("Der cache wurde nicht benutzt.\n");
  202.  UnLock (lock);
  203.  exit();
  204. }
  205. /***********************************************************************
  206.  *    FindPath () sucht bei angabenlosen Aufrufen den Pfad und ent-    *
  207.  *    scheidet,ob die schnelle oder langsame Routine genommen wird.    *
  208.  ***********************************************************************/
  209. FindPath ()
  210. {
  211.  UBYTE *addr;
  212.  ULONG loop,loop2=0l;
  213.  if (!(proc=FindTask (0l))) { printf ("No Task found\n"); exit(); }
  214.  cli=(struct CommandLineInterface *)BADDR(proc->pr_CLI);
  215.  addr=(UBYTE *)BADDR (cli->cli_SetName);
  216.  for (loop=(ULONG)*addr++;loop2<loop;loop2++) dpath[1][loop2]=*addr++;
  217.  dpath[1][loop2]=0;
  218.  printf ("Pfad:%s\n",dpath[1]);
  219. }
  220. /***********************************************************
  221.  *    catinfo() gibt eine kurze Erklaerung aus             *
  222.  ***********************************************************/
  223. catinfo()
  224. {
  225.  printf ("\233\61;32;40mCat V1.5 von \233\64;33;40mFelix Wente\n");
  226.  printf ("\233\60;31;40mUsage:cat [dfx:[Pfad]][n*#[Block]][Pfad]\n");
  227.  printf ("Feel free to copy the program (for non-commerial use only!)\n");
  228.  exit();
  229. }
  230. /***********************************************************************
  231. *  Hier stehen folgende Hilfsroutinen:                                 *
  232. *  bigletter() wandelt eine Zeichenkette in Grossbuchstaben um         *
  233. *  bcprint  () gibt einen BCPL-String aus (mit C-Adresse !!!!)         *
  234. *  GetHash  () berechnet den Hashwert einer Zeichenkette               *
  235. *  turn     () dreht eine Zeichenkette um                              *
  236. ************************************************************************/
  237. bigletter (addr)
  238. char *addr;
  239. {
  240.  short sicherung=0;
  241.  while ((*addr)!=0)
  242.  {
  243.   *addr=toupper(*addr);
  244.   sicherung++;addr++;
  245.   if (sicherung>=30) { printf ("Da ging was schief\n"); exit(); }
  246.  } 
  247. }
  248. bcprint (addr)
  249. char *addr;
  250. {
  251.  short schleife;
  252.  for (schleife=1;schleife<=(short)*addr;schleife++)
  253.  {
  254.   if ((addr[schleife]>31)&&(addr[schleife]<128))
  255.   printf ("%c",addr[schleife]);
  256.   else  printf ("#");
  257.  }
  258.  printf ("\266");
  259. }
  260. ULONG GetHash (addr)
  261. UBYTE *addr;
  262. {
  263.  ULONG laenge,hash;
  264.  laenge=(ULONG)(strlen(addr));
  265.  for (hash=laenge;laenge--;)
  266.   hash=((hash*13l+toupper(*addr++))&0x7ffl);
  267.  return ((LONG)(hash%72l+6l));
  268. }
  269. turn(string)
  270. char *string;
  271. {
  272.  char *start=string,help;
  273.  while (*++string); while (start<=--string)
  274.  { help=*start; *start++=*string; *string=help; }
  275. }