Big update: 1/N fix

This commit is contained in:
Your Name 2023-11-06 23:00:26 +03:00
parent e15a549ed6
commit d18e770ce9
3 changed files with 83 additions and 55 deletions

View File

@ -11,9 +11,8 @@
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path); int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
unsigned int r_flag; unsigned int r_flag;
unsigned int s_flag; unsigned int s_flag;
struct group *grp;
int change(const char *file) { int change(const char *file, const struct group *grp) {
struct stat stat_path; struct stat stat_path;
if (mu_get_stat("chgrp", file, &stat_path)) if (mu_get_stat("chgrp", file, &stat_path))
return 1; return 1;
@ -28,8 +27,8 @@ int change(const char *file) {
return 0; return 0;
} }
int chtree(const char *dst) { int chtree(const char *dst, const struct group *grp) {
int ret = change(dst); int ret = change(dst, grp);
struct stat stat_path; struct stat stat_path;
if (get_stat("chgrp", dst, &stat_path)) if (get_stat("chgrp", dst, &stat_path))
@ -55,7 +54,7 @@ int chtree(const char *dst) {
if (full_path == NULL) if (full_path == NULL)
continue; continue;
if (chtree(full_path)) if (chtree(full_path, grp))
ret = 1; ret = 1;
free(full_path); free(full_path);
@ -66,45 +65,46 @@ int chtree(const char *dst) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i;
get_stat = mu_get_lstat; get_stat = mu_get_lstat;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-r")) int opt;
r_flag = 1; while ((opt = getopt(argc, argv, "RfH") != -1)) {
switch (opt) {
case 'R':
r_flag = 1;
break;
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "-f")) case 'f':
s_flag = 1; s_flag = 1;
break;
else if (!strcmp(argv[i], "-H")) case 'H':
get_stat = mu_get_stat; get_stat = mu_get_stat;
break;
else if (!strcmp(argv[i], "--help")) { default:
printf("chgrp [-H if a command line argument is a symbolic link] [-r recursive] [-s silent] [group] [file1 file2...]\n"); printf("chgrp [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R recursive] [-f silent]\n");
return 0; return 0;
} }
} }
if (argc - i < 2) { if (argv[optind] == NULL) {
fprintf(stderr, "chgrp: missing operand\n"); fprintf(stderr, "chgrp: missing operand\n");
return 1; return 1;
} }
grp = getgrnam(argv[i]); struct group *grp = getgrnam(argv[optind]);
if (!grp) { if (!grp) {
fprintf(stderr, "chgrp: unknow group\n"); fprintf(stderr, "chgrp: unknow group\n");
return 1; return 1;
} }
argv++; argv += optind;
argc--; argc -= optind;
int ret = 0; int ret = 0;
for (; i < argc; i++) for (int i = 1; i < argc; i++)
if (chtree(argv[i])) if (chtree(argv[i], grp))
ret = 1; ret = 1;
return ret; return ret;

View File

@ -66,42 +66,59 @@ int chtree(const char *dst, const char *mode_arg) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i;
get_stat = mu_get_lstat; get_stat = mu_get_lstat;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') /* Arg hacking */
char *arg, **argp;
argp = argv;
while ((arg = *++argp)) {
if (arg[0] != '-') {
arg = NULL;
break; break;
else if (!strcmp(argv[i], "-r"))
r_flag = 1;
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "-f"))
s_flag = 1;
else if (!strcmp(argv[i], "-H"))
get_stat = mu_get_stat;
else if (!strcmp(argv[i], "--help")) {
printf("chmod [-H if a command line argument is a symbolic link] [-r recursive] [-s silent] [ugoa]{+|-|=}[rwxXst] / [sstrwxrwxrwx] [file1 file2...]\n");
return 0;
} }
if (argc - i <= 2) if (arg[1] && (!strchr("-x", arg[1]) || !strchr("-r", arg[1]) || !strchr("-w", arg[1]))) {
arg[0] = 'a';
break; break;
}
} }
if (argc - i < 2) { int opt;
while ((opt = getopt(argc, argv, "RfH") != -1)) {
switch (opt) {
case 'R':
r_flag = 1;
break;
case 'f':
s_flag = 1;
break;
case 'H':
get_stat = mu_get_stat;
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");
return 0;
}
}
if (argv[optind] == NULL) {
fprintf(stderr, "chmod: missing operand\n"); fprintf(stderr, "chmod: missing operand\n");
return 1; return 0;
} }
char *mode_arg = argv[i]; /* Fix arg */
argv++; if (arg)
argc--; arg[0] = '-';
char *mode_arg = argv[optind];
argv += optind;
argc -= optind;
int ret = 0; int ret = 0;
for (; i < argc; i++) for (int i = 1; i < argc; i++)
if (chtree(argv[i], mode_arg)) if (chtree(argv[i], mode_arg))
ret = 1; ret = 1;

View File

@ -10,6 +10,7 @@
#include "get_stat.h" #include "get_stat.h"
unsigned int r_flag; unsigned int r_flag;
unsigned int s_flag;
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path); int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
int (*chown_func)(const char *pathname, uid_t owner, gid_t group); int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
long gid; long gid;
@ -17,7 +18,9 @@ long uid;
int change(const char *file) { int change(const char *file) {
if (chown_func(file, uid, gid)) { if (chown_func(file, uid, gid)) {
fprintf(stderr, "chown: unable to chown %s: %s\n", file, strerror(errno)); if (!s_flag)
fprintf(stderr, "chown: unable to chown %s: %s\n", file, strerror(errno));
return 1; return 1;
} }
@ -27,7 +30,6 @@ int change(const char *file) {
int cntree(const char *dst) { int cntree(const char *dst) {
int ret = change(dst); int ret = change(dst);
struct stat stat_path; struct stat stat_path;
if (get_stat("chown", dst, &stat_path)) if (get_stat("chown", dst, &stat_path))
return 1; return 1;
@ -37,7 +39,9 @@ int cntree(const char *dst) {
DIR *dir = opendir(dst); DIR *dir = opendir(dst);
if (dir == NULL) { if (dir == NULL) {
fprintf(stderr, "chown: %s: Can`t open directory\n", dst); if (!s_flag)
fprintf(stderr, "chown: %s: Can`t open directory\n", dst);
return 1; return 1;
} }
@ -78,7 +82,9 @@ void get_owner(const char *arg) {
struct group *grp = getgrnam(group); struct group *grp = getgrnam(group);
if (!grp) { if (!grp) {
fprintf(stderr, "chown: invalid group: %s\n", group); if (!s_flag)
fprintf(stderr, "chown: invalid group: %s\n", group);
exit(1); exit(1);
} }
@ -88,7 +94,9 @@ void get_owner(const char *arg) {
if (u_flag) { if (u_flag) {
struct passwd *pwd = getpwnam(arg); struct passwd *pwd = getpwnam(arg);
if (!pwd) { if (!pwd) {
fprintf(stderr, "chown: invalid user: %s\n", arg); if (!s_flag)
fprintf(stderr, "chown: invalid user: %s\n", arg);
exit(1); exit(1);
} }
@ -108,13 +116,16 @@ int main(const int argc, char **argv) {
else if (!strcmp(argv[i], "-r")) else if (!strcmp(argv[i], "-r"))
r_flag = 1; r_flag = 1;
else if (!strcmp(argv[i], "-s"))
s_flag = 1;
else if (!strcmp(argv[i], "-H")) { else if (!strcmp(argv[i], "-H")) {
chown_func = chown; chown_func = chown;
get_stat = mu_get_stat; get_stat = mu_get_stat;
} }
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 [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-r recursive]\n");
return 0; return 0;
} }
} }