From: Francois Fleuret Date: Sun, 7 Mar 2010 09:14:56 +0000 (+0100) Subject: Added an option to set a lower size bound for printing. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=934e451507e97423741a3e38eb591a095c46726b;p=dus.git Added an option to set a lower size bound for printing. --- diff --git a/dus.1 b/dus.1 index 4644d4d..d81f3f5 100644 --- a/dus.1 +++ b/dus.1 @@ -42,6 +42,10 @@ otherwise) .TP \fB-l \fI\fR same as \fB-c\fR but for the number of lines +.TP +\fB-m \fI\fR +restrict the printing of file and directories to these of size bigger +than a lower bound .SH "BUGS" diff --git a/dus.c b/dus.c index e215207..736b136 100644 --- a/dus.c +++ b/dus.c @@ -22,7 +22,7 @@ * */ -#define VERSION_NUMBER "1.0alpha" +#define VERSION_NUMBER "1.1alpha" #define _BSD_SOURCE @@ -61,6 +61,10 @@ int reverse_sorting = 0; /* 1 means to show the large ones first */ int show_top = 0; /* 1 means to show the top of the sorted list instead of the bottom */ +size_sum_t size_min = -1; /* -1 means no minimum size, otherwise lower + bound on the size to display a + file/dir */ + /********************************************************************/ /* malloc with error checking. */ @@ -135,6 +139,31 @@ size_sum_t file_or_dir_size(const char *name) { return result; } +size_sum_t atoss(const char *string) { + size_sum_t total, partial_total; + const char *c; + total = 0; + partial_total = 0; + + for(c = string; *c; c++) { + if(*c >= '0' && *c <= '9') { + partial_total = 10 * partial_total + ((int) (*c - '0')); + } else if(*c == 'K' || *c == 'k') { + total += partial_total * 1024; + partial_total = 0; + } else if(*c == 'M' || *c == 'm') { + total += partial_total * 1024 * 1024; + partial_total = 0; + } else if(*c == 'G' || *c == 'g') { + total += partial_total * 1024 * 1024 * 1024; + partial_total = 0; + } else { + fprintf(stderr, "Syntax error in %s\n", string); + } + } + return total; +} + /**********************************************************************/ struct file_with_size { @@ -218,22 +247,22 @@ void raw_print(char *buffer, char *filename, size_sum_t size) { void fancy_print(char *buffer, char *filename, size_sum_t size) { if(size < 1024) { sprintf(buffer, - "% 7d %s\n", + "% 7d -- %s\n", ((int) size), filename); } else if(size < 1024 * 1024) { sprintf(buffer, - "% 6.1fK %s\n", + "% 6.1fK -- %s\n", ((double) (size))/(1024.0), filename); } else if(size < 1024 * 1024 * 1024) { sprintf(buffer, - "% 6.1fM %s\n", + "% 6.1fM -- %s\n", ((double) (size))/(1024.0 * 1024), filename); } else { sprintf(buffer, - "% 6.1fG %s\n", + "% 6.1fG -- %s\n", ((double) (size))/(1024.0 * 1024.0 * 1024.0), filename); } @@ -277,15 +306,17 @@ void print_sorted(struct file_with_size *root, int width, int height) { } for(n = first; n < last; n++) { - if(fancy_size_display) { - fancy_print(line, nodes[n]->filename, nodes[n]->size); - } else { - raw_print(line, nodes[n]->filename, nodes[n]->size); - } - if(width >= 0 && width < BUFFER_SIZE) { - line[width] = '\0'; + if(size_min < 0 || nodes[n]->size >= size_min) { + if(fancy_size_display) { + fancy_print(line, nodes[n]->filename, nodes[n]->size); + } else { + raw_print(line, nodes[n]->filename, nodes[n]->size); + } + if(width >= 0 && width < BUFFER_SIZE) { + line[width] = '\0'; + } + printf(line); } - printf(line); } free(nodes); @@ -306,6 +337,7 @@ void print_help(FILE *out) { fprintf(out, " not a tty.\n"); fprintf(out, " -l same as -c for number of lines.\n"); fprintf(out, " -h show this help.\n"); + fprintf(out, " -m size min.\n"); fprintf(out, "\n"); fprintf(out, "Report bugs and comments to \n"); } @@ -321,7 +353,7 @@ int main(int argc, char **argv) { setlocale (LC_ALL, ""); while (1) { - c = getopt(argc, argv, "dfrtl:c:hdu"); + c = getopt(argc, argv, "dfrtl:c:m:hdu"); if (c == -1) break; @@ -351,6 +383,10 @@ int main(int argc, char **argv) { forced_width = atoi(optarg); break; + case 'm': + size_min = atoss(optarg); + break; + case 'h': print_help(stdout); exit(EXIT_SUCCESS);