home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / JSAGE / ZSUS / PROGPACK / CFORZ02.LBR / CDIR.CZ / CDIR.C
Text File  |  2000-06-30  |  3KB  |  132 lines

  1. /* CDIR
  2.    Example of low level dos access with BDS C.  Prints a sorted directory
  3.    with file sizes to the nearest K.  This version is for ZCPR systems
  4.    and will accept DIR: specifications.  You must have BDSZ to use this!
  5.    You need to link this with the command:
  6.    clink cdir -f z3func05 z3func03 z3func01 sfn zsutil cvlib itoa -n
  7. */
  8.  
  9. #include    <STDIO.H>
  10.  
  11. #define DIRSTO 14    /* Size of storage for each dir entry */
  12. #define NDIR 1024    /* Maximum number of directory entries */
  13.  
  14. char *fnbuf, *bufptr;
  15. int numfn;
  16.  
  17. main(argc,argv)
  18.     int    argc;
  19.     char    *argv[];
  20. {
  21.     char *alloc(), *fn2as(), *dir2a();
  22.     int  fcomp();
  23.     void    storefn();
  24.  
  25.     char *fnptr, *fcb, *p, tempd[13], tempn[13];
  26.     int    i, user, crow, ccol, nrow, ncol;
  27.  
  28.     /* initialize variables */
  29.  
  30.     fcb = 0x5c;    /* use default fcb */ 
  31.     fnbuf = bufptr = alloc(DIRSTO*NDIR);    /* create buffer for files */
  32.     numfn = 0;
  33.  
  34.     iobreak(0);
  35.     printf("CDIR Version 0.2\n");
  36.  
  37.     if(!fnbuf) {
  38.         printf("*** Not enough memory!\n");
  39.         exit(-1);
  40.     }
  41.  
  42.     /* Parse file spec into fcb */
  43.  
  44.     if(zsetfcb(fcb, (argc <= 1 ? "*.*": argv[1]), &user) == ERROR)
  45.         printf("*** Illegal filename!\n");
  46.     else {
  47.         /* Test for du: only and wildcard if needed */
  48.  
  49.         if(fcb[1] == ' ' && fcb[9] == ' ')
  50.             for(i=1;i<12;fcb[i++]='?');
  51.  
  52.         /* print header */
  53.  
  54.         printf("Directory for %s:%s\n",dir2a(getfdu(fcb),tempd),fn2as(fcb,tempn));
  55.  
  56.         fcb[12] = fcb[14] = '?';    /* get all extents */
  57.  
  58.         /* read dir, sort into alpha order by name */
  59.  
  60.         i = getdir(fcb, storefn);
  61.         qsort(fnbuf,numfn,DIRSTO,fcomp);
  62.  
  63.         /* eliminate all but the last extent of a file */
  64.  
  65.         for(p=fnbuf,i=1;i<numfn;) {
  66.             if(ncomp(p,p+DIRSTO,12) == 0) {
  67.                 movmem(p+DIRSTO,p,(numfn-i)*DIRSTO);
  68.                 --numfn;
  69.             } else {
  70.                 p+=DIRSTO;
  71.                 ++i;
  72.             }
  73.         }
  74.  
  75.         /* Display names and sizes, 4 per row */
  76.  
  77.         nrow = ckrow(255);        /* get number of rows from env */
  78.         ncol = ckcol(255);        /* get number of cols from env */
  79.         ncol /= 20;        /* number of names per line */
  80.         crow = ccol = 1;
  81.         for(p=fnbuf,i=0;i<numfn;++i,p+=DIRSTO) {
  82.             if(--ccol == 0) {
  83.                 if(++crow == nrow-1) {
  84.                     getchar();    /* page break */
  85.                     crow = 1;    /* reset counter */
  86.                 }
  87.                 printf("\n ");
  88.                 ccol = ncol;
  89.             } else {
  90.                 printf(" | ");
  91.             }
  92.             printf("%s%4dk",fn2as(p,tempn),(p[12] << 8)+p[13]);
  93.         }
  94.         printf("\n%d files matched\n",numfn);
  95.     }
  96. }
  97.  
  98. /* Dir manipulation routine passed to getdir.  Copies the user and filename
  99.    to the buffer, then computes the file size as if this entry represents
  100.    the last extent of the file.  The size in k is stored in REVERSE order
  101.    so that when the entries are sorted, the extent with the largest size
  102.    is LAST.  After sorting, all but the last entry will be removed from
  103.    the buffer.  Note this is the virtual size of the file.
  104. */ 
  105.  
  106. void storefn(dir)
  107.     char *dir;
  108. {
  109.     int i;
  110.  
  111.     if(*dir < 0x20) {
  112.         for(i=0 ; i < 12; ++i)
  113.             bufptr[i] = dir[i];
  114.         i = fsize(dir);
  115.         bufptr[12] = i >> 8;    /* msb */
  116.         bufptr[13] = i & 0xff;    /* lsb */
  117.         if(numfn < NDIR) {
  118.             ++numfn;
  119.             bufptr += DIRSTO;
  120.         }
  121.     }
  122. }
  123.  
  124. /* file name compare routine for qsort */
  125.  
  126. int fcomp(x,y)
  127.     char *x,*y;
  128. {
  129.     return(ncomp(x,y,DIRSTO));
  130. }
  131.  
  132.