home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 195_01 / _d.c < prev    next >
Text File  |  1987-10-05  |  4KB  |  217 lines

  1. /* [@D.C of JUGPDS Vol.18]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. /* @D - a directory program */
  15.  
  16. #include <stdio.h>
  17.  
  18. struct tnode {
  19.     char  *filename;
  20.     int   fs;
  21.     struct tnode *left;
  22.     struct tnode *right;
  23. };
  24.  
  25. int   bls, spt, sector, off, sectcnt, fcnt;
  26. int   *sectran, *dph;
  27. char  *dpb;
  28.  
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char     *argv[];
  33.  
  34. {
  35.     char name[14], *p, *pp, *adrive, *aucode, *dfname, *fn;
  36.     char cdrive, cucode, buf[128], *cindex();
  37.     struct tnode *root, *tree();
  38.     int    i, n, blm, fsize;
  39.  
  40.     _allocp = NULL;
  41.  
  42.     fcnt = 0;
  43.     adrive = &name[0];
  44.     aucode = &name[1];
  45.     dfname = &name[2];
  46.     name[13] = '\0';
  47.     p = dfname;
  48.     for (i = 2; i < 13; i++) *p++ = '?';
  49.     *adrive = cdrive = bdos(25,0);
  50.     *aucode = cucode = bdos(32,255) + 1;
  51.  
  52.     if (argc > 1) {
  53.         p = argv[1];
  54.         if ((pp = cindex(p, '/')) != -1) {
  55.             *aucode = user(p);
  56.             p = pp + 1;
  57.             }
  58.         if ((pp = cindex(p, ':')) != -1) {
  59.             if (*p >= 'A' && *p <= 'P')
  60.                 *adrive = *p - 'A';
  61.             p = pp + 1;
  62.             }
  63.         pp = &name[2];
  64.         if (*p != '\0') {
  65.             for (i = 1; i <= 8; i++)
  66.                 if (*p == '\0' || *p == '.')
  67.                     *pp++ = ' ';
  68.                 else if (*p == '*')
  69.                     *pp++ = '?';
  70.                 else
  71.                     *pp++ = *p++;
  72.             }
  73.         pp = &name[10];
  74.         if (*p == '*')
  75.             p++;
  76.         if (*p == '.')
  77.             p++;
  78.         if (*p != '\0') {
  79.             for (i = 9; i <= 11; i++)
  80.                 if (*p == '\0')
  81.                     *pp++ = ' ';
  82.                 else if (*p == '*')
  83.                     *pp++ = '?';
  84.                 else
  85.                     *pp++ = *p++;
  86.             }
  87.         }
  88.     dph = biosh( 9,*adrive);
  89.     sectran = *dph;
  90.     dpb = *(dph + 5);
  91.     spt = *dpb;
  92.     sectcnt = *(dpb + 7) + *(dpb + 8) * 256 + 1;
  93.     sectcnt /= 4;
  94.     blm = *(dpb + 3);
  95.     bls = (blm + 1) / 8;
  96.     off = *(dpb + 13) + *(dpb+14) * 256;
  97.     root = NULL;
  98.     for (sector = 0; sector < sectcnt; sector++) {
  99.         bios(10, off+sector/spt);
  100.         bios(11, biosh(16, sector%spt, sectran));
  101.         bios(12, buf);
  102.         if (bios(13) == 0)
  103.             for (i = 0; i < 4; i++) {
  104.                 fn = &buf[32*i];
  105.                 fsize = *(fn +12) * 128 + *(fn + 15);
  106.                 *(fn + 12) = '\0';
  107.                 if (*fn < 0 || *fn > 15)
  108.                     continue;
  109.                 (*fn)++;
  110.                 if (!compafn(aucode, fn))
  111.                     continue;
  112.                 if ((root = tree(root, fn, fsize)) == NULL)
  113.                     error("alloc over flow.");
  114.                 }
  115.         }
  116.     *adrive += 'A';
  117.     treeprint(root);
  118.     bdos(14, cdrive);
  119.     bdos(32, cucode-1);
  120. }
  121.  
  122.  
  123. struct tnode *tree(p, fname, fsize)
  124. struct tnode *p;
  125. char *fname;
  126. {
  127.     struct tnode *talloc();
  128.     char *strsave();
  129.     int  cond;
  130.  
  131.     if (p == NULL) {
  132.         if ((p = talloc()) == NULL)
  133.             return NULL;
  134.         else if ((p->filename = strsave(fname)) == NULL)
  135.             return NULL;
  136.         else {
  137.             p->fs = fsize;
  138.             p->left = p->right = NULL;
  139.             }
  140.         }
  141.     else if ((cond = strcmp(fname, p->filename)) == 0)
  142.         p->fs = max(p->fs, fsize);
  143.     else if (cond < 0)
  144.         p->left = tree(p->left, fname, fsize);
  145.     else
  146.         p->right = tree(p->right, fname, fsize);
  147.     return(p);
  148. }
  149.  
  150.  
  151. treeprint(p)
  152. struct tnode *p;
  153.  
  154. {
  155.     if (p != NULL) {
  156.         treeprint(p->left);
  157.         p->fs = (p->fs -1 ) / 8 + 1;
  158.         p->fs = ((p->fs -1 ) / bls + 1) * bls;
  159.         if (++fcnt%4)
  160.             printf("%2d/%11s%4d |",
  161.                  --(*p->filename), p->filename+1, p->fs);
  162.         else
  163.             printf("%2d/%11s%4d\n",
  164.                  --(*p->filename), p->filename+1, p->fs);
  165.         treeprint(p->right);
  166.     }
  167. }
  168.  
  169.  
  170. struct tnode *talloc()
  171. {
  172.     char *alloc();
  173.     struct tnode *p;
  174.  
  175.     return(p = alloc(sizeof(*p)));
  176. }
  177.  
  178.  
  179. user(s)
  180. char *s;
  181.  
  182. {
  183.     int n;
  184.  
  185.     n = 0;
  186.     if (isdigit(*s))
  187.         while (isdigit(*s))
  188.             n = n * 10 + *s++ - '0';
  189.     else if (*s == '?')
  190.         return ('?');
  191.     return (n+1);
  192. }
  193.  
  194.  
  195. char *cindex(s, c)
  196. char *c, *s;
  197.     while (*s) {
  198.         if (toupper(*s) == c)
  199.             return s;
  200.         s++;
  201.         }
  202.     return (-1);
  203. }
  204.  
  205.  
  206. compafn(s,t)
  207. char *s, *t;
  208.  
  209. {
  210.     while ((*s == *t || *s == '?') && *s != '\0') {
  211.         s++;
  212.         t++;
  213.         }
  214.     return (*s == '\0');
  215. }
  216.