From 2e5aeeb9c1988c20bd235103a0134a36e2c73fbe Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 28 Oct 2023 09:11:02 +0300 Subject: [PATCH] chown test --- coreutils/chown.c | 51 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/coreutils/chown.c b/coreutils/chown.c index a317a62..3d64c26 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -1,5 +1,3 @@ -//TODO - #include #include #include @@ -9,18 +7,51 @@ #include #include #include +unsigned int r_flag; int (*chown_func)(const char *pathname, uid_t owner, gid_t group); +int (*stat_func)(const char *restrict pathname, struct stat *restrict statbuf); long gid; long uid; -int cntree(const char *dst) { - if (chown_func(dst, uid, gid) == 0) - puts("changed"); +int get_stat(const char *path, struct stat *stat_path) { + if (stat_func(path, stat_path)) { + fprintf(stderr, "chown: unable to stat %s: %s\n", path, strerror(errno)); + return 1; + } return 0; } +int change(const char *file) { + struct stat old_file; + if (get_stat(file, &old_file)) + return 1; + + if (chown_func(file, uid, gid) == 0) { + struct stat new_file; + if (get_stat(file, &new_file)) + return 1; + + if (old_file.st_gid != new_file.st_gid || old_file.st_uid != new_file.st_uid) + return 0; + + fprintf(stderr, "chown: %s unchanged\n", file); + } + + else + fprintf(stderr, "chown: unable to stat %s: %s\n", file, strerror(errno)); + + return 1; +} + +int cntree(const char *dst) { + change(dst); + + //Recurs + return 0; +} + void get_owner(const char *arg) { char *group = strchr(arg, ':'); @@ -58,17 +89,23 @@ void get_owner(const char *arg) { int main(const int argc, char **argv) { chown_func = lchown; + stat_func = stat; int i; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') break; - else if (!strcmp(argv[i], "-H")) + else if (!strcmp(argv[i], "-r")) + r_flag = 1; + + else if (!strcmp(argv[i], "-H")) { chown_func = chown; + stat_func = stat; + } else if (!strcmp(argv[i], "--help")) { - printf("chown [-H (if a command line argument is a symbolic link)] [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; } }