From 7ce8e610f5f1014edb063c08a41c93686b424664 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 17 Nov 2025 02:47:53 +0200 Subject: [PATCH] stat: Fix handling of symlinks --- userspace/programs/stat/main.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/userspace/programs/stat/main.cpp b/userspace/programs/stat/main.cpp index 3cdd0e88..b3ecdf2c 100644 --- a/userspace/programs/stat/main.cpp +++ b/userspace/programs/stat/main.cpp @@ -1,6 +1,8 @@ +#include #include #include #include +#include #include @@ -21,7 +23,7 @@ int main(int argc, char** argv) for (int i = 1; i < argc; i++) { struct stat st; - if (stat(argv[i], &st) == -1) + if (lstat(argv[i], &st) == -1) { perror("stat"); ret = 1; @@ -67,7 +69,15 @@ int main(int argc, char** argv) access[9] = (st.st_mode & S_ISVTX) ? ((st.st_mode & S_IXOTH) ? 't' : 'T') : (st.st_mode & S_IXOTH) ? 'x' : '-'; access[10] = '\0'; - printf(" File: %s\n", argv[i]); + printf(" File: %s", argv[i]); + if (S_ISLNK(st.st_mode)) + { + char buffer[PATH_MAX]; + if (int length = readlink(argv[i], buffer, sizeof(buffer)); length > 0) + printf(" -> %.*s", length, buffer); + } + printf("\n"); + printf(" Size: %-15ld Blocks: %-10ld IO Block: %-6ld %s\n", st.st_size, st.st_blocks, st.st_blksize, type); printf("Device: %u,%-5u Inode: %-11llu Links: %-5lu Device type: %u,%u\n", major(st.st_dev), minor(st.st_dev), st.st_ino, st.st_nlink, major(st.st_rdev), minor(st.st_rdev)); printf("Access: (%04o/%s) Uid: %5d Gid: %5d\n", st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX), access, st.st_uid, st.st_gid);