This commit is contained in:
Your Name 2023-10-30 18:09:33 +03:00
parent 6d82e300c5
commit d3fc1c1f88
13 changed files with 68 additions and 53 deletions

View File

@ -4,42 +4,24 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
unsigned int n_flag;
unsigned int lines;
void cat(const int fd) { void cat(const int fd) {
char buf[4024]; char buf[4096];
ssize_t len; ssize_t len;
while ((len = read(fd, buf, sizeof(buf))) > 0) { while ((len = read(fd, buf, sizeof(buf))) > 0)
for (ssize_t i = 0; i < len; i++) { if (write(1, buf, len) != len)
if (((i > 0) ? buf[i - 1] == '\n' : 1) && n_flag) fprintf(stderr, "cat: write error copying\n");
printf(" %d ", ++lines);
printf("%c", buf[i]);
}
}
} }
int main(const int argc, const char **argv) { int main(const int argc, const char **argv) {
int i; if (argc == 1)
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)
cat(STDIN_FILENO); cat(STDIN_FILENO);
else { else {
for (; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-')
break;
int fd = open(argv[i], O_RDONLY); int fd = open(argv[i], O_RDONLY);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno)); fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno));

View File

@ -21,7 +21,7 @@ int main(const int argc, const char **argv) {
s_flag = 1; s_flag = 1;
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -146,7 +146,7 @@ int main(const int argc, char **argv) {
} }
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -116,7 +116,7 @@ int main(const int argc, const char **argv) {
} }
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -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' : '-'); 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 */ /* Permissions */
PrintPerm(sb); PrintPerm(sb);
@ -38,12 +54,12 @@ void PrintInfo(struct stat sb, const char *filename) {
struct passwd *pw = getpwuid(sb.st_uid); struct passwd *pw = getpwuid(sb.st_uid);
struct group *gr = getgrgid(sb.st_gid); 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) { int list(const char *path, int label) {
struct stat sb; struct stat sb;
if (stat(path, &sb)) { if (lstat(path, &sb)) {
fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno)); fprintf(stderr, "ls: unable to stat %s: %s\n", path, strerror(errno));
return 1; return 1;
} }
@ -79,7 +95,7 @@ int list(const char *path, int label) {
continue; continue;
if (l_flag) { if (l_flag) {
if (lstat(ep->d_name, &sb) == -1) { if (stat(ep->d_name, &sb) == -1) {
fprintf(stderr, "ls: lstat()\n"); fprintf(stderr, "ls: lstat()\n");
return 1; return 1;
} }
@ -88,7 +104,8 @@ int list(const char *path, int label) {
} }
else else
printf("%s ", ep->d_name); printf("%s%s ", ep->d_name, fileflag(ep->d_name));
} }
closedir(dp); closedir(dp);
@ -111,7 +128,7 @@ int main(const int argc, const char **argv) {
l_flag = 1; l_flag = 1;
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -44,7 +44,7 @@ int main(const int argc, const char **argv) {
flag = 1; flag = 1;
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -55,7 +55,7 @@ int main(const int argc, char **argv) {
d_flag = 1; d_flag = 1;
else if (!strcmp(argv[i], "--help")) { else if (!strcmp(argv[i], "--help")) {
printf("mktemp [-d] [file]\n"); printf("mktemp [-d dir] [file]\n");
return 0; return 0;
} }
} }

View File

@ -29,7 +29,7 @@ int main(const int argc, const char **argv, const char **envp) {
nullline = 1; nullline = 1;
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }

View File

@ -5,10 +5,12 @@
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
unsigned int f_flag;
int get_stat(const char *path, struct stat *stat_path) { int get_stat(const char *path, struct stat *stat_path) {
if (lstat(path, 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; 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 (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) {
if (unlink(path) < 0) { 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; return 1;
} }
@ -43,7 +46,8 @@ int rmtree(const char *path) {
DIR *dir = opendir(path); DIR *dir = opendir(path);
if (dir == NULL) { 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; return 1;
} }
@ -62,21 +66,33 @@ int rmtree(const char *path) {
closedir(dir); closedir(dir);
if (rmdir(path) < 0) 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; return 0;
} }
int main(const int argc, char **argv) { int main(const int argc, char **argv) {
if (argc == 1 || !strcmp(argv[argc - 1], "--help")) { int i;
printf("rm [src1 src2...]\n"); for (i = 1; i < argc; i++) {
return 0; 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; int status = 0;
for (int i = 1; i < argc; i++) { for (; i < argc; i++) {
if (argv[i][0] == '-') if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){
continue; printf("rm: refusing to remove '.' or '..' directory\n");
break;
}
int status = rmtree(argv[i]); int status = rmtree(argv[i]);
if (status != 0) if (status != 0)

View File

@ -69,7 +69,7 @@ int main(const int argc, const char **argv) {
} }
else if (!strcmp(argv[i], "--help")) { 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; return 1;
} }
} }

View File

@ -14,7 +14,7 @@ double convert(const char *num) {
int main(const int argc, const char **argv) { int main(const int argc, const char **argv) {
if (argc == 1 || !strcmp(argv[argc - 1], "--help")) { 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; return 0;
} }

View File

@ -36,7 +36,7 @@ int main(const int argc, const char **argv) {
flags[O_MACHINE] = 1; flags[O_MACHINE] = 1;
else if (!strcmp("--help", argv[i])) { 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; return 0;
} }
} }

View File

@ -76,7 +76,7 @@ int main(const int argc, const char **argv) {
w_flag = 1; w_flag = 1;
else if (!strcmp(argv[i], "--help")) { 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; return 0;
} }
} }