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;
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;
}
}

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;
chown_func = lchown;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "RfH") != -1)) {
switch (opt) {
case 'R':
r_flag = 1;
break;
else if (!strcmp(argv[i], "-r"))
r_flag = 1;
else if (!strcmp(argv[i], "-s"))
case 'f':
s_flag = 1;
break;
else if (!strcmp(argv[i], "-H")) {
case 'H':
chown_func = chown;
get_stat = mu_get_stat;
}
break;
else if (!strcmp(argv[i], "--help")) {
default:
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");
return 1;
}
gid = -1;
uid = -1;
get_owner(argv[i++]);
get_owner(argv[optind]);
if (argc - i == 0) {
fprintf(stderr, "chown: missing operand\n");
return 1;
}
argv += optind;
argc -= optind;
int ret = 0;
for (; i < argc; i++)
for (int i = 1; i < argc; i++)
if (cntree(argv[i]))
ret = 1;

View File

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

View File

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

View File

@ -48,7 +48,7 @@ double du(const char *path, int recurs_flag) {
struct stat sb;
if (mu_get_lstat("du", path, &sb))
return 1;
return 0;
if (S_ISDIR(sb.st_mode)) {
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);
if (new_path == NULL)
return 1;
return 0;
sum += du(new_path, 1);
@ -92,39 +92,46 @@ double du(const char *path, int recurs_flag) {
return sum;
}
int main(const int argc, const char **argv) {
int main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "hsbm")) != -1) {
switch (opt) {
case 'h':
h_flag = 1;
break;
else if (!strcmp(argv[i], "-h"))
h_flag = 1;
else if (!strcmp(argv[i], "-s"))
case 's':
s_flag = 1;
break;
else if (!strcmp(argv[i], "-b"))
case 'b':
b_flag = 1;
break;
else if (!strcmp(argv[i], "-m")) {
case 'm':
b_flag = 1;
m_flag = 1;
}
break;
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");
default:
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;
}
}
if (argc - i == 0)
if (argv[optind] == NULL)
du(".", 0);
else
for (; i < argc; i++)
else {
argv += optind;
argc -= optind;
for (int i = 0; i < argc; i++)
du(argv[i], 0);
}
return 0;
}

View File

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