Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ftruncate, ftruncate64, truncate, truncate64 - set file to specified length

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

int ftruncate (int fildes, off_t length);
int ftruncate64 (int fildes, off64_t length);
int truncate (const char *path, off_t length);
int truncate64 (const char *path, off64_t length); 

Description

ftruncate() sets the length of a normal file with the file descriptor fildes to length bytes.
truncate() differs from ftruncate() only in that the file is addressed via a pointer path which references a pathname.

The effect of ftruncate() and truncate() on other types of file is undefined. If the file was previously longer than length bytes, the bytes after the position length can no longer be accessed. If the file was previously shorter, the bytes between the EOF mark before the call and the EOF mark after the call are padded with zeros. With ftruncate() the file must be opened for writing; with truncate() the effective user ID of the process must have write permission for the file.

If the request would cause the file size to exceed the current limit defined for the process for the maximum length of a file, the function is not executed and the system sends the SIGXFSZ signal to the process.

These functions do not change the current position in the file. On successful execution, if the file size was changed, these functions update the st_ctime and st_mtime fields of the file. The S_ISUID and S_ISGID bits of the file mode may be deleted.

There is no difference in functionality between ftruncate()/truncate() and ftruncate64()/ truncate64() except that for ftruncate64() and truncate64() the length is specified as offset type off64_t.

Return val.

0

-1

if successful.

if an error occurs. errno is set to indicate the error.

Errors

ftruncate(), ftruncate64(), truncate() and truncate64()will fail if: 

 

EINTR

A signal was received during execution.

 

EINVAL

The value of length is negative.

 

EFBIG or EINVAL 



The value of length is greater than the maximum permissible file size.

 

EIO

An I/O error occurred when reading from or writing to the file system.

 

Extension

 

EINVAL

An attempt was made to access a BS2000 file. (End)

 

ftruncate() and ftruncate64()will fail if:

EBADF or EINVAL 



fildes is not a file descriptor that is opened for writing.

 

EINVAL

fildes identifies a file that was opened for reading only.

 

truncate() and truncate64()will fail if:

 

EACCES

No search authorization exists for a component of the path prefix or no write authorization exists for the file addressed via path.

 

EISDIR

The file addressed via path is a directory.

 

ELOOP

Too many symbolic links were encountered in resolving path.

 

ENAMETOOLONG 



The length of a component of the pathname exceeds {NAME_MAX} bytes, or the length of the pathname exceeds {PATH_MAX} bytes.

The resolving of symbolic links in the pathname leads to an interim result whose length exceeds {PATH_MAX}.

 

ENOENT

Either a component of the path prefix does not exist, or path references an empty string.

 

ENOTDIR

A component of the path prefix from path is not a directory.

 

EROFS

The file addressed via path resides on a read-only file system.

Notes

ftruncate(), ftruncate64(), truncate() and truncate64() are only allowed for POSIX files.

See also

open(), unistd.h.