os/ossrv/genericopenlibs/openenvcore/libc/src/ftruncate.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/ftruncate.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,105 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Name        : ftruncate.cpp
    1.18 +// Part of     : LIBC
    1.19 +// Contains the source for fchdir
    1.20 +// Version     : 1.0
    1.21 +//
    1.22 +
    1.23 +
    1.24 + 
    1.25 +
    1.26 +#include <unistd.h>
    1.27 +#include <sys/types.h>
    1.28 +#include <sys/stat.h>
    1.29 +#include <errno.h> 
    1.30 +#include <fcntl.h> 
    1.31 +#include "reent.h"
    1.32 +#include "sysif.h"
    1.33 +
    1.34 +extern "C" {
    1.35 +
    1.36 +/*
    1.37 +The reentrant version for ftruncate function.
    1.38 +*/
    1.39 +int _truncate_r(struct _reent *r, int fd, off_t len)
    1.40 +	{
    1.41 +	struct stat st;
    1.42 +	
    1.43 +	if(fstat(fd,&st) != 0)
    1.44 +		{
    1.45 +		r->_errno  = EINVAL; 	//KErrArgument
    1.46 +		return -1;
    1.47 +		}
    1.48 +	else 
    1.49 +		{
    1.50 +		if (!(S_ISREG(st.st_mode)))
    1.51 +			{
    1.52 +			r->_errno = EBADF;	//KErrBadHandle
    1.53 +			return -1;
    1.54 +			}
    1.55 +		}
    1.56 +	
    1.57 +  	return Backend()->Truncate( fd, len, r->_errno);
    1.58 +	}
    1.59 +
    1.60 +/*
    1.61 +Truncates the given file (using file descriptor) to the length specified.
    1.62 +Calls the reentrant version.
    1.63 +The ftruncate() function shall cause the size of the file to be truncated
    1.64 +to length arument. If the size of the file previously exceeded length, the
    1.65 +extra data shall no longer be available to reads on the file. If the file
    1.66 +previously was smaller than this size, ftruncate() shall either increase 
    1.67 +the size of the file or fail. Upon successful completion, ftruncate() shall
    1.68 +return 0; otherwise, -1 shall be returned and errno set to indicate the error.
    1.69 +*/
    1.70 +EXPORT_C int ftruncate(int filedesc, off_t length)
    1.71 +	{
    1.72 +	return _truncate_r(_REENT, filedesc, length);
    1.73 +	}
    1.74 +
    1.75 +/*
    1.76 +Truncates the given file (using file name) to the length specified.
    1.77 +Calls the reentrant version.
    1.78 +The truncate() function shall cause the size of the file to be truncated
    1.79 +to length arument. If the size of the file previously exceeded length, the
    1.80 +extra data shall no longer be available to reads on the file. If the file
    1.81 +previously was smaller than this size, truncate() shall either increase 
    1.82 +the size of the file or fail. Upon successful completion, truncate() shall
    1.83 +return 0; otherwise, -1 shall be returned and errno set to indicate the error.
    1.84 +*/
    1.85 +EXPORT_C int truncate(const char *file , off_t length) 
    1.86 +	{
    1.87 +	int fd = 0;
    1.88 +	int retval = 0;
    1.89 +	int errVal = 0;
    1.90 +	 
    1.91 +	  if((fd = open(file , O_WRONLY)) < 0) 
    1.92 +	  	{
    1.93 +	  	 return -1;
    1.94 +	  	}
    1.95 +	  retval =  ftruncate(fd , length);
    1.96 +	  if(retval < 0) 
    1.97 +	  	{
    1.98 +	  	errVal = errno; //Store the error value 
    1.99 +	  	close(fd) ;
   1.100 +	  	errno = errVal;
   1.101 +	  	return retval;
   1.102 +	  	}
   1.103 +	  	
   1.104 +	 close(fd);
   1.105 +	 return retval;
   1.106 +	}
   1.107 +
   1.108 +} //extern "C"