sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Name : ftruncate.cpp sl@0: // Part of : LIBC sl@0: // Contains the source for fchdir sl@0: // Version : 1.0 sl@0: // sl@0: sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "reent.h" sl@0: #include "sysif.h" sl@0: sl@0: extern "C" { sl@0: sl@0: /* sl@0: The reentrant version for ftruncate function. sl@0: */ sl@0: int _truncate_r(struct _reent *r, int fd, off_t len) sl@0: { sl@0: struct stat st; sl@0: sl@0: if(fstat(fd,&st) != 0) sl@0: { sl@0: r->_errno = EINVAL; //KErrArgument sl@0: return -1; sl@0: } sl@0: else sl@0: { sl@0: if (!(S_ISREG(st.st_mode))) sl@0: { sl@0: r->_errno = EBADF; //KErrBadHandle sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: return Backend()->Truncate( fd, len, r->_errno); sl@0: } sl@0: sl@0: /* sl@0: Truncates the given file (using file descriptor) to the length specified. sl@0: Calls the reentrant version. sl@0: The ftruncate() function shall cause the size of the file to be truncated sl@0: to length arument. If the size of the file previously exceeded length, the sl@0: extra data shall no longer be available to reads on the file. If the file sl@0: previously was smaller than this size, ftruncate() shall either increase sl@0: the size of the file or fail. Upon successful completion, ftruncate() shall sl@0: return 0; otherwise, -1 shall be returned and errno set to indicate the error. sl@0: */ sl@0: EXPORT_C int ftruncate(int filedesc, off_t length) sl@0: { sl@0: return _truncate_r(_REENT, filedesc, length); sl@0: } sl@0: sl@0: /* sl@0: Truncates the given file (using file name) to the length specified. sl@0: Calls the reentrant version. sl@0: The truncate() function shall cause the size of the file to be truncated sl@0: to length arument. If the size of the file previously exceeded length, the sl@0: extra data shall no longer be available to reads on the file. If the file sl@0: previously was smaller than this size, truncate() shall either increase sl@0: the size of the file or fail. Upon successful completion, truncate() shall sl@0: return 0; otherwise, -1 shall be returned and errno set to indicate the error. sl@0: */ sl@0: EXPORT_C int truncate(const char *file , off_t length) sl@0: { sl@0: int fd = 0; sl@0: int retval = 0; sl@0: int errVal = 0; sl@0: sl@0: if((fd = open(file , O_WRONLY)) < 0) sl@0: { sl@0: return -1; sl@0: } sl@0: retval = ftruncate(fd , length); sl@0: if(retval < 0) sl@0: { sl@0: errVal = errno; //Store the error value sl@0: close(fd) ; sl@0: errno = errVal; sl@0: return retval; sl@0: } sl@0: sl@0: close(fd); sl@0: return retval; sl@0: } sl@0: sl@0: } //extern "C"