diff --git a/coreutils/ls.c b/coreutils/ls.c index 9269a63..41ea58f 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -20,6 +20,27 @@ void PrintPerm(struct stat sb) { printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-'); } +void PrintInfo(struct stat sb, const char *filename) { + + /* Permissions */ + PrintPerm(sb); + + /* Date */ + struct tm *tm = localtime(&sb.st_mtime); + + char date[14]; + if (strftime(date, sizeof(date), "%b %d %H:%M", tm) == 0) { + fprintf(stderr, "ls: strftime()\n"); + return; + } + + /* 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, filename); +} + int list(const char *path, int label) { struct stat sb; if (stat(path, &sb)) { @@ -29,7 +50,12 @@ int list(const char *path, int label) { /* If its file */ if (!S_ISDIR(sb.st_mode)) { - puts(path); + if (l_flag) + PrintInfo(sb, path); + + else + puts(path); + return 0; } @@ -58,23 +84,7 @@ int list(const char *path, int label) { return 1; } - /* Permissions */ - PrintPerm(sb); - - /* Date */ - struct tm *tm = localtime(&sb.st_mtime); - - char date[14]; - if (strftime(date, sizeof(date), "%b %d %H:%M", tm) == 0) { - fprintf(stderr, "ls: strftime()\n"); - return 1; - } - - /* 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); + PrintInfo(sb, ep->d_name); } else