os/ossrv/genericopenlibs/openenvcore/libc/src/ftruncate.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Name        : ftruncate.cpp
    15 // Part of     : LIBC
    16 // Contains the source for fchdir
    17 // Version     : 1.0
    18 //
    19 
    20 
    21  
    22 
    23 #include <unistd.h>
    24 #include <sys/types.h>
    25 #include <sys/stat.h>
    26 #include <errno.h> 
    27 #include <fcntl.h> 
    28 #include "reent.h"
    29 #include "sysif.h"
    30 
    31 extern "C" {
    32 
    33 /*
    34 The reentrant version for ftruncate function.
    35 */
    36 int _truncate_r(struct _reent *r, int fd, off_t len)
    37 	{
    38 	struct stat st;
    39 	
    40 	if(fstat(fd,&st) != 0)
    41 		{
    42 		r->_errno  = EINVAL; 	//KErrArgument
    43 		return -1;
    44 		}
    45 	else 
    46 		{
    47 		if (!(S_ISREG(st.st_mode)))
    48 			{
    49 			r->_errno = EBADF;	//KErrBadHandle
    50 			return -1;
    51 			}
    52 		}
    53 	
    54   	return Backend()->Truncate( fd, len, r->_errno);
    55 	}
    56 
    57 /*
    58 Truncates the given file (using file descriptor) to the length specified.
    59 Calls the reentrant version.
    60 The ftruncate() function shall cause the size of the file to be truncated
    61 to length arument. If the size of the file previously exceeded length, the
    62 extra data shall no longer be available to reads on the file. If the file
    63 previously was smaller than this size, ftruncate() shall either increase 
    64 the size of the file or fail. Upon successful completion, ftruncate() shall
    65 return 0; otherwise, -1 shall be returned and errno set to indicate the error.
    66 */
    67 EXPORT_C int ftruncate(int filedesc, off_t length)
    68 	{
    69 	return _truncate_r(_REENT, filedesc, length);
    70 	}
    71 
    72 /*
    73 Truncates the given file (using file name) to the length specified.
    74 Calls the reentrant version.
    75 The truncate() function shall cause the size of the file to be truncated
    76 to length arument. If the size of the file previously exceeded length, the
    77 extra data shall no longer be available to reads on the file. If the file
    78 previously was smaller than this size, truncate() shall either increase 
    79 the size of the file or fail. Upon successful completion, truncate() shall
    80 return 0; otherwise, -1 shall be returned and errno set to indicate the error.
    81 */
    82 EXPORT_C int truncate(const char *file , off_t length) 
    83 	{
    84 	int fd = 0;
    85 	int retval = 0;
    86 	int errVal = 0;
    87 	 
    88 	  if((fd = open(file , O_WRONLY)) < 0) 
    89 	  	{
    90 	  	 return -1;
    91 	  	}
    92 	  retval =  ftruncate(fd , length);
    93 	  if(retval < 0) 
    94 	  	{
    95 	  	errVal = errno; //Store the error value 
    96 	  	close(fd) ;
    97 	  	errno = errVal;
    98 	  	return retval;
    99 	  	}
   100 	  	
   101 	 close(fd);
   102 	 return retval;
   103 	}
   104 
   105 } //extern "C"