home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- ** **
- ** Kermit for Microsoft Windows **
- ** ---------------------------- **
- ** KERMSYS.C **
- ** **
- ** This module contains system dependent routines for the Kermit protocol. **
- ** With some luck. These routines were based on those for the Unix **
- ** environment, but were pretty heavily modified for MS Windows. **
- ** **
- *******************************************************************************/
-
- // INCLUDES -------------------------------------------------------------------
-
- #include "kermit.h"
- #include "kermprot.h"
-
- //-----------------------------------------------------------------------------
- void traw(char *s, int len)
-
- // Description of what function does.
-
- // Param(s): x...............Description.
-
- // Returns: Result description.
-
- {
- WriteTermStr(s, len, TRUE);
- }
-
- //-----------------------------------------------------------------------------
- void tchar(char c)
- {
- WriteTermChar(c, FALSE);
- }
-
- //-----------------------------------------------------------------------------
- void tmsg(char *fmt, ...)
- {
- char buf[128];
- va_list arg_ptr;
- int len;
-
- va_start(arg_ptr, fmt);
- len = vsprintf(buf, fmt, arg_ptr);
- va_end(arg_ptr);
-
- WriteTermStr(buf, len, FALSE);
- }
-
- //-----------------------------------------------------------------------------
- void ttflui(void)
- {
- FlushComm(nCid,1);
- clrinl = TRUE;
- }
-
- //-----------------------------------------------------------------------------
- int ttinl (char *dest, int max, char eol, int timo)
- {
- static int newinl;
- static int x = 0, ccn = 0;
- int comerr;
- COMSTAT ComStat;
- int count;
- char c;
- static clock_t wTime;
-
-
- if (clrinl) {
- ccn = 0;
- newinl = TRUE;
- clrinl = FALSE;
- }
-
- if (newinl) {
- *dest = '\0';
- x = 0;
- wTime = 0;
- newinl = FALSE;
- }
-
- if (wTime == 0) {
- while (GetCommError(nCid, &ComStat));
-
- if (ComStat.fTxim)
- return(0);
- else
- wTime = clock();
- }
-
- if (cc || cr) {
- newinl = TRUE;
- return(-1);
- }
-
- if ((clock() - wTime) > (timo * CLK_TCK)) {
- newinl = TRUE;
- return(-1);
- }
-
- // count = ReadComm(nCid, dest + x, sizeof(rcvpkt) - x);
- //
- // if (count <= 0) {
- // if (comerr = GetCommError(nCid, NULL))
- // tmsg("<ComErr: %i>", comerr);
- // count = -count;
- // }
-
- count = ReadCommStr(dest + x, sizeof(rcvpkt) - x, FALSE);
-
- for (; count > 0; count--) {
- if (dest [x] == 3) {
- if (++ccn > 1) {
- tmsg("\n\r^C...");
- cc = TRUE;
- return(-1);
- }
- }
- else
- ccn = 0;
-
- if (dest[x++] == eol) {
- dest[x] = '\0';
- newinl = TRUE;
- return(x);
- }
- }
-
- return(0);
- }
-
- //-----------------------------------------------------------------------------
- short ttol (char *s, int n)
- {
- if (ProtSet.DebugPacket) {
- tmsg("\n\rSent: ");
- traw(s, n);
- }
-
- return(WriteComm(nCid,s,n));
- }
-
- //-----------------------------------------------------------------------------
- int zopeni(char *name)
- {
- ifp = fopen(name,"rb");
- if (ifp == NULL)
- return(-1);
- else
- return(0);
- }
-
- //-----------------------------------------------------------------------------
- int zopeno(char *name)
- {
- ofp = fopen(name,"wb");
- if (ofp == NULL)
- return(-1);
- else
- return(0);
- }
-
- //-----------------------------------------------------------------------------
- int zclosi(void)
- {
- if (fclose(ifp) == EOF)
- return(-1);
- else
- return(0);
- }
-
- //-----------------------------------------------------------------------------
- int zcloso(int x)
- {
- if (fclose(ofp) == EOF)
- return(-1);
- else {
- if (x) {
- if (remove(filnam) == EOF)
- tmsg("Discard Failed!",15);
- else
- tmsg("File Discarded!",15);
- }
- return(0);
- }
- }
-
- //-----------------------------------------------------------------------------
- void zrtol(char *n1, char *n2, int warn)
- {
- strcpy (n2,n1);
- }
-
- //-----------------------------------------------------------------------------
- void zltor(char *n1, char *n2)
- {
- char *sptr;
-
- sptr = strrchr (n1, '\\');
- if (sptr)
- strcpy (n2, sptr + 1);
- else
- strcpy(n2, n1);
- }
-
- //-----------------------------------------------------------------------------
- int zgetc(void)
- {
- #define MAXREC 100
-
- static char recbuf[MAXREC+1];
- static char *rbp;
- static int i = 0;
- int c;
-
- if (i == 0) {
- for (i=0; i < MAXREC - 1 && (c=getc(ifp)) != EOF; i++)
- recbuf[i] = (char)c;
- recbuf[i] = '\0';
- if (i == 0)
- return(-1);
- rbp = recbuf;
- }
- i--;
- Stats.Bytes++;
- return(*rbp++ & 0377);
- }
-
- //-----------------------------------------------------------------------------
- int zputc(int c)
- {
- unsigned int x;
-
- c &= 255;
- x = putc(c,ofp) & 255;
- Stats.Bytes++;
- if (c == 255)
- return(0);
- return((x != c) ? -1 : 0);
- }
-