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 / ENTERPRS / CPM / TERMS / ZMP-SRC.LZH / ZMTERM3.C < prev    next >
Text File  |  2000-06-30  |  6KB  |  275 lines

  1. /*            TERM module File 3                */
  2.  
  3. #include "zmp.h"
  4.  
  5. #ifdef   AZTEC_C
  6. #include "libc.h"
  7. #else
  8. #include <stdio.h>
  9. #endif
  10.  
  11.  
  12. static short Lines, Entries;
  13.  
  14. directory()
  15. {
  16.     short factor, cpm3;
  17.     long *lp = (long *) CPMBUF;
  18.     unsigned i, dtotal, atotal, allen, remaining, bls, dirbufsize;
  19.     char *grabmem(), *alloca, *p, *dirbuf;
  20.     struct dpb *thedpb;
  21.  
  22.     cls();
  23.     sprintf(Buf,"Directory for Drive %c%d:",Currdrive,Curruser);
  24.     putlabel(Buf);
  25.     bdos(SETDMA,CPMBUF);         /* set dma address */
  26.     cpm3 = (bdoshl(12) >= 0x30);    /* get cp/m version (BCD) */
  27.     dirbuf = grabmem(&dirbufsize);
  28.     if (dirbuf != (char *) MEMORY_FULL)
  29.         sorted_dir(dirbuf,dirbufsize);
  30.     else            /* not enough room */
  31.         unsort_dir();    /* do unsorted directory */
  32.  
  33.         /* Now print free space on disk */
  34.     if (cpm3) {    /* cp/m 3 */
  35.         bdos(46,Currdrive - 'A');
  36.         p = (char *) (CPMBUF + 3);
  37.         *p = 0;    /* clear hi byte for long */
  38.         remaining = (short) (*lp / 8L);
  39.     }
  40.     else {        /* cp/m 2.2 */
  41.         thedpb = (struct dpb *) bdoshl(GETDPB,NULL); /* fill dpb */
  42.         alloca = (char *) bdoshl(GETALL,NULL);
  43.         bls = 0x0001;
  44.         bls <<= thedpb->bsh + 7;
  45.         factor = bls/1024;
  46.         dtotal = factor * (thedpb->dsm+1);
  47.         allen = (thedpb->dsm / 8) + 1;
  48.         for (atotal = i = 0; i < allen; i++)
  49.             atotal += cntbits(*(alloca + i));
  50.         atotal *= factor;
  51.         remaining = dtotal - atotal;
  52.     }
  53.     if (Lines >= DIRLINES - 1)
  54.         domore();
  55.     if (Entries)
  56.         printf("\n\t%d",Entries);
  57.     else
  58.         printf("\n\tNo");
  59.     printf(" File(s).\t\t");
  60.     printf("Space remaining on %c:  %dk\n",Currdrive,remaining);
  61. }
  62.  
  63. /* Do sorted directory with filesizes */
  64. sorted_dir(dirbuf,dirbufsize)
  65. unsigned char *dirbuf;
  66. unsigned dirbufsize;
  67.  
  68. {
  69.     short count, limit, dirsort(), dircode, ksize, i;
  70.     unsigned size;
  71.     char filename[15];
  72.     unsigned char *p, *q;
  73.     struct sortentry *se;
  74.     struct fcb srcfcb;
  75.  
  76.     q = dirbuf;
  77.     Lines = 2;
  78.     memset(srcfcb.filename,'?',14);    /* all filenames, all extents */
  79.     limit = dirbufsize / sizeof(struct sortentry); /* how many */
  80.     dircode = bdos(17,&srcfcb);    /* search first */
  81.     for (count = 0; dircode != -1; count++) {
  82.         p = ((unsigned char *) CPMBUF + dircode * 32);
  83.         for (i = 0; i < 16; i++)
  84.             *q++ = *p++ & ((i > 0 && i < 12)
  85.                 ? 0x7f : 0xff);
  86.         if (count == limit) { /* can't fit them in */
  87.             free(dirbuf);
  88.             unsort_dir();    /* do unsorted directory */
  89.             return;
  90.         }
  91.         dircode = bdos(18,&srcfcb);    /* search next */
  92.     }
  93.     qsort(dirbuf,count,16,dirsort);    /* sort in alpha order */
  94.  
  95.         /* ok, now print them all */
  96.     se = (struct sortentry *) dirbuf;
  97.     for (i = Entries = 0; i < count; i++) {
  98.         if (!i || memcmp(se, se - 1, 12)) {
  99.             size = se->rc + (se->s2 * 32
  100.                 + se->ex) * 128;
  101.             ksize = (size / 8) + ((size % 8) ? 1 : 0);
  102.             memcpy(filename,se->name,8);
  103.             filename[8] = '.';
  104.             memcpy(filename + 9,se->type,3);
  105.             filename[12] = '\0';
  106.             printf("%s%4dk",filename,ksize);
  107.             if (printsep(SORTCOLS))
  108.                 break;
  109.             Entries++;
  110.         }
  111.         se++;
  112.     }
  113.     free(dirbuf);
  114.     if (Entries % SORTCOLS)
  115.         printf("\n");
  116. }
  117.  
  118. /* Do unsorted directory */
  119. unsort_dir()
  120. {
  121.     short dircode, i;
  122.     struct direntry *dp;
  123.  
  124.     Lines = 2;
  125.     dircode = getfirst("????????.???");
  126.     for (Entries = 0; dircode != 0xff; Entries++) {
  127.         dp = (struct direntry *) (CPMBUF + dircode * 32);
  128.         memcpy(Pathname,dp->flname,8);
  129.         Pathname[8]= '.';
  130.         memcpy(Pathname+9,dp->ftype,3);
  131.         Pathname[12] = '\0';
  132.         for (i = 0; i < 11; i++)    /* remove attributes */
  133.             Pathname[i] = Pathname[i] & 0x7f;
  134.         printf("%s",Pathname);
  135.         if (printsep(UNSORTCOLS))
  136.             break;
  137.         dircode = getnext();
  138.     }
  139.     if (Entries % UNSORTCOLS)
  140.         printf("\n");
  141. }
  142.  
  143. /* Print separator between directory entries. Do [more] if page full */
  144. /* Return TRUE if end of page and ctl-c or ctl-k typed */
  145. printsep(count)
  146. short count;
  147. {
  148.     if ((Entries % count) == count - 1) {
  149.         printf("\n");
  150.         if (++Lines == DIRLINES) {    /* bump line count */
  151.             Lines = 0;        /* pause if done a page */
  152.             return domore();    /* then do [more] */
  153.         }
  154.     }
  155.     else
  156.         printf(" | ");
  157.     return FALSE;
  158. }
  159.  
  160. /* Print [more] and wait for a key. Return TRUE if user hit ctl-c or ctl-k */
  161. domore()
  162. {
  163.     char c;
  164.  
  165.     printf("[more]");
  166.     flush();
  167.     while (!(c = bdos(DIRCTIO,INPUT)));    /* loop till we get one */
  168.     printf("\b\b\b\b\b\b      \b\b\b\b\b\b");
  169.     if (c == CTRLC || c == CTRLK)
  170.         return TRUE;
  171.     else
  172.         return FALSE;
  173. }
  174.  
  175. /* Function for qsort to compare two directory entries */
  176. dirsort(p1,p2)
  177. char *p1, *p2;
  178. {
  179.     short j;
  180.  
  181.     if (j = memcmp(p1,p2,12))
  182.         return j;
  183.  
  184.         /* Both are the same file -- sort on extent */
  185.     if ((j = (p1[14] * 32 + p1[12]) - (p2[14] * 32 + p2[12])) > 0)
  186.         return -1;
  187.     else
  188.         return 1;
  189. }
  190.  
  191. /* Compare memory */
  192. memcmp(p1,p2,count)
  193. char *p1, *p2;
  194. int count;
  195. {
  196.     short i, j;
  197.  
  198.     for (i = 0; i < count; i++)
  199.         if (j = p1[i] - p2[i])
  200.             return j;
  201.     return 0;    /* both equal */
  202. }
  203.  
  204. cntbits(byte)
  205. char byte;
  206. {
  207.     static int i,count;
  208.  
  209.     for (count=i=0; i<8; i++) {
  210.         count += (byte & 1);
  211.         byte >>= 1;
  212.     }
  213.     return count;
  214. }
  215.  
  216. resetace()  /* to default values */
  217. {
  218.     Current.cbaudindex = Line.baudindex;
  219.     Current.cparity = Line.parity;
  220.     Current.cdatabits = Line.databits;
  221.     Current.cstopbits = Line.stopbits;
  222.     updateace();
  223. }
  224.  
  225. updateace()
  226. {
  227.     initace(Current.cbaudindex,Current.cparity,
  228.         Current.cdatabits,Current.cstopbits);
  229. }
  230.  
  231. hangup()
  232. {
  233.     stndout();
  234.     printf("\n ZMP: Disconnect (Y/N) <N>? \007");
  235.     stndend();
  236.     if (toupper(dio()) != 'Y') {
  237.         printf("\n");
  238.         return;
  239.     }
  240.     printf("\nHanging up...\n");
  241.     dtroff();            /* Turn DTR off for a bit */
  242.     mswait(200);            /* Like 200 ms */
  243.     dtron();            /* Then back on again */
  244.     mstrout(Modem.hangup,FALSE);
  245.     resetace();
  246. }
  247.  
  248. tlabel() /*print level 1 labels on the 25th line*/
  249. {
  250. /*                    Removed for now
  251.    killlabel();
  252.    printf(
  253.    "%s1> \033pReceive%s  Log  %s Dir  %sPrScr%s Send%sHangup%s Level %s Help %s",
  254.       Stline,Vl,Vl,Vl,Vl,Vl,Vl,Vl,Vl);
  255.    printf(
  256.    "Quit%s%02d%c%d%d%s%s%s%s%s%s\033q%s",Vl,Baudtable[Current.cbaudindex]/100,
  257.     Current.cparity,Current.cdatabits,Current.cstopbits,Vl,BFlag?"LG":"--",Vl,
  258.     PFlag?"PR":"--",Vl,FDx?"FDX":"HDX",Endline);
  259. */
  260.  
  261. }
  262.  
  263. /* Prompt user and get any key */
  264. waitakey()
  265. {
  266.     char c;
  267.  
  268.     printf("\n Any key to continue: ");
  269.     flush();
  270.     while (!(c = bdos(DIRCTIO,INPUT)));
  271.     return c;
  272. }
  273.  
  274. /*            End of TERM module                */
  275.