56 lines
1.8 KiB
C
Executable File
56 lines
1.8 KiB
C
Executable File
#include "test_utils.h"
|
|
|
|
static int test_lseek(const char *path)
|
|
{
|
|
printf("\n=== test_lseek ===\n");
|
|
|
|
int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
|
|
if (fd < 0) { perror("open"); return 1; }
|
|
|
|
const char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
if (write(fd, alpha, 26) != 26) { perror("write"); return 2; }
|
|
printf("write 26 bytes: %s\n", alpha);
|
|
|
|
off_t pos = lseek(fd, 0, SEEK_SET);
|
|
printf("lseek SEEK_SET 0 -> %ld\n", (long)pos);
|
|
char buf[32] = {0};
|
|
ssize_t r = read(fd, buf, 5);
|
|
printf("read 5 bytes: %.*s (expect: ABCDE)\n", (int)r, buf);
|
|
|
|
pos = lseek(fd, 3, SEEK_CUR);
|
|
printf("lseek SEEK_CUR +3 -> %ld\n", (long)pos);
|
|
memset(buf, 0, sizeof(buf));
|
|
r = read(fd, buf, 5);
|
|
printf("read 5 bytes: %.*s (expect: IJKLM)\n", (int)r, buf);
|
|
|
|
pos = lseek(fd, -5, SEEK_END);
|
|
printf("lseek SEEK_END -5 -> %ld\n", (long)pos);
|
|
memset(buf, 0, sizeof(buf));
|
|
r = read(fd, buf, 10);
|
|
printf("read %zd bytes: %.*s (expect: VWXYZ)\n", r, (int)r, buf);
|
|
|
|
pos = lseek(fd, 30, SEEK_SET);
|
|
printf("lseek SEEK_SET 30 -> %ld\n", (long)pos);
|
|
if (write(fd, "!", 1) != 1) { perror("write hole"); return 3; }
|
|
|
|
lseek(fd, 26, SEEK_SET);
|
|
memset(buf, 0xAA, sizeof(buf));
|
|
r = read(fd, buf, 5);
|
|
printf("read hole+1: %zd bytes, hole[0]=%02X hole[1]=%02X hole[2]=%02X "
|
|
"hole[3]=%02X last='%c' (expect: 00 00 00 00 '!')\n",
|
|
r, (unsigned char)buf[0], (unsigned char)buf[1],
|
|
(unsigned char)buf[2], (unsigned char)buf[3], buf[4]);
|
|
|
|
close(fd);
|
|
unlink(path);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
char path[PATH_MAX];
|
|
make_path(path, sizeof(path), argc >= 2 ? argv[1] : NULL, "file.dat");
|
|
int rc = test_lseek(path);
|
|
return report_result("test_lseek", rc);
|
|
}
|