home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / CGETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.2 KB  |  82 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - cgets.c
  3.  *
  4.  * function(s)
  5.  *        cgets - reads string from console
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma inline
  18. #include <asmrules.h>
  19. #include <conio.h>
  20.  
  21. /*--------------------------------------------------------------------------*
  22.  
  23. Name            cgets - reads string from console
  24.  
  25. Usage           char *cgets(char *string);
  26.  
  27. Prototype in    conio.h
  28.  
  29. Description     cgets reads a string of characters from the console,
  30.                 storing the string (and the string length) in the
  31.                 location pointed to by string.
  32.  
  33.                 cgets reads characters until it encounters a CR/LF or
  34.                 until the maximum allowable number of characters have
  35.                 been read.
  36.  
  37.                 Before cgets is called, string[0] should be set to the
  38.                 maximum length of the string to be read.
  39.  
  40. Return value    string[1] is set to the number of characters actually
  41.                 read.
  42.                 &string[2], a pointer to the string of characters that
  43.                 were read.  There is no error return.
  44.  
  45. *---------------------------------------------------------------------------*/
  46. char * _FARFUNC cgets( char *s )
  47.   {
  48.   int c, len, maxlen;
  49.   char *p = s + 2;
  50.  
  51.   len = 0;
  52.   maxlen = s[ 0 ] & 0xff;
  53.  
  54.   while( 1 )
  55.     {
  56.     switch( c = getch() )
  57.       {
  58.       case 0:     if( getch() != 75 )  break;      /* keypad left arrow */
  59.       case '\b':  if( len )
  60.                     {
  61.                     putch( '\b' );
  62.                     putch( ' ' );
  63.                     putch( '\b' );
  64.                     --len;
  65.                     --p;
  66.                     }
  67.                   break;
  68.  
  69.       case '\r':  *p = '\0';
  70.                   s[ 1 ] = len;
  71.                   return( s + 2 );
  72.  
  73.       default:    if( len < maxlen - 1 )
  74.                     {
  75.                     putch( c );
  76.                     *p++ = c;
  77.                     ++len;
  78.                     }
  79.       }
  80.     }
  81.   }
  82.