update for HEAD-2003091401
[reactos.git] / lib / msvcrt / io / write.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/msvcrt/io/write.c
5  * PURPOSE:     Writes to a file
6  * PROGRAMER:   Boudewijn Dekker
7  * UPDATE HISTORY:
8  *              28/12/98: Created
9  */
10 #include <windows.h>
11 #include <msvcrt/io.h>
12 #include <msvcrt/stdlib.h>
13 #include <msvcrt/internal/file.h>
14
15 #define NDEBUG
16 #include <msvcrt/msvcrtdbg.h>
17
18 #define BUFSIZE 4096
19 /*
20 void ReportLastError(void)
21 {
22     DWORD error = GetLastError();
23     if (error != ERROR_SUCCESS) {
24         PTSTR msg;
25         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
26             0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL)) {
27             printf("ReportLastError() %d - %s\n", error, msg);
28         } else {
29             printf("ReportLastError() %d - unknown error\n", error);
30         }
31         LocalFree(msg);
32     }
33 }
34  */
35 /*
36  * @implemented
37  */
38 size_t _write(int _fd, const void* _buf, size_t _nbyte)
39 {
40    char *tmp, *in, *out;
41    int result;
42    unsigned int count;
43    DWORD wbyte;
44
45    DPRINT("_write(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte);
46    if (__fileno_getmode(_fd) & O_TEXT) {
47       result = _nbyte; 
48       tmp = (char*) malloc(BUFSIZE);
49       if (tmp == NULL) {
50          return -1;
51       }
52       count = BUFSIZE;
53       out = tmp;
54       in = (char*) _buf;
55       while (_nbyte--) {
56          if (*in == 0x0a) {
57             *out++ = 0x0d;
58             count--;
59             if (count == 0) {
60                 if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE, &wbyte, NULL)) {
61                    //ReportLastError();
62                    result = -1;
63                    break;
64                 }
65                 if (wbyte < BUFSIZE) {
66                    result = in - (char*)_buf;
67                    break;
68                 }
69                 count = BUFSIZE;
70                 out = tmp;
71             }
72          }
73          *out++ = *in++;
74          count--;
75          if (count == 0 || _nbyte == 0) {
76             int tmp_len_debug = strlen(tmp);
77             if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE - count, &wbyte, NULL)) {
78                //ReportLastError();
79                result = -1; 
80                tmp_len_debug = 0;
81                break;
82             }
83             if (wbyte < (BUFSIZE - count)) {
84                result = in - (char*)_buf;
85                break;
86             }
87             count = BUFSIZE;
88             out = tmp;
89          }
90       }
91       free(tmp);
92       return result;
93    } else {
94       if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL)) {
95           //ReportLastError();
96           return -1;
97       }
98       return wbyte;
99    }
100 }