Implement and test _chsize.
authorHans Leidekker <hans@it.vu.nl>
Thu, 31 Mar 2005 19:06:10 +0000 (19:06 +0000)
committerAlexandre Julliard <julliard@winehq.org>
Thu, 31 Mar 2005 19:06:10 +0000 (19:06 +0000)
dlls/msvcrt/file.c
dlls/msvcrt/tests/file.c

index a55b6e8ac5d3cd670b577d2ed35075ef3e0b348d..fbd5961b660a7ded47c9766f5ed5b824d1bada81 100644 (file)
@@ -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.@)
  */
index 5685d93760496b074df99557947eab0dc19adaa3..f5f1c821b48d578ca936cf31626f4f92ab83c517 100644 (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();
 }