X-Git-Url: http://git.jankratochvil.net/?p=reactos.git;a=blobdiff_plain;f=lib%2Fmsvcrt%2Fio%2Fwrite.c;h=609486c24e1aac9f511c1b673b84a7383a2b08e2;hp=299d3042ef1eeb20ded3a0e89798d4fa94c51edc;hb=03af8776dc14167b078911b0c7c5327d1bcdd128;hpb=f4077c1bf64ef89d74a8d4822d2d7aada3ba9927 diff --git a/lib/msvcrt/io/write.c b/lib/msvcrt/io/write.c index 299d304..609486c 100644 --- a/lib/msvcrt/io/write.c +++ b/lib/msvcrt/io/write.c @@ -9,79 +9,88 @@ */ #include #include +#include #include #define NDEBUG #include -#define BUFSIZE 4096 - -size_t _write(int _fd, const void *_buf, size_t _nbyte) +#define BUFSIZE 4096 +/* +void ReportLastError(void) +{ + DWORD error = GetLastError(); + if (error != ERROR_SUCCESS) { + PTSTR msg; + if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, + 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL)) { + printf("ReportLastError() %d - %s\n", error, msg); + } else { + printf("ReportLastError() %d - unknown error\n", error); + } + LocalFree(msg); + } +} + */ +size_t _write(int _fd, const void* _buf, size_t _nbyte) { char *tmp, *in, *out; - int count, result; + int result; + unsigned int count; DWORD wbyte; DPRINT("_write(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte); - if (__fileno_getmode(_fd) & O_TEXT) - { + if (__fileno_getmode(_fd) & O_TEXT) { result = _nbyte; tmp = (char*) malloc(BUFSIZE); - if (tmp == NULL) - { - return -1; + if (tmp == NULL) { + return -1; } count = BUFSIZE; out = tmp; in = (char*) _buf; - while (_nbyte--) - { - if (*in == 0x0a) - { - *out++ = 0x0d; - count--; - if (count == 0) - { - if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE, &wbyte, NULL)) - { - result = -1; - break; - } - if (wbyte < BUFSIZE) - { - result = in - (char*)_buf; - break; - } - count = BUFSIZE; - out = tmp; - } - } - *out++ = *in++; - count--; - if (count == 0 || _nbyte == 0) - { - if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE - count, &wbyte, NULL)) - { - result = -1; - break; - } - if (wbyte < BUFSIZE - count) - { - result = in - (char*)_buf; - break; - } - count = BUFSIZE; - out = tmp; - } + while (_nbyte--) { + if (*in == 0x0a) { + *out++ = 0x0d; + count--; + if (count == 0) { + if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE, &wbyte, NULL)) { + //ReportLastError(); + result = -1; + break; + } + if (wbyte < BUFSIZE) { + result = in - (char*)_buf; + break; + } + count = BUFSIZE; + out = tmp; + } + } + *out++ = *in++; + count--; + if (count == 0 || _nbyte == 0) { + int tmp_len_debug = strlen(tmp); + if (!WriteFile(_get_osfhandle(_fd), tmp, BUFSIZE - count, &wbyte, NULL)) { + //ReportLastError(); + result = -1; + tmp_len_debug = 0; + break; + } + if (wbyte < (BUFSIZE - count)) { + result = in - (char*)_buf; + break; + } + count = BUFSIZE; + out = tmp; + } } free(tmp); return result; - } - else - { - if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL)) - { - return -1; + } else { + if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL)) { + //ReportLastError(); + return -1; } return wbyte; }