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.

114 lines
3.7 KiB

  1. /***
  2. *_getbuf.c - Get a stream buffer
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Allocate a buffer and init stream data bases.
  8. *
  9. *Revision History:
  10. * 11-06-87 JCR Initial version (split off from _filbuf.c)
  11. * 01-11-88 JCR Moved from mthread/dll only to main code
  12. * 06-06-88 JCR Optimized _iob2 references
  13. * 06-10-88 JCR Use near pointer to reference _iob[] entries
  14. * 07-27-88 GJF Added _cflush++ to force pre-terminator (necessary in
  15. * case stdout has been redirected to a file and acquires
  16. * a buffer here, and pre-terminator has not already been
  17. * forced).
  18. * 08-25-88 GJF Don't use FP_OFF() macro for the 386
  19. * 08-28-89 JCR Removed _NEAR_ for 386
  20. * 02-15-90 GJF _iob[], _iob2[] merge. Also, fixed copyright and
  21. * indents.
  22. * 03-16-90 GJF Replaced near cdecl with _CALLTYPE1, added #include
  23. * <cruntime.h> and removed #include <register.h>. Also,
  24. * removed some leftover 16-bit support.
  25. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  26. * 10-03-90 GJF New-style function declarator.
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 04-27-93 CFW Change _IONBF size to 2 bytes to hold wide char.
  29. * 05-11-93 GJF Replaced BUFSIZ with _INTERNAL_BUFSIZ.
  30. * 11-04-93 SRW Dont call malloc in _NTSUBSET_ version
  31. * 11-05-93 GJF Merged with NT SDK version (picked up _NTSUBSET_
  32. * stuff).
  33. * 04-05-94 GJF #ifdef-ed out _cflush reference for msvcrt*.dll, it
  34. * is unnecessary.
  35. * 01-10-95 CFW Debug CRT allocs.
  36. * 02-06-94 CFW assert -> _ASSERTE.
  37. * 02-17-95 GJF Merged in Mac version.
  38. * 05-17-99 PML Remove all Macintosh support.
  39. *
  40. *******************************************************************************/
  41. #include <cruntime.h>
  42. #include <stdio.h>
  43. #include <file2.h>
  44. #include <malloc.h>
  45. #include <internal.h>
  46. #include <dbgint.h>
  47. /***
  48. *_getbuf() - Allocate a buffer and init stream data bases
  49. *
  50. *Purpose:
  51. * Allocates a buffer for a stream and inits the stream data bases.
  52. *
  53. * [NOTE 1: This routine assumes the caller has already checked to make
  54. * sure the stream needs a buffer.
  55. *
  56. * [NOTE 2: Multi-thread - Assumes caller has aquired stream lock, if
  57. * needed.]
  58. *
  59. *Entry:
  60. * FILE *stream = stream to allocate a buffer for
  61. *
  62. *Exit:
  63. * void
  64. *
  65. *Exceptions:
  66. *
  67. *******************************************************************************/
  68. void __cdecl _getbuf (
  69. FILE *str
  70. )
  71. {
  72. REG1 FILE *stream;
  73. _ASSERTE(str != NULL);
  74. #if !defined(_NTSUBSET_) && !defined(CRTDLL)
  75. /* force library pre-termination procedure */
  76. _cflush++;
  77. #endif
  78. /* Init pointers */
  79. stream = str;
  80. #ifndef _NTSUBSET_
  81. /* Try to get a big buffer */
  82. if (stream->_base = _malloc_crt(_INTERNAL_BUFSIZ))
  83. {
  84. /* Got a big buffer */
  85. stream->_flag |= _IOMYBUF;
  86. stream->_bufsiz = _INTERNAL_BUFSIZ;
  87. }
  88. else {
  89. #endif /* _NTSUBSET_ */
  90. /* Did NOT get a buffer - use single char buffering. */
  91. stream->_flag |= _IONBF;
  92. stream->_base = (char *)&(stream->_charbuf);
  93. stream->_bufsiz = 2;
  94. #ifndef _NTSUBSET_
  95. }
  96. #endif /* _NTSUBSET_ */
  97. stream->_ptr = stream->_base;
  98. stream->_cnt = 0;
  99. return;
  100. }