MSVCRT: implement _chsize

Dmitry Timoshkov dmitry at baikal.ru
Wed Mar 30 06:47:44 CST 2005


"Hans Leidekker" <hans at it.vu.nl> wrote:

>  int _chsize(int fd, long size)
>  {
> -    FIXME("(fd=%d, size=%ld): stub\n", fd, size);
> -    return -1;
> +    DWORD 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) goto exit;
> +
> +    /* save the current file pointer */
> +    cur = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
> +    if (cur == INVALID_SET_FILE_POINTER) goto exit;
> +
> +    pos = SetFilePointer(handle, size, NULL, FILE_BEGIN);
> +    if (pos == INVALID_SET_FILE_POINTER) goto restore;
> +
> +    ret = SetEndOfFile(handle);
> +
> +restore:
> +    /* restore the file pointer */
> +    pos = SetFilePointer(handle, cur, NULL, FILE_BEGIN);
> +
> +exit:
> +    if (ret == FALSE)
> +    { UNLOCK_FILES(); return -1; }
> +    else
> +    { UNLOCK_FILES(); return 0; }
>  }

This a pure coding style comment, just ignore it if you are not agree.

In order to avoid goto's (absolutely not needed here IMO) and doubled
UNLOCK_FILES():

    LOCK_FILES();

    handle = msvcrt_fdtoh(fd);
    if (handle != INVALID_HANDLE_VALUE)
    {
        /* save the current file pointer */
        cur = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
        if (cur != INVALID_SET_FILE_POINTER)
        {
            pos = SetFilePointer(handle, size, NULL, FILE_BEGIN);
            if (pos != INVALID_SET_FILE_POINTER)
                ret = SetEndOfFile(handle);

            /* restore the file pointer */
            pos = SetFilePointer(handle, cur, NULL, FILE_BEGIN);
        }
    }

    UNLOCK_FILES();
    return ret ? 0 : -1;

-- 
Dmitry.




More information about the wine-devel mailing list