return -1;
}
-/*********************************************************************
- * _chsize (MSVCRT.@)
- */
-int _chsize(int fd, long size)
-{
- FIXME("(fd=%d, size=%ld): stub\n", fd, size);
- return -1;
-}
-
/*********************************************************************
* _unlink (MSVCRT.@)
*/
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.@)
*/
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)
{
test_file_write_read();
test_file_inherit(arg_v[0]);
test_tmpnam();
+ test_chsize();
}