Skip to content

Commit

Permalink
Merge pull request #34 from tim-tm/rmdir
Browse files Browse the repository at this point in the history
implement -p|--parents option for rmdir
  • Loading branch information
proh14 authored Apr 19, 2024
2 parents 9ae87ba + 5b86b93 commit 31fe0cd
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/rmdir/rmdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ int rm_dir(char *dirname) {
return 0;
}

void strip_off_slash(char *str) {
if (str == NULL) return;

size_t str_len = strlen(str);
if (str[str_len-1] == '/') {
str[str_len-1] = '\0';
}
}

int main(int argc, char **argv) {
if (argc <= 1) {
fprintf(stderr, "Not enough arguments.\nSee rmdir --help for more information.\n");
Expand Down Expand Up @@ -88,11 +97,26 @@ int main(int argc, char **argv) {
}
}

char *str = argv[argc-1];
if (parents) {
// TODO: Implement -p|--parents flag
strip_off_slash(str);

long i = -1;
while (i != 0) {
strip_off_slash(str);
char *strip = strrchr(str, '/');
if (strip == NULL) {
i = 0;
} else {
i = strip-str+1;
}

if (rm_dir(str) != 0) return 1;
str[i] = '\0';
}
return 0;
} else {
return rm_dir(argv[argc-1]);
return rm_dir(str);
}
}

0 comments on commit 31fe0cd

Please sign in to comment.