From 539836ef78a77b7741f04d43b38eae667b62fead Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Fri, 11 Sep 2020 12:04:51 -0400 Subject: Add a dryrun flag and treat times as days instead of seconds --- cleanup.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cleanup.c b/cleanup.c index 429c8c2..3dd6e36 100644 --- a/cleanup.c +++ b/cleanup.c @@ -44,14 +44,18 @@ main(int argc, char * const argv[]) double file_age = 0; time_t curr_time = time(NULL); - int verbose, ch; + int verbose, dryrun, ch; verbose = 0; - while ((ch = getopt(argc, argv, "v")) != -1) { + dryrun = 0; + while ((ch = getopt(argc, argv, "vn")) != -1) { switch (ch) { case 'v': verbose = 1; break; + case 'n': + dryrun = 1; + break; } } argc -= optind; @@ -68,14 +72,15 @@ main(int argc, char * const argv[]) case FTS_F: if (stat(node->fts_name, &attr) == -1) err(1, NULL); - file_age = difftime(curr_time, attr.st_mtime); - cutoff = mind + (mind - maxd) * 86400 * - pow((double) attr.st_size / (double) max_size - 1, 3.0); + file_age = difftime(curr_time, attr.st_mtime) / 86400.0; + cutoff = mind + (mind - maxd) * + pow((double) attr.st_size / (double) max_size - 1, 3.0); if (verbose) - printf("%s age %fs cuttoff %fs size %lldB\n", + printf("%s age %fd cuttoff %fd size %lldB\n", node->fts_name, file_age, cutoff, attr.st_size); if (file_age >= cutoff) { - unlink(node->fts_name); + if (!dryrun) + unlink(node->fts_name); if (verbose) printf("%s deleted\n", node->fts_name); } -- cgit v1.2.3