home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / CGETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.5 KB  |  82 lines

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