home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / DOS / dos_calls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  25.5 KB  |  1,497 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dosextens.h>
  4. #include <dos/record.h>
  5.  
  6. #include <clib/exec_protos.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. /* normally pragmas would be included here */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14.  
  15. #define SAME 0
  16.  
  17. /* so the compiler doesn't get upset */
  18. void help(int *done);
  19. void quit(int *done);
  20. void source(int *done);
  21. void showbuffer(int *done);
  22. void open(int *done);
  23. void close(int *done);
  24. void showfiles(int *done);
  25. void lock(int *done);
  26. void unlock(int *done);
  27. void duplock(int *done);
  28. void showlocks(int *done);
  29. void myread(int *done);
  30. void mywrite(int *done);
  31. void lockrecord(int *done);
  32. void unlockrecord(int *done);
  33. void myfgets(int *done);
  34. void myfputs(int *done);
  35. void myfgetc(int *done);
  36. void myfputc(int *done);
  37. void myseek(int *done);
  38. void myflush(int *done);
  39. void dumpfh(int *done);
  40. void dumplock(int *done);
  41. void samelock(int*done);
  42. void execute(int *done);
  43. void deletefile(int *done);
  44. void splitname(int *done);
  45. void findvar(int *done);
  46. void duplockfromfh(int *done);
  47. void namefromlock(int *done);
  48. void namefromfh(int *done);
  49. void parentdir(int *done);
  50. void parentoffh(int *done);
  51. void examine(int *done);
  52. void exnext(int *done);
  53. void parsepattern(int *done);
  54. void matchpattern(int *done);
  55. void parsepatternnocase(int *done);
  56. void matchpatternnocase(int *done);
  57. void changemode(int *done);
  58. void openfromlock(int *done);
  59. void setfilesize(int *done);
  60. void isfilesystem(int *done);
  61. void getvar(int *done);
  62. void setvar(int *done);
  63. void setbuffer(int *done);
  64. void loadseg(int*done);
  65. void unloadseg(int*done);
  66. /* more commands can be _easily_ added */
  67.  
  68. struct commands {
  69.     char *name;
  70.     char *helpstr;
  71.     void (*rtn)(int *done);
  72. } comm[] = {
  73.     "?"," - list all commands",help,
  74.     "quit"," - exit this program",quit,
  75.     "Open","<filename> <mode> - open a file",open,
  76.     "Close","<file> - close a file",close,
  77.     "Read","<file> <offset> <length> - read bytes from file",myread,
  78.     "Write","<file> <offset> <length> - write bytes from file",mywrite,
  79.     "Seek","<file> <offset> <mode> - seek to position in file",myseek,
  80.     "SetFileSize","<file> <offset> <mode> - set the size of the file",
  81.         setfilesize,
  82.     "Lock","<name> <mode> - lock an object",lock,
  83.     "UnLock","<lock> - unlock a lock",unlock,
  84.     "DupLock","<lock> - duplicate a lock",duplock,
  85.     "SameLock","<lock> <lock> - determine if two locks are on same object",
  86.         samelock,
  87.     "ParentDir","<lock> - get a lock on the parent of a lock",parentdir,
  88.     "OpenFromLock","<lock> - open a filehandle on the locked item",
  89.         openfromlock,
  90.     "Examine","<lock> <offset> - examine a lock",examine,
  91.     "ExNext","<lock> <offset> - examine next file/dir",exnext,
  92.     "ParentOfFH","<file> - returns lock on parent dir of object",parentoffh,
  93.     "DupLockFromFH","<number> - get a lock on open file",duplockfromfh,
  94.     "NameFromLock","<lock> <offset> <length> - get name of object",
  95.         namefromlock,
  96.     "NameFromFH","<file> <offset> <length> - get name of object",
  97.         namefromfh,
  98.     "LockRecord","<file> <offset> <length> <mode> <timeout>- lock a part of a file",
  99.         lockrecord,
  100.     "UnlockRecord","<file> <offset> <length> - unlock a part of a file",
  101.         unlockrecord,
  102.     "ChangeMode","[FH|Lock] <#> <newmode> - change mode of item",
  103.         changemode,
  104.     "FGets","<file> <offset> <length> - buffered read to len or \\n",
  105.         myfgets,
  106.     "FPuts","<file> <offset>|<string> - buffered write of string",myfputs,
  107.     "FGetC","<file> <offset> - buffered character read",myfgetc,
  108.     "FPutC","<file> <offset>|<'char'> - buffered character write",myfputc,
  109.     "Flush","<file> - flush IO buffers for file",myflush,
  110.     "Execute","<command> <input file> <output file>",execute,
  111.     "DeleteFile","<filename> - delete a file or directory",deletefile,
  112.     "SplitName","<string> <'char'> <offset> <old pos> <size>",splitname,
  113.     "GetVar","<name> <offset> <size> <flags> - get a variable's value",
  114.         getvar,
  115.     "SetVar","<name> <offset> <size> <flags> - set a variable's value",
  116.         setvar,
  117.     "FindVar","<name> <type> - find a local variable",findvar,
  118.     "ParsePattern","<pattern> <offset> <len> - parse a pattern",
  119.         parsepattern,
  120.     "MatchPattern","<offset> <string> - match against a pattern",
  121.         matchpattern,
  122.     "ParsePatternNoCase","<pattern> <offset> <len> - parse a pattern",
  123.         parsepatternnocase,
  124.     "MatchPatternNoCase","<offset> <string> - match against a pattern",
  125.         matchpatternnocase,
  126.     "IsFileSystem","<string> - determine if a handler is a filesystem",
  127.         isfilesystem,
  128.     "LoadSeg","<file> - load an executable",loadseg,
  129.     "UnLoadSeg","0x<addr> - unload an executable",unloadseg,
  130.     "DumpFH","<file> - show filehandle",dumpfh,
  131.     "DumpLock","<lock> - show filelock",dumplock,
  132.     "ShowFiles"," - show all open files",showfiles,
  133.     "ShowLocks"," - show all locks",showlocks,
  134.     "Source","<file> - read commands from file",source,
  135.     "SetBuffer","[offset] [hex <hex digits>]|<string>",setbuffer,
  136.     "ShowBuffer","[hex] [offset [length]] - show part of buffer",
  137.         showbuffer,
  138. };
  139.  
  140. extern struct DosLibrary *DOSBase;
  141.  
  142. /* filehandle for input */
  143. FILE *fp = stdin;
  144.  
  145. int
  146. main (argc,argv)
  147.     int argc;
  148.     char **argv;
  149. {
  150.     char buff[256];
  151.     int done = FALSE,i,len;
  152.     char *command;
  153.  
  154.     if (argc != 1)
  155.     {
  156.         printf("usage: %s\n",argv[0]);
  157.         exit(10);
  158.     }
  159.  
  160.     if (DOSBase->dl_lib.lib_Version < 37)
  161.     {
  162.         printf("You need a dos.library V37 or better!\n");
  163.         exit(20);
  164.     }
  165.  
  166.     printf("%s 2.0 - use ? to get a list of commands\n\n",argv[0]);
  167.     printf("\tWarning: there is little error checking in this program.\n");
  168.     printf("You HAVE been warned!\n");
  169.  
  170.     while (!done)
  171.     {
  172.         if (fp == stdin)
  173.         {
  174.             printf("%s> ",argv[0]);
  175.             fflush(stdout);
  176.         }
  177.         if (fgets(buff,sizeof(buff),fp) == NULL)
  178.         {
  179.             if (fp == stdin)
  180.                 break;            /* eof */
  181.             else {
  182.                 fp = stdin;        /* end of 'source' */
  183.                 continue;
  184.             }
  185.         }
  186.  
  187.         len = strlen(buff);        /* kill \n */
  188.         if (len && buff[len-1] == '\n')
  189.             buff[len-1] = '\0';
  190.  
  191.         command = strtok(buff," ");
  192.         if (!command || !*command)        /* no command */
  193.             continue;
  194.  
  195.         for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  196.         {
  197.             if (stricmp(command,comm[i].name) == SAME)
  198.             {
  199.                 /* found it - execute */
  200.  
  201.                 (*(comm[i].rtn))(&done);
  202.                 break;
  203.             }
  204.         }
  205.         if (i >= sizeof(comm)/sizeof(comm[0]))    /* failure */
  206.             printf("Error: command %s not known!\n",command);
  207.     }
  208.  
  209.     return 0;
  210. }
  211.  
  212. void
  213. help (int *done)
  214. {
  215.     int i;
  216.  
  217.     for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  218.     {
  219.         printf(" %s %s\n",comm[i].name,comm[i].helpstr);
  220.     }
  221.     printf("\n <file> or <lock> refers to an index into the arrays\n");
  222.     printf(" <offset> refers to an offset into the buffer\n");
  223. }
  224.  
  225. void
  226. quit (int *done)
  227. {
  228.     *done = TRUE;
  229. }
  230.  
  231. void
  232. source (int *done)
  233. {
  234.     char *arg;
  235.     FILE *myfp;
  236.  
  237.     arg = strtok(NULL," ");
  238.     if (!arg)
  239.     {
  240.         printf("error: use source <filename>\n");
  241.         return;
  242.     }
  243.  
  244.     myfp = fopen(arg,"r");
  245.     if (!myfp)
  246.     {
  247.         printf("error: Can't open %s!\n",arg);
  248.         return;
  249.     }
  250.  
  251.     fp = myfp;
  252. }
  253.  
  254. unsigned char buffer[1024];
  255.  
  256. void
  257. showbuffer (int *done)
  258. {
  259.     char *arg;
  260.     int i;
  261.     int hex = FALSE;
  262.     long offset = 0;
  263.     long len    = 80;
  264.  
  265.     arg = strtok(NULL," ");
  266.     if (arg)
  267.     {
  268.         if (stricmp(arg,"hex") == SAME)
  269.         {
  270.             hex = TRUE;
  271.             arg = strtok(NULL," ");
  272.         }
  273.     }
  274.  
  275.     /* optional offset */
  276.     if (arg)
  277.     {
  278.         if (strlen(arg) != stcd_l(arg,&offset))
  279.         {
  280. err:            printf("error: bad number (%s)\n",arg);
  281.             printf("error: use showbuffer [hex] [offset [length]]\n");
  282.             return;
  283.         }
  284.  
  285.         arg = strtok(NULL," ");
  286.         if (arg)
  287.         {
  288.             if (strlen(arg) != stcd_l(arg,&len))
  289.             {
  290.                 goto err;
  291.             }
  292.         }
  293.     }
  294.  
  295.     for (i = 0; i < len; i++)
  296.     {
  297.         if (!hex)
  298.         {
  299.             if (!buffer[offset+i])
  300.                 break;
  301.             putchar(buffer[offset+i]);
  302.         } else {
  303.             if (i % 20 == 0)
  304.             {
  305.                 printf("\n%04ld: ",i+offset);
  306.             }
  307.             printf("%02x ",buffer[offset+i]);
  308.         }
  309.     }
  310.     putchar('\n');
  311. }
  312.  
  313. void
  314. setbuffer (int *done)
  315. {
  316.     char *str;
  317.     long offset;
  318.     char hexdigit[4];
  319.     long digit;
  320.  
  321.     str = strtok(NULL," ");
  322.     if (!str)
  323.     {
  324. err:        printf("Usage: SetBuffer [offset] [hex <hex digits>]|<string>\n");
  325.         return;
  326.     }
  327.     if (strlen(str) != stcd_l(str,&offset))
  328.     {
  329.         offset = 0;
  330.     } else {
  331.         /* got offset, get next string */
  332.         str = strtok(NULL," ");
  333.         if (!str)
  334.             goto err;
  335.     }
  336.  
  337.     if (stricmp(str,"hex") == SAME)
  338.     {
  339.         str = strtok(NULL," ");
  340.         if (!str)
  341.             goto err;
  342.  
  343.         while (*str)
  344.         {
  345.             hexdigit[0] = *str++;
  346.             if (!*str)
  347.                 goto err;
  348.  
  349.             hexdigit[1] = *str++;
  350.             hexdigit[2] = 0;
  351.  
  352.             if (stch_l(hexdigit,&digit) != 2)
  353.             {
  354.                 printf("Bad hex value '%s'!\n",hexdigit);
  355.                 return;
  356.             }
  357.  
  358.             buffer[offset++] = digit;
  359.         }
  360.     } else {
  361.         /* string */
  362.         strcpy(&(buffer[offset]),str);
  363.     }
  364. }
  365.  
  366. /* end standard code */
  367.  
  368. /* data */
  369.  
  370. BPTR files[10];
  371. BPTR locks[10];
  372. char *lastarg;        /* for getnum failures */
  373.  
  374. int
  375. getnum (int *num)
  376. {
  377.     lastarg = strtok(NULL," ");
  378.     if (!lastarg)
  379.         return FALSE;
  380.  
  381.     if (strlen(lastarg) != stcd_l(lastarg,(long *)num))
  382.     {
  383.         printf("error: bad number (%s)\n",lastarg);
  384.         return FALSE;
  385.     }
  386.  
  387.     return TRUE;
  388. }
  389.  
  390. int
  391. get2nums (int *num1, int *num2)
  392. {
  393.     if (!getnum(num1))
  394.         return FALSE;
  395.  
  396.     return getnum(num2);
  397. }
  398.  
  399. int
  400. get3nums (int *num1, int *num2, int *num3)
  401. {
  402.     if (!getnum(num1) || !getnum(num2))
  403.         return FALSE;
  404.  
  405.     return getnum(num3);
  406. }
  407.  
  408. /* open file */
  409. void
  410. open (int *done)
  411. {
  412.     char *name,*modestr;
  413.     LONG mode;
  414.     BPTR fh;
  415.     int i;
  416.  
  417.     name = strtok(NULL," ");
  418.     if (!name)
  419.     {
  420. err:        printf("error: use open <filename> <mode>\n");
  421.         return;
  422.     }
  423.  
  424.     modestr = strtok(NULL," ");
  425.     if (!modestr)
  426.         goto err;
  427.  
  428.     if (stricmp(modestr,"mode_oldfile") == SAME)
  429.         mode = MODE_OLDFILE;
  430.     else if (stricmp(modestr,"mode_newfile") == SAME)
  431.         mode = MODE_NEWFILE;
  432.     else
  433.         if (strlen(modestr) != stcd_l(modestr,&mode))
  434.         {
  435.             printf("error: bad number (%s)\n",modestr);
  436.             return;
  437.         }
  438.  
  439.     fh = Open(name,mode);
  440.     if (!fh)
  441.     {
  442.         printf("error: Can't open %s, ioerr = %ld\n",name,IoErr());
  443.         return;
  444.     }
  445.  
  446.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  447.     {
  448.         if (files[i] == NULL)
  449.         {
  450.             files[i] = fh;
  451.             printf("Opened as file %d\n",i);
  452.             return;
  453.         }
  454.     }
  455.  
  456.     printf("no space for more files!\n");
  457.     Close(fh);
  458. }
  459.  
  460. void
  461. close (int *done)
  462. {
  463.     int num;
  464.  
  465.     if (!getnum(&num))
  466.     {
  467.         printf("error: use close <filenumber>\n");
  468.         return;
  469.     }
  470.  
  471.     Close(files[num]);
  472.     files[num] = NULL;
  473. }
  474.  
  475. void
  476. openfromlock (int *done)
  477. {
  478.     BPTR fh;
  479.     int i;
  480.  
  481.     if (!getnum(&i))
  482.     {
  483.         printf("error: use OpenFromLock <lock>\n");
  484.         return;
  485.     }
  486.  
  487.     fh = OpenFromLock(locks[i]);
  488.     if (!fh)
  489.     {
  490.         printf("error: Can't open from lock %d (0x%lx), IoErr = %ld\n",
  491.             i,locks[i],IoErr());
  492.         return;
  493.     }
  494.     locks[i] = NULL;
  495.  
  496.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  497.     {
  498.         if (files[i] == NULL)
  499.         {
  500.             files[i] = fh;
  501.             printf("Opened as file %d\n",i);
  502.             return;
  503.         }
  504.     }
  505.  
  506.     printf("no space for more files!\n");
  507.     Close(fh);
  508. }
  509.  
  510. void
  511. showfiles (int *done)
  512. {
  513.     int i;
  514.  
  515.     printf("\tNumber\t   FH\n");
  516.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  517.     {
  518.         if (files[i])
  519.             printf("\t%d\t- 0x%lx\n",i,files[i]);
  520.     }
  521. }
  522.  
  523. /* open file */
  524. void
  525. lock (int *done)
  526. {
  527.     char *name,*modestr;
  528.     LONG mode;
  529.     BPTR lock;
  530.     int i;
  531.  
  532.     name = strtok(NULL," ");
  533.     if (!name)
  534.     {
  535. err:        printf("error: use lock <name> <mode>\n");
  536.         return;
  537.     }
  538.  
  539.     modestr = strtok(NULL," ");
  540.     if (!modestr)
  541.         goto err;
  542.  
  543.     if (stricmp(modestr,"shared") == SAME)
  544.         mode = SHARED_LOCK;
  545.     else if (stricmp(modestr,"exclusive") == SAME)
  546.         mode = EXCLUSIVE_LOCK;
  547.     else
  548.         if (strlen(modestr) != stcd_l(modestr,&mode))
  549.         {
  550.             printf("error: bad number (%s)\n",modestr);
  551.             return;
  552.         }
  553.  
  554.     lock = Lock(name,mode);
  555.     if (!lock)
  556.     {
  557.         printf("error: Can't lock %s, ioerr = %ld\n",name,IoErr());
  558.         return;
  559.     }
  560.  
  561.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  562.     {
  563.         if (locks[i] == NULL)
  564.         {
  565.             locks[i] = lock;
  566.             printf("Locked as lock %d\n",i);
  567.             return;
  568.         }
  569.     }
  570.  
  571.     printf("no space for more locks!\n");
  572.     UnLock(lock);
  573. }
  574.  
  575. void
  576. unlock (int *done)
  577. {
  578.     int num;
  579.  
  580.     if (!getnum(&num))
  581.     {
  582.         printf("error: use unlock <locknumber>\n");
  583.         return;
  584.     }
  585.  
  586.     UnLock(locks[num]);
  587.     locks[num] = NULL;
  588. }
  589.  
  590. void
  591. duplock (int *done)
  592. {
  593.     int num,i;
  594.     BPTR lock;
  595.  
  596.     if (!getnum(&num))
  597.     {
  598.         printf("error: use duplock <locknumber>\n");
  599.         return;
  600.     }
  601.  
  602.     lock = DupLock(locks[num]);
  603.     if (!lock)
  604.     {
  605.         printf("error: Can't duplock 0x%lx, ioerr = %ld\n",
  606.             locks[num],IoErr());
  607.         printf(" (note: DupLock(0) == 0)\n");
  608.         return;
  609.     }
  610.  
  611.  
  612.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  613.     {
  614.         if (locks[i] == NULL)
  615.         {
  616.             locks[i] = lock;
  617.             printf("Locked as lock %d\n",i);
  618.             return;
  619.         }
  620.     }
  621.  
  622.     printf("no space for more locks!\n");
  623.     UnLock(lock);
  624. }
  625.  
  626. void
  627. parentdir (int *done)
  628. {
  629.     int num,i;
  630.     BPTR lock;
  631.  
  632.     if (!getnum(&num))
  633.     {
  634.         printf("error: use ParentDir <locknumber>\n");
  635.         return;
  636.     }
  637.  
  638.     lock = ParentDir(locks[num]);
  639.     if (!lock)
  640.     {
  641.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  642.             locks[num],IoErr());
  643.         return;
  644.     }
  645.  
  646.  
  647.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  648.     {
  649.         if (locks[i] == NULL)
  650.         {
  651.             locks[i] = lock;
  652.             printf("Locked as lock %d\n",i);
  653.             return;
  654.         }
  655.     }
  656.  
  657.     printf("no space for more locks!\n");
  658.     UnLock(lock);
  659. }
  660.  
  661. void
  662. duplockfromfh (int *done)
  663. {
  664.     int num,i;
  665.     BPTR lock;
  666.  
  667.     if (!getnum(&num))
  668.     {
  669.         printf("error: use DupLockFromFH <filenumber>\n");
  670.         return;
  671.     }
  672.  
  673.     lock = DupLockFromFH(files[num]);
  674.     if (!lock)
  675.     {
  676.         printf("error: Can't duplockfromfh 0x%lx, ioerr = %ld\n",
  677.             files[num],IoErr());
  678.         return;
  679.     }
  680.  
  681.  
  682.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  683.     {
  684.         if (locks[i] == NULL)
  685.         {
  686.             locks[i] = lock;
  687.             printf("Locked as lock %d\n",i);
  688.             return;
  689.         }
  690.     }
  691.  
  692.     printf("no space for more locks!\n");
  693.     UnLock(lock);
  694. }
  695.  
  696. void
  697. parentoffh (int *done)
  698. {
  699.     int num,i;
  700.     BPTR lock;
  701.  
  702.     if (!getnum(&num))
  703.     {
  704.         printf("error: use ParentOfFH <filenumber>\n");
  705.         return;
  706.     }
  707.  
  708.     lock = ParentOfFH(files[num]);
  709.     if (!lock)
  710.     {
  711.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  712.             files[num],IoErr());
  713.         return;
  714.     }
  715.  
  716.  
  717.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  718.     {
  719.         if (locks[i] == NULL)
  720.         {
  721.             locks[i] = lock;
  722.             printf("Locked as lock %d\n",i);
  723.             return;
  724.         }
  725.     }
  726.  
  727.     printf("no space for more locks!\n");
  728.     UnLock(lock);
  729. }
  730.  
  731. void
  732. examine (int *done)
  733. {
  734.     struct FileInfoBlock *fib;
  735.     LONG num,offset,rc;
  736.  
  737.     if (!getnum(&num))
  738.     {
  739. err:        printf("error: use Examine <lock> <offset>\n");
  740.         return;
  741.     }
  742.  
  743.     if (!getnum(&offset))
  744.         goto err;
  745.  
  746.     fib = (void *) &buffer[offset];
  747.  
  748.     rc = Examine(locks[num],fib);
  749.     printf("Examine returned %ld, ioerr = %ld\n",rc,IoErr());
  750. }
  751.  
  752. void
  753. exnext (int *done)
  754. {
  755.     struct FileInfoBlock *fib;
  756.     LONG num,offset,rc;
  757.  
  758.     if (!getnum(&num))
  759.     {
  760. err:        printf("error: use ExNext <lock> <offset>\n");
  761.         return;
  762.     }
  763.  
  764.     if (!getnum(&offset))
  765.         goto err;
  766.  
  767.     fib = (void *) &buffer[offset];
  768.  
  769.     rc = ExNext(locks[num],fib);
  770.     printf("ExNext returned %ld, ioerr = %ld\n",rc,IoErr());
  771. }
  772.  
  773. void
  774. showlocks (int *done)
  775. {
  776.     int i;
  777.  
  778.     printf("\tNumber\t   Lock\n");
  779.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  780.     {
  781.         if (locks[i])
  782.             printf("\t%d\t- 0x%lx\n",i,locks[i]);
  783.     }
  784. }
  785.  
  786. void
  787. myread (int *done)
  788. {
  789.     long offset,length,file,len;
  790.  
  791.     if (!get3nums(&file,&offset,&length))
  792.     {
  793.         printf("error: use read <filenumber> <offset> <length>\n");
  794.         return;
  795.     }
  796.  
  797.     len = Read(files[file],&(buffer[offset]),length);
  798.     if (len < 0)
  799.     {
  800.         printf("error on read - %ld\n",IoErr());
  801.         return;
  802.     }
  803.  
  804.     printf("Read %ld characters\n",len);
  805. }
  806.  
  807. void
  808. mywrite (int *done)
  809. {
  810.     long offset,length,file,len;
  811.  
  812.     if (!get3nums(&file,&offset,&length))
  813.     {
  814.         printf("error: use write <filenumber> <offset> <length>\n");
  815.         return;
  816.     }
  817.  
  818.     len = Write(files[file],&(buffer[offset]),length);
  819.     if (len < 0)
  820.     {
  821.         printf("error on write - %ld\n",IoErr());
  822.         return;
  823.     }
  824.  
  825.     printf("Wrote %ld characters\n",len);
  826. }
  827.  
  828. void
  829. myseek (int *done)
  830. {
  831.     long offset,mode,file,oldpos;
  832.  
  833.     if (!get3nums(&file,&offset,&mode))
  834.     {
  835.         printf("error: use seek <filenumber> <offset> <mode>\n");
  836.         return;
  837.     }
  838.  
  839.     oldpos = Seek(files[file],offset,mode);
  840.     if (oldpos < 0)
  841.     {
  842.         printf("error on seek - %ld\n",IoErr());
  843.         return;
  844.     }
  845.  
  846.     printf("old position was %ld\n",oldpos);
  847. }
  848.  
  849. void
  850. setfilesize (int *done)
  851. {
  852.     long offset,mode,file,oldpos;
  853.  
  854.     if (!get3nums(&file,&offset,&mode))
  855.     {
  856.         printf("error: use SetFileSize <filenumber> <offset> <mode>\n");
  857.         return;
  858.     }
  859.  
  860.     oldpos = SetFileSize(files[file],offset,mode);
  861.     if (oldpos < 0)
  862.     {
  863.         printf("error on setfilesize - %ld\n",IoErr());
  864.         return;
  865.     }
  866.  
  867.     printf("old position was %ld, current position is %ld\n",oldpos,
  868.         Seek(files[file],0,OFFSET_CURRENT));
  869. }
  870.  
  871. void
  872. lockrecord (int *done)
  873. {
  874.     long offset,length,file,res,mode,timeout = 50;
  875.  
  876.     if (!get3nums(&file,&offset,&length) ||
  877.         !get2nums(&mode,&timeout))
  878.     {
  879.         printf(
  880.  "error: use lockrecord <filenumber> <offset> <length> <mode> <timeout>\n");
  881.         return;
  882.     }
  883.  
  884.     res = LockRecord(files[file],offset,length,mode,timeout);
  885.     if (res == 0)
  886.     {
  887.         printf("error on lock - %ld\n",IoErr());
  888.         return;
  889.     }
  890.  
  891.     printf("Locked (%ld,%ld)\n",res,IoErr());
  892. }
  893.  
  894. void
  895. unlockrecord (int *done)
  896. {
  897.     long offset,length,file,res;
  898.  
  899.     if (!get3nums(&file,&offset,&length))
  900.     {
  901.      printf("error: use unlockrecord <filenumber> <offset> <length>\n");
  902.      return;
  903.     }
  904.  
  905.     res = UnLockRecord(files[file],offset,length);
  906.     if (res == 0)
  907.     {
  908.         printf("error on unlock - %ld\n",IoErr());
  909.         return;
  910.     }
  911.  
  912.     printf("Unlocked\n");
  913. }
  914.  
  915. void
  916. myfgets (int *done)
  917. {
  918.     long offset,length,file;
  919.     char *p;
  920.  
  921.     if (!get3nums(&file,&offset,&length))
  922.     {
  923.         printf("error: use fgets <filenumber> <offset> <length>\n");
  924.         return;
  925.     }
  926.  
  927.     p = (char *) FGets(files[file],&(buffer[offset]),length);
  928.     if (p == 0)
  929.     {
  930.         printf("eof or error on FGets - %ld\n",IoErr());
  931.         return;
  932.     }
  933.  
  934.     printf("Read %ld characters\n",strlen(p));
  935. }
  936.  
  937. void
  938. myfputs (int *done)
  939. {
  940.     long offset,file;
  941.     unsigned char *p;
  942.  
  943.     if (!getnum(&file))
  944.     {
  945. err:        printf("error: use fputs <filenumber> <offset>|<string>\n");
  946.         return;
  947.     }
  948.     if (!(p = strtok(NULL," ")))
  949.         goto err;
  950.  
  951.     if (strlen(p) == stcd_l(p,&offset))
  952.     {
  953.         p = &(buffer[offset]);
  954.     }
  955.  
  956.     if (FPuts(files[file],p))
  957.     {
  958.         printf("eof or error on FPuts - %ld\n",IoErr());
  959.         return;
  960.     }
  961.  
  962.     printf("Wrote %ld characters\n",strlen(p));
  963. }
  964.  
  965. void
  966. myfgetc (int *done)
  967. {
  968.     long offset,file;
  969.     long p;
  970.  
  971.     if (!get2nums(&file,&offset))
  972.     {
  973.         printf("error: use fgetc <filenumber> <offset>\n");
  974.         return;
  975.     }
  976.  
  977.     p = FGetC(files[file]);
  978.     if (p == -1)
  979.     {
  980.         printf("eof on FGetC - %ld\n",IoErr());
  981.         return;
  982.     }
  983.  
  984.     buffer[offset] = p;
  985.  
  986.     printf("Read 0x%lx\n",p);
  987. }
  988.  
  989. void
  990. myfputc (int *done)
  991. {
  992.     long offset,file;
  993.     unsigned char *p;
  994.  
  995.     if (!getnum(&file))
  996.     {
  997. err:        printf("error: use fputc <filenumber> <offset>|<'char'>\n");
  998.         return;
  999.     }
  1000.     if (!(p = strtok(NULL," ")))
  1001.         goto err;
  1002.  
  1003.     if (strlen(p) == stcd_l(p,&offset))
  1004.     {
  1005.         p = &(buffer[offset]);
  1006.     }
  1007.  
  1008.     FPutC(files[file],*p);
  1009.  
  1010.     printf("Wrote 0x%lx\n",*p);
  1011. }
  1012.  
  1013. void
  1014. myflush (int *done)
  1015. {
  1016.     long file;
  1017.  
  1018.     if (!getnum(&file))
  1019.     {
  1020.         printf("error: use flush <filenumber>\n");
  1021.         return;
  1022.     }
  1023.  
  1024.     Flush(files[file]);
  1025.  
  1026.     printf("flushed\n");
  1027. }
  1028.  
  1029. void
  1030. dumpfh (int *done)
  1031. {
  1032.     long file;
  1033.     struct FileHandle *fh;
  1034.  
  1035.     if (!getnum(&file))
  1036.     {
  1037.         printf("error: use dumpfh <filenumber>\n");
  1038.         return;
  1039.     }
  1040.  
  1041.     fh = (void *) BADDR(files[file]);
  1042.     printf("link = 0x%lx, port = 0x%lx, type = 0x%lx\n",
  1043.         fh->fh_Link,fh->fh_Port,fh->fh_Type);
  1044.     printf("buf = 0x%lx, pos = %ld, end = %ld\n",
  1045.         fh->fh_Buf,fh->fh_Pos,fh->fh_End);
  1046.     printf("func1 = 0x%lx, func2 = 0x%lx, func3 = 0x%lx\n",
  1047.         fh->fh_Func1,fh->fh_Func2,fh->fh_Func3);
  1048.     printf("fh_Arg1 = 0x%lx, fh-Arg2 = 0x%lx\n",
  1049.         fh->fh_Arg1,fh->fh_Arg2);
  1050. }
  1051.  
  1052. void
  1053. dumplock (int *done)
  1054. {
  1055.     long num;
  1056.     struct FileLock *lock;
  1057.  
  1058.     if (!getnum(&num))
  1059.     {
  1060.         printf("error: use dumplock <locknumber>\n");
  1061.         return;
  1062.     }
  1063.  
  1064.     lock = (void *) BADDR(locks[num]);
  1065.     if (!lock)
  1066.     {
  1067.         printf("lock on boot fs root (NULL)\n");
  1068.         return;
  1069.     }
  1070.     printf("link = 0x%lx, key = 0x%lx, access = 0x%lx\n",
  1071.         lock->fl_Link,lock->fl_Key,lock->fl_Access);
  1072.     printf("port = 0x%lx, volume = %ld (%s)\n",
  1073.         lock->fl_Task,lock->fl_Volume, (lock->fl_Volume ?
  1074.             ((char *) BADDR(((struct DosList *)
  1075.                     BADDR(lock->fl_Volume))->dol_Name))+1 :
  1076.             ""));
  1077. }
  1078.  
  1079. void
  1080. samelock (int *done)
  1081. {
  1082.     LONG rc;
  1083.     BPTR lock1,lock2;
  1084.  
  1085.     if (!get2nums(&lock1,&lock2))
  1086.     {
  1087.         printf("error: use samelock <lock1> <lock2>\n");
  1088.         return;
  1089.     }
  1090.  
  1091.     rc = SameLock(locks[lock1],locks[lock2]);
  1092.  
  1093.     printf("SameLock returned %ld\n",rc);
  1094. }
  1095.  
  1096. void
  1097. changemode (int *done)
  1098. {
  1099.     LONG rc,type,num,mode;
  1100.     char *str;
  1101.  
  1102.     str = strtok(NULL," ");
  1103.     if (!str)
  1104.     {
  1105. err:        printf("error: use ChangeMode FH|Lock <number> <newmode>\n");
  1106.         return;
  1107.     }
  1108.  
  1109.     if (stricmp(str,"fh") == SAME)
  1110.         type = CHANGE_FH;
  1111.     else if (stricmp(str,"lock") == SAME)
  1112.         type = CHANGE_LOCK;
  1113.     else {
  1114.         printf("'%s' is not a valid type\n",str);
  1115.         goto err;
  1116.     }
  1117.  
  1118.     if (!get2nums(&num,&mode))
  1119.         goto err;
  1120.  
  1121.     rc = ChangeMode(type,type == CHANGE_FH ? files[num] : locks[num],
  1122.             mode);
  1123.  
  1124.     printf("ChangeMode returned %ld, ioerr = %ld\n",rc,IoErr());
  1125. }
  1126.  
  1127. void
  1128. execute (int *done)
  1129. {
  1130.     char *command;
  1131.     long input,output;
  1132.     long rc;
  1133.     char buf[256];
  1134.  
  1135.     command = strtok(NULL," ");
  1136.     if (!command)
  1137.     {
  1138. err:        printf("error: use execute <command> <input fh> <output fh>\n");
  1139.         return;
  1140.     }
  1141.     if (*command == '"')
  1142.     {
  1143.         /* strip quotes */
  1144.         command++;
  1145.         if (command[strlen(command)-1] == '"')
  1146.         {
  1147.             command[strlen(command)-1] = 0;
  1148.         } else {
  1149.             strcpy(buf,command);
  1150.             do {
  1151.                 command = strtok(NULL," ");
  1152.                 if (!command)
  1153.                     goto err;
  1154.                 strcat(buf,command);
  1155.             } while (command[strlen(command)-1] != '"');
  1156.             buf[strlen(buf)-1] = 0;
  1157.             command = buf;
  1158.         }
  1159.     }
  1160.  
  1161.     if (!get2nums(&input,&output))
  1162.         goto err;
  1163.  
  1164.     if (input == -1)
  1165.         input = NULL;
  1166.     else
  1167.         input = files[input];
  1168.  
  1169.     if (output == -1)
  1170.         output = NULL;
  1171.     else
  1172.         output = files[output];
  1173.  
  1174.     printf("Execute(\"%s\",0x%lx,0x%lx)\n",command,input,output);
  1175.  
  1176.     rc = Execute(command,input,output);
  1177.  
  1178.     printf("Execute returned %ld (ioerr = %ld)\n",rc,IoErr());
  1179. }
  1180.  
  1181. void
  1182. deletefile (int *done)
  1183. {
  1184.     LONG rc;
  1185.  
  1186.     rc = DeleteFile(strtok(NULL," "));
  1187.  
  1188.     printf("DeleteFile returned %ld, ioerr = %ld\n",rc,IoErr());
  1189. }
  1190.  
  1191. void
  1192. splitname (int *done)
  1193. {
  1194.     char *string,*separator;
  1195.     unsigned char *buf;
  1196.     LONG size,pos,offset;
  1197.  
  1198.     string = strtok(NULL," ");
  1199.     if (!string)
  1200.     {
  1201. err:    printf("usage: SplitName <string> <'char'> <offset> <old pos> <size>\n");
  1202.     return;
  1203.     }
  1204.  
  1205.     separator = strtok(NULL," ");
  1206.     if (!separator)
  1207.         goto err;
  1208.  
  1209.     if (!getnum(&offset))
  1210.         goto err;
  1211.  
  1212.     buf = &(buffer[offset]);
  1213.  
  1214.     if (!getnum(&pos))
  1215.         goto err;
  1216.  
  1217.     if (!getnum(&size))
  1218.         goto err;
  1219.  
  1220.     printf("SplitName(%s,%lc,0x%lx,%ld,%ld)\n",
  1221.         string,*separator,buf,pos,size);
  1222.     pos = SplitName(string,*separator,buf,(UWORD) pos,size);
  1223.     printf("  returned %ld (ioerr = %ld)\n",pos,IoErr());
  1224.  
  1225. }
  1226.  
  1227. void
  1228. getvar (int *done)
  1229. {
  1230.     char *name;
  1231.     LONG offset,size,flags,len;
  1232.  
  1233.     name = strtok(NULL," ");
  1234.     if (!name)
  1235.     {
  1236. err:        printf("usage: GetVar <name> <offset> <size> <flags>\n");
  1237.         return;
  1238.     }
  1239.  
  1240.     if (!get3nums(&offset,&size,&flags))
  1241.         goto err;
  1242.  
  1243.     len = GetVar(name,&(buffer[offset]),size,flags);
  1244.     printf("GetVar returned %ld (string = %s)\n",len,&(buffer[offset]));
  1245. }
  1246.  
  1247. void
  1248. setvar (int *done)
  1249. {
  1250.     char *name;
  1251.     LONG offset,size,flags,rc;
  1252.  
  1253.     name = strtok(NULL," ");
  1254.     if (!name)
  1255.     {
  1256. err:        printf("usage: SetVar <name> <offset> <size> <flags>\n");
  1257.         return;
  1258.     }
  1259.  
  1260.     if (!get3nums(&offset,&size,&flags))
  1261.         goto err;
  1262.  
  1263.     rc = SetVar(name,&(buffer[offset]),size,flags);
  1264.     if (!rc)
  1265.         printf("SetVar failed!  (IoErr = %ld)\n",IoErr());
  1266. }
  1267.  
  1268. void
  1269. findvar (int *done)
  1270. {
  1271.     char *name;
  1272.     LONG type;
  1273.     struct LocalVar *lv;
  1274.  
  1275.     name = strtok(NULL," ");
  1276.     if (!name)
  1277.     {
  1278. err:        printf("usage: FindVar <name> <type>\n");
  1279.         return;
  1280.     }
  1281.  
  1282.     if (!getnum(&type))
  1283.         goto err;
  1284.  
  1285.     lv = FindVar(name,type);
  1286.     printf("FindVar(%s,%ld) returned 0x%lx\n",name,type,lv);
  1287. }
  1288.  
  1289. void
  1290. namefromlock (int *done)
  1291. {
  1292.     unsigned char *buf;
  1293.     LONG size,num,offset,rc;
  1294.  
  1295.     if (!getnum(&num))
  1296.     {
  1297. err:    printf("usage: NameFromLock <lock> <offset> <length>\n");
  1298.     return;
  1299.     }
  1300.  
  1301.     if (!getnum(&offset))
  1302.         goto err;
  1303.  
  1304.     buf = &(buffer[offset]);
  1305.  
  1306.     if (!getnum(&size))
  1307.         goto err;
  1308.  
  1309.     rc = NameFromLock(locks[num],buf,size);
  1310.     printf("NameFromLock returned %ld (ioerr = %ld)\n",rc,IoErr());
  1311.  
  1312.     if (rc)
  1313.         printf("\t\"%s\"\n",buf);
  1314. }
  1315.  
  1316. void
  1317. namefromfh (int *done)
  1318. {
  1319.     unsigned char *buf;
  1320.     LONG size,num,offset,rc;
  1321.  
  1322.     if (!getnum(&num))
  1323.     {
  1324. err:    printf("usage: NameFromFH <file> <offset> <length>\n");
  1325.     return;
  1326.     }
  1327.  
  1328.     if (!getnum(&offset))
  1329.         goto err;
  1330.  
  1331.     buf = &(buffer[offset]);
  1332.  
  1333.     if (!getnum(&size))
  1334.         goto err;
  1335.  
  1336.     rc = NameFromFH(files[num],buf,size);
  1337.     printf("NameFromFH returned %ld (ioerr = %ld)\n",rc,IoErr());
  1338.  
  1339.     if (rc)
  1340.         printf("\t\"%s\"\n",buf);
  1341. }
  1342.  
  1343. void
  1344. parsepattern (int *done)
  1345. {
  1346.     char *pattern;
  1347.     char *dest;
  1348.     LONG offset,len,rc;
  1349.  
  1350.     pattern = strtok(NULL," ");
  1351.     if (!pattern)
  1352.     {
  1353. err:        printf("usage: ParsePattern <pattern> <offset> <len>\n");
  1354.         return;
  1355.     }
  1356.  
  1357.     if (!get2nums(&offset,&len))
  1358.         goto err;
  1359.  
  1360.     dest = &(buffer[offset]);
  1361.  
  1362.     rc = ParsePattern(pattern,dest,len);
  1363.  
  1364.     printf("ParsePattern returned %ld\n",rc);
  1365. }
  1366.  
  1367. void
  1368. matchpattern (int *done)
  1369. {
  1370.     char *pattern;
  1371.     char *string;
  1372.     LONG offset,rc;
  1373.  
  1374.     if (!getnum(&offset))
  1375.         goto err;
  1376.  
  1377.     string = strtok(NULL," ");
  1378.     if (!string)
  1379.     {
  1380. err:        printf("usage: MatchPattern <offset> <string>\n");
  1381.         return;
  1382.     }
  1383.  
  1384.     pattern = &(buffer[offset]);
  1385.  
  1386.     rc = MatchPattern(pattern,string);
  1387.  
  1388.     printf("MatchPattern returned %ld\n",rc);
  1389. }
  1390.  
  1391. void
  1392. parsepatternnocase (int *done)
  1393. {
  1394.     char *pattern;
  1395.     char *dest;
  1396.     LONG offset,len,rc;
  1397.  
  1398.     pattern = strtok(NULL," ");
  1399.     if (!pattern)
  1400.     {
  1401. err:        printf("usage: ParsePatternNoCase <pattern> <offset> <len>\n");
  1402.         return;
  1403.     }
  1404.  
  1405.     if (!get2nums(&offset,&len))
  1406.         goto err;
  1407.  
  1408.     dest = &(buffer[offset]);
  1409.  
  1410.     rc = ParsePatternNoCase(pattern,dest,len);
  1411.  
  1412.     printf("ParsePatternNoCase returned %ld\n",rc);
  1413. }
  1414.  
  1415. void
  1416. matchpatternnocase (int *done)
  1417. {
  1418.     char *pattern;
  1419.     char *string;
  1420.     LONG offset,rc;
  1421.  
  1422.     if (!getnum(&offset))
  1423.         goto err;
  1424.  
  1425.     string = strtok(NULL," ");
  1426.     if (!string)
  1427.     {
  1428. err:        printf("usage: MatchPatternNoCase <offset> <string>\n");
  1429.         return;
  1430.     }
  1431.  
  1432.     pattern = &(buffer[offset]);
  1433.  
  1434.     rc = MatchPatternNoCase(pattern,string);
  1435.  
  1436.     printf("MatchPatternNoCase returned %ld\n",rc);
  1437. }
  1438.  
  1439. void
  1440. isfilesystem (int *done)
  1441. {
  1442.     char *string;
  1443.     LONG rc;
  1444.  
  1445.     string = strtok(NULL," ");
  1446.     if (!string)
  1447.     {
  1448.         printf("usage: IsFileSystem <device name>:\n");
  1449.         return;
  1450.     }
  1451.  
  1452.     rc = IsFileSystem(string);
  1453.  
  1454.     printf("IsFileSystem returned %ld\n",rc);
  1455. }
  1456.  
  1457. void
  1458. loadseg (int *done)
  1459. {
  1460.     BPTR seg;
  1461.     char *string;
  1462.  
  1463.     string = strtok(NULL," ");
  1464.     if (!string)
  1465.     {
  1466.         printf("usage: LoadSeg <file>\n");
  1467.         return;
  1468.     }
  1469.  
  1470.     seg = LoadSeg(string);
  1471.     printf("LoadSeg returned 0x%lx, ioerr = %ld\n",seg,IoErr());
  1472. }
  1473.  
  1474. void
  1475. unloadseg (int *done)
  1476. {
  1477.     BPTR seg;
  1478.     char *string;
  1479.  
  1480.     string = strtok(NULL," ");
  1481.     if (!string)
  1482.     {
  1483. err:        printf("usage: UnLoadSeg 0x<addr>\n");
  1484.         return;
  1485.     }
  1486.  
  1487.     if (*string++ != '0' || *string++ != 'x')
  1488.         goto err;
  1489.  
  1490.     if (stch_l(string,&seg) != strlen(string))
  1491.         goto err;
  1492.  
  1493.     UnLoadSeg(seg);
  1494.  
  1495.     printf("unloaded 0x%lx\n",seg);
  1496. }
  1497.