LSEEK(3P)
π LSEEK(3P) β the POSIX Programmer's Manual glow-up π
okay so bestie, quick heads up before we dive in: this is the POSIX Programmer's Manual page for lseek. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not even be implemented on Linux at all. π iykyk.
β¨ NAME β¨
lseek β move the read/write file offset. that's it. that's the whole vibe. she slides the little bookmark around inside your file. π
πΈ SYNOPSIS πΈ
#include <unistd.h>
off_t lseek(int fildes, off_t offset, int whence);
look at her. three arguments. no notes. π
π¦ DESCRIPTION π¦
The lseek() function shall set the file offset for the open file description associated with the file descriptor fildes, and here's exactly how, depending on what whence is:
- If
whenceisSEEK_SET, the file offset shall be set tooffsetbytes. absolute. no math, just vibes. - If
whenceisSEEK_CUR, the file offset shall be set to its current location plusoffset. relative to where you're standing rn. - If
whenceisSEEK_END, the file offset shall be set to the size of the file plusoffset. measured from the very end, bestie.
The symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END are defined in <unistd.h>. π
Now the messy part: the behavior of lseek() on devices which are incapable of seeking is implementation-defined. The value of the file offset associated with such a device is undefined. it's giving chaos, honestly.
The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap. spooky little zero-hole. π
The lseek() function shall not, by itself, extend the size of a file. she moves the bookmark, she does not add pages. periodt.
If fildes refers to a shared memory object, the result of the lseek() function is unspecified.
If fildes refers to a typed memory object, the result of the lseek() function is unspecified. (yes she said it twice, because there are two separate objects and both of them are unhinged about this.)
β RETURN VALUE β
Upon successful completion, the resulting offset, as measured in bytes from the beginning of the file, shall be returned. slay.
Otherwise, -1 shall be returned, errno shall be set to indicate the error, and the file offset shall remain unchanged. so like, no half-moves. she either commits or she doesn't. π«Ά
π ERRORS π
The lseek() function shall fail if:
EBADFβ Thefildesargument is not an open file descriptor. girl that's not a file. πEINVALβ Thewhenceargument is not a proper value, or the resulting file offset would be negative for a regular file, block special file, or directory. nope, can't go negative, not on these file types.EOVERFLOWβ The resulting file offset would be a value which cannot be represented correctly in an object of typeoff_t. it's too big for the box bestie, the box isoff_t. πESPIPEβ Thefildesargument is associated with a pipe, FIFO, or socket. pipes do not do bookmarks. they flow. π
The following sections are informative. (translation: the real ones know these are the bonus lore chapters π)
π EXAMPLES π
None. literally none. the void. π³οΈ
π§ APPLICATION USAGE π§
None. also none. two empty chapters back to back, iconic honestly.
π RATIONALE π
The ISO C standard includes the functions fgetpos() and fsetpos(), which work on very large files by use of a special positioning type. fancy cousins. β¨
Although lseek() may position the file offset beyond the end of the file, this function does not itself extend the size of the file. While the only function in POSIX.1β2008 that may directly extend the size of the file is write(), truncate(), and ftruncate(), several functions originally derived from the ISO C standard, such as fwrite(), fprintf(), and so on, may do so (by causing calls on write()). so they're all secretly pulling the same strings. puppeteer energy.
An invalid file offset that would cause [EINVAL] to be returned may be both implementation-defined and device-dependent (for example, memory may have few invalid values). A negative file offset may be valid for some devices in some implementations. so like, negative isn't always a crime, it depends on the device. π
The POSIX.1β1990 standard did not specifically prohibit lseek() from returning a negative offset. Therefore, an application was required to clear errno prior to the call and check errno upon return to determine whether a return value of (off_t)-1 is a negative offset or an indication of an error condition. The standard developers did not wish to require this action on the part of a conforming application, and chose to require that errno be set to [EINVAL] when the resulting file offset would be negative for a regular file, block special file, or directory. so they fixed it for us and we say thank you. π
π₯ FUTURE DIRECTIONS π₯
None. the future is bright and empty and we love her for it. π
πΈ SEE ALSO πΈ
open()- The Base Definitions volume of POSIX.1β2017,
<sys_types.h>,<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. 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 .
not me reading the whole copyright section out loud, but the rules are the rules and every single word stays. π«‘
and that's the page bestie!! brought to you by IEEE/The Open Group, 2017, LSEEK(3P) β go forth and seek responsibly, mwah ππβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms