Commit a19bf290 authored by Hans Leidekker's avatar Hans Leidekker Committed by Alexandre Julliard

Implement and test _chsize.

parent 313554f0
......@@ -525,15 +525,6 @@ int _wchmod(const MSVCRT_wchar_t *path, int flags)
}
/*********************************************************************
* _chsize (MSVCRT.@)
*/
int _chsize(int fd, long size)
{
FIXME("(fd=%d, size=%ld): stub\n", fd, size);
return -1;
}
/*********************************************************************
* _unlink (MSVCRT.@)
*/
int _unlink(const char *path)
......@@ -910,6 +901,42 @@ int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence)
}
/*********************************************************************
* _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.@)
*/
void MSVCRT_clearerr(MSVCRT_FILE* file)
......
......@@ -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();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment