#include #include #include /* * Exercises lseek() with offsets that span the 16-bit boundary. * big.txt is 102400 bytes (100 KB); every 256-byte block opens with a * "[off=NNNNNNN] block #BBBB " label, so any seek aligned to 256 lands * on a self-identifying marker. */ static void show_marker(int fd, long off) { long pos = lseek(fd, off, SEEK_SET); printf("\nseek %ld -> pos=%ld\n", off, pos); char buf[28]; int n = read(fd, buf, 27); if (n <= 0) { printf(" read failed: %d\n", n); return; } buf[n] = 0; printf(" bytes: \"%s\"\n", buf); } int main(void) { int fd = open("BIG.TXT", O_RDONLY); if (fd < 0) { printf("open failed: %d\n", fd); (void)getchar(); return 1; } /* Total file size. */ long size = lseek(fd, 0L, SEEK_END); printf("file size = %ld bytes\n", size); /* Seek inside the first 64 KB (high16 = 0). */ show_marker(fd, 0L); show_marker(fd, 256L); show_marker(fd, 32768L); /* Right at the 64 KB boundary (offset 0x00010000, high16 = 1). */ show_marker(fd, 65536L); /* Past 64 KB (high16 still 1, low16 != 0). */ show_marker(fd, 65536L + 256L); show_marker(fd, 81920L); /* Near end of file (size = 102400 = 0x19000, high16 = 1). */ show_marker(fd, 102400L - 256L); /* SEEK_CUR: from current position, +256. After the previous SEEK_SET * to 102144, then read of 27 bytes, pos is 102171. +256 -> 102427, * which is past EOF — ESTEX should clamp or error. */ long after_cur = lseek(fd, 256L, SEEK_CUR); printf("\nSEEK_CUR +256 -> pos=%ld (past EOF)\n", after_cur); close(fd); puts(""); puts("Press any key to exit..."); (void)getchar(); return 0; }