#define _GNU_SOURCE
#include "test-common.h"
#include <fcntl.h>
#include <unistd.h>
int test_openat(const char *base_path) {
char path[512];
int fd, dirfd;
snprintf(path, sizeof(path), "%s/test.txt", base_path);
fd = openat(AT_FDCWD, path, O_RDONLY);
TEST_ASSERT_ERRNO(fd >= 0, "openat with AT_FDCWD should succeed");
close(fd);
dirfd = open(base_path, O_RDONLY | O_DIRECTORY);
TEST_ASSERT_ERRNO(dirfd >= 0, "open directory should succeed");
fd = openat(dirfd, "test.txt", O_RDONLY);
TEST_ASSERT_ERRNO(fd >= 0, "openat with directory fd should succeed");
close(fd);
close(dirfd);
snprintf(path, sizeof(path), "%s/created.txt", base_path);
fd = openat(AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
TEST_ASSERT_ERRNO(fd >= 0, "openat with O_CREAT should succeed");
close(fd);
snprintf(path, sizeof(path), "%s/nonexistent.txt", base_path);
fd = openat(AT_FDCWD, path, O_RDONLY);
TEST_ASSERT(fd < 0 && errno == ENOENT, "openat non-existent file should fail with ENOENT");
return 0;
}