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 */
/********************************************************************/
+/* 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 ||
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;
}
}
-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;
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) {
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);
setlocale (LC_ALL, "");
while (1) {
- c = getopt(argc, argv, "dfrtl:hdu");
+ c = getopt(argc, argv, "dfrtl:c:hdu");
if (c == -1)
break;
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");
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);