Source code of Windows XP (NT5)
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.

162 lines
4.5 KiB

  1. /***
  2. *setmode.c - set file translation mode
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defined _setmode() - set file translation mode of a file
  8. *
  9. *Revision History:
  10. * 08-16-84 RN initial version
  11. * 10-29-87 JCR Multi-thread support
  12. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  13. * 05-25-88 PHG Merged DLL and normal versions
  14. * 03-13-90 GJF Made calling type _CALLTYPE1, added #include
  15. * <cruntime.h>, removed #include <register.h> and
  16. * fixed the copyright. Also, cleaned up the formatting
  17. * a bit.
  18. * 04-04-90 GJF Added #include <io.h>.
  19. * 10-01-90 GJF New-style function declarators.
  20. * 12-04-90 GJF Appended Win32 version onto the source with #ifdef-s.
  21. * Two versions should be merged together, the differences
  22. * are trivial.
  23. * 12-06-90 SRW Changed to use _osfile and _osfhnd instead of _osfinfo
  24. * 01-17-91 GJF ANSI naming.
  25. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  26. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  27. * 09-06-94 CFW Replace MTHREAD with _MT.
  28. * 01-04-95 GJF _WIN32_ -> _WIN32
  29. * 06-12-95 GJF Replaced _osfile[] with _osfile() (macro referencing
  30. * field in ioinfo struct).
  31. * 06-27-95 GJF Revised check that the file handle is open.
  32. * 07-09-96 GJF Replaced defined(_WIN32) with !defined(_MAC). Also,
  33. * detab-ed.
  34. * 08-01-96 RDK For PMac, add check for handle being open.
  35. * 12-29-97 GJF Exception-safe locking.
  36. * 05-17-99 PML Remove all Macintosh support.
  37. *
  38. *******************************************************************************/
  39. #include <cruntime.h>
  40. #include <stdio.h>
  41. #include <io.h>
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <msdos.h>
  45. #include <mtdll.h>
  46. #include <stddef.h>
  47. #include <internal.h>
  48. /***
  49. *int _setmode(fh, mode) - set file translation mode
  50. *
  51. *Purpose:
  52. * changes file mode to text/binary, depending on mode arg. this affects
  53. * whether read's and write's on the file translate between CRLF and LF
  54. * or is untranslated
  55. *
  56. *Entry:
  57. * int fh - file handle to change mode on
  58. * int mode - file translation mode (one of O_TEXT and O_BINARY)
  59. *
  60. *Exit:
  61. * returns old file translation mode
  62. * returns -1 and sets errno if fails
  63. *
  64. *Exceptions:
  65. *
  66. *******************************************************************************/
  67. #ifdef _MT /* multi-thread code calls _lk_setmode() */
  68. int __cdecl _setmode (
  69. int fh,
  70. int mode
  71. )
  72. {
  73. int retval;
  74. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  75. !(_osfile(fh) & FOPEN) )
  76. {
  77. errno = EBADF;
  78. return(-1);
  79. }
  80. /* lock the file */
  81. _lock_fh(fh);
  82. __try {
  83. if ( _osfile(fh) & FOPEN )
  84. /* set the text/binary mode */
  85. retval = _setmode_lk(fh, mode);
  86. else {
  87. errno = EBADF;
  88. retval = -1;
  89. }
  90. }
  91. __finally {
  92. /* unlock the file */
  93. _unlock_fh(fh);
  94. }
  95. /* Return to user (_setmode_lk sets errno, if needed) */
  96. return(retval);
  97. }
  98. /***
  99. *_setmode_lk() - Perform core setmode operation
  100. *
  101. *Purpose:
  102. * Core setmode code. Assumes:
  103. * (1) Caller has validated fh to make sure it's in range.
  104. * (2) Caller has locked the file handle.
  105. *
  106. * [See _setmode() description above.]
  107. *
  108. *Entry: [Same as _setmode()]
  109. *
  110. *Exit: [Same as _setmode()]
  111. *
  112. *Exceptions:
  113. *
  114. *******************************************************************************/
  115. int __cdecl _setmode_lk (
  116. REG1 int fh,
  117. int mode
  118. )
  119. {
  120. int oldmode;
  121. #else /* non multi-thread code */
  122. int __cdecl _setmode (
  123. REG1 int fh,
  124. int mode
  125. )
  126. {
  127. int oldmode;
  128. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  129. !(_osfile(fh) & FOPEN) )
  130. {
  131. errno = EBADF;
  132. return(-1);
  133. }
  134. #endif /* now join common code */
  135. oldmode = _osfile(fh) & FTEXT;
  136. if (mode == _O_BINARY)
  137. _osfile(fh) &= ~FTEXT;
  138. else if (mode == _O_TEXT)
  139. _osfile(fh) |= FTEXT;
  140. else {
  141. errno = EINVAL;
  142. return(-1);
  143. }
  144. return(oldmode ? _O_TEXT : _O_BINARY);
  145. }