Problem reported by Sudhip Nashi (Bug#51857#47).
* lib/lseek.c (rpl_lseek): Work around macOS lseek+SEEK_HOLE
returning -1 with ENXIO if there are no holes before EOF,
contrary to the macOS documentation.
2021-11-15 Paul Eggert <eggert@cs.ucla.edu>
+ lseek: port around macOS SEEK_HOLE glitch
+ Problem reported by Sudhip Nashi (Bug#51857#47).
+ * lib/lseek.c (rpl_lseek): Work around macOS lseek+SEEK_HOLE
+ returning -1 with ENXIO if there are no holes before EOF,
+ contrary to the macOS documentation.
+
lseek: port around macOS SEEK_DATA glitch
Problem reported by Sudhip Nashi (Bug#51857).
* doc/posix-functions/lseek.texi (lseek): Mention macOS SEEK_DATA
data followed by a possibly-empty hole. To work around this
portability glitch, check whether OFFSET is within data by
using lseek+SEEK_HOLE, and if so return to OFFSET by using
- lseek+SEEK_SET. */
+ lseek+SEEK_SET. Also, contrary to the macOS documentation,
+ lseek+SEEK_HOLE can fail with ENXIO if there are no holes on
+ or after OFFSET. What a mess! */
off_t next_hole = lseek (fd, offset, SEEK_HOLE);
if (next_hole < 0)
- return next_hole;
+ return errno == ENXIO ? offset : next_hole;
if (next_hole != offset)
whence = SEEK_SET;
}