From 6e3f176457ef90d6a5f869a6d8eaac4980cd11a1 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 11 Nov 2023 23:17:18 +0200 Subject: [PATCH] ls: print link targets when listing files --- userspace/ls/main.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/userspace/ls/main.cpp b/userspace/ls/main.cpp index b962f852..b18a0c5e 100644 --- a/userspace/ls/main.cpp +++ b/userspace/ls/main.cpp @@ -74,7 +74,18 @@ void list_directory(const char* path) } if (list) + { printf("%s %4d %4d %6d %s%s\e[m", mode_string(st.st_mode), st.st_uid, st.st_gid, st.st_size, color_string(st.st_mode), dirent->d_name); + if (S_ISLNK(st.st_mode)) + { + char link_buffer[128]; + ssize_t ret = readlinkat(dirfd(dirp), dirent->d_name, link_buffer, sizeof(link_buffer)); + if (ret >= 0) + printf(" -> %.*s", ret, link_buffer); + else + perror("readlink"); + } + } else printf("%s%s\e[m", color_string(st.st_mode), dirent->d_name);