From b8fcbcce946af49b297232e44dac639d5a3309e1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Oct 2023 00:19:46 +0300 Subject: [PATCH] . --- coreutils/clear.c | 5 ++--- coreutils/ls.c | 56 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/coreutils/clear.c b/coreutils/clear.c index 245d0a1..5e22f71 100644 --- a/coreutils/clear.c +++ b/coreutils/clear.c @@ -1,6 +1,5 @@ -#include +#include int main(void) { - puts("\033c"); - return 0; + return write(1, "\033[H\033[J", 6) != 6; } diff --git a/coreutils/ls.c b/coreutils/ls.c index 04428ad..65478bf 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -1,11 +1,21 @@ +#include #include #include #include #include #include #include +unsigned int a_flag; +unsigned int l_flag; -int list(const char *path, int flag, int label) { +void PrintPerm(struct stat sb) { + printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-'); + printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-'); + printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWGRP) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-'); + printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-'); +} + +int list(const char *path, int label) { struct stat statbuf; if (stat(path, &statbuf)) { fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); @@ -23,53 +33,77 @@ int list(const char *path, int flag, int label) { printf("\n%s: \n", path); /* Open and print dir */ - struct dirent *ep; DIR *dp = opendir(path); if (dp == NULL) { fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); return 1; } + struct dirent *ep; while ((ep = readdir(dp)) != NULL) { - if (ep->d_name[0] == '.' && !flag) + if (ep->d_name[0] == '.' && !a_flag) continue; + if (l_flag) { + struct stat sb; + if (lstat(ep->d_name, &sb) == -1) { + fprintf(stderr, "ls: lstat()\n"); + return 1; + } + + /* Permissions */ + PrintPerm(sb); + + /* Date and other info */ + 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; + } + + printf(" %jd %jd %s ", (uintmax_t)sb.st_mode, (uintmax_t)sb.st_size, date); + } + printf("%s ", ep->d_name); + printf("\n"); } closedir(dp); - printf("\n"); return 0; } int main(const int argc, const char **argv) { - int flag = 0; - int i; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') break; else if (!strcmp(argv[i], "-a")) - flag = 1; + a_flag = 1; + + else if (!strcmp(argv[i], "-l")) + l_flag = 1; else if (!strcmp(argv[i], "-h")) { - printf("ls [-a (Show hidden files)] [Path]\n"); + printf("ls [-a (Show hidden files)] [-l (use a long listing format (month / day / hour:min))] [Path]\n"); return 0; } } if (i == argc) - return list(".", flag, 0); + return list(".", 0); if (i == argc - 1) - return list(argv[i], flag, 0); + return list(argv[i], 0); else for (int i = 1; i < argc; i++) - if (list(argv[i], flag, 1)) + if (list(argv[i], 1)) return 1; + return 0; }