diff --git a/coreutils/cat.c b/coreutils/cat.c index bd99efd..2254a15 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -4,42 +4,24 @@ #include #include -unsigned int n_flag; -unsigned int lines; void cat(const int fd) { - char buf[4024]; + char buf[4096]; ssize_t len; - while ((len = read(fd, buf, sizeof(buf))) > 0) { - for (ssize_t i = 0; i < len; i++) { - if (((i > 0) ? buf[i - 1] == '\n' : 1) && n_flag) - printf(" %d ", ++lines); - - printf("%c", buf[i]); - } - } + while ((len = read(fd, buf, sizeof(buf))) > 0) + if (write(1, buf, len) != len) + fprintf(stderr, "cat: write error copying\n"); } int main(const int argc, const char **argv) { - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; - - else if (!strcmp(argv[i], "-n")) - n_flag = 1; - - else if (!strcmp(argv[i], "--help")) { - printf("cat [-n (numerate)] [file1 file2 ...]\n"); - return 0; - } - } - - if (i == argc) + if (argc == 1) cat(STDIN_FILENO); else { - for (; i < argc; i++) { + for (int i = 1; i < argc; i++) { + if (argv[i][0] == '-') + break; + int fd = open(argv[i], O_RDONLY); if (fd < 0) { fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno)); diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 48fbe22..4f37eac 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c @@ -21,7 +21,7 @@ int main(const int argc, const char **argv) { s_flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("chmod [-r (recursive)] [-s (silent)] [file1 file2...]\n"); + printf("chmod [-r recursive] [-s silent] [file1 file2...]\n"); return 0; } } diff --git a/coreutils/chown.c b/coreutils/chown.c index 56f9238..e7ab089 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -146,7 +146,7 @@ int main(const int argc, char **argv) { } else if (!strcmp(argv[i], "--help")) { - printf("chown [-H (if a command line argument is a symbolic link)] [-r (recursive)] [owner]:[group] [file file2...]\n"); + printf("chown [-H if a command line argument is a symbolic link] [-r recursive] [owner]:[group] [file file2...]\n"); return 0; } } diff --git a/coreutils/du.c b/coreutils/du.c index c0a8818..707fd42 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -116,7 +116,7 @@ int main(const int argc, const char **argv) { } else if (!strcmp(argv[i], "--help")) { - printf("du [-h (Sizes in human readable format (e.g., 1K 243M 2G))] [-s (Display only a total for each argument)] [-b (Apparent size)] [-m (Size in megabyte)] [file file2...]\n"); + printf("du [-h Sizes in human readable format (e.g., 1K 243M 2G)] [-s Display only a total for each argument] [-b Apparent size] [-m Size in megabyte] [file file2...]\n"); return 0; } } diff --git a/coreutils/ls.c b/coreutils/ls.c index 41ea58f..dd1bbbf 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -20,8 +20,24 @@ 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) { +char *fileflag(const char *path) { + struct stat sb; + if (stat(path, &sb)) { + fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); + return " "; + } + if (S_ISDIR(sb.st_mode)) + return "/"; + + else if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || (sb.st_mode & S_IXOTH)) + return "*"; + + else + return " "; +} + +void PrintInfo(struct stat sb, const char *filename) { /* Permissions */ PrintPerm(sb); @@ -38,12 +54,12 @@ void PrintInfo(struct stat sb, const char *filename) { 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); + printf(" %s %s %jd %s %s%s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, filename, fileflag(filename)); } int list(const char *path, int label) { struct stat sb; - if (stat(path, &sb)) { + if (lstat(path, &sb)) { fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); return 1; } @@ -79,7 +95,7 @@ int list(const char *path, int label) { continue; if (l_flag) { - if (lstat(ep->d_name, &sb) == -1) { + if (stat(ep->d_name, &sb) == -1) { fprintf(stderr, "ls: lstat()\n"); return 1; } @@ -88,7 +104,8 @@ int list(const char *path, int label) { } else - printf("%s ", ep->d_name); + printf("%s%s ", ep->d_name, fileflag(ep->d_name)); + } closedir(dp); @@ -111,7 +128,7 @@ int main(const int argc, const char **argv) { l_flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("ls [-a (show hidden files)] [-l (use a long listing format)] [Path]\n"); + printf("ls [-a show hidden files] [-l use a long listing format] [Path]\n"); return 0; } } diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c index 2615d00..b47bd85 100644 --- a/coreutils/mkdir.c +++ b/coreutils/mkdir.c @@ -44,7 +44,7 @@ int main(const int argc, const char **argv) { flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("mkdir [-p (make parent dir)] [dir1 dir2...]\n"); + printf("mkdir [-p make parent dir] [dir1 dir2...]\n"); return 0; } } diff --git a/coreutils/mktemp.c b/coreutils/mktemp.c index 76795af..4bdf288 100644 --- a/coreutils/mktemp.c +++ b/coreutils/mktemp.c @@ -55,7 +55,7 @@ int main(const int argc, char **argv) { d_flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("mktemp [-d] [file]\n"); + printf("mktemp [-d dir] [file]\n"); return 0; } } diff --git a/coreutils/printenv.c b/coreutils/printenv.c index 63b20a2..fcc25ec 100644 --- a/coreutils/printenv.c +++ b/coreutils/printenv.c @@ -29,7 +29,7 @@ int main(const int argc, const char **argv, const char **envp) { nullline = 1; else if (!strcmp(argv[i], "--help")) { - printf("printenv [-n (end each output line with NUL, not newline)]\n"); + printf("printenv [-n end each output line with NUL, not newline]\n"); return 0; } } diff --git a/coreutils/rm.c b/coreutils/rm.c index aa03c30..7527368 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -5,10 +5,12 @@ #include #include #include +unsigned int f_flag; int get_stat(const char *path, struct stat *stat_path) { if (lstat(path, stat_path)) { - fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno)); + if (!f_flag) + fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno)); return 1; } @@ -34,7 +36,8 @@ int rmtree(const char *path) { if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) { if (unlink(path) < 0) { - fprintf(stderr, "rm: %s: is not directory\n", path); + if (!f_flag) + fprintf(stderr, "rm: %s: is not directory\n", path); return 1; } @@ -43,7 +46,8 @@ int rmtree(const char *path) { DIR *dir = opendir(path); if (dir == NULL) { - fprintf(stderr, "rm: %s: Can`t open directory\n", path); + if (!f_flag) + fprintf(stderr, "rm: %s: Can`t open directory\n", path); return 1; } @@ -62,21 +66,33 @@ int rmtree(const char *path) { closedir(dir); if (rmdir(path) < 0) - fprintf(stderr, "rm: %s: can`t remove a directory\n", path); + if (!f_flag) + fprintf(stderr, "rm: %s: can`t remove a directory\n", path); return 0; } int main(const int argc, char **argv) { - if (argc == 1 || !strcmp(argv[argc - 1], "--help")) { - printf("rm [src1 src2...]\n"); - return 0; + int i; + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + + else if (!strcmp(argv[i], "-f")) + f_flag = 1; + + else if (!strcmp(argv[i], "--help")) { + printf("rm [-f force] [src1 src2...]\n"); + return 0; + } } int status = 0; - for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-') - continue; + for (; i < argc; i++) { + if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){ + printf("rm: refusing to remove '.' or '..' directory\n"); + break; + } int status = rmtree(argv[i]); if (status != 0) diff --git a/coreutils/shred.c b/coreutils/shred.c index 70abe7a..a5054c4 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -69,7 +69,7 @@ int main(const int argc, const char **argv) { } else if (!strcmp(argv[i], "--help")) { - printf("shred [-n=N (Overwrite N times (default 3))] [-z (Final overwrite with zeros)] [-u (Remove file)] [-f (Chmod to ensure writability)] [FILE...]\n"); + printf("shred [-n=N Overwrite N times (default 3)] [-z Final overwrite with zeros] [-u Remove file] [-f Chmod to ensure writability] [file file2...]\n"); return 1; } } diff --git a/coreutils/sleep.c b/coreutils/sleep.c index e730feb..6241ab5 100644 --- a/coreutils/sleep.c +++ b/coreutils/sleep.c @@ -14,7 +14,7 @@ double convert(const char *num) { int main(const int argc, const char **argv) { if (argc == 1 || !strcmp(argv[argc - 1], "--help")) { - printf("sleep [num[m - minute / h - hour / d - days]] / [inf (infinity)]\n"); + printf("sleep [num[m - minute / h - hour / d - days]] / [inf infinity]\n"); return 0; } diff --git a/coreutils/uname.c b/coreutils/uname.c index 44fd2e3..b66ae32 100644 --- a/coreutils/uname.c +++ b/coreutils/uname.c @@ -36,7 +36,7 @@ int main(const int argc, const char **argv) { flags[O_MACHINE] = 1; else if (!strcmp("--help", argv[i])) { - printf("uname [-a (All)] [-s (Sys)] [-n (Nodename)] [-r (Release)] [-v (Version)] [-m (Machine)]\n"); + printf("uname [-a All] [-s Sys] [-n Nodename] [-r Release] [-v Version] [-m Machine]\n"); return 0; } } diff --git a/coreutils/wc.c b/coreutils/wc.c index 83b82a4..391e71d 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c @@ -76,7 +76,7 @@ int main(const int argc, const char **argv) { w_flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("wc [-l (lines)] [-c (bytes)] [-w (words)] [file1 file2...]\n"); + printf("wc [-l lines] [-c bytes] [-w words] [file1 file2...]\n"); return 0; } }