1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00
Files
amiga-clib2/library/unistd_ftruncate.c
Olaf Barthel a9c8cdc554 - Moved the Workbench console stream initialization into the
initialization code for the stdin/stdout/stderr streams and
  out of the program parameter setup.

- The current program name is now set up in the stdlib
  initialization function.

- Simplified the machine test code; moved the FPU check into
  the math initialization code.

- Added more safety checks to verify that file descriptor
  file handles are valid.


git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14864 87f5fb63-7c3d-0410-a384-fd976d0f7a62
2005-03-07 11:16:43 +00:00

161 lines
3.9 KiB
C

/*
* $Id: unistd_ftruncate.c,v 1.7 2005-03-07 11:16:43 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _UNISTD_HEADERS_H
#include "unistd_headers.h"
#endif /* _UNISTD_HEADERS_H */
/****************************************************************************/
/* The following is not part of the ISO 'C' (1994) standard. */
/****************************************************************************/
int
ftruncate(int file_descriptor, off_t length)
{
D_S(struct FileInfoBlock,fib);
int result = -1;
struct fd * fd = NULL;
long int position;
BOOL success;
ENTER();
SHOWVALUE(file_descriptor);
SHOWVALUE(length);
assert( file_descriptor >= 0 && file_descriptor < __num_fd );
assert( __fd[file_descriptor] != NULL );
assert( FLAG_IS_SET(__fd[file_descriptor]->fd_Flags,FDF_IN_USE) );
if(__check_abort_enabled)
__check_abort();
fd = __get_file_descriptor(file_descriptor);
if(fd == NULL)
{
__set_errno(EBADF);
goto out;
}
__fd_lock(fd);
if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET))
{
__set_errno(EINVAL);
goto out;
}
if(fd->fd_DefaultFile == ZERO)
{
__set_errno(EBADF);
goto out;
}
if(length < 0)
{
SHOWMSG("invalid length");
__set_errno(EINVAL);
goto out;
}
assert( FLAG_IS_SET(fd->fd_Flags,FDF_IN_USE) );
if(FLAG_IS_CLEAR(fd->fd_Flags,FDF_WRITE))
{
SHOWMSG("file descriptor is not write-enabled");
__set_errno(EINVAL);
goto out;
}
/* Remember where we started. */
position = lseek(file_descriptor,0,SEEK_CUR);
if(position < 0)
goto out;
if(CANNOT __safe_examine_file_handle(fd->fd_DefaultFile,fib))
{
SHOWMSG("couldn't examine file");
__set_errno(__translate_io_error_to_errno(IoErr()));
goto out;
}
PROFILE_OFF();
if(length < fib->fib_Size)
{
/* Careful: seek to a position where the file can be safely truncated. */
success = (Seek(fd->fd_DefaultFile,length,OFFSET_BEGINNING) != -1 && SetFileSize(fd->fd_DefaultFile,length,OFFSET_BEGINNING) != -1);
}
else if (length > fib->fib_Size)
{
success = (Seek(fd->fd_DefaultFile,fib->fib_Size,OFFSET_BEGINNING) != -1 && __grow_file_size(fd,length - fib->fib_Size) == OK);
}
else
{
success = TRUE;
}
PROFILE_ON();
if(NO success)
{
int error;
error = __translate_io_error_to_errno(IoErr());
/* Return to the original file position. */
lseek(file_descriptor,position,SEEK_SET);
__set_errno(error);
goto out;
}
/* Return to the original file position. */
if(lseek(file_descriptor,position,SEEK_SET) < 0)
goto out;
result = 0;
out:
__fd_unlock(fd);
RETURN(result);
return(result);
}