Big update: 2/N fix

This commit is contained in:
Your Name 2023-11-06 23:52:48 +03:00
parent d18e770ce9
commit aff6eb449a
6 changed files with 113 additions and 79 deletions

View File

@ -99,7 +99,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("chmod [ugoa]{+|-|=}[rwxXst] / [sstrwxrwxrwx] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R recursive] [-s silent]\n"); printf("chmod [ugoa]{+|-}[rwxXst] / [0 - 777] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R recursive] [-s silent]\n");
return 0; return 0;
} }
} }

View File

@ -104,48 +104,46 @@ void get_owner(const char *arg) {
} }
} }
int main(const int argc, char **argv) { int main(int argc, char **argv) {
get_stat = mu_get_lstat; get_stat = mu_get_lstat;
chown_func = lchown; chown_func = lchown;
int i; int opt;
for (i = 1; i < argc; i++) { while ((opt = getopt(argc, argv, "RfH") != -1)) {
if (argv[i][0] != '-') switch (opt) {
break; case 'R':
r_flag = 1;
break;
else if (!strcmp(argv[i], "-r")) case 'f':
r_flag = 1; s_flag = 1;
break;
else if (!strcmp(argv[i], "-s")) case 'H':
s_flag = 1; chown_func = chown;
get_stat = mu_get_stat;
break;
else if (!strcmp(argv[i], "-H")) { default:
chown_func = chown; printf("chown [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-r recursive]\n");
get_stat = mu_get_stat; return 0;
}
else if (!strcmp(argv[i], "--help")) {
printf("chown [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-r recursive]\n");
return 0;
} }
} }
if (argc - i == 0) { if (argv[optind] == NULL) {
fprintf(stderr, "chown: missing operand\n"); fprintf(stderr, "chown: missing operand\n");
return 1; return 1;
} }
gid = -1; gid = -1;
uid = -1; uid = -1;
get_owner(argv[i++]); get_owner(argv[optind]);
if (argc - i == 0) { argv += optind;
fprintf(stderr, "chown: missing operand\n"); argc -= optind;
return 1;
}
int ret = 0; int ret = 0;
for (; i < argc; i++) for (int i = 1; i < argc; i++)
if (cntree(argv[i])) if (cntree(argv[i]))
ret = 1; ret = 1;

View File

@ -6,7 +6,7 @@
int main(const int argc, char **argv) { int main(const int argc, char **argv) {
if (argc < 3 || !strcmp(argv[argc - 1], "--help")) { if (argc < 3 || !strcmp(argv[argc - 1], "--help")) {
printf("chroot [dir] [command arg arg2...]\n"); printf("chroot [dir] [command] [arg arg2...]\n");
return 0; return 0;
} }

View File

@ -10,6 +10,8 @@
#include "make_path.h" #include "make_path.h"
#include "get_stat.h" #include "get_stat.h"
unsigned int s_flag;
int write_buffer(int mode, int ifd, int ofd, const char *dst) { int write_buffer(int mode, int ifd, int ofd, const char *dst) {
off_t len = lseek(ifd, 0, SEEK_END); off_t len = lseek(ifd, 0, SEEK_END);
if (len <= 0) { if (len <= 0) {
@ -81,7 +83,8 @@ int copy(int mode, const char *src, const char *dst) {
} }
if (write_buffer(mode, ifd, ofd, new_path)) if (write_buffer(mode, ifd, ofd, new_path))
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno)); if (!s_flag)
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
close(ofd); close(ofd);
@ -106,7 +109,9 @@ int cptree(const char *src, const char *dst) {
if (!S_ISDIR(stat_path.st_mode)) { if (!S_ISDIR(stat_path.st_mode)) {
if (copy(stat_path.st_mode, src, dst)) { if (copy(stat_path.st_mode, src, dst)) {
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno)); if (!s_flag)
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno));
return 1; return 1;
} }
@ -114,7 +119,8 @@ int cptree(const char *src, const char *dst) {
} }
else if (mkdir(dst, 0777) < 0) else if (mkdir(dst, 0777) < 0)
fprintf(stderr, "cp: %s\n", strerror(errno)); if (!s_flag)
fprintf(stderr, "cp: %s\n", strerror(errno));
DIR *dir = opendir(src); DIR *dir = opendir(src);
if (dir == NULL) { if (dir == NULL) {
@ -150,25 +156,38 @@ int cptree(const char *src, const char *dst) {
return ret; return ret;
} }
int main(const int argc, char **argv) { int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "frR")) != -1) {
switch (opt) {
case 'f':
s_flag = 1;
break;
int i; case 'r':
for (i = 1; i < argc; i++) { case 'R':
if (argv[i][0] != '-') break;
break;
else if (!strcmp(argv[i], "--help")) { default:
printf("cp [Src] [Dst]\n"); printf("chmod [src1 src2...] [dst]\n\t[-f silent]\n");
return 0; return 0;
} }
} }
if (argv[optind] == NULL) {
fprintf(stderr, "cp: missing operand\n");
return 1;
}
argv += optind;
argc -= optind;
int ret = 0; int ret = 0;
if (argc - i == 2) if (argc == 2)
ret = cptree(argv[i], argv[argc - 1]); ret = cptree(argv[0], argv[argc - 1]);
else { else {
for (; i < argc - 1; i++) { for (int i = 1; i < argc - 1; i++) {
char *new_path = mu_make_path("cp", argv[argc - 1], basename(argv[i])); char *new_path = mu_make_path("cp", argv[argc - 1], basename(argv[i]));
if (new_path == NULL) if (new_path == NULL)
return 1; return 1;

View File

@ -48,7 +48,7 @@ double du(const char *path, int recurs_flag) {
struct stat sb; struct stat sb;
if (mu_get_lstat("du", path, &sb)) if (mu_get_lstat("du", path, &sb))
return 1; return 0;
if (S_ISDIR(sb.st_mode)) { if (S_ISDIR(sb.st_mode)) {
DIR *dp = opendir(path); DIR *dp = opendir(path);
@ -64,7 +64,7 @@ double du(const char *path, int recurs_flag) {
char *new_path = mu_make_path("du", path, ep->d_name); char *new_path = mu_make_path("du", path, ep->d_name);
if (new_path == NULL) if (new_path == NULL)
return 1; return 0;
sum += du(new_path, 1); sum += du(new_path, 1);
@ -92,39 +92,46 @@ double du(const char *path, int recurs_flag) {
return sum; return sum;
} }
int main(const int argc, const char **argv) { int main(int argc, char **argv) {
int i; int opt;
for (i = 1; i < argc; i++) { while ((opt = getopt(argc, argv, "hsbm")) != -1) {
if (argv[i][0] != '-') switch (opt) {
break; case 'h':
h_flag = 1;
break;
else if (!strcmp(argv[i], "-h")) case 's':
h_flag = 1; s_flag = 1;
break;
else if (!strcmp(argv[i], "-s")) case 'b':
s_flag = 1; b_flag = 1;
break;
else if (!strcmp(argv[i], "-b")) case 'm':
b_flag = 1; b_flag = 1;
m_flag = 1;
break;
else if (!strcmp(argv[i], "-m")) {
b_flag = 1;
m_flag = 1;
}
else if (!strcmp(argv[i], "--help")) { default:
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 [file1 file2...]\n\t[-h Sizes in human readable format (e.g., 1K 243M 2G)]\n\t[-s Display only a total for each argument]\n\t[-b Apparent size] [-m Size in megabyte]\n");
return 0; return 0;
} }
} }
if (argc - i == 0) if (argv[optind] == NULL)
du(".", 0); du(".", 0);
else else {
for (; i < argc; i++) argv += optind;
argc -= optind;
for (int i = 0; i < argc; i++)
du(argv[i], 0); du(argv[i], 0);
}
return 0; return 0;
} }

View File

@ -55,33 +55,43 @@ int rmtree(const char *path) {
return ret; return ret;
} }
int main(const int argc, char **argv) { int main(int argc, char **argv) {
int i; int opt;
for (i = 1; i < argc; i++) { while ((opt = getopt(argc, argv, "frR")) != -1) {
if (argv[i][0] != '-') switch (opt) {
break; case 'f':
f_flag = 1;
break;
else if (!strcmp(argv[i], "-f")) case 'r':
f_flag = 1; case 'R':
break;
else if (!strcmp(argv[i], "--help")) { default:
printf("rm [-f force] file1 file2...\n"); printf("rm [file1 file2...]\n\t[-f force]\n");
return 0; return 0;
} }
} }
int status = 0; if (argv[optind] == NULL) {
for (; i < argc; i++) { fprintf(stderr, "rm: missing operand\n");
return 1;
}
argv += optind;
argc -= optind;
int ret = 0;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){ if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){
printf("rm: refusing to remove '.' or '..' directory\n"); printf("rm: refusing to remove '.' or '..' directory\n");
break; break;
} }
int status = rmtree(argv[i]); if (rmtree(argv[i]))
if (status != 0) ret = 1;
status = 1;
} }
return status; return ret;
} }