#include "test_utils.h" static int test_dual_open_same_file(const char *path) { printf("\n=== test_dual_open_same_file ===\n"); int fd_init = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); if (fd_init < 0) { perror("open init"); return 1; } const char *init = "0123456789"; if (write(fd_init, init, 10) != 10) { perror("write init"); return 2; } close(fd_init); int fd_w = open(path, O_WRONLY); if (fd_w < 0) { perror("open W"); return 3; } int fd_r = open(path, O_RDONLY); if (fd_r < 0) { perror("open R"); return 4; } printf("fd_w=%d fd_r=%d\n", fd_w, fd_r); if (write(fd_w, "HELLO", 5) != 5) { perror("write"); return 5; } printf("write via fd_w: HELLO (overwrite first 5 bytes)\n"); char buf[32] = {0}; lseek(fd_r, 0, SEEK_SET); ssize_t r = read(fd_r, buf, sizeof(buf)); printf("read via fd_r: %zd bytes: %.*s (expect: HELLO56789)\n", r, (int)r, buf); lseek(fd_w, 0, SEEK_END); if (write(fd_w, "!!!", 3) != 3) { perror("write append"); return 6; } printf("write append via fd_w: !!!\n"); lseek(fd_r, 10, SEEK_SET); memset(buf, 0, sizeof(buf)); r = read(fd_r, buf, sizeof(buf)); printf("read appended via fd_r: %zd bytes: %.*s (expect: !!!)\n", r, (int)r, buf); close(fd_w); close(fd_r); 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_dual_open_same_file(path); return report_result("test_dual_open_same_file", rc); }