Skip to content

Instantly share code, notes, and snippets.

@doug65536
Last active June 12, 2021 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doug65536/34039c36f808f68d171ebd6395f97e0f to your computer and use it in GitHub Desktop.
Save doug65536/34039c36f808f68d171ebd6395f97e0f to your computer and use it in GitHub Desktop.
Find first erring seek position
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/fcntl.h>
#include <inttypes.h>
int main()
{
int fd = open("test", O_RDWR | O_CREAT, 0644);
off_t st = 0;
off_t en = INT64_MAX;
while (st < en) {
off_t md = ((en - st) >> 1) + st;
if (lseek(fd, md, SEEK_SET) < 0)
en = md;
else
st = md + 1;
}
printf("%" PRIx64 "", st);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment