home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / c / WINKERM.ZIP / KERMSYS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-31  |  5.7 KB  |  242 lines

  1. /*******************************************************************************
  2. **                                                                            **
  3. **                      Kermit for Microsoft Windows                          **
  4. **                      ----------------------------                          **
  5. **                               KERMSYS.C                                    **
  6. **                                                                            **
  7. **  This module contains system dependent routines for the Kermit protocol.   **
  8. **  With some luck.  These routines were based on those for the Unix          **
  9. **  environment, but were pretty heavily modified for MS Windows.             **
  10. **                                                                            **
  11. *******************************************************************************/
  12.  
  13. // INCLUDES -------------------------------------------------------------------
  14.  
  15. #include "kermit.h"
  16. #include "kermprot.h"
  17.  
  18. //-----------------------------------------------------------------------------
  19. void traw(char *s, int len)
  20.  
  21. //  Description of what function does.
  22.  
  23. //  Param(s):   x...............Description.
  24.  
  25. //  Returns:    Result description.
  26.  
  27. {
  28.     WriteTermStr(s, len, TRUE);
  29. }
  30.  
  31. //-----------------------------------------------------------------------------
  32. void tchar(char c)
  33. {
  34.     WriteTermChar(c, FALSE);
  35. }
  36.  
  37. //-----------------------------------------------------------------------------
  38. void tmsg(char *fmt, ...)
  39. {
  40.     char    buf[128];
  41.     va_list arg_ptr;
  42.     int     len;
  43.  
  44.     va_start(arg_ptr, fmt);
  45.     len = vsprintf(buf, fmt, arg_ptr);
  46.     va_end(arg_ptr);
  47.  
  48.     WriteTermStr(buf, len, FALSE);
  49. }
  50.  
  51. //-----------------------------------------------------------------------------
  52. void ttflui(void)
  53. {
  54.     FlushComm(nCid,1);
  55.     clrinl = TRUE;
  56. }
  57.  
  58. //-----------------------------------------------------------------------------
  59. int ttinl (char *dest, int max, char eol, int timo)
  60. {
  61.     static int newinl;
  62.     static int x = 0, ccn = 0;
  63.     int comerr;
  64.     COMSTAT ComStat;
  65.     int count;
  66.     char c;
  67.     static clock_t wTime;
  68.  
  69.  
  70.     if (clrinl) {
  71.         ccn = 0;
  72.         newinl = TRUE;
  73.         clrinl = FALSE;
  74.     }
  75.  
  76.     if (newinl) {
  77.         *dest = '\0';
  78.         x = 0;
  79.         wTime = 0;
  80.         newinl = FALSE;
  81.     }
  82.  
  83.     if (wTime == 0) {
  84.         while (GetCommError(nCid, &ComStat));
  85.  
  86.         if (ComStat.fTxim)
  87.             return(0);
  88.         else
  89.             wTime = clock();
  90.     }
  91.  
  92.     if (cc || cr) {
  93.         newinl = TRUE;
  94.         return(-1);
  95.     }
  96.  
  97.     if ((clock() - wTime) > (timo * CLK_TCK)) {
  98.         newinl = TRUE;
  99.         return(-1);
  100.     }
  101.  
  102. //    count = ReadComm(nCid, dest + x, sizeof(rcvpkt) - x);
  103. //
  104. //    if (count <= 0) {
  105. //        if (comerr = GetCommError(nCid, NULL))
  106. //            tmsg("<ComErr: %i>", comerr);
  107. //        count = -count;
  108. //    }
  109.  
  110.     count = ReadCommStr(dest + x, sizeof(rcvpkt) - x, FALSE);
  111.  
  112.     for (; count > 0; count--) {
  113.         if (dest [x] == 3) {
  114.             if (++ccn > 1) {
  115.                 tmsg("\n\r^C...");
  116.                 cc = TRUE;
  117.                 return(-1);
  118.             }
  119.         }
  120.         else
  121.             ccn = 0;
  122.  
  123.         if (dest[x++] == eol) {
  124.             dest[x] = '\0';
  125.             newinl = TRUE;
  126.             return(x);
  127.         }
  128.     }
  129.  
  130.     return(0);
  131. }
  132.  
  133. //-----------------------------------------------------------------------------
  134. short ttol (char *s, int n)
  135. {
  136.     if (ProtSet.DebugPacket) {
  137.         tmsg("\n\rSent: ");
  138.         traw(s, n);
  139.     }
  140.  
  141.     return(WriteComm(nCid,s,n));
  142. }
  143.  
  144. //-----------------------------------------------------------------------------
  145. int zopeni(char *name)
  146. {
  147.     ifp = fopen(name,"rb");
  148.     if (ifp == NULL)
  149.         return(-1);
  150.     else
  151.         return(0);
  152. }
  153.  
  154. //-----------------------------------------------------------------------------
  155. int zopeno(char *name)
  156. {
  157.     ofp = fopen(name,"wb");
  158.     if (ofp == NULL)
  159.         return(-1);
  160.     else
  161.         return(0);
  162. }
  163.  
  164. //-----------------------------------------------------------------------------
  165. int zclosi(void)
  166. {
  167.     if (fclose(ifp) == EOF)
  168.         return(-1);
  169.     else
  170.         return(0);
  171. }
  172.  
  173. //-----------------------------------------------------------------------------
  174. int zcloso(int x)
  175. {
  176.     if (fclose(ofp) == EOF)
  177.         return(-1);
  178.     else {
  179.         if (x) {
  180.             if (remove(filnam) == EOF)
  181.                 tmsg("Discard Failed!",15);
  182.             else
  183.                 tmsg("File Discarded!",15);
  184.         }
  185.         return(0);
  186.     }
  187. }
  188.  
  189. //-----------------------------------------------------------------------------
  190. void zrtol(char *n1, char *n2, int warn)
  191. {
  192.     strcpy (n2,n1);
  193. }
  194.  
  195. //-----------------------------------------------------------------------------
  196. void zltor(char *n1, char *n2)
  197. {
  198.     char *sptr;
  199.  
  200.     sptr = strrchr (n1, '\\');
  201.     if (sptr)
  202.         strcpy (n2, sptr + 1);
  203.     else
  204.         strcpy(n2, n1);
  205. }
  206.  
  207. //-----------------------------------------------------------------------------
  208. int zgetc(void)
  209. {
  210. #define MAXREC 100
  211.  
  212.     static char recbuf[MAXREC+1];
  213.     static char *rbp;
  214.     static int i = 0;
  215.     int c;
  216.  
  217.     if (i == 0) {
  218.         for (i=0; i < MAXREC - 1 && (c=getc(ifp)) != EOF; i++)
  219.             recbuf[i] = (char)c;
  220.         recbuf[i] = '\0';
  221.         if (i == 0)
  222.             return(-1);
  223.         rbp = recbuf;
  224.     }
  225.     i--;
  226.     Stats.Bytes++;
  227.     return(*rbp++ & 0377);
  228. }
  229.  
  230. //-----------------------------------------------------------------------------
  231. int zputc(int c)
  232. {
  233.     unsigned int x;
  234.  
  235.     c &= 255;
  236.     x = putc(c,ofp) & 255;
  237.     Stats.Bytes++;
  238.     if (c == 255)
  239.         return(0);
  240.     return((x != c) ? -1 : 0);
  241. }
  242.