Seth Woolley's Man Viewer

Manual for truncate - man 2 truncate

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

TRUNCATE(2)                Linux Programmer's Manual               TRUNCATE(2)



NAME
       truncate(2,7), ftruncate - truncate(2,7) a file(1,n) to a specified length

SYNOPSIS
       #include <unistd.h>
       #include <sys/types.h>

       int truncate(2,7)(const char *path, off_t length);
       int ftruncate(int fd, off_t length);

DESCRIPTION
       The  truncate(2,7)  and  ftruncate functions cause the regular file(1,n) named(5,8) by
       path or referenced by fd to be truncated to a size of precisely  length
       bytes.

       If  the  file(1,n)  previously  was larger than this size, the extra data is
       lost.  If the file(1,n) previously was shorter,  it  is  extended,  and  the
       extended part reads as zero bytes.

       The file(1,n) pointer is not changed.

       If  the  size changed, then the ctime and mtime fields for the file(1,n) are
       updated, and suid and sgid mode bits may be cleared.

       With ftruncate, the file(1,n) must be open(2,3,n) for writing; with  truncate(2,7),  the
       file(1,n) must be writable.

RETURN VALUE
       On  success,  zero is returned.  On error(8,n), -1 is returned, and errno is
       set(7,n,1 builtins) appropriately.

ERRORS
       For truncate(2,7):

       EACCES Search permission is denied for a component of the path  prefix,
              or  the  named(5,8)  file(1,n)  is  not  writable  by the user.  (See also
              path_resolution(2).)

       EFAULT Path points outside the process's allocated address space.

       EFBIG  The argument length is larger than the maximum file(1,n) size. (XSI)

       EINTR  A signal(2,7) was caught during execution.

       EINVAL The argument length is negative or larger than the maximum  file(1,n)
              size.

       EIO    An I/O error(8,n) occurred updating the inode.

       EISDIR The named(5,8) file(1,n) is a directory.

       ELOOP  Too  many  symbolic  links  were  encountered in(1,8) translating the
              pathname.

       ENAMETOOLONG
              A component of a pathname exceeded 255 characters, or an  entire
              path name exceeded 1023 characters.

       ENOENT The named(5,8) file(1,n) does not exist.

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

       EROFS  The named(5,8) file(1,n) resides on a read-only file(1,n) system.

       ETXTBSY
              The  file(1,n)  is  a pure procedure (shared text) file(1,n) that is being
              executed.

       For ftruncate the same errors apply, but instead of things that can  be
       wrong with path, we now have things that can be wrong with fd:

       EBADF  The fd is not a valid descriptor.

       EBADF or EINVAL
              The fd is not open(2,3,n) for writing.

       EINVAL The fd does not reference a regular file.

CONFORMING TO
       4.4BSD,  SVr4  (these function calls first appeared in(1,8) BSD 4.2).  POSIX
       1003.1-1996 has ftruncate.  POSIX 1003.1-2001 also has truncate(2,7), as  an
       XSI extension.

       SVr4  documents  additional truncate(2,7) error(8,n) conditions EMFILE, EMULTIHP,
       ENFILE, ENOLINK.  SVr4 documents for  ftruncate  an  additional  EAGAIN
       error(8,n) condition.

NOTES
       The  above  description is for XSI-compliant systems.  For non-XSI-com-
       pliant systems, the POSIX standard allows two behaviours for  ftruncate
       when  length  exceeds the file(1,n) length (note that truncate(2,7) is not speci-
       fied at all in(1,8) such an environment):  either  returning  an  error(8,n),  or
       extending the file.  (Most Unices follow the XSI requirement.)

SEE ALSO
       open(2,3,n)(2), path_resolution(2)



Linux 2.6.7                       2004-06-23                       TRUNCATE(2)

References for this manual (incoming links)