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.

279 lines
10 KiB

  1. /***
  2. *write.c - write to a file handle
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _write() - write to a file handle
  8. *
  9. *Revision History:
  10. * 06-14-89 PHG Module created, based on asm version
  11. * 03-13-90 GJF Made calling type _CALLTYPE2 (for now), added #include
  12. * <cruntime.h>, fixed compiler warnings and fixed the
  13. * copyright. Also, cleaned up the formatting a bit.
  14. * 04-03-90 GJF Now _CALLTYPE1.
  15. * 07-24-90 SBM Removed '32' from API names
  16. * 08-14-90 SBM Compiles cleanly with -W3
  17. * 10-01-90 GJF New-style function declarators.
  18. * 12-04-90 GJF Appended Win32 version onto source with #ifdef-s.
  19. * Should come back latter and do a better merge.
  20. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  21. * 12-06-90 SRW Changed to use _osfile and _osfhnd instead of _osfinfo
  22. * 12-28-90 SRW Added _CRUISER_ conditional around check_stack pragma
  23. * 12-28-90 SRW Added cast of void * to char * for Mips C Compiler
  24. * 01-17-91 GJF ANSI naming.
  25. * 02-25-91 MHL Adapt to ReadFile/WriteFile changes (_WIN32_)
  26. * 04-09-91 PNT Added _MAC_ conditional
  27. * 07-18-91 GJF Removed unreferenced local variable from _write_lk
  28. * routine [_WIN32_].
  29. * 10-24-91 GJF Added LPDWORD casts to make MIPS compiler happy.
  30. * ASSUMES THAT sizeof(int) == sizeof(DWORD).
  31. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  32. * 02-15-92 GJF Increased BUF_SIZE and simplified LF translation code
  33. * for Win32.
  34. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  35. * 09-06-94 CFW Remove Cruiser support.
  36. * 09-06-94 CFW Replace MTHREAD with _MT.
  37. * 12-03-94 SKS Clean up OS/2 references
  38. * 01-04-95 GJF _WIN32_ -> _WIN32
  39. * 02-15-95 GJF Appended Mac version of source file (somewhat cleaned
  40. * up), with appropriate #ifdef-s.
  41. * 06-12-95 GJF Changed _osfile[] and _osfhnd[] to _osfile() and
  42. * _osfhnd(), which reference __pioinfo[].
  43. * 06-27-95 GJF Added check that the file handle is open.
  44. * 07-09-96 GJF Replaced defined(_WIN32) with !defined(_MAC) and
  45. * defined(_M_M68K) || defined(_M_MPPC) with
  46. * defined(_MAC). Also, detab-ed and cleaned up the
  47. * format a bit.
  48. * 12-30-97 GJF Exception-safe locking.
  49. * 03-03-98 RKP Forced number of bytes written to always be an int
  50. * 05-17-99 PML Remove all Macintosh support.
  51. * 11-10-99 GB Replaced lseek for lseeki64 so as to able to append
  52. * files longer than 4GB
  53. *
  54. *******************************************************************************/
  55. #include <cruntime.h>
  56. #include <oscalls.h>
  57. #include <io.h>
  58. #include <errno.h>
  59. #include <msdos.h>
  60. #include <mtdll.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include <internal.h>
  64. #define BUF_SIZE 1025 /* size of LF translation buffer */
  65. #define LF '\n' /* line feed */
  66. #define CR '\r' /* carriage return */
  67. #define CTRLZ 26 /* ctrl-z */
  68. /***
  69. *int _write(fh, buf, cnt) - write bytes to a file handle
  70. *
  71. *Purpose:
  72. * Writes count bytes from the buffer to the handle specified.
  73. * If the file was opened in text mode, each LF is translated to
  74. * CR-LF. This does not affect the return value. In text
  75. * mode ^Z indicates end of file.
  76. *
  77. * Multi-thread notes:
  78. * (1) _write() - Locks/unlocks file handle
  79. * _write_lk() - Does NOT lock/unlock file handle
  80. *
  81. *Entry:
  82. * int fh - file handle to write to
  83. * char *buf - buffer to write from
  84. * unsigned int cnt - number of bytes to write
  85. *
  86. *Exit:
  87. * returns number of bytes actually written.
  88. * This may be less than cnt, for example, if out of disk space.
  89. * returns -1 (and set errno) if fails.
  90. *
  91. *Exceptions:
  92. *
  93. *******************************************************************************/
  94. #ifdef _MT
  95. /* define normal version that locks/unlocks, validates fh */
  96. int __cdecl _write (
  97. int fh,
  98. const void *buf,
  99. unsigned cnt
  100. )
  101. {
  102. int r; /* return value */
  103. /* validate handle */
  104. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  105. !(_osfile(fh) & FOPEN) )
  106. {
  107. /* out of range -- return error */
  108. errno = EBADF;
  109. _doserrno = 0; /* not o.s. error */
  110. return -1;
  111. }
  112. _lock_fh(fh); /* lock file */
  113. __try {
  114. if ( _osfile(fh) & FOPEN )
  115. r = _write_lk(fh, buf, cnt); /* write bytes */
  116. else {
  117. errno = EBADF;
  118. _doserrno = 0; /* not o.s. error */
  119. r = -1;
  120. }
  121. }
  122. __finally {
  123. _unlock_fh(fh); /* unlock file */
  124. }
  125. return r;
  126. }
  127. /* now define version that doesn't lock/unlock, validate fh */
  128. int __cdecl _write_lk (
  129. int fh,
  130. const void *buf,
  131. unsigned cnt
  132. )
  133. {
  134. int lfcount; /* count of line feeds */
  135. int charcount; /* count of chars written so far */
  136. int written; /* count of chars written on this write */
  137. ULONG dosretval; /* o.s. return value */
  138. char ch; /* current character */
  139. char *p, *q; /* pointers into buf and lfbuf resp. */
  140. char lfbuf[BUF_SIZE]; /* lf translation buffer */
  141. #else
  142. /* now define normal version */
  143. int __cdecl _write (
  144. int fh,
  145. const void *buf,
  146. unsigned cnt
  147. )
  148. {
  149. int lfcount; /* count of line feeds */
  150. int charcount; /* count of chars written so far */
  151. int written; /* count of chars written on this write */
  152. ULONG dosretval; /* o.s. return value */
  153. char ch; /* current character */
  154. char *p, *q; /* pointers into buf and lfbuf resp. */
  155. char lfbuf[BUF_SIZE]; /* lf translation buffer */
  156. /* validate handle */
  157. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  158. !(_osfile(fh) & FOPEN) )
  159. {
  160. /* out of range -- return error */
  161. errno = EBADF;
  162. _doserrno = 0; /* not o.s. error */
  163. return -1;
  164. }
  165. #endif
  166. lfcount = charcount = 0; /* nothing written yet */
  167. if (cnt == 0)
  168. return 0; /* nothing to do */
  169. if (_osfile(fh) & FAPPEND) {
  170. /* appending - seek to end of file; ignore error, because maybe
  171. file doesn't allow seeking */
  172. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  173. (void)_lseeki64_lk(fh, 0, FILE_END);
  174. #else
  175. (void)_lseek_lk(fh, 0, FILE_END);
  176. #endif
  177. }
  178. /* check for text mode with LF's in the buffer */
  179. if ( _osfile(fh) & FTEXT ) {
  180. /* text mode, translate LF's to CR/LF's on output */
  181. p = (char *)buf; /* start at beginning of buffer */
  182. dosretval = 0; /* no OS error yet */
  183. while ( (unsigned)(p - (char *)buf) < cnt ) {
  184. q = lfbuf; /* start at beginning of lfbuf */
  185. /* fill the lf buf, except maybe last char */
  186. while ( q - lfbuf < BUF_SIZE - 1 &&
  187. (unsigned)(p - (char *)buf) < cnt ) {
  188. ch = *p++;
  189. if ( ch == LF ) {
  190. ++lfcount;
  191. *q++ = CR;
  192. }
  193. *q++ = ch;
  194. }
  195. /* write the lf buf and update total */
  196. if ( WriteFile( (HANDLE)_osfhnd(fh),
  197. lfbuf,
  198. (int)(q - lfbuf),
  199. (LPDWORD)&written,
  200. NULL) )
  201. {
  202. charcount += written;
  203. if (written < q - lfbuf)
  204. break;
  205. }
  206. else {
  207. dosretval = GetLastError();
  208. break;
  209. }
  210. }
  211. }
  212. else {
  213. /* binary mode, no translation */
  214. if ( WriteFile( (HANDLE)_osfhnd(fh),
  215. (LPVOID)buf,
  216. cnt,
  217. (LPDWORD)&written,
  218. NULL) )
  219. {
  220. dosretval = 0;
  221. charcount = written;
  222. }
  223. else
  224. dosretval = GetLastError();
  225. }
  226. if (charcount == 0) {
  227. /* If nothing was written, first check if an o.s. error,
  228. otherwise we return -1 and set errno to ENOSPC,
  229. unless a device and first char was CTRL-Z */
  230. if (dosretval != 0) {
  231. /* o.s. error happened, map error */
  232. if (dosretval == ERROR_ACCESS_DENIED) {
  233. /* wrong read/write mode should return EBADF, not
  234. EACCES */
  235. errno = EBADF;
  236. _doserrno = dosretval;
  237. }
  238. else
  239. _dosmaperr(dosretval);
  240. return -1;
  241. }
  242. else if ((_osfile(fh) & FDEV) && *(char *)buf == CTRLZ)
  243. return 0;
  244. else {
  245. errno = ENOSPC;
  246. _doserrno = 0; /* no o.s. error */
  247. return -1;
  248. }
  249. }
  250. else
  251. /* return adjusted bytes written */
  252. return charcount - lfcount;
  253. }