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