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

  1. /***
  2. *setmode.c - set file translation mode
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defined _setmode() - set file translation mode of a file
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <msdos.h>
  17. #include <mtdll.h>
  18. #include <stddef.h>
  19. #include <internal.h>
  20.  
  21. /***
  22. *int _setmode(fh, mode) - set file translation mode
  23. *
  24. *Purpose:
  25. *       changes file mode to text/binary, depending on mode arg. this affects
  26. *       whether read's and write's on the file translate between CRLF and LF
  27. *       or is untranslated
  28. *
  29. *Entry:
  30. *       int fh - file handle to change mode on
  31. *       int mode - file translation mode (one of O_TEXT and O_BINARY)
  32. *
  33. *Exit:
  34. *       returns old file translation mode
  35. *       returns -1 and sets errno if fails
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. #ifdef _MT
  42.  
  43. int __cdecl _setmode (
  44.         int fh,
  45.         int mode
  46.         )
  47. {
  48.         int retval;
  49. #ifndef _MAC
  50.         if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  51.              !(_osfile(fh) & FOPEN) )
  52. #else  /* _MAC */
  53.         if (fh < 0 || fh >= _nfile)
  54. #endif  /* _MAC */
  55.         {
  56.                 errno = EBADF;
  57.                 return(-1);
  58.         }
  59.  
  60.         /* lock the file */
  61.         _lock_fh(fh);
  62.  
  63.         /* set the text/binary mode */
  64.         retval = _setmode_lk(fh, mode);
  65.  
  66.         /* unlock the file */
  67.         _unlock_fh(fh);
  68.  
  69.         /* Return to user (_setmode_lk sets errno, if needed) */
  70.         return(retval);
  71.  
  72. }
  73.  
  74. /***
  75. *_setmode_lk() - Perform core setmode operation
  76. *
  77. *Purpose:
  78. *       Core setmode code.  Assumes:
  79. *       (1) Caller has validated fh to make sure it's in range.
  80. *       (2) Caller has locked the file handle.
  81. *
  82. *       [See _setmode() description above.]
  83. *
  84. *Entry: [Same as _setmode()]
  85. *
  86. *Exit:  [Same as _setmode()]
  87. *
  88. *Exceptions:
  89. *
  90. *******************************************************************************/
  91.  
  92. int __cdecl _setmode_lk (
  93.         REG1 int fh,
  94.         int mode
  95.         )
  96. {
  97.         int oldmode;
  98.  
  99. #else  /* _MT */
  100.  
  101. int __cdecl _setmode (
  102.         REG1 int fh,
  103.         int mode
  104.         )
  105. {
  106.         int oldmode;
  107.  
  108. #ifndef _MAC
  109.         if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  110.              !(_osfile(fh) & FOPEN) )
  111. #else  /* _MAC */
  112.         if ( (fh < 0 || fh >= _nfile) ||
  113.              !(_osfile[fh] & FOPEN) )
  114. #endif  /* _MAC */
  115.         {
  116.                 errno = EBADF;
  117.                 return(-1);
  118.         }
  119.  
  120. #endif  /* _MT */
  121.  
  122. #ifndef _MAC
  123.         oldmode = _osfile(fh) & FTEXT;
  124.  
  125.         if (mode == _O_BINARY)
  126.                 _osfile(fh) &= ~FTEXT;
  127.         else if (mode == _O_TEXT)
  128.                 _osfile(fh) |= FTEXT;
  129.         else    {
  130.                 errno = EINVAL;
  131.                 return(-1);
  132.         }
  133. #else  /* _MAC */
  134.         oldmode = _osfile[fh] & FTEXT;
  135.  
  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. #endif  /* _MAC */
  145.  
  146.         return(oldmode ? _O_TEXT : _O_BINARY);
  147.  
  148. }
  149.