From d5c55abd21e03b4726ea3426953e6320b0ac716d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Oct 2023 22:24:35 +0300 Subject: [PATCH] Ls --- coreutils/ls.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/coreutils/ls.c b/coreutils/ls.c index 7db9303..b04a312 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -7,6 +9,7 @@ #include #include #include + unsigned int a_flag; unsigned int l_flag; @@ -18,14 +21,14 @@ void PrintPerm(struct stat sb) { } int list(const char *path, int label) { - struct stat statbuf; - if (stat(path, &statbuf)) { + struct stat sb; + if (stat(path, &sb)) { fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); return 1; } /* If its file */ - if (S_ISDIR(statbuf.st_mode) == 0) { + if (!S_ISDIR(sb.st_mode)) { puts(path); return 0; } @@ -41,10 +44,8 @@ int list(const char *path, int label) { /* Open and print dir */ DIR *dp = opendir("."); - if (dp == NULL) { - fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); + if (dp == NULL) return 1; - } struct dirent *ep; while ((ep = readdir(dp)) != NULL) { @@ -52,7 +53,6 @@ int list(const char *path, int label) { continue; if (l_flag) { - struct stat sb; if (lstat(ep->d_name, &sb) == -1) { fprintf(stderr, "ls: lstat()\n"); return 1; @@ -61,7 +61,7 @@ int list(const char *path, int label) { /* Permissions */ PrintPerm(sb); - /* Date and other info */ + /* Date */ struct tm *tm = localtime(&sb.st_mtime); char date[14]; @@ -70,7 +70,11 @@ int list(const char *path, int label) { return 1; } - printf(" %jd %jd %s %s\n", (uintmax_t)sb.st_mode, (uintmax_t)sb.st_size, date, ep->d_name); + /* Group and user name */ + struct passwd *pw = getpwuid(sb.st_uid); + struct group *gr = getgrgid(sb.st_gid); + + printf(" %s %s %jd %s %s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, ep->d_name); } else