FTRUNCATE(3POSIX)
π FTRUNCATE π (that's the 3POSIX one, from the POSIX Programmer's Manual, bestie)
okay so real quick before we dive in π«Ά
π’ PROLOG π’
so this page is part of the POSIX Programmer's Manual, okay? and the Linux version of this interface may be different β like, if you're on Linux, go check the actual Linux manual page for how Linux behaves. or honestly it might not even be implemented on Linux at all. no cap, just go look. π
β¨ NAME β¨
ftruncate β truncate a file to a specified length π
πΈ SYNOPSIS πΈ
#include <unistd.h>
int ftruncate(int fildes, off_t length);
π¦ DESCRIPTION π¦
so here's the tea. if fildes isn't a valid file descriptor that's open for writing, then ftruncate() shall fail. periodt. it's giving "write access only". β
if fildes refers to a regular file, ftruncate() shall cause the size of the file to be truncated to length. and like β if the file used to be bigger than length, that extra data shall no longer be available to reads on the file. gone. poof. π«₯
but if the file was previously smaller than that size? then ftruncate() shall increase the size of the file. and if the file size gets increased, that extended area shall appear as if it were zero-filled. literally zeroes. β¨
also β super important β the value of the seek pointer shall not be modified by a call to ftruncate(). she stays exactly where she was. π
upon successful completion, if fildes refers to a regular file, ftruncate() shall mark for update the last data modification and last file status change timestamps of the file, and the S_ISUID and S_ISGID bits of the file mode may be cleared. if the ftruncate() function is unsuccessful, the file is unaffected β no harm no foul. π
now. if the request would cause the file size to exceed the soft file size limit for the process, the request shall fail and the implementation shall generate the SIGXFSZ signal for the thread. yeah, she's dramatic like that. π
if fildes refers to a directory, ftruncate() shall fail. obviously. not today. π
if fildes refers to any other file type β except a shared memory object β the result is unspecified. so like, don't. π
if fildes refers to a shared memory object, ftruncate() shall set the size of the shared memory object to length. slay. π
if the effect of ftruncate() is to decrease the size of a memory mapped file or a shared memory object, and whole pages beyond the new end were previously mapped, then the whole pages beyond the new end shall be discarded. bye bye pages π§
and references to those discarded pages shall result in the generation of a SIGBUS signal. β οΈ so like, don't touch them fr fr.
if the effect of ftruncate() is to increase the size of a memory object, it is unspecified whether the contents of any mapped pages between the old end-of-file and the new are flushed to the underlying object. we just don't know. vibe check inconclusive. π
π RETURN VALUE π
upon successful completion, ftruncate() shall return 0. otherwise, -1 shall be returned and errno set to indicate the error. that's the whole deal. π«Ά
π ERRORS π
the ftruncate() function shall fail if:
EINTRβ a signal was caught during execution. πEINVALβ thelengthargument was less than0. negative length is not the vibe.EFBIGorEINVALβ thelengthargument was greater than the maximum file size. π«EFBIGβ the file is a regular file andlengthis greater than the offset maximum established in the open file description associated withfildes.EIOβ an I/O error occurred while reading from or writing to a file system. πEBADForEINVALβ thefildesargument is not a file descriptor open for writing.
the following sections are informative. so like, optional reading, but we love a little extra credit. π
π EXAMPLES π
none. literally nothing here. bestie said no examples today. π
π APPLICATION USAGE π
none. also nothing. we're keeping it mysterious. π
π RATIONALE π
none. the reasoning is between her and the standards committee. β¨
β FUTURE DIRECTIONS β
none. the future is unwritten, lowkey. π¦
π SEE ALSO π
open()truncate()
the Base Definitions volume of POSIX.1β2017, <unistd.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. π
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. like, she's the final boss, no arguing. βοΈ
the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . π
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . π
and that's the whole page bestie!! this icon was brought to you by IEEE/The Open Group, 2017 β yeah, 2017, she's been iconic for a minute ππ mwah, go truncate something responsibly β¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms