From 7c81f026ce616e9d429c3b6f38a9921393246071 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Thu, 29 Aug 2024 18:18:02 +0300 Subject: [PATCH] Add test_stat function --- apps/base/utest/userspace_test.c | 55 +++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/apps/base/utest/userspace_test.c b/apps/base/utest/userspace_test.c index b88739c6..9250cf12 100644 --- a/apps/base/utest/userspace_test.c +++ b/apps/base/utest/userspace_test.c @@ -1,5 +1,6 @@ #define _POSIX_C_SOURCE 200809L #define _POSIX_SOURCE +#define _GNU_SOURCE #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#define _GNU_SOURCE #include #include #include @@ -347,6 +347,58 @@ void test_file() fclose(test); } +void test_stat() +{ + printf("- Testing stat\n"); + + struct stat st; + if (stat("/etc/passwd", &st) == -1) + { + perror("stat"); + fflush(stdout); + fflush(stderr); + return; + } + + printf("File size: %ld\n", st.st_size); + printf("File mode: %o\n", st.st_mode); + printf("File inode: %ld\n", st.st_ino); + + int fd = open("/etc/passwd", O_RDONLY); + if (fd == -1) + { + perror("open"); + fflush(stdout); + fflush(stderr); + return; + } + + if (fstat(fd, &st) == -1) + { + perror("fstat"); + fflush(stdout); + fflush(stderr); + return; + } + + printf("File size: %ld\n", st.st_size); + printf("File mode: %o\n", st.st_mode); + printf("File inode: %ld\n", st.st_ino); + close(fd); + + if (lstat("/etc/passwd", &st) == -1) + { + perror("lstat"); + fflush(stdout); + fflush(stderr); + return; + } + + printf("File size: %ld\n", st.st_size); + printf("File mode: %o\n", st.st_mode); + printf("File inode: %ld\n", st.st_ino); +} + void test_ptmx() { printf("- Testing PTMX\n"); @@ -837,6 +889,7 @@ int main(int argc, char *argv[], char *envp[]) test_args(argc, argv, envp); test_system(); test_file(); + test_stat(); test_dirent(); test_execve_fork(); test_watch_file();