home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / C80TCOG.ZIP / ALLDIR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-08  |  4.5 KB  |  217 lines

  1. /*    alldir.c - dump all CP/M directory entries        */
  2. /*                    1982/08/17 19:38    */
  3. /*
  4.     Inspired by the assembly language routine ALLDIR in
  5. David E. Cortesi's INSIDE CP/M: A Guide for Users and Programmers,
  6. Holt, Rinehart and Winston, New York 1982, p. 226-228.
  7.  
  8. C version:
  9.  
  10.     Copyright 1982    William G. Hutchison, Jr.
  11.             P.O. Box 278
  12.             Exton, PA 19341-0278
  13.             U.S.A.
  14.  
  15.             CompuServe 70665,1307
  16.  
  17.  
  18.  
  19.  
  20.     This  program  may be used freely for any non-commercial
  21. purpose, provided that the user does  not  remove  or  alter
  22. this notice or the copyright statement.
  23.     Those  who  wish  to  sell  or lease this program, or to
  24. incorporate it into a product  for  sale  or  lease,  should
  25. apply to the author (above) for licensing information.
  26.     This  program  is  not  covered  by  a  warranty, either
  27. express or implied. The author shall not be responsible  for
  28. any  damages (including consequential) caused by reliance on
  29. the  materials  presented,  including  but  not  limited  to
  30. typographical errors or arithmetic errors.
  31.  
  32.     This version is for C/80 Version 2 from Software Toolworks.
  33.  
  34.  
  35.  */
  36.  
  37. #define ALLOC_ERR -1
  38. #define ASCII
  39. #define begin {
  40. #define Boolean char
  41. #define close fclose
  42. #define CP_M
  43. #define end ;}
  44. #define EOF -1
  45. #define EOS '\0'
  46. #define ERR 0
  47. #define ESCAPE '\\'
  48. #define FILE int
  49. #define NEWLINE '\n'
  50. #define NIL 0
  51. #define NO 0
  52. #define NULL '\0'
  53. #define open fopen
  54. #define READ "r"
  55. #define READ_BINARY "rb"
  56. #define STDERR erfd
  57. #define STDIN fin
  58. #define STDOUT fout
  59. #define then
  60. #define UPDATE "u"
  61. #define UPDATE_BINARY "ub"
  62. #define WRITE "w"
  63. #define WRITE_BINARY "wb"
  64. #define YES 1
  65. #define ZAP_SCREEN '\32'
  66. #define DEFAULT_DRIVE 25
  67. #define SELECT_DRIVE 14
  68. #define SRCH_FIRST 17
  69. #define SRCH_NEXT 18
  70.  
  71. #include "printf.c"
  72.  
  73. /*    define the number of Bytes Per Line to be dumped:    */
  74. #define BPL 16
  75.  
  76. struct dir {            /* CP/M directory entry        */
  77.     char user;
  78.     char filename[8];
  79.     char type[3];
  80.     char extent;
  81.     char s1, s2, rec_count;
  82.     char map[16];
  83.     };
  84.  
  85. #define DIR_SIZE sizeof(struct dir)
  86.  
  87. struct fcb {            /* CP/M FCB entry        */
  88.     char drv;
  89.     char filename[8];
  90.     char type[3];
  91.     char extent;
  92.     char s1, s2, rec_count;
  93.     char map[16];
  94.     char rec, d1, d2, d3;
  95.     };
  96.  
  97. #define FCB_SIZE sizeof(struct fcb)
  98.  
  99.  
  100. static struct fcb *cpmfcb= 0x5C;
  101. static struct dir *cpmbuff=0x80;
  102.  
  103. main()
  104. {
  105. int drive, place;
  106. struct fcb *locfcb;
  107.  
  108. if    ((drive= cpmfcb->drv) == 0)
  109.     drive= bdos(DEFAULT_DRIVE, 0);
  110. else
  111.     drive--            /* 1..26 => 0..25        */;
  112.  
  113. init_fcb(locfcb= alloc(FCB_SIZE), '?', "????????", "???", '?');
  114.  
  115. printf("All directory entries, drive %c.\n", 'A'+drive);
  116. bdos(SELECT_DRIVE, drive);
  117.  
  118. place= bdos(SRCH_FIRST, locfcb) & 0xFF;
  119. while    (place != 0xFF) {
  120.     printf("\n");
  121.     hex_dump(cpmbuff + place, DIR_SIZE);
  122.     place= bdos(SRCH_NEXT, locfcb) & 0xFF;
  123.     }
  124. }                /* end of main            */
  125.  
  126. hex_dump(base, len)
  127. char *base;
  128. int len;
  129. {
  130. char *p;
  131. int i;
  132.  
  133. for    (; len > 0; len-= BPL, base+= BPL) {
  134. /*
  135.     printf("%4x:", (unsigned)base);
  136.  */
  137.     for    (p= base, i= (len > BPL)? BPL : len; i > 0; i--)
  138.         printf(" %2x", peek(p++));
  139.     for    (i= BPL-len; i > 0; i--)
  140.         printf("   ");
  141.     printf("  ");
  142.     for    (p= base, i= (len > BPL)? BPL : len; i > 0; i--)
  143.         printf("%c", printable(*p++));
  144.     printf("\n");
  145.     }
  146. }                /* end of hex_dump        */
  147.  
  148. init_fcb(f, d, fn, ft, x)    /* initialize an FCB        */
  149. struct fcb *f;
  150. char d, *fn, *ft, x;
  151. {
  152. int i;
  153.  
  154. f->drv= d;
  155. strmove(fn, f->filename);
  156. strmove(ft, f->type);
  157. f->extent= x;
  158. f->s1= f->s2= f->rec_count= 0;
  159. for    (i= 16-1; i >= 0; i--)
  160.     f->map[i]= 0;
  161. }                /* end of init_fcb        */
  162.  
  163. peek(p)
  164. char *p;
  165. {return    (0xFF & *p);}
  166.  
  167. #ifdef ASCII
  168. printable(c)
  169. char c;
  170. {
  171. return    (32 <= c && c <= 127? c : '.');
  172. }
  173. #endif
  174.  
  175. strmove(from, to)        /* move string but not '\0'    */
  176. char   *from,*to;
  177. {
  178. while    (*from)
  179.     *to++= *from++;
  180. }                /* end of strmove        */
  181.  
  182. bdos(fcn, arg)            /* do BDOS function fcn with arg */
  183. unsigned fcn, arg;
  184. {
  185. arg;
  186. #asm
  187.     MOV    D,H
  188.     MOV    E,L
  189. #endasm
  190. fcn;
  191. #asm
  192.     MOV    C,L
  193.     CALL    5        ;CALL BDOS
  194.     PUSH    PSW        ;SAVE VALUE IF RETURNED IN A
  195.     PUSH    H        ;SAVE VALUE IF RETURNED IN HL
  196. #endasm
  197. switch    (fcn) {            /* is value already in HL?    */
  198.     case 12:
  199.     case 24:
  200.     case 27:
  201.     case 29:
  202.     case 31:
  203. #asm
  204.     POP    H        ;PUT VALUE BACK IN HL
  205.     POP    B        ;DISCARD VALUE FROM A
  206.     RET            ;AND SILENTLY STEAL AWAY
  207. #endasm
  208.     default:
  209. #asm
  210.     POP    H        ;DISCARD VALUE FROM HL
  211.     POP    PSW        ;RECOVER VALUE FROM A
  212.     MOV    L,A        ;PUT IT INTO HL
  213.     MVI    H,0
  214. #endasm
  215.     }
  216. }                /* end of bdos            */
  217.