Fix recursion in rm

This commit is contained in:
Your Name 2023-10-20 18:29:50 +03:00
parent d712265401
commit 51b51a6eb1

View File

@ -7,7 +7,7 @@
#include <unistd.h>
int get_stat(const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
if (lstat(path, stat_path)) {
fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
@ -17,9 +17,10 @@ int get_stat(const char *path, struct stat *stat_path) {
int rmtree(const char *path) {
struct stat stat_path;
get_stat(path, &stat_path);
if (get_stat(path, &stat_path))
return 1;
if (S_ISDIR(stat_path.st_mode) == 0) {
if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) {
if (unlink(path) < 0) {
fprintf(stderr, "rm: %s: is not directory\n", path);
return 1;