diff --git a/reactos/apps/testsets/msvcrt/fileio/_tfileio.c b/reactos/apps/testsets/msvcrt/fileio/_tfileio.c index 586d765624c..1de03040438 100644 --- a/reactos/apps/testsets/msvcrt/fileio/_tfileio.c +++ b/reactos/apps/testsets/msvcrt/fileio/_tfileio.c @@ -49,8 +49,9 @@ extern BOOL verbose_flagged; extern BOOL status_flagged; -extern TCHAR test_buffer[TEST_BUFFER_SIZE]; +//extern TCHAR test_buffer[TEST_BUFFER_SIZE]; +static TCHAR test_buffer[TEST_BUFFER_SIZE]; static TCHAR dos_data[] = _T("line1: this is a bunch of readable text.\r\n")\ _T("line2: some more printable text and punctuation !@#$%^&*()\r\n")\ @@ -111,31 +112,33 @@ static BOOL verify_output_file(TCHAR* file_name, TCHAR* file_mode, TCHAR* file_d _tprintf(_T("ERROR: (%s) Can't open file for reading\n"), file_name); _tprintf(_T("ERROR: ferror returned %d\n"), ferror(file)); return FALSE; - } else if (verbose_flagged) { + } else if (status_flagged) { _tprintf(_T("STATUS: (%s) opened file for reading\n"), file_name); } #ifndef _NO_NEW_DEPENDS_ while (_fgetts(test_buffer, TEST_BUFFER_SIZE, file)) { int length = _tcslen(test_buffer); + int req_len = _tcschr(file_data+offset, _T('\n')) - (file_data+offset) + 1; + ++line_num; - if (verbose_flagged) { + if (length > req_len) { + _tprintf(_T("ERROR: read excess bytes from line %d, length %d, but expected %d\n"), line_num, length, req_len); + error_flagged = TRUE; + break; + } + if (status_flagged) { _tprintf(_T("STATUS: Verifying %d bytes read from line %d\n"), length, line_num); } - if (_tcsncmp(test_buffer, file_data+offset, length) == 0) { + if (_tcsncmp(test_buffer, file_data+offset, length - 1) == 0) { result = TRUE; } else { - if (verbose_flagged) { + if (status_flagged) { int i; _tprintf(_T("WARNING: (%s) failed to verify file\n"), file_name); - //_tprintf(_T("expected: \"%s\"\n"), file_data+offset); - //_tprintf(_T(" found: \"%s\"\n"), test_buffer); - _tprintf(_T("expected: ")); - for (i = 0; i < length, i < 10; i++) { - _tprintf(_T("0x%02x "), file_data+offset+i); - } - _tprintf(_T("\n found: ")); - for (i = 0; i < length, i < 10; i++) { - _tprintf(_T("0x%02x "), test_buffer+i); + for (i = 0; i < length; i++) { + if (file_data[offset+i] != test_buffer[i]) { + _tprintf(_T("line %d, offset %d expected: 0x%04x found: 0x%04x\n"), line_num, i, (int)file_data[offset+i], (int)test_buffer[i]); + } } _tprintf(_T("\n")); } else { @@ -196,7 +199,6 @@ static int check_file_size(TCHAR* file_name, TCHAR* file_mode, int expected) //_tprintf(_T("STATUS: (%s) checking for %d bytes in %s mode\n"), file_name, expected, _tcschr(file_mode, _T('b')) ? _T("binary") : _T("text")); _tprintf(_T("STATUS: (%s) checking for %d bytes with mode %s\n"), file_name, expected, file_mode); } - file = _tfopen(file_name, file_mode); if (file == NULL) { _tprintf(_T("ERROR: (%s) failed to open file for reading\n"), file_name); diff --git a/reactos/apps/testsets/msvcrt/fileio/main.c b/reactos/apps/testsets/msvcrt/fileio/main.c index 4ffef18b315..50022dfb762 100644 --- a/reactos/apps/testsets/msvcrt/fileio/main.c +++ b/reactos/apps/testsets/msvcrt/fileio/main.c @@ -32,14 +32,10 @@ #define VERSION 1 -#define TEST_BUFFER_SIZE 200 - #ifdef UNICODE #define TARGET "UNICODE" -w_char_t test_buffer[TEST_BUFFER_SIZE]; #else #define TARGET "MBCS" -char test_buffer[TEST_BUFFER_SIZE*2]; #endif BOOL verbose_flagged = 0; @@ -145,10 +141,18 @@ int __cdecl main(int argc, char* argv[]) printf("finished\n"); return result; } +/* +ANSI: + all tests passing +UNICODE: + writing binary files short one byte. ie. odd number file lengths. + reading text files returns one extra byte. + + */ #ifndef __GNUC__ -char* args[] = { "fileio.exe", "0", "ansi", "verbose"}; +char* args[] = { "fileio.exe", "0", "unicode", "verbose"}; char* args1[] = { "fileio.exe", "1", "unicode" }; char* args2[] = { "fileio.exe", "2", "unicode" }; diff --git a/reactos/lib/msvcrt/Makefile b/reactos/lib/msvcrt/Makefile index 451563d1a71..d31f734fb8c 100644 --- a/reactos/lib/msvcrt/Makefile +++ b/reactos/lib/msvcrt/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.28 2002/11/29 15:59:02 robd Exp $ +# $Id: Makefile,v 1.29 2002/12/05 15:30:43 robd Exp $ PATH_TO_TOP = ../.. @@ -266,7 +266,8 @@ PROCESS_OBJECTS = \ process/process.o \ process/procid.o \ process/thread.o \ - process/threadid.o + process/threadid.o \ + process/threadx.o SEARCH_OBJECTS = \ search/lfind.o \ diff --git a/reactos/lib/msvcrt/io/open.c b/reactos/lib/msvcrt/io/open.c index d3866eb815c..7cb7969fa4d 100644 --- a/reactos/lib/msvcrt/io/open.c +++ b/reactos/lib/msvcrt/io/open.c @@ -1,4 +1,4 @@ -/* $Id: open.c,v 1.12 2002/11/24 18:42:22 robd Exp $ +/* $Id: open.c,v 1.13 2002/12/05 15:30:44 robd Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -29,6 +29,8 @@ #define NDEBUG #include +//#define _OLD_BUILD_ + #define STD_AUX_HANDLE 3 #define STD_PRINTER_HANDLE 4 @@ -39,11 +41,11 @@ typedef struct _fileno_modes_type int fd; } fileno_modes_type; -static fileno_modes_type *fileno_modes = NULL; +static fileno_modes_type* fileno_modes = NULL; int maxfno = 0; -char __is_text_file(FILE *p) +char __is_text_file(FILE* p) { if ( p == NULL || fileno_modes == NULL ) return FALSE; @@ -71,12 +73,11 @@ int _open(const char* _path, int _oflag,...) // DPRINT("_open('%s', %x, (%x))\n", _path, _oflag, pmode); - if (( _oflag & S_IREAD ) == S_IREAD) + if ((_oflag & S_IREAD ) == S_IREAD) dwShareMode = FILE_SHARE_READ; - else if ( ( _oflag & S_IWRITE) == S_IWRITE ) { + else if ((_oflag & S_IWRITE) == S_IWRITE) { dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; } - /* * * _O_BINARY Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.) @@ -84,95 +85,91 @@ int _open(const char* _path, int _oflag,...) * * _O_APPEND Moves file pointer to end of file before every write operation. */ - if (( _oflag & _O_RDWR ) == _O_RDWR ) - dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ ; - else if (( _oflag & O_RDONLY ) == O_RDONLY ) - dwDesiredAccess |= GENERIC_READ ; - else if (( _oflag & _O_WRONLY ) == _O_WRONLY ) +#ifdef _OLD_BUILD_ + if ((_oflag & _O_RDWR) == _O_RDWR) + dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ; + else if ((_oflag & O_RDONLY) == O_RDONLY) + dwDesiredAccess |= GENERIC_READ; + else if ((_oflag & _O_WRONLY) == _O_WRONLY) dwDesiredAccess |= GENERIC_WRITE ; - - if (( _oflag & S_IREAD ) == S_IREAD ) +#else + if ((_oflag & _O_WRONLY) == _O_WRONLY ) + dwDesiredAccess |= GENERIC_WRITE ; + else if ((_oflag & _O_RDWR) == _O_RDWR ) + dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ; + else //if ((_oflag & O_RDONLY) == O_RDONLY) + dwDesiredAccess |= GENERIC_READ; +#endif + if (( _oflag & S_IREAD ) == S_IREAD) dwShareMode |= FILE_SHARE_READ; - if (( _oflag & S_IWRITE ) == S_IWRITE ) - dwShareMode |= FILE_SHARE_WRITE; + if (( _oflag & S_IWRITE ) == S_IWRITE) + dwShareMode |= FILE_SHARE_WRITE; - if (( _oflag & (_O_CREAT | _O_EXCL ) ) == (_O_CREAT | _O_EXCL) ) + if (( _oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL)) dwCreationDistribution |= CREATE_NEW; - else if (( _oflag & O_TRUNC ) == O_TRUNC ) { - if (( _oflag & O_CREAT ) == O_CREAT ) + else if ((_oflag & O_TRUNC ) == O_TRUNC) { + if ((_oflag & O_CREAT ) == O_CREAT) dwCreationDistribution |= CREATE_ALWAYS; - else if (( _oflag & O_RDONLY ) != O_RDONLY ) + else if ((_oflag & O_RDONLY ) != O_RDONLY) dwCreationDistribution |= TRUNCATE_EXISTING; } - else if (( _oflag & _O_APPEND ) == _O_APPEND ) + else if ((_oflag & _O_APPEND) == _O_APPEND) dwCreationDistribution |= OPEN_EXISTING; - else if (( _oflag & _O_CREAT ) == _O_CREAT ) + else if ((_oflag & _O_CREAT) == _O_CREAT) dwCreationDistribution |= OPEN_ALWAYS; else dwCreationDistribution |= OPEN_EXISTING; - - if (( _oflag & _O_RANDOM ) == _O_RANDOM ) - dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; - if (( _oflag & _O_SEQUENTIAL ) == _O_SEQUENTIAL ) - dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN; - - if (( _oflag & _O_TEMPORARY ) == _O_TEMPORARY ) - { - dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; - DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n"); - } - - if (( _oflag & _O_SHORT_LIVED ) == _O_SHORT_LIVED ) - { - dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; - DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n"); - } + if ((_oflag & _O_RANDOM) == _O_RANDOM ) + dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; + if ((_oflag & _O_SEQUENTIAL) == _O_SEQUENTIAL) + dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN; + if ((_oflag & _O_TEMPORARY) == _O_TEMPORARY) { + dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; + DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n"); + } + if ((_oflag & _O_SHORT_LIVED) == _O_SHORT_LIVED) { + dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; + DPRINT("FILE_FLAG_DELETE_ON_CLOSE\n"); + } if (_oflag & _O_NOINHERIT) sa.bInheritHandle = FALSE; - hFile = CreateFileA(_path, dwDesiredAccess, dwShareMode, - &sa, - dwCreationDistribution, + &sa, + dwCreationDistribution, dwFlagsAndAttributes, NULL); - if (hFile == (HANDLE)-1) - { + if (hFile == (HANDLE)-1) { dwLastError = GetLastError(); - if (dwLastError == ERROR_ALREADY_EXISTS) - { - DPRINT("ERROR_ALREADY_EXISTS\n"); - __set_errno(EEXIST); - } - else - { - DPRINT("%x\n", dwLastError); + if (dwLastError == ERROR_ALREADY_EXISTS) { + DPRINT("ERROR_ALREADY_EXISTS\n"); + __set_errno(EEXIST); + } else { + DPRINT("%x\n", dwLastError); __set_errno(ENOFILE); } return -1; } DPRINT("OK\n"); - if (!(_oflag & (_O_TEXT|_O_BINARY))) - { + if (!(_oflag & (_O_TEXT|_O_BINARY))) { _oflag |= _fmode; } - return __fileno_alloc(hFile,_oflag); + return __fileno_alloc(hFile,_oflag); } -int -__fileno_alloc(HANDLE hFile, int mode) +int __fileno_alloc(HANDLE hFile, int mode) { int i; /* Check for bogus values */ if (hFile < 0) return -1; - for(i=5;i= maxfno || fileno_modes[fileno].fd == -1) - { - return (void *)-1; + if (fileno < 0 || fileno >= maxfno || fileno_modes[fileno].fd == -1) { + return (void*)-1; } return fileno_modes[fileno].hFile; } @@ -216,12 +210,10 @@ void *filehnd(int fileno) int __fileno_setmode(int _fd, int _newmode) { int m; - if ( _fd < 0 || _fd >= maxfno ) - { + if (_fd < 0 || _fd >= maxfno) { __set_errno(EBADF); return -1; } - m = fileno_modes[_fd].mode; fileno_modes[_fd].mode = _newmode; return m; @@ -229,8 +221,7 @@ int __fileno_setmode(int _fd, int _newmode) int __fileno_getmode(int _fd) { - if ( _fd < 0 || _fd >= maxfno ) - { + if (_fd < 0 || _fd >= maxfno) { __set_errno(EBADF); return -1; } @@ -240,81 +231,72 @@ int __fileno_getmode(int _fd) int __fileno_close(int _fd) { - if ( _fd < 0 || _fd >= maxfno ) - { + if (_fd < 0 || _fd >= maxfno) { __set_errno(EBADF); return -1; } - fileno_modes[_fd].fd = -1; fileno_modes[_fd].hFile = (HANDLE)-1; return 0; } -int _open_osfhandle (void *osfhandle, int flags ) +int _open_osfhandle(void* osfhandle, int flags) { return __fileno_alloc((HANDLE)osfhandle, flags); } -void *_get_osfhandle( int fileno ) +void* _get_osfhandle( int fileno ) { return filehnd(fileno); } -int __fileno_dup2( int handle1, int handle2 ) +int __fileno_dup2(int handle1, int handle2) { HANDLE hProcess; BOOL result; - if (handle1 >= maxfno || handle1 < 0 || handle2 >= maxfno || handle2 < 0 ) - { + if (handle1 >= maxfno || handle1 < 0 || handle2 >= maxfno || handle2 < 0) { __set_errno(EBADF); return -1; } - if (fileno_modes[handle1].fd == -1) - { + if (fileno_modes[handle1].fd == -1) { __set_errno(EBADF); return -1; } if (handle1 == handle2) return handle1; - if (fileno_modes[handle2].fd != -1) - { + if (fileno_modes[handle2].fd != -1) { _close(handle2); } hProcess = GetCurrentProcess(); result = DuplicateHandle(hProcess, - fileno_modes[handle1].hFile, + fileno_modes[handle1].hFile, hProcess, &fileno_modes[handle2].hFile, 0, TRUE, DUPLICATE_SAME_ACCESS); - if (result) - { + if (result) { fileno_modes[handle2].fd = handle2; fileno_modes[handle2].mode = fileno_modes[handle1].mode; - switch (handle2) - { - case 0: - SetStdHandle(STD_INPUT_HANDLE, fileno_modes[handle2].hFile); - break; - case 1: - SetStdHandle(STD_OUTPUT_HANDLE, fileno_modes[handle2].hFile); - break; - case 2: - SetStdHandle(STD_ERROR_HANDLE, fileno_modes[handle2].hFile); - break; - case 3: - SetStdHandle(STD_AUX_HANDLE, fileno_modes[handle2].hFile); - break; - case 4: - SetStdHandle(STD_AUX_HANDLE, fileno_modes[handle2].hFile); - break; + switch (handle2) { + case 0: + SetStdHandle(STD_INPUT_HANDLE, fileno_modes[handle2].hFile); + break; + case 1: + SetStdHandle(STD_OUTPUT_HANDLE, fileno_modes[handle2].hFile); + break; + case 2: + SetStdHandle(STD_ERROR_HANDLE, fileno_modes[handle2].hFile); + break; + case 3: + SetStdHandle(STD_AUX_HANDLE, fileno_modes[handle2].hFile); + break; + case 4: + SetStdHandle(STD_AUX_HANDLE, fileno_modes[handle2].hFile); + break; } return handle1; - } - else - { + } else { __set_errno(EMFILE); // Is this the correct error no.? return -1; } @@ -326,14 +308,12 @@ void* malloc(size_t sizeObject); BOOL __fileno_init(void) { ULONG count = 0, i; - HANDLE *pFile; + HANDLE* pFile; char* pmode; STARTUPINFO StInfo; GetStartupInfoA(&StInfo); - - if (StInfo.lpReserved2 && StInfo.cbReserved2 >= sizeof(ULONG)) - { + if (StInfo.lpReserved2 && StInfo.cbReserved2 >= sizeof(ULONG)) { count = *(ULONG*)StInfo.lpReserved2; /* if (sizeof(ULONG) + count * (sizeof(HANDLE) + sizeof(char)) != StInfo.cbReserved2) @@ -347,10 +327,11 @@ BOOL __fileno_init(void) maxfno += 255; { +#ifdef _OLD_BUILD_ // why was this here ???? - robd. - //int result; - //result = malloc(50); - + int result; + result = malloc(50); +#endif } //fileno_modes = (fileno_modes_type*)malloc(sizeof(fileno_modes_type) * maxfno); fileno_modes = malloc(sizeof(fileno_modes_type) * maxfno); @@ -358,53 +339,43 @@ BOOL __fileno_init(void) return FALSE; } memset(fileno_modes, -1, sizeof(fileno_modes_type) * maxfno); - - if (count) - { + if (count) { pFile = (HANDLE*)(StInfo.lpReserved2 + sizeof(ULONG) + count * sizeof(char)); pmode = (char*)(StInfo.lpReserved2 + sizeof(ULONG)); - for (i = 0; i < count; i++) - { - if (*pFile != INVALID_HANDLE_VALUE) - { + for (i = 0; i < count; i++) { + if (*pFile != INVALID_HANDLE_VALUE) { fileno_modes[i].fd = i; fileno_modes[i].mode = ((*pmode << 8) & (_O_TEXT|_O_BINARY)) | (*pmode & _O_ACCMODE); fileno_modes[i].hFile = *pFile; - } + } pFile++; pmode++; } } - - if (fileno_modes[0].fd == -1) - { + if (fileno_modes[0].fd == -1) { fileno_modes[0].fd = 0; fileno_modes[0].hFile = GetStdHandle(STD_INPUT_HANDLE); fileno_modes[0].mode = _O_RDONLY|_O_TEXT; } - if (fileno_modes[1].fd == -1) - { + if (fileno_modes[1].fd == -1) { fileno_modes[1].fd = 1; fileno_modes[1].hFile = GetStdHandle(STD_OUTPUT_HANDLE); fileno_modes[1].mode = _O_WRONLY|_O_TEXT; } - if (fileno_modes[2].fd == -1) - { + if (fileno_modes[2].fd == -1) { fileno_modes[2].fd = 2; fileno_modes[2].hFile = GetStdHandle(STD_ERROR_HANDLE); fileno_modes[2].mode = _O_WRONLY|_O_TEXT; } - if (fileno_modes[3].fd == -1) - { + if (fileno_modes[3].fd == -1) { fileno_modes[3].fd = 3; fileno_modes[3].hFile = GetStdHandle(STD_AUX_HANDLE); fileno_modes[3].mode = _O_WRONLY|_O_TEXT; } - if (fileno_modes[4].fd == -1) - { + if (fileno_modes[4].fd == -1) { fileno_modes[4].fd = 4; fileno_modes[4].hFile = GetStdHandle(STD_PRINTER_HANDLE); fileno_modes[4].mode = _O_WRONLY|_O_TEXT; } return TRUE; -} +} diff --git a/reactos/lib/msvcrt/io/wopen.c b/reactos/lib/msvcrt/io/wopen.c index f8bd7a29e25..117871a0a31 100644 --- a/reactos/lib/msvcrt/io/wopen.c +++ b/reactos/lib/msvcrt/io/wopen.c @@ -1,4 +1,4 @@ -/* $Id: wopen.c,v 1.1 2002/11/24 18:42:22 robd Exp $ +/* $Id: wopen.c,v 1.2 2002/12/05 15:30:44 robd Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -49,10 +49,10 @@ int _wopen(const wchar_t* _path, int _oflag, ...) // DPRINT("_wopen('%S', %x, (%x))\n", _path, _oflag, pmode); - if (( _oflag & S_IREAD ) == S_IREAD) + if ((_oflag & S_IREAD) == S_IREAD) dwShareMode = FILE_SHARE_READ; - else if ( ( _oflag & S_IWRITE) == S_IWRITE ) { - dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; + else if ( ( _oflag & S_IWRITE) == S_IWRITE) { + dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; } /* @@ -62,48 +62,61 @@ int _wopen(const wchar_t* _path, int _oflag, ...) * * _O_APPEND Moves file pointer to end of file before every write operation. */ - if (( _oflag & _O_RDWR ) == _O_RDWR ) +#if 0 + if ((_oflag & _O_RDWR) == _O_RDWR) dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES; - else if (( _oflag & O_RDONLY ) == O_RDONLY ) + else if ((_oflag & O_RDONLY) == O_RDONLY) dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES; - else if (( _oflag & _O_WRONLY ) == _O_WRONLY ) + else if ((_oflag & _O_WRONLY) == _O_WRONLY) dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES; +#else + if ((_oflag & _O_WRONLY) == _O_WRONLY) + dwDesiredAccess |= GENERIC_WRITE | FILE_WRITE_DATA | + FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES; + else if ((_oflag & _O_RDWR) == _O_RDWR) + dwDesiredAccess |= GENERIC_WRITE|GENERIC_READ | FILE_READ_DATA | + FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | + FILE_WRITE_ATTRIBUTES; + else //if ((_oflag & O_RDONLY) == O_RDONLY) + dwDesiredAccess |= GENERIC_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | + FILE_WRITE_ATTRIBUTES; +#endif - if (( _oflag & S_IREAD ) == S_IREAD ) + if ((_oflag & S_IREAD) == S_IREAD) dwShareMode |= FILE_SHARE_READ; - if (( _oflag & S_IWRITE ) == S_IWRITE ) + if ((_oflag & S_IWRITE) == S_IWRITE) dwShareMode |= FILE_SHARE_WRITE; - if (( _oflag & (_O_CREAT | _O_EXCL ) ) == (_O_CREAT | _O_EXCL) ) + if ((_oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL)) dwCreationDistribution |= CREATE_NEW; - else if (( _oflag & O_TRUNC ) == O_TRUNC ) { - if (( _oflag & O_CREAT ) == O_CREAT ) + else if ((_oflag & O_TRUNC) == O_TRUNC) { + if ((_oflag & O_CREAT) == O_CREAT) dwCreationDistribution |= CREATE_ALWAYS; - else if (( _oflag & O_RDONLY ) != O_RDONLY ) + else if ((_oflag & O_RDONLY) != O_RDONLY) dwCreationDistribution |= TRUNCATE_EXISTING; } - else if (( _oflag & _O_APPEND ) == _O_APPEND ) + else if ((_oflag & _O_APPEND) == _O_APPEND) dwCreationDistribution |= OPEN_EXISTING; - else if (( _oflag & _O_CREAT ) == _O_CREAT ) + else if ((_oflag & _O_CREAT) == _O_CREAT) dwCreationDistribution |= OPEN_ALWAYS; else dwCreationDistribution |= OPEN_EXISTING; - if (( _oflag & _O_RANDOM ) == _O_RANDOM ) + if ((_oflag & _O_RANDOM) == _O_RANDOM) dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; - if (( _oflag & _O_SEQUENTIAL ) == _O_SEQUENTIAL ) + if ((_oflag & _O_SEQUENTIAL) == _O_SEQUENTIAL) dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN; - if (( _oflag & _O_TEMPORARY ) == _O_TEMPORARY ) + if ((_oflag & _O_TEMPORARY) == _O_TEMPORARY) dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; - if (( _oflag & _O_SHORT_LIVED ) == _O_SHORT_LIVED ) + if ((_oflag & _O_SHORT_LIVED) == _O_SHORT_LIVED) dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE; if (_oflag & _O_NOINHERIT) @@ -121,7 +134,7 @@ int _wopen(const wchar_t* _path, int _oflag, ...) return __fileno_alloc(hFile,_oflag); } -int _wsopen(wchar_t *path, int access, int shflag, int mode) +int _wsopen(wchar_t* path, int access, int shflag, int mode) { - return _wopen((path), (access)|(shflag), (mode)); + return _wopen((path), (access)|(shflag), (mode)); } diff --git a/reactos/lib/msvcrt/io/write.c b/reactos/lib/msvcrt/io/write.c index ca1bb8f1c61..609486c24e1 100644 --- a/reactos/lib/msvcrt/io/write.c +++ b/reactos/lib/msvcrt/io/write.c @@ -15,9 +15,24 @@ #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 result; @@ -25,86 +40,57 @@ size_t _write(int _fd, const void *_buf, size_t _nbyte) 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)) - { - 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("_write(fd %d, buf %x, nbyte %d) - %s\n", _fd, _buf, _nbyte, msg); - else - printf("_write(fd %d, buf %x, nbyte %d) - unknown error: %d\n", _fd, _buf, _nbyte, msg, error); - LocalFree(msg); - } -/* -DWORD FormatMessage( - DWORD dwFlags, // source and processing options - LPCVOID lpSource, // message source - DWORD dwMessageId, // message identifier - DWORD dwLanguageId, // language identifier - LPTSTR lpBuffer, // message buffer - DWORD nSize, // maximum size of message buffer - va_list *Arguments // array of message inserts -); - */ - return -1; + } else { + if(!WriteFile(_get_osfhandle(_fd), _buf, _nbyte, &wbyte, NULL)) { + //ReportLastError(); + return -1; } return wbyte; } diff --git a/reactos/lib/msvcrt/mbstring/mbbtype.c b/reactos/lib/msvcrt/mbstring/mbbtype.c index 6ecb1935be4..327c72a9df9 100644 --- a/reactos/lib/msvcrt/mbstring/mbbtype.c +++ b/reactos/lib/msvcrt/mbstring/mbbtype.c @@ -1,7 +1,7 @@ /* * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries - * FILE: lib/crtdll/mbstring/mbbtype.c + * FILE: lib/msvcrt/mbstring/mbbtype.c * PURPOSE: Determines the type of a multibyte character * PROGRAMER: Boudewijn Dekker * UPDATE HISTORY: @@ -23,9 +23,7 @@ int _mbbtype(unsigned char c , int type) return _MBC_ILLEGAL; else return 0; - - } - else { + } else { if (( c >= 0x20 && c <= 0x7E ) || ( c >= 0xA1 && c <= 0xDF )) { return _MBC_SINGLE; } @@ -36,10 +34,7 @@ int _mbbtype(unsigned char c , int type) return _MBC_ILLEGAL; else return 0; - } - - return 0; } diff --git a/reactos/lib/msvcrt/misc/crtmain.c b/reactos/lib/msvcrt/misc/crtmain.c index 27ed7a90f6f..e3bb05aa5d3 100644 --- a/reactos/lib/msvcrt/misc/crtmain.c +++ b/reactos/lib/msvcrt/misc/crtmain.c @@ -1,4 +1,4 @@ -/* $Id: crtmain.c,v 1.2 2002/11/25 17:41:39 robd Exp $ +/* $Id: crtmain.c,v 1.3 2002/12/05 15:30:44 robd Exp $ * * ReactOS MSVCRT.DLL Compatibility Library */ @@ -17,9 +17,7 @@ int _fltused; /* FUNCTIONS **************************************************************/ -/* - */ -int +int STDCALL _except_handler3(void) { diff --git a/reactos/lib/msvcrt/stdio/filbuf.c b/reactos/lib/msvcrt/stdio/filbuf.c index 5893c44f12a..836f8b94802 100644 --- a/reactos/lib/msvcrt/stdio/filbuf.c +++ b/reactos/lib/msvcrt/stdio/filbuf.c @@ -11,46 +11,36 @@ #include #include -int _readcnv(int fn, void *buf, size_t siz ); +int _readcnv(int fn, void* buf, size_t siz); -int -_filbuf(FILE *f) +int _filbuf(FILE* f) { int size; char c; - if ( !OPEN4READING(f)) { __set_errno (EINVAL); return EOF; } - - if (f->_flag&(_IOSTRG|_IOEOF)) return EOF; f->_flag &= ~_IOUNGETC; - if (f->_base==NULL && (f->_flag&_IONBF)==0) { + if (f->_base == NULL && (f->_flag & _IONBF) == 0) { size = 4096; - if ((f->_base = malloc(size+1)) == NULL) - { + if ((f->_base = malloc(size+1)) == NULL) { // error ENOMEM f->_flag |= _IONBF; f->_flag &= ~(_IOFBF|_IOLBF); - } - else - { + } else { f->_flag |= _IOMYBUF; f->_bufsiz = size; } } - - if (f->_flag&_IONBF) f->_base = &c; - -// fush stdout before reading from stdin + // flush stdout before reading from stdin if (f == stdin) { if (stdout->_flag&_IOLBF) fflush(stdout); @@ -58,8 +48,8 @@ _filbuf(FILE *f) fflush(stderr); } -// if we have a dirty stream we flush it - if ( (f->_flag &_IODIRTY) == _IODIRTY ) + // if we have a dirty stream we flush it + if ((f->_flag &_IODIRTY) == _IODIRTY) fflush(f); @@ -99,6 +89,7 @@ _filbuf(FILE *f) } f->_cnt--; + return *f->_ptr++ & 0377; } diff --git a/reactos/lib/msvcrt/stdio/flsbuf.c b/reactos/lib/msvcrt/stdio/flsbuf.c index 569c3da618d..6d5ccb57644 100644 --- a/reactos/lib/msvcrt/stdio/flsbuf.c +++ b/reactos/lib/msvcrt/stdio/flsbuf.c @@ -10,147 +10,147 @@ #include -int cntcr(char *bufp, int bufsiz); -int convert(char *endp, int bufsiz,int n); -int _writecnv(int fn, void *buf, size_t bufsiz); +int cntcr(char* bufp, int bufsiz); +int convert(char* endp, int bufsiz, int n); +int _writecnv(int fn, void* buf, size_t bufsiz); -int _flsbuf(int c, FILE *f) +int _flsbuf(int c, FILE* f) { - char *base; - int n, rn; - char c1; - int size; + char* base; + int n, rn; + char c1; + int size; - if (!OPEN4WRITING(f)) { - __set_errno(EINVAL); - return EOF; - } - - // no file associated with buffer, this is a memory stream - if (fileno(f) == -1) { - return c; - } - - /* if the buffer is not yet allocated, allocate it */ - if ((base = f->_base) == NULL && (f->_flag & _IONBF) == 0) { - size = 4096; - if ((f->_base = base = malloc (size)) == NULL) { - f->_flag |= _IONBF; - f->_flag &= ~(_IOFBF|_IOLBF); - } else { - f->_flag |= _IOMYBUF; - f->_cnt = f->_bufsiz = size; - f->_ptr = base; - rn = 0; - if (f == stdout && isatty (fileno (stdout))) { - f->_flag |= _IOLBF; - } + if (!OPEN4WRITING(f)) { + __set_errno(EINVAL); + return EOF; } - } - if (f->_flag & _IOLBF) { - /* in line-buffering mode we get here on each character */ - *f->_ptr++ = c; - rn = f->_ptr - base; - if (c == '\n' || rn >= f->_bufsiz) { - /* time for real flush */ - f->_ptr = base; - f->_cnt = 0; - } else { - /* we got here because _cnt is wrong, so fix it */ - /* Negative _cnt causes all output functions to call */ - /* _flsbuf for each character, thus realizing line-buffering */ - f->_cnt = -rn; - return c; + // no file associated with buffer, this is a memory stream + if (fileno(f) == -1) { + return c; } - } else if (f->_flag & _IONBF) { - c1 = c; - rn = 1; - base = &c1; - f->_cnt = 0; - } else { /* _IOFBF */ - rn = f->_ptr - base; - f->_ptr = base; - if ( (f->_flag & _IOAHEAD) == _IOAHEAD ) - _lseek(fileno(f),-(rn+f->_cnt), SEEK_CUR); - f->_cnt = f->_bufsiz; - f->_flag &= ~_IOAHEAD; - } - f->_flag &= ~_IODIRTY; - while (rn > 0) { - n = _write(fileno(f), base, rn); - if (n <= 0) { - f->_flag |= _IOERR; - return EOF; + + /* if the buffer is not yet allocated, allocate it */ + if ((base = f->_base) == NULL && (f->_flag & _IONBF) == 0) { + size = 4096; + if ((f->_base = base = malloc(size)) == NULL) { + f->_flag |= _IONBF; + f->_flag &= ~(_IOFBF|_IOLBF); + } else { + f->_flag |= _IOMYBUF; + f->_cnt = f->_bufsiz = size; + f->_ptr = base; + rn = 0; + if (f == stdout && isatty(fileno(stdout))) { + f->_flag |= _IOLBF; + } + } } - rn -= n; - base += n; - } - if ((f->_flag&(_IOLBF|_IONBF)) == 0) { - f->_cnt--; - *f->_ptr++ = c; - } - return c; + + if (f->_flag & _IOLBF) { + /* in line-buffering mode we get here on each character */ + *f->_ptr++ = c; + rn = f->_ptr - base; + if (c == '\n' || rn >= f->_bufsiz) { + /* time for real flush */ + f->_ptr = base; + f->_cnt = 0; + } else { + /* we got here because _cnt is wrong, so fix it */ + /* Negative _cnt causes all output functions to call */ + /* _flsbuf for each character, thus realizing line-buffering */ + f->_cnt = -rn; + return c; + } + } else if (f->_flag & _IONBF) { + c1 = c; + rn = 1; + base = &c1; + f->_cnt = 0; + } else { /* _IOFBF */ + rn = f->_ptr - base; + f->_ptr = base; + if ((f->_flag & _IOAHEAD) == _IOAHEAD) + _lseek(fileno(f), -(rn+f->_cnt), SEEK_CUR); + f->_cnt = f->_bufsiz; + f->_flag &= ~_IOAHEAD; + } + f->_flag &= ~_IODIRTY; + while (rn > 0) { + n = _write(fileno(f), base, rn); + if (n <= 0) { + f->_flag |= _IOERR; + return EOF; + } + rn -= n; + base += n; + } + if ((f->_flag & (_IOLBF|_IONBF)) == 0) { + f->_cnt--; + *f->_ptr++ = c; + } + return c; } -wint_t _flswbuf(wchar_t c, FILE *fp) +wint_t _flswbuf(wchar_t c, FILE* fp) { int result; result = _flsbuf((int)c, fp); if (result == EOF) return WEOF; - return (wint_t)result; + return (wint_t)result; } -int _writecnv(int fn, void *buf, size_t siz) +int _writecnv(int fn, void* buf, size_t siz) { - char *bufp = (char*)buf; - int bufsiz = siz; - char *tmp; - int cr1 = 0; - int cr2 = 0; - int n; - - cr1 = cntcr(bufp,bufsiz); - tmp = malloc(cr1); - memcpy(tmp,bufp+bufsiz-cr1,cr1); - cr2 = cntcr(tmp,cr1); - convert(bufp,bufsiz-cr2,cr1-cr2); - n = _write(fn, bufp, bufsiz + cr1); - convert(tmp,cr1,cr2); - n += _write(fn, tmp, cr1 + cr2); - free(tmp); - return n; + char* bufp = (char*)buf; + int bufsiz = siz; + char* tmp; + int cr1 = 0; + int cr2 = 0; + int n; + + cr1 = cntcr(bufp, bufsiz); + tmp = malloc(cr1); + memcpy(tmp, bufp + bufsiz - cr1, cr1); + cr2 = cntcr(tmp, cr1); + convert(bufp, bufsiz - cr2, cr1 - cr2); + n = _write(fn, bufp, bufsiz + cr1); + convert(tmp, cr1, cr2); + n += _write(fn, tmp, cr1 + cr2); + free(tmp); + return n; } -int convert(char *endp, int bufsiz,int n) -{ - endp = endp + bufsiz + n; - while (bufsiz > 0) { - *endp = *(endp - n); - if (*endp == '\n') { - *endp--; - n--; - *endp = '\r'; - } - endp--; - bufsiz--; - } - return n; -} - -int cntcr(char *bufp, int bufsiz) -{ - int cr = 0; - - while (bufsiz > 0) { - if (*bufp == '\n') { - cr++; +int convert(char* endp, int bufsiz, int n) +{ + endp = endp + bufsiz + n; + while (bufsiz > 0) { + *endp = *(endp - n); + if (*endp == '\n') { + *endp--; + n--; + *endp = '\r'; } - bufp++; - bufsiz--; - } - return cr; + endp--; + bufsiz--; + } + return n; +} + +int cntcr(char* bufp, int bufsiz) +{ + int cr = 0; + + while (bufsiz > 0) { + if (*bufp == '\n') { + cr++; + } + bufp++; + bufsiz--; + } + return cr; } diff --git a/reactos/lib/msvcrt/stdio/getc.c b/reactos/lib/msvcrt/stdio/getc.c index fced061c36a..7dfa1075b17 100644 --- a/reactos/lib/msvcrt/stdio/getc.c +++ b/reactos/lib/msvcrt/stdio/getc.c @@ -1,4 +1,4 @@ -/* $Id: getc.c,v 1.5 2002/11/24 18:42:24 robd Exp $ +/* $Id: getc.c,v 1.6 2002/12/05 15:30:44 robd Exp $ * * ReactOS msvcrt library * @@ -38,19 +38,17 @@ int getc(FILE *fp) { int c = -1; -// check for invalid stream + // check for invalid stream if ( !__validfp (fp) ) { __set_errno(EINVAL); return EOF; } -// check for read access on stream - + // check for read access on stream if ( !OPEN4READING(fp) ) { __set_errno(EINVAL); return EOF; } - if(fp->_cnt > 0) { fp->_cnt--; c = (int)*fp->_ptr++; @@ -69,25 +67,22 @@ wint_t getwc(FILE *fp) __set_errno(EINVAL); return WEOF; } - // check for read access on stream //#define OPEN4READING(f) ((((f)->_flag & _IOREAD) == _IOREAD ) ) if (!OPEN4READING(fp)) { __set_errno(EINVAL); return WEOF; } - // might check on multi bytes if text mode if (fp->_flag & _IOBINARY) { - - if (fp->_cnt > 0) { + if (fp->_cnt > 1) { fp->_cnt -= sizeof(wchar_t); c = (wint_t)*((wchar_t*)(fp->_ptr))++; } else { c = _filwbuf(fp); } - } else { +#if 0 BOOL get_bytes = 0; int mb_cnt = 0; int found_cr = 0; @@ -95,41 +90,19 @@ wint_t getwc(FILE *fp) char mbchar[MB_CUR_MAX]; do { - if (fp->_cnt > 0) { fp->_cnt--; mbchar[mb_cnt] = *fp->_ptr++; } else { mbchar[mb_cnt] = _filbuf(fp); } -/* - // convert cr/lf pairs into a single lf. - if (mbchar[mb_cnt] == '\r') { - //found_cr = 1; - if (fp->_cnt > 0) { - fp->_cnt--; - mbchar[mb_cnt+1] = *fp->_ptr++; - } else { - mbchar[mb_cnt+1] = _filbuf(fp); - } - if (mbchar[mb_cnt+1] == '\n') { - mbchar[mb_cnt] = '\n'; - } else { - ++mb_cnt; - } - - } - */ - if (isleadbyte(mbchar[mb_cnt])) { get_bytes = 1; } else { get_bytes = 0; } - if (_ismbblead(mbchar[mb_cnt])) { } - ++mb_cnt; //} } while (get_bytes); @@ -140,9 +113,15 @@ wint_t getwc(FILE *fp) fp->_flag |= _IOERR; return WEOF; } +#else + if (fp->_cnt > 0) { + fp->_cnt--; + c = *fp->_ptr++; + } else { + c = _filbuf(fp); + } +#endif } return c; } -// MultiByteToWideChar -// WideCharToMultiByte diff --git a/reactos/lib/msvcrt/stdio/printf.c b/reactos/lib/msvcrt/stdio/printf.c index 36cd1ae8466..ca273c4fa72 100644 --- a/reactos/lib/msvcrt/stdio/printf.c +++ b/reactos/lib/msvcrt/stdio/printf.c @@ -20,35 +20,25 @@ #include #include -/* Write formatted output to stdout from the format string FORMAT. */ -/* VARARGS1 */ -int -printf (const char *format, ...) -{ - va_list arg; - int done; - va_start (arg, format); - done = vprintf (format, arg); - va_end (arg); - return done; +int printf(const char* format, ...) +{ + va_list arg; + int done; + + va_start(arg, format); + done = vprintf(format, arg); + va_end(arg); + return done; } -int -wprintf (const wchar_t *format, ...) +int wprintf(const wchar_t* format, ...) { - va_list arg; - int done; + va_list arg; + int done; - va_start (arg, format); - done = vwprintf (format, arg); - va_end (arg); - return done; + va_start(arg, format); + done = vwprintf(format, arg); + va_end(arg); + return done; } - -#ifdef USE_IN_LIBIO -# undef _IO_printf -/* This is for libg++. */ -strong_alias (printf, _IO_printf); -#endif - diff --git a/reactos/lib/msvcrt/stdio/putc.c b/reactos/lib/msvcrt/stdio/putc.c index 086a148f15b..80c279bb20d 100644 --- a/reactos/lib/msvcrt/stdio/putc.c +++ b/reactos/lib/msvcrt/stdio/putc.c @@ -1,4 +1,4 @@ -/* $Id: putc.c,v 1.5 2002/11/24 18:42:24 robd Exp $ +/* $Id: putc.c,v 1.6 2002/12/05 15:30:44 robd Exp $ * * ReactOS msvcrt library * @@ -38,7 +38,7 @@ #define MB_CUR_MAX_CONST 10 #endif -int putc(int c, FILE *fp) +int putc(int c, FILE* fp) { // valid stream macro should check that fp is dword aligned if (!__validfp(fp)) { @@ -61,13 +61,9 @@ int putc(int c, FILE *fp) return EOF; } -//wint_t putwc(wint_t c, FILE *fp) -int putwc(wint_t c, FILE *fp) +//wint_t putwc(wint_t c, FILE* fp) +int putwc(wint_t c, FILE* fp) { - int i; - int mb_cnt; - char mbchar[MB_CUR_MAX_CONST]; - // valid stream macro should check that fp is dword aligned if (!__validfp(fp)) { __set_errno(EINVAL); @@ -83,17 +79,33 @@ int putwc(wint_t c, FILE *fp) //if (1) { fp->_flag |= _IODIRTY; - if (fp->_cnt > 0 ) { + if (fp->_cnt > 0) { fp->_cnt -= sizeof(wchar_t); //*((wchar_t*)(fp->_ptr))++ = c; *((wchar_t*)(fp->_ptr)) = c; ++((wchar_t*)(fp->_ptr)); return (wint_t)c; } else { +#if 1 + wint_t result; + result = _flsbuf((int)(c >> 8), fp); + if (result == EOF) + return WEOF; + result = _flsbuf(c, fp); + if (result == EOF) + return WEOF; + return result; +#else return _flswbuf(c, fp); +#endif } } else { +#if 0 + int i; + int mb_cnt; + char mbchar[MB_CUR_MAX_CONST]; + // Convert wide character to the corresponding multibyte character. mb_cnt = wctomb(mbchar, (wchar_t)c); if (mb_cnt == -1) { @@ -105,19 +117,6 @@ int putwc(wint_t c, FILE *fp) } for (i = 0; i < mb_cnt; i++) { fp->_flag |= _IODIRTY; -#if 0 - // convert lf's into a cr/lf pair. - if (mbchar[i] == '\n') { - if (fp->_cnt > 0) { - fp->_cnt--; - *(fp)->_ptr++ = (unsigned char)'\r'; - } else { - if (_flsbuf((unsigned char)'\r', fp) == EOF) { - return WEOF; - } - } - } -#endif if (fp->_cnt > 0) { fp->_cnt--; *(fp)->_ptr++ = (unsigned char)mbchar[i]; @@ -127,6 +126,17 @@ int putwc(wint_t c, FILE *fp) } } } +#else + fp->_flag |= _IODIRTY; + if (fp->_cnt > 0) { + fp->_cnt--; + *(fp)->_ptr++ = (unsigned char)c; + } else { + if (_flsbuf(c, fp) == EOF) { + return WEOF; + } + } +#endif return c; } return WEOF; diff --git a/reactos/lib/msvcrt/stdio/vfprintf.c b/reactos/lib/msvcrt/stdio/vfprintf.c index f0f22f10e15..971c5f122fc 100644 --- a/reactos/lib/msvcrt/stdio/vfprintf.c +++ b/reactos/lib/msvcrt/stdio/vfprintf.c @@ -11,36 +11,34 @@ int _isinf(double x); -int -__vfprintf (FILE *fp, const char *fmt0, va_list args); +int __vfprintf(FILE*, const char*, va_list); -int -vfprintf(FILE *f, const char *fmt, va_list ap) + +int vfprintf(FILE* f, const char* fmt, va_list ap) { - int len; - char localbuf[BUFSIZ]; + int len; + char localbuf[BUFSIZ]; #if 0 - __fileno_lock(fileno(f)); + __fileno_lock(fileno(f)); #endif - if (f->_flag & _IONBF) - { - f->_flag &= ~_IONBF; - f->_ptr = f->_base = localbuf; - f->_bufsiz = BUFSIZ; - len = __vfprintf(f,fmt, ap); - (void)fflush(f); - f->_flag |= _IONBF; - f->_base = NULL; - f->_bufsiz = 0; - f->_cnt = 0; - } - else - len = __vfprintf(f,fmt, ap); + if (f->_flag & _IONBF) { + f->_flag &= ~_IONBF; + f->_ptr = f->_base = localbuf; + f->_bufsiz = BUFSIZ; + len = __vfprintf(f, fmt, ap); + (void)fflush(f); + f->_flag |= _IONBF; + f->_base = NULL; + f->_bufsiz = 0; + f->_cnt = 0; + } else { + len = __vfprintf(f,fmt, ap); + } #if 0 - __fileno_unlock(fileno(f)); + __fileno_unlock(fileno(f)); #endif - return (ferror(f) ? EOF : len); + return (ferror(f) ? EOF : len); } diff --git a/reactos/lib/msvcrt/stdio/vfscanf.c b/reactos/lib/msvcrt/stdio/vfscanf.c index 0b6d53da1ae..349aabae345 100644 --- a/reactos/lib/msvcrt/stdio/vfscanf.c +++ b/reactos/lib/msvcrt/stdio/vfscanf.c @@ -794,7 +794,7 @@ int __vfscanf (FILE *s, const char *format, va_list argptr) else { if (flags & LONGDBL) { - *ARG (LONGLONG *) = num.q; + *ARG (LONGLONG*) = num.q; } else if (flags & LONG) *ARG (long int *) = num.l; diff --git a/reactos/lib/msvcrt/stdio/vprintf.c b/reactos/lib/msvcrt/stdio/vprintf.c index f7247499e96..33c74b8d037 100644 --- a/reactos/lib/msvcrt/stdio/vprintf.c +++ b/reactos/lib/msvcrt/stdio/vprintf.c @@ -21,27 +21,57 @@ Cambridge, MA 02139, USA. */ #include #include + #undef vprintf #undef vwprintf -/* Write formatted output to stdout according to the - format string FORMAT, using the argument list in ARG. */ -int -vprintf (format, arg) - const char *format; - va_list arg; +//#define _USE_VARARG_ + +#ifndef _USE_VARARG_ + +int vprintf(const char* format, va_list arg) { - int ret = vfprintf (stdout, format, arg); - fflush(stdout); - return ret; + int ret; + + ret = vfprintf(stdout, format, arg); + fflush(stdout); + return ret; } -int -vwprintf (format, arg) - const wchar_t *format; - va_list arg; +int vwprintf(const wchar_t* format, va_list arg) { - int ret = vfwprintf (stdout, format, arg); - fflush(stdout); - return ret; + int ret; + + ret = vfwprintf(stdout, format, arg); + fflush(stdout); + return ret; } + +#else + +int vprintf(const char* format, ...) +{ + va_list arg; + int ret; + + va_start(arg, format); + ret = vfprintf(stdout, format, arg); + va_end(arg); + + fflush(stdout); + return ret; +} + +int vwprintf(const wchar_t* format, ...) +{ + va_list arg; + int ret; + + va_start(arg, format); + ret = vfwprintf(stdout, format, arg); + va_end(arg); + fflush(stdout); + return ret; +} + +#endif diff --git a/reactos/lib/msvcrt/stdlib/mbstowcs.c b/reactos/lib/msvcrt/stdlib/mbstowcs.c index 39923b6390e..752f2fc220b 100644 --- a/reactos/lib/msvcrt/stdlib/mbstowcs.c +++ b/reactos/lib/msvcrt/stdlib/mbstowcs.c @@ -1,37 +1,118 @@ +#include #include -size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count ) +#if 1 +size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count) { - size_t Size; - return (size_t)Size; + size_t size; + int i; + + printf("\nmbstowcs(%p, %p, %d) called.\n\n", wcstr, mbstr, count); + + if (count <= 0 || !mbstr) + return 0; + + if (!*mbstr) + return 0; + + + if (wcstr == NULL) { + // return required size for the converted string + return strlen(mbstr); // TODO: fixme + } + for (size = 0, i = 0; i < count; size++) { + int result; + +////int mbtowc( wchar_t *wchar, const char *mbchar, size_t count ) +//// result = mbtowc(wcstr + size, mbstr + i, count - i); +// result = mbtowc(wcstr + size, mbstr + i, 1); + +///////////////////////////////////////// + if (mbstr[i] == 0) { + result = 0; + } else { + wcstr[size] = mbstr[i]; + result = 1; + } +///////////////////////////////////////// + if (result == -1) { + return -1; + } else if (result == 0) { + wcstr[size] = L'\0'; + break; + } else { + i += result; + } + + } + return size; } -/* - NTSTATUS Status; - ULONG Size; - ULONG Length; +#else +#if 1 - Length = strlen (mbstr); +//int mbtowc(wchar_t *dst, const char *str, size_t n) +size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count) +{ + size_t len; - if (wcstr == NULL) - { - RtlMultiByteToUnicodeSize (&Size, - (char *)mbstr, - Length); + if (count <= 0 || !mbstr) + return 0; + len = MultiByteToWideChar(CP_ACP, 0, mbstr, count, wcstr, (wcstr == NULL) ? 0 : count); - return (size_t)Size; - } + if (!len) { + DWORD err = GetLastError(); + switch (err) { + case ERROR_INSUFFICIENT_BUFFER: + break; + case ERROR_INVALID_FLAGS: + break; + case ERROR_INVALID_PARAMETER: + break; + case ERROR_NO_UNICODE_TRANSLATION: + break; + default: + return 1; + } + return -1; + } + /* return the number of bytes from src that have been used */ + if (!*mbstr) + return 0; +// if (count >= 2 && isleadbyte(*mbstr) && mbstr[1]) +// return 2; + return len; +} - Status = RtlMultiByteToUnicodeN (wcstr, - count, - &Size, - (char *)mbstr, - Length); - if (!NT_SUCCESS(Status)) - return -1; - */ +#else + +size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count) +{ + size_t size; + int i; + + if (wcstr == NULL) { + // return required size for the converted string + return strlen(mbstr); // TODO: fixme + } + for (size = 0, i = 0; i < count; size++) { + int result; //int mbtowc( wchar_t *wchar, const char *mbchar, size_t count ) -//{ -// return 0; -//} +// result = mbtowc(wcstr + size, mbstr + i, count - i); + result = mbtowc(wcstr + size, mbstr + i, 1); + if (result == -1) { + return -1; + } else if (result == 0) { + wcstr[size] = L'\0'; + break; + } else { + i += result; + } + + } + return (size_t)size; +} + +#endif +#endif diff --git a/reactos/lib/msvcrt/stdlib/mbtowc.c b/reactos/lib/msvcrt/stdlib/mbtowc.c index e7f0d025ccc..501f124c207 100644 --- a/reactos/lib/msvcrt/stdlib/mbtowc.c +++ b/reactos/lib/msvcrt/stdlib/mbtowc.c @@ -1,12 +1,52 @@ +#include #include +#include -//int mbtowc(wchar_t* wcDest, const char* mbConvert, size_t size) -int mbtowc(wchar_t *wchar, const char *mbchar, size_t count) +#if 1 + +int mbtowc(wchar_t *dst, const char *str, size_t n) { - *wchar = (wchar_t)*mbchar; - return 1; +// printf("\t\t\tmbtowc(%p, %p, %d) called.\n", dst, str, n); - // WideCharToMultiByte - // MultiByteToWideChar + if (n <= 0 || !str) + return 0; + + *dst = *str; + + if (!*str) + return 0; + return 1; } + +#else + +int mbtowc(wchar_t *dst, const char *str, size_t n) +{ + if (n <= 0 || !str) + return 0; + if (!MultiByteToWideChar(CP_ACP, 0, str, n, dst, (dst == NULL) ? 0 : 1)) { + DWORD err = GetLastError(); + switch (err) { + case ERROR_INSUFFICIENT_BUFFER: + break; + case ERROR_INVALID_FLAGS: + break; + case ERROR_INVALID_PARAMETER: + break; + case ERROR_NO_UNICODE_TRANSLATION: + break; + default: + return 1; + } + return -1; + } + /* return the number of bytes from src that have been used */ + if (!*str) + return 0; + if (n >= 2 && isleadbyte(*str) && str[1]) + return 2; + return 1; +} + +#endif diff --git a/reactos/lib/msvcrt/stdlib/wcstomb.c b/reactos/lib/msvcrt/stdlib/wcstomb.c index c5e6492c4e9..978bd2a7d42 100644 --- a/reactos/lib/msvcrt/stdlib/wcstomb.c +++ b/reactos/lib/msvcrt/stdlib/wcstomb.c @@ -29,7 +29,7 @@ static const wchar_t encoding_mask[] = { - (wchar_t)~0x7ff, (wchar_t)~0xffff, (wchar_t)~0x1fffff, (wchar_t)~0x3ffffff + ~0x7ff, ~0xffff, ~0x1fffff, ~0x3ffffff }; static const unsigned char encoding_byte[] = diff --git a/reactos/lib/msvcrt/stdlib/wctomb.c b/reactos/lib/msvcrt/stdlib/wctomb.c index f96fbab3c4e..1603a5a3b07 100644 --- a/reactos/lib/msvcrt/stdlib/wctomb.c +++ b/reactos/lib/msvcrt/stdlib/wctomb.c @@ -16,11 +16,43 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include #include +#include #include #include #include + +int +STDCALL +WideCharToMultiByte( + UINT CodePage, + DWORD dwFlags, + LPCWSTR lpWideCharStr, + int cchWideChar, + LPSTR lpMultiByteStr, + int cchMultiByte, + LPCSTR lpDefaultChar, + LPBOOL lpUsedDefaultChar); + + +int wctomb(char* dst, wchar_t ch) +{ +#if 0 + return WideCharToMultiByte(CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL); +#else + if (dst == NULL) { + return 1; + } + *dst = ch; + return 1; +#endif +} + + +#if 0 + #ifndef EILSEQ #define EILSEQ EINVAL #endif @@ -110,3 +142,5 @@ size_t __wcrtomb(char *s, wchar_t wc) } return written; } + +#endif diff --git a/reactos/lib/msvcrt/stdlib/witow.c b/reactos/lib/msvcrt/stdlib/witow.c index dd8bdca7b93..202bf3bd282 100644 --- a/reactos/lib/msvcrt/stdlib/witow.c +++ b/reactos/lib/msvcrt/stdlib/witow.c @@ -1,4 +1,4 @@ -/* $Id: witow.c,v 1.1 2002/11/24 18:42:25 robd Exp $ +/* $Id: witow.c,v 1.2 2002/12/05 15:30:45 robd Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -26,8 +26,7 @@ wchar_t* _i64tow(__int64 value, wchar_t* string, int radix) int sign; wchar_t* sp; - if (radix > 36 || radix <= 1) - { + if (radix > 36 || radix <= 1) { __set_errno(EDOM); return 0; } @@ -37,8 +36,7 @@ wchar_t* _i64tow(__int64 value, wchar_t* string, int radix) v = -value; else v = (unsigned)value; - while (v || tp == tmp) - { + while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) @@ -67,14 +65,12 @@ wchar_t* _ui64tow(unsigned __int64 value, wchar_t* string, int radix) unsigned long v = value; wchar_t* sp; - if (radix > 36 || radix <= 1) - { + if (radix > 36 || radix <= 1) { __set_errno(EDOM); return 0; } - while (v || tp == tmp) - { + while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) diff --git a/reactos/lib/msvcrt/string/memcmp.c b/reactos/lib/msvcrt/string/memcmp.c index 997be7a5f16..95d3d25f4c0 100644 --- a/reactos/lib/msvcrt/string/memcmp.c +++ b/reactos/lib/msvcrt/string/memcmp.c @@ -1,6 +1,7 @@ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include +#pragma function(memcmp) int memcmp(const void *s1, const void *s2, size_t n) { diff --git a/reactos/lib/msvcrt/string/memcpy.c b/reactos/lib/msvcrt/string/memcpy.c index 7b2dd74fcd3..894bc8442af 100644 --- a/reactos/lib/msvcrt/string/memcpy.c +++ b/reactos/lib/msvcrt/string/memcpy.c @@ -1,5 +1,6 @@ #include +#pragma function(memcpy) /* This is the most reliable way to avoid incompatibilities in available built-in functions on various systems. */ diff --git a/reactos/lib/msvcrt/string/memset.c b/reactos/lib/msvcrt/string/memset.c index 01164bf033f..9bb4bf8766e 100644 --- a/reactos/lib/msvcrt/string/memset.c +++ b/reactos/lib/msvcrt/string/memset.c @@ -1,5 +1,6 @@ #include +#pragma function(memset) void* memset(void* src, int val, size_t count) { diff --git a/reactos/lib/msvcrt/string/strcat.c b/reactos/lib/msvcrt/string/strcat.c index 9f8a8ac3076..2b87ce9463a 100644 --- a/reactos/lib/msvcrt/string/strcat.c +++ b/reactos/lib/msvcrt/string/strcat.c @@ -1,6 +1,7 @@ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include +#pragma function(strcat) char* strcat(char* s, const char* append) { diff --git a/reactos/lib/msvcrt/string/strcmp.c b/reactos/lib/msvcrt/string/strcmp.c index 52d3448f1d0..35aa66f0cd9 100644 --- a/reactos/lib/msvcrt/string/strcmp.c +++ b/reactos/lib/msvcrt/string/strcmp.c @@ -1,6 +1,7 @@ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include +#pragma function(strcmp) int strcmp(const char* s1, const char* s2) { diff --git a/reactos/lib/msvcrt/string/strlen.c b/reactos/lib/msvcrt/string/strlen.c index a2995f87dd6..ebac5d777f8 100644 --- a/reactos/lib/msvcrt/string/strlen.c +++ b/reactos/lib/msvcrt/string/strlen.c @@ -1,6 +1,7 @@ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include +#pragma function(strlen) size_t strlen(const char* str) { diff --git a/reactos/lib/msvcrt/time/ctime.c b/reactos/lib/msvcrt/time/ctime.c index 307ebf5f84d..0f27e8e58c3 100644 --- a/reactos/lib/msvcrt/time/ctime.c +++ b/reactos/lib/msvcrt/time/ctime.c @@ -245,8 +245,7 @@ settzname(void) } } -static char * -tzdir(void) +static char* tzdir(void) { static char dir[80]={0}, *cp; if (dir[0] == 0) @@ -266,8 +265,7 @@ tzdir(void) return dir; } -static int -tzload(const char *name, struct state * CPP_CONST sp) +static int tzload(const char* name, struct state* CPP_CONST sp) { const char * p; int i; @@ -407,8 +405,8 @@ DAYSPERNYEAR, DAYSPERLYEAR ** character. */ -static const char * -getzname(const char *strp) +static const char* +getzname(const char* strp) { char c; @@ -425,8 +423,8 @@ getzname(const char *strp) ** Otherwise, return a pointer to the first character not part of the number. */ -static const char * -getnum(const char *strp, int * CPP_CONST nump, const int min, const int max) +static const char* +getnum(const char* strp, int* CPP_CONST nump, const int min, const int max) { char c; int num; diff --git a/reactos/lib/msvcrt/wstring/wcsncmp.c b/reactos/lib/msvcrt/wstring/wcsncmp.c index e661043f2f9..a3e0746f0d4 100644 --- a/reactos/lib/msvcrt/wstring/wcsncmp.c +++ b/reactos/lib/msvcrt/wstring/wcsncmp.c @@ -1,9 +1,10 @@ #include -int wcsncmp(const wchar_t * cs,const wchar_t * ct,size_t count) +#if 0 + +int wcsncmp(const wchar_t* cs, const wchar_t* ct, size_t count) { - while ((*cs) == (*ct) && count > 0) - { + while ((*cs) == (*ct) && count > 0) { if (*cs == 0) return 0; cs++; @@ -11,6 +12,22 @@ int wcsncmp(const wchar_t * cs,const wchar_t * ct,size_t count) count--; } return (*cs) - (*ct); - } +#else + +int wcsncmp(const wchar_t* cs, const wchar_t* ct, size_t count) +{ + if (count == 0) + return 0; + do { + if (*cs != *ct++) + //return *(unsigned const char *)cs - *(unsigned const char *)--ct; + return (*cs) - (*(--ct)); + if (*cs++ == 0) + break; + } while (--count != 0); + return 0; +} + +#endif