From a19bf290f0b3a352b7c09c55eb1b6f3f6afeeb11 Mon Sep 17 00:00:00 2001 From: Hans Leidekker Date: Thu, 31 Mar 2005 19:06:10 +0000 Subject: [PATCH] Implement and test _chsize. --- dlls/msvcrt/file.c | 45 ++++++++++++++++++++++++++++++++-------- dlls/msvcrt/tests/file.c | 35 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index a55b6e8ac5d..fbd5961b660 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -524,15 +524,6 @@ int _wchmod(const MSVCRT_wchar_t *path, int flags) return -1; } -/********************************************************************* - * _chsize (MSVCRT.@) - */ -int _chsize(int fd, long size) -{ - FIXME("(fd=%d, size=%ld): stub\n", fd, size); - return -1; -} - /********************************************************************* * _unlink (MSVCRT.@) */ @@ -909,6 +900,42 @@ int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence) return (_lseek(file->_file,offset,whence) == -1)?-1:0; } +/********************************************************************* + * _chsize (MSVCRT.@) + */ +int _chsize(int fd, long size) +{ + LONG cur, pos; + HANDLE handle; + BOOL ret = FALSE; + + TRACE("(fd=%d, size=%ld)\n", fd, size); + + LOCK_FILES(); + + handle = msvcrt_fdtoh(fd); + if (handle != INVALID_HANDLE_VALUE) + { + /* save the current file pointer */ + cur = _lseek(fd, 0, SEEK_CUR); + if (cur >= 0) + { + pos = _lseek(fd, size, SEEK_SET); + if (pos >= 0) + { + ret = SetEndOfFile(handle); + if (!ret) msvcrt_set_errno(GetLastError()); + } + + /* restore the file pointer */ + _lseek(fd, cur, SEEK_SET); + } + } + + UNLOCK_FILES(); + return ret ? 0 : -1; +} + /********************************************************************* * clearerr (MSVCRT.@) */ diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c index 5685d937604..f5f1c821b48 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -316,7 +316,41 @@ static void test_tmpnam( void ) ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n"); } +static void test_chsize( void ) +{ + int fd; + long cur, pos, count; + char temptext[] = "012345678"; + char *tempfile = _tempnam( ".", "tst" ); + + ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile ); + fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE ); + ok( fd > 0, "Couldn't open test file\n" ); + + count = _write( fd, temptext, sizeof(temptext) ); + ok( count > 0, "Couldn't write to test file\n" ); + + /* get current file pointer */ + cur = _lseek( fd, 0, SEEK_CUR ); + + /* make the file smaller */ + ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" ); + + pos = _lseek( fd, 0, SEEK_CUR ); + ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos ); + ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" ); + + /* enlarge the file */ + ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); + + pos = _lseek( fd, 0, SEEK_CUR ); + ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos ); + ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" ); + + _close( fd ); + _unlink( tempfile ); +} START_TEST(file) { @@ -338,4 +372,5 @@ START_TEST(file) test_file_write_read(); test_file_inherit(arg_v[0]); test_tmpnam(); + test_chsize(); }