home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / gets.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  76 lines

  1. /***
  2. *gets.c - read a line from stdin
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines gets() and getws() - read a line from stdin into buffer
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <dbgint.h>
  14. #include <file2.h>
  15. #include <mtdll.h>
  16. #include <tchar.h>
  17.  
  18. /***
  19. *char *gets(string) - read a line from stdin
  20. *
  21. *Purpose:
  22. *       Gets a string from stdin terminated by '\n' or EOF; don't include '\n';
  23. *       append '\0'.
  24. *
  25. *Entry:
  26. *       char *string - place to store read string, assumes enough room.
  27. *
  28. *Exit:
  29. *       returns string, filled in with the line of input
  30. *       null string if \n found immediately
  31. *       NULL if EOF found immediately
  32. *
  33. *Exceptions:
  34. *
  35. *******************************************************************************/
  36.  
  37. _TCHAR * __cdecl _getts (
  38.         _TCHAR *string
  39.         )
  40. {
  41.         int ch;
  42.         _TCHAR *pointer = string;
  43.         _TCHAR *retval = string;
  44.  
  45.         _ASSERTE(string != NULL);
  46.  
  47.         _lock_str2(0, stdin);
  48.  
  49. #ifdef _UNICODE
  50.         while ((ch = _getwchar_lk()) != L'\n')
  51. #else  /* _UNICODE */
  52.         while ((ch = _getchar_lk()) != '\n')
  53. #endif  /* _UNICODE */
  54.         {
  55.                 if (ch == _TEOF)
  56.                 {
  57.                         if (pointer == string)
  58.                         {
  59.                                 retval = NULL;
  60.                                 goto done;
  61.                         }
  62.  
  63.                         break;
  64.                 }
  65.  
  66.                 *pointer++ = (_TCHAR)ch;
  67.         }
  68.  
  69.         *pointer = _T('\0');
  70.  
  71. /* Common return */
  72. done:
  73.         _unlock_str2(0, stdin);
  74.         return(retval);
  75. }
  76.