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 / RZMP-SRC.LZH / RZMP3.C < prev    next >
Text File  |  2000-06-30  |  10KB  |  465 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /*            RZMP Source File #3                */
  4. /*                                    */
  5. /************************************************************************/
  6.  
  7. #define RZMP
  8.  
  9. #include "zmp.h"
  10. #include <stdio.h>
  11.  
  12. char *alloc();
  13.  
  14. fstat(fname,status)
  15. char *fname;
  16. struct stat *status;
  17. {
  18.     unsigned filelength();
  19.  
  20.     fcbinit(fname,&Thefcb);
  21.     status->records = filelength(&Thefcb);
  22.     getfirst(fname);
  23.     fcbinit("????????.???",&Thefcb);
  24. }
  25.  
  26. unsigned
  27. filelength(fcbp)
  28. struct fcb *fcbp;
  29. {
  30.     int olduser;
  31.  
  32.     bdos(SETDMA,CPMBUF);         /* set dma address */
  33.     olduser = getuid();        /* save this user number */
  34.     setuid(fcbp->freserved & 0x0f);    /* go to file's user no. */
  35.     bdos(35,fcbp);
  36.     setuid(olduser);        /* restore original */
  37.     return fcbp->ranrec;
  38. }
  39.  
  40. roundup(dividend,divisor)
  41. int dividend, divisor;
  42. {
  43.      return (dividend/divisor + ((dividend%divisor) ? 1 : 0));
  44. }
  45.  
  46. getfirst(aname)      /* ambiguous file name */
  47. char *aname;
  48. {
  49.     bdos(SETDMA,CPMBUF);         /* set dma address */
  50.     fcbinit(aname,&Thefcb);
  51.     return bdos(SFF,&Thefcb) & 0xff;
  52. }
  53.  
  54. getnext()
  55. {
  56.     bdos(SETDMA,CPMBUF);         /* set dma address */
  57.     return bdos(SFN,NULL) & 0xff;
  58. }
  59.  
  60. memcpy(dest,source,count)
  61. char *dest, *source;
  62. int count;
  63. {
  64.     while (count--)
  65.         *dest++ = *source++;
  66. }
  67.  
  68. memset(dest,byte,count)
  69. char *dest, byte;
  70. int count;
  71. {
  72.     while (count--)
  73.         *dest++ = byte;
  74. }
  75.  
  76. /* command: expand wild cards in the command line.  (7/25/83)
  77.  * usage: command(&argc, &argv) modifies argc and argv as necessary
  78.  * uses sbrk to create the new arg list
  79.  * NOTE: requires fcbinit() and bdos() from file stdlib.c.  When used
  80.  *    with a linker and stdlib.rel, remove the #include stdlib.c.
  81.  *
  82.  * Written by Dr. Jim Gillogly; Modified for CP/M by Walt Bilofsky.
  83.  * Modified by HM to just get ambiguous fn for zmodem, ymodem.
  84.  */
  85.  
  86. int COMnf,*COMfn,COMc,*COMv;
  87. char *COMarg,*COMs;
  88. static expand();
  89.  
  90. command(argcp,argvp)
  91. int *argcp,*argvp;
  92. {
  93.     char *p, c, *index();
  94.     int drive, user;
  95.     int f_alloc[MAXFILES];
  96.  
  97.     COMfn = f_alloc;
  98.     COMc = *argcp;
  99.     COMv = *argvp;
  100.     COMnf = 0;
  101.     for (COMarg = *COMv; COMc--; COMarg = *++COMv) {
  102.  
  103. #ifdef   DEBUG
  104.         printf("\nDoing %s",COMarg);
  105. #endif
  106.  
  107.         if (!(*Wheel) && (index(COMarg,':'))) { /* ensure du: in range */
  108.             drive = (int) *COMarg;
  109.             user = atoi(COMarg+1);
  110.             if ((drive > MAdrive) || (user > MAuser))
  111.                 continue;    /* out of range */
  112.         }
  113.         for (COMs = COMarg; *COMs; COMs++)
  114.             if (*COMs == '?' || *COMs == '*') {
  115.                 if (Private)
  116.                     goto contn; /* no wildcards on private */
  117.                         if (!expand()) {    /* Too many names */
  118.                     *argcp = 0;
  119.                     return;
  120.                 }
  121.                 goto contn;  /* expand each name at most once */
  122.             }
  123.         COMfn[COMnf] = alloc(FNSIZE);
  124.         p = COMarg;
  125.         while (c = *p)            /* convert to lower case */
  126.             *p++ = isupper(c) ? tolower(c) : c;
  127.         strcpy(COMfn[COMnf++],COMarg);     /* no expansion */
  128. contn:
  129.         ;
  130.     }
  131.     *argcp = COMnf;
  132.     COMfn[COMnf++] = -1;
  133.     COMv = *argvp = alloc(2 * COMnf);
  134.     while (COMnf--) 
  135.         COMv[COMnf] = COMfn[COMnf];
  136. }
  137.  
  138. static expand()        /* Returns FALSE if error */
  139. {
  140.     char fcb[36];
  141.     static char *p,*q,*r,c;
  142.     static int i,flg,olduser;
  143.  
  144. #ifdef   DEBUG
  145.    printf("\nExpanding %s",COMarg);
  146. #endif
  147.     olduser = getuid();            /* save original user area */
  148.     fcbinit(COMarg,fcb);
  149.     if (fcb[0] == -1) 
  150.         fcb[0] = '?';             /* Check for all users */
  151.     if (!(*Wheel) && (fcb[0] == '?'))    /* but only for wheels */
  152.         fcb[0] = olduser;
  153.     for (i = flg = 1; i <= 11; ++i) {    /* Expand *'s */
  154.         if (i == 9) 
  155.                 flg = 1;
  156.         if (fcb[i] == '*') 
  157.                 flg = 0;
  158.         if (flg == 0) 
  159.             fcb[i] = '?';
  160.     }
  161.     setuid(fcb[13]);        /* go to specified user area */
  162.     flg = 17;
  163.     bdos(26,0x80);            /* Make sure DMA address OK */
  164.     while (((i = bdos(flg,fcb)) & 0xff) != 0xff) {
  165.         COMfn[COMnf++] = q = alloc(FNSIZE);
  166.         if (COMnf >= MAXFILES-1) {
  167.             for (p = "Too many file names.\n"; putchar(*p++); ); {
  168.                 setuid(olduser);
  169.                 return FALSE;
  170.             }
  171.         }
  172.         p = 0x81 + i * 32;        /* Where to find dir. record */
  173.  
  174.         /* Transfer du: first */
  175.         if ((index(COMarg,':')) && COMarg[0] != '?') {
  176.             r = COMarg;
  177.             do
  178.                 *q++ = c = *r++;
  179.                     while (c != ':');
  180.               }
  181.  
  182.         /* Now transfer filename */
  183.         r = q;            /* save pointer for test */
  184.         for (i = 12; --i; ) {
  185.             if (i == 3) 
  186.                         *q++ = '.';
  187.             if ((*q = isupper(c = *p++ & 0177) ? tolower(c) : c) != ' ')
  188.                         ++q; 
  189.               }
  190.         *q = 0;
  191.         flg = 18;
  192.     }
  193.     setuid(olduser);
  194.     return TRUE;
  195. }
  196.  
  197.  
  198. ctr(p)
  199. char *p;
  200. {
  201.     return max((80 - strlen(p))/2,0);
  202. }
  203.  
  204. opabort()
  205. {
  206.     char lastkey;
  207.  
  208.     if (bdos(66)) {        /* local console input status */
  209.         if ((lastkey = bdos(67) & 0xff) == ESC) {
  210.             flush();
  211.             report(MESSAGE,"Operator abort");
  212.             QuitFlag = TRUE;
  213.         }
  214.     }
  215.     return QuitFlag;
  216. }
  217.  
  218. /*
  219.  * readock(timeout, count) reads character(s) from modem
  220.  *  (1 <= count <= 3)
  221.  * it attempts to read count characters. If it gets more than one,
  222.  * it is an error unless all are CAN
  223.  * (otherwise, only normal response is ACK, CAN, or C)
  224.  *
  225.  * timeout is in tenths of seconds
  226.  */
  227.  
  228. readock(timeout, count)
  229. int timeout, count;
  230. {
  231.     static int c;
  232.     static char byt[5];
  233.  
  234.     c = mread(byt,count,timeout);
  235.     if (c < 1)
  236.         return TIMEOUT;
  237.     if (c == 1)
  238.         return (byt[0] & 0xff);
  239.     else
  240.         while (c)
  241.            if (byt[--c] != CAN)
  242.                return NERROR;
  243.     return CAN;
  244. }
  245.  
  246. readline(n)
  247. int n;
  248. {
  249.     return (readock(n,1));
  250. }
  251.  
  252. putlabel(string)
  253. char string[];
  254. {
  255.     cls();
  256.     locate(0,ctr(string) - 1);    /* Centre on top line */   
  257.     stndout();            /* Inverse video */
  258.     lprint(string);            /* Print the string */
  259.     stndend();            /* Inverse off */
  260. }
  261.  
  262. killlabel() /*disable 25th line*/
  263. {
  264.     cls();            /* just clear screen */
  265. }
  266.  
  267. box()          /* put box on screen for file transfer */
  268. {
  269.     register int i;
  270.     static char *headings[] = { "","Protocol:","File Name:","File Size:",
  271.                 "Block Check:","Transfer Time:",
  272.                 "Bytes Transferred:","Blocks Transferred:",
  273.                 "Sectors in File:","Error Count:",
  274.                 "Last Message:  NONE" };
  275.     static int start[] = { 0,13+LC,12+LC,12+LC,10+LC,8+LC,4+LC,3+LC,6+LC,
  276.                 10+LC,9+LC };
  277.  
  278.     LOCATE(TR,LC);
  279.     lputch(UL);
  280.     for (i = 1; i < WD-1; i++)
  281.         lputch(HORIZ);
  282.     lputch(UR);
  283.     LOCATE(BR,LC);
  284.     lputch(LL);
  285.     for (i = 1; i < WD-1; i++)
  286.         lputch(HORIZ);
  287.     lputch(LR);
  288.     for (i = 1; i < HT-1; i++) {
  289.         LOCATE(TR+i,LC);
  290.         lputch(VERT);
  291.         LOCATE(TR+i,RC);
  292.         lputch(VERT);
  293.     }
  294.     clrbox();
  295.     for (i=1; i<11; i++) {
  296.         locate(TR+i,start[i]);
  297.         lprint(headings[i]);
  298.     }   
  299. }
  300.  
  301. clrbox()
  302. {
  303.     register int i;
  304.  
  305.     for (i=TR+1; i < BR; i++) {
  306.         locate(i,LC+1);
  307.         lprint("                                       ");
  308.     }
  309. }
  310.  
  311. mread(buffer, count, timeout)    /* time in tenths of secs */
  312. char *buffer;
  313. int count, timeout;
  314. {
  315.     int i, c;
  316.  
  317.     i = 0;
  318.     while (!(c = mrd()) && (timeout--) && !opabort());
  319.     if (c)
  320.         buffer[i++] = mcharinp();
  321.     return i;
  322. }
  323.  
  324. mcharinp()
  325. {
  326.     static unsigned c;
  327.  
  328.     c = mchin();
  329.     if (Stopped)
  330.     {
  331.         mchout(CTRLQ);
  332.         Stopped = FALSE;
  333.     }
  334.     return c;
  335. }
  336.  
  337. mcharout(c)
  338. char c;
  339. {
  340.     while (!moutrdy())
  341.         opabort();    /* Test for operator abort while we wait */
  342.     mchout(c);        /* Then send it */
  343. }
  344.  
  345. minprdy()
  346. {
  347.     return mirdy() || Stopped;
  348. }
  349.  
  350. /* Check transmission time of a batch of files and return error if too long */
  351. checktime(count,list)
  352. short count;
  353. char *list[];
  354. {
  355.     short result = NERROR, i, mins, ksize, lcount = 0;
  356.     unsigned short baudrate, curtime, maxtime;
  357.     long total_size = 0L;
  358.     struct stat fs;
  359.     char filename[20];
  360.  
  361.     if (!Shutup)
  362.         printf("Calculating....");
  363.     for (i = 0; i < count; i++) {
  364.         strcpy(filename,list[i]);
  365.         if (Private) {
  366.             deldrive(filename);
  367.             addu(filename,Dprivdrive,Dprivuser);
  368.         }
  369.         if (!checkname(filename)) { /* only if it's ok to send */
  370.             fstat(filename,&fs);    /* add file length */
  371.             total_size += (long)fs.records * 128L;
  372.             lcount++;        /* bump local count */
  373.         }
  374.         if (!Shutup)
  375.             printf(".");    /* reassure user we're still awake */
  376.     }
  377.     if (lcount) {        /* if there's any to send, */
  378.                 /* calculate transmission time */
  379.         baudrate = Baudtable[Current.cbaudindex];
  380.         mins = ((int) (total_size / (baudrate * 9 / 100)) / 60) + 1;
  381.         curtime = bdos(79);    /* current time on system */
  382.         maxtime = bdos(81,255);    /* max time allowed on system (0 = no limit) */
  383.         if (!maxtime || (mins < (maxtime - curtime))) {
  384.             result = OK;
  385.             ksize = (short)(total_size / 1024L) + 1;
  386.             if (!Shutup)
  387.                 printf("\n%dk in %d files, transmission time around %d minutes.\n",
  388.                     ksize,lcount,mins);
  389.         }
  390.         else
  391.             printf("\nInsufficient time remaining.");
  392.     }
  393.     if (result == NERROR)
  394.         printf("\nNo files sent.\n");
  395.     return result;
  396. }
  397.  
  398. /* Try to open temporary .LOG file */
  399. openlog()
  400. {
  401.     char filename[20], *malloc();
  402.  
  403.     Outopen = FALSE;
  404.     if (Logdrive) {        /* only if user wants it */
  405.         strcpy(filename,Outfile);
  406.         addu(filename,Logdrive,Loguser);
  407.         unlink(filename);    /* erase any already there */
  408.         Outbuf = malloc(SECSIZ);
  409.         if (!allocerror(Outbuf)) {      /* get a 128-byte buffer */
  410.             Outfd = creat(filename,0); /* Then try to open it */
  411.             Outopen = (Outfd != UBIOT);    /* set the flag */
  412.             memset( Outbuf, 0x1a, SECSIZ);    /* fill the buffer */
  413.         }
  414.     }
  415. }
  416.  
  417. /* Close temporary .log file */
  418. closelog()
  419. {
  420.     if (Outopen) {            /* only if it's open */
  421.         putlog(CPMEOF, TRUE);    /* store a 1a hex and flush */
  422.         close(Outfd);        /* then close the file */
  423.     }
  424. }
  425.  
  426. /* Store a filename in the temporary file */
  427. savelog(filename)
  428. char *filename;
  429. {
  430.     char c, *p;
  431.  
  432.     p = filename;
  433.     if (Outopen) {                /* only if open */
  434.         while (c = *p++)
  435.             putlog(c,FALSE);    /* store the name */
  436.         putlog(CR,FALSE);        /* then cr/lf */
  437.         putlog(LF,FALSE);
  438.     }
  439. }
  440.  
  441. /* Put a character in the temporary file and write the sector if full */
  442.     /* Flush the buffer to disk if flag is true */
  443.     /* Assumes the file has been checked and found open */
  444. putlog(c, flag)
  445. char c;
  446. short flag;
  447. {
  448.     static short i = 0;    /* pointer to place in buffer */
  449.     short bytes;
  450.  
  451.     Outbuf[i++] = c;        /* store the character */
  452.     if ((i == SECSIZ) || flag){    /* if buffer full or flag set, */
  453.         bytes = write(Outfd, Outbuf, SECSIZ); /* write it */
  454.  
  455.         if (bytes != SECSIZ) {            /* error */
  456.             Outopen = FALSE;        /* say it's closed */
  457.             perror("Can\'t write output file.");
  458.         }
  459.         memset( Outbuf, 0x1a, SECSIZ);    /* fill the buffer */
  460.         i = 0;        /* reset the pointer */
  461.     }
  462. }
  463.  
  464. /*             End of RZMP module 3                */
  465.