gets.c

来自「主要是异步通讯编程」· C语言 代码 · 共 54 行

C
54
字号
/* A module of ASYNCx.LIB version 1.02 */

#include <dos.h>
#include <stdio.h>
#include "asyncdef.h"

/***************************************************************
 Get a string from the input buffer of the async port
 associated with p. Read up to n-1 characters from the port or
 until a '\r' character is reached. Store the string into the
 buffer pointed to by s. Spend a maximum of t+1 eighteenths of
 a second waiting for this string to arrive. If at least one
 character is successfully received into the string, return
 the address of the string. If no characters have been
 read into the string at the end of between t and t+1
 eighteenths of a second, return NULL.
***************************************************************/
char *a_gets(char *s,int n,ASYNC *p,int t)
{unsigned long t0;      /* regulate the time spent in this function     */
 unsigned long far *t1;
 word count;            /* keeps track of how many bytes have been read */
 int eof;               /* TRUE only if no characters are received      */
 char *orgs;            /* remembers where we're putting the data       */
 if (p==NULL)
   return NULL;
 t1=(unsigned long far *)MK_FP(0x40,0x6c); /* t1 -> system clock        */
 t0=*t1;                /* t0 =  time at the beginning of this function */
 count=1;
 eof=1;  /* assume that no characters have will be received */
 orgs=s;
 do
  {if (p->ibufhead!=p->ibuftail)
    {eof=0;
     if ((*s=a_getc(p))!=0) /* add only non-null characters to the string */
      {if (*s=='\n') /* ignore '\n' characters */
         continue;
       if (*s=='\r') /* '\r' terminates input  */
         break;
       else
        {s++;
         count++;
        }
      }
    }
  }
 while((*t1)-t0<=(unsigned long)t && count<n);
 *s='\0';
 if (eof) /* if no characters were read from the port, */
   return NULL; /* return NULL to indicate "end-of-file".    */
 return orgs;
} /* a_gets(s,n,p,t) */


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?