This commit is contained in:
Your Name 2023-10-28 10:02:24 +03:00
parent 2e5aeeb9c1
commit 112ecf8fb5
2 changed files with 57 additions and 7 deletions

View File

@ -23,6 +23,18 @@ int get_stat(const char *path, struct stat *stat_path) {
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "cp: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
int change(const char *file) {
struct stat old_file;
if (get_stat(file, &old_file))
@ -46,10 +58,39 @@ int change(const char *file) {
}
int cntree(const char *dst) {
change(dst);
int ret = change(dst);
//Recurs
return 0;
struct stat stat_path;
if (get_stat(dst, &stat_path))
return 1;
if (!S_ISDIR(stat_path.st_mode))
return ret;
DIR *dir = opendir(dst);
if (dir == NULL) {
fprintf(stderr, "chown: %s: Can`t open directory\n", dst);
return 1;
}
struct dirent *ep;
while ((ep = readdir(dir)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
char *full_path = make_path(dst, ep->d_name);
if (full_path == NULL)
continue;
if (cntree(full_path))
ret = 1;
free(full_path);
}
closedir(dir);
return ret;
}
void get_owner(const char *arg) {

View File

@ -15,6 +15,18 @@ int get_stat(const char *path, struct stat *stat_path) {
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "rm: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
int rmtree(const char *path) {
struct stat stat_path;
if (get_stat(path, &stat_path))
@ -40,13 +52,10 @@ int rmtree(const char *path) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
size_t len = strlen(path) + strlen(ep->d_name) + 2;
char *full_path = malloc(len + 1);
char *full_path = make_path(path, ep->d_name);
if (full_path == NULL)
continue;
snprintf(full_path, len, "%s/%s", path, ep->d_name);
rmtree(full_path);
free(full_path);
}