From 5ab9705a44acb1c9782448fcb0f01c986b7b0c8c Mon Sep 17 00:00:00 2001 From: GJDuck Date: Fri, 21 Jul 2023 18:53:37 +0800 Subject: [PATCH] Fix stat() and fstat(). Also add perror() --- examples/stdlib.c | 9 ++++++++- test/regtest/patch.cpp | 9 +++++++++ test/regtest/stat.exp | 3 +++ test/regtest/stat.in | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/regtest/stat.exp create mode 100644 test/regtest/stat.in diff --git a/examples/stdlib.c b/examples/stdlib.c index 9a6ec41..e332724 100644 --- a/examples/stdlib.c +++ b/examples/stdlib.c @@ -721,10 +721,11 @@ struct stat { unsigned long st_dev; unsigned long st_ino; - mode_t st_mode; unsigned long st_nlink; + unsigned st_mode; uid_t st_uid; gid_t st_gid; + unsigned __pad; unsigned long st_rdev; off_t st_size; long st_blksize; @@ -732,6 +733,7 @@ struct stat struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; + unsigned long __unused[3]; }; struct rusage @@ -4154,6 +4156,11 @@ static int printf_unlocked(const char *format, ...) return result; } +static void perror(const char *msg) +{ + fprintf(stderr, "%s: %s\n", msg, strerror(errno)); +} + /****************************************************************************/ /* SCANF */ /****************************************************************************/ diff --git a/test/regtest/patch.cpp b/test/regtest/patch.cpp index fce2e31..c4dd6a0 100644 --- a/test/regtest/patch.cpp +++ b/test/regtest/patch.cpp @@ -1095,6 +1095,15 @@ void test_string(void) free(s2); } +void test_stat(const char *filename) +{ + struct stat buf; + if (stat(filename, &buf) < 0) + perror("stat()"); + fprintf(stderr, "mode = %o\n", buf.st_mode); + fprintf(stderr, "size = %zu\n", buf.st_size); +} + static int tree_compare(const void *a, const void *b) { return (int)*(size_t *)a - (int)*(size_t *)b; diff --git a/test/regtest/stat.exp b/test/regtest/stat.exp new file mode 100644 index 0000000..9509b9a --- /dev/null +++ b/test/regtest/stat.exp @@ -0,0 +1,3 @@ +mode = 100664 +size = 62 +PASSED diff --git a/test/regtest/stat.in b/test/regtest/stat.in new file mode 100644 index 0000000..ce1e747 --- /dev/null +++ b/test/regtest/stat.in @@ -0,0 +1 @@ +./test -M 'addr == 0xa0001d9' -P 'test_stat("stat.in")@patch'