Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

  1. /***
  2. *gets.c - read a line from stdin
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines gets() and getws() - read a line from stdin into buffer
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 11-06-87 JCR Multi-thread support
  12. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  13. * 05-27-88 PHG Merged DLL and normal versions
  14. * 02-15-90 GJF Fixed copyright, indents
  15. * 03-19-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  16. * <cruntime.h> and removed #include <register.h>.
  17. * 07-24-90 SBM Replaced <assertm.h> by <assert.h>
  18. * 08-14-90 SBM Compiles cleanly with -W3
  19. * 10-02-90 GJF New-style function declarator.
  20. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  21. * 01-31-94 CFW Unicode enable.
  22. * 09-06-94 CFW Replace MTHREAD with _MT.
  23. * 02-06-94 CFW assert -> _ASSERTE.
  24. * 02-22-95 GJF Replaced WPRFLAG with _UNICODE.
  25. * 03-07-95 GJF Use _[un]lock_str2 instead of _[un]lock_str. Also,
  26. * removed useless local and macros.
  27. * 03-02-98 GJF Exception-safe locking.
  28. *
  29. *******************************************************************************/
  30. #include <cruntime.h>
  31. #include <stdio.h>
  32. #include <dbgint.h>
  33. #include <file2.h>
  34. #include <mtdll.h>
  35. #include <tchar.h>
  36. /***
  37. *char *gets(string) - read a line from stdin
  38. *
  39. *Purpose:
  40. * Gets a string from stdin terminated by '\n' or EOF; don't include '\n';
  41. * append '\0'.
  42. *
  43. *Entry:
  44. * char *string - place to store read string, assumes enough room.
  45. *
  46. *Exit:
  47. * returns string, filled in with the line of input
  48. * null string if \n found immediately
  49. * NULL if EOF found immediately
  50. *
  51. *Exceptions:
  52. *
  53. *******************************************************************************/
  54. _TCHAR * __cdecl _getts (
  55. _TCHAR *string
  56. )
  57. {
  58. int ch;
  59. _TCHAR *pointer = string;
  60. _TCHAR *retval = string;
  61. _ASSERTE(string != NULL);
  62. #ifdef _MT
  63. _lock_str2(0, stdin);
  64. __try {
  65. #endif
  66. #ifdef _UNICODE
  67. while ((ch = _getwchar_lk()) != L'\n')
  68. #else
  69. while ((ch = _getchar_lk()) != '\n')
  70. #endif
  71. {
  72. if (ch == _TEOF)
  73. {
  74. if (pointer == string)
  75. {
  76. retval = NULL;
  77. goto done;
  78. }
  79. break;
  80. }
  81. *pointer++ = (_TCHAR)ch;
  82. }
  83. *pointer = _T('\0');
  84. /* Common return */
  85. done:
  86. #ifdef _MT
  87. ; }
  88. __finally {
  89. _unlock_str2(0, stdin);
  90. }
  91. #endif
  92. return(retval);
  93. }