From: Francois Fleuret Date: Sat, 27 Feb 2010 09:33:47 +0000 (+0100) Subject: Added malloc check, and printing constrained to the tty width. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=1047d40d8422a70581e9a526d8f3cc84e6565e9f;p=dus.git Added malloc check, and printing constrained to the tty width. --- diff --git a/dus.c b/dus.c index 88221f6..6ca0067 100644 --- a/dus.c +++ b/dus.c @@ -45,6 +45,9 @@ typedef int64_t size_sum_t; int ignore_dotfiles = 0; /* 1 means ignore files and directories starting with a dot */ +int forced_width = 0; /* -1 means no width limit, strictly positive + means limit, 0 means not active */ + int forced_height = 0; /* -1 means no height limit, strictly positive means limit, 0 means not active */ @@ -58,6 +61,19 @@ int show_top = 0; /* 1 means to show the top of the sorted list /********************************************************************/ +/* malloc with error checking. */ + +void *safe_malloc(size_t n) { + void *p = malloc(n); + if (!p && n != 0) { + printf("Can not allocate memory: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + +/********************************************************************/ + int ignore_entry(const char *name) { return strcmp(name, ".") == 0 || @@ -127,7 +143,7 @@ struct file_with_size { struct file_with_size *create(char *name, struct file_with_size *current) { struct file_with_size *result; - result = malloc(sizeof(struct file_with_size)); + result = safe_malloc(sizeof(struct file_with_size)); result->filename = strdup(name); result->size = file_or_dir_size(name); result->next = current; @@ -171,7 +187,58 @@ int compare_files(const void *x1, const void *x2) { } } -void print_sorted(struct file_with_size *root, int height) { +void raw_print(char *buffer, char *filename, size_t size) { + char *a, *b, *c, u; + + b = buffer; + if(size) { + while(size) { + *(b++) = size%10 + '0'; + size /= 10; + } + } else { + *(b++) = '0'; + } + + a = buffer; + c = b; + while(a < c) { + u = *a; + *(a++) = *(--c); + *c = u; + } + + *(b++) = ' '; + + sprintf(b, " %s\n", filename); +} + +void fancy_print(char *buffer, char *filename, size_t size) { + if(size < 1024) { + sprintf(buffer, + "% 7d %s\n", + ((int) size), + filename); + } else if(size < 1024 * 1024) { + sprintf(buffer, + "% 6.1fK %s\n", + ((double) (size))/(1024.0), + filename); + } else if(size < 1024 * 1024 * 1024) { + sprintf(buffer, + "% 6.1fM %s\n", + ((double) (size))/(1024.0 * 1024), + filename); + } else { + sprintf(buffer, + "% 6.1fG %s\n", + ((double) (size))/(1024.0 * 1024.0 * 1024.0), + filename); + } +} + +void print_sorted(struct file_with_size *root, int width, int height) { + char line[BUFFER_SIZE]; struct file_with_size *node; struct file_with_size **nodes; int nb, n, first, last; @@ -181,7 +248,7 @@ void print_sorted(struct file_with_size *root, int height) { nb++; } - nodes = malloc(nb * sizeof(struct file_with_size *)); + nodes = safe_malloc(nb * sizeof(struct file_with_size *)); n = 0; for(node = root; node; node = node->next) { @@ -207,31 +274,16 @@ void print_sorted(struct file_with_size *root, int height) { first = nb - n; } - if(fancy_size_display) { - for(n = first; n < last; n++) { - if(nodes[n]->size < 1024) { - printf("% 7d %s\n", - ((int) nodes[n]->size), - nodes[n]->filename); - } else if(nodes[n]->size < 1024 * 1024) { - printf("% 6.1fK %s\n", - ((double) (nodes[n]->size))/(1024.0), - nodes[n]->filename); - } else if(nodes[n]->size < 1024 * 1024 * 1024) { - printf("% 6.1fM %s\n", - ((double) (nodes[n]->size))/(1024.0 * 1024), - nodes[n]->filename); - } else { - printf("% 6.1fG %s\n", - ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0), - nodes[n]->filename); - } + 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); } - } else { - for(n = first; n < last; n++) { - print_size_sum(nodes[n]->size); - printf(" %s\n", nodes[n]->filename); + if(width >= 0 && width < BUFFER_SIZE) { + line[width] = '\0'; } + printf(line); } free(nodes); @@ -248,7 +300,7 @@ int main(int argc, char **argv) { setlocale (LC_ALL, ""); while (1) { - c = getopt(argc, argv, "dfrtl:hdu"); + c = getopt(argc, argv, "dfrtl:c:hdu"); if (c == -1) break; @@ -274,6 +326,10 @@ int main(int argc, char **argv) { forced_height = atoi(optarg); break; + case 'c': + forced_width = atoi(optarg); + break; + case 'h': printf("Usage: dus [OPTION]... [FILE]...\n"); printf("List files and directories sorted according to their size or content size (the content of the current directory by default).\n"); @@ -322,9 +378,9 @@ int main(int argc, char **argv) { printf("Can not get the tty size: %s\n", strerror(errno)); exit (EXIT_FAILURE); } - print_sorted(root, win.ws_row - 2); + print_sorted(root, win.ws_col, win.ws_row - 2); } else { - print_sorted(root, -1); + print_sorted(root, -1, -1); } destroy(root);