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.

132 lines
3.9 KiB

  1. /***
  2. *fgets.c - get string from a file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines fgets() - read a string from a file
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 04-16-87 JCR changed count from an unsigned int to an int (ANSI)
  12. * and modified comparisons accordingly
  13. * 11-06-87 JCR Multi-thread support
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 05-31-88 PHG Merged DLL and normal versions
  16. * 06-14-88 JCR Use near pointer to reference _iob[] entries
  17. * 08-24-88 GJF Don't use FP_OFF() macro for the 386
  18. * 08-28-89 JCR Removed _NEAR_ for 386
  19. * 02-15-90 GJF Fixed copyright and indents
  20. * 03-19-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  21. * <cruntime.h> and added #include <register.h>. Also,
  22. * removed some leftover 16-bit support.
  23. * 07-24-90 SBM Replaced <assertm.h> by <assert.h>
  24. * 08-14-90 SBM Compiles cleanly with -W3
  25. * 10-02-90 GJF New-style function declarator.
  26. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  27. * 10-01-93 CFW Enable for fgetws().
  28. * 12-07-93 CFW Change _TCHAR to _TSCHAR.
  29. * 09-06-94 CFW Replace MTHREAD with _MT.
  30. * 02-06-94 CFW assert -> _ASSERTE.
  31. * 02-22-95 GJF Replaced WPRFLAG with _UNICODE.
  32. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  33. * 02-27-98 GJF Exception-safe locking.
  34. *
  35. *******************************************************************************/
  36. #include <cruntime.h>
  37. #include <stdio.h>
  38. #include <dbgint.h>
  39. #include <file2.h>
  40. #include <internal.h>
  41. #include <mtdll.h>
  42. #include <tchar.h>
  43. /***
  44. *char *fgets(string, count, stream) - input string from a stream
  45. *
  46. *Purpose:
  47. * get a string, up to count-1 chars or '\n', whichever comes first,
  48. * append '\0' and put the whole thing into string. the '\n' IS included
  49. * in the string. if count<=1 no input is requested. if EOF is found
  50. * immediately, return NULL. if EOF found after chars read, let EOF
  51. * finish the string as '\n' would.
  52. *
  53. *Entry:
  54. * char *string - pointer to place to store string
  55. * int count - max characters to place at string (include \0)
  56. * FILE *stream - stream to read from
  57. *
  58. *Exit:
  59. * returns string with text read from file in it.
  60. * if count <= 0 return NULL
  61. * if count == 1 put null string in string
  62. * returns NULL if error or end-of-file found immediately
  63. *
  64. *Exceptions:
  65. *
  66. *******************************************************************************/
  67. #ifdef _UNICODE
  68. wchar_t * __cdecl fgetws (
  69. #else
  70. char * __cdecl fgets (
  71. #endif
  72. _TSCHAR *string,
  73. int count,
  74. FILE *str
  75. )
  76. {
  77. REG1 FILE *stream;
  78. REG2 _TSCHAR *pointer = string;
  79. _TSCHAR *retval = string;
  80. int ch;
  81. _ASSERTE(string != NULL);
  82. _ASSERTE(str != NULL);
  83. if (count <= 0)
  84. return(NULL);
  85. /* Init stream pointer */
  86. stream = str;
  87. #ifdef _MT
  88. _lock_str(stream);
  89. __try {
  90. #endif
  91. while (--count)
  92. {
  93. #ifdef _UNICODE
  94. if ((ch = _getwc_lk(stream)) == WEOF)
  95. #else
  96. if ((ch = _getc_lk(stream)) == EOF)
  97. #endif
  98. {
  99. if (pointer == string) {
  100. retval=NULL;
  101. goto done;
  102. }
  103. break;
  104. }
  105. if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))
  106. break;
  107. }
  108. *pointer = _T('\0');
  109. /* Common return */
  110. done:
  111. #ifdef _MT
  112. ; }
  113. __finally {
  114. _unlock_str(stream);
  115. }
  116. #endif
  117. return(retval);
  118. }