From ee38eb565a0b8fdb8c54b4e45b00b30254287cd4 Mon Sep 17 00:00:00 2001 From: Jan Cybulski Date: Mon, 20 Oct 2014 15:25:58 +0200 Subject: [PATCH] ls: Fix indentation of -Ci and -Cs Idantation for -C option was not correctly displayed. The lenght of column has to be calculated on basis of biggest lenght of all inodes not only current lenght. What is more lenght of inodes which were bigger than MAX_INT were incorrect. Change-Id: I07f994b90c82ac750b8aa63a4146e17773fd2919 Signed-off-by: Jan Cybulski --- lib/lib.c | 7 +++++-- toys/posix/ls.c | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 0f2b012..39f4b6f 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -283,10 +283,13 @@ long atolx_range(char *numstr, long low, long high) int numlen(long l) { int len = 0; - while (l) { + if( l < 0 )//one additional place for minus + len ++; + do {//a "0" has lenght of 1 l /= 10; len++; - } + }while (l); + return len; } diff --git a/toys/posix/ls.c b/toys/posix/ls.c index ef64d19..2c7a2e8 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -134,7 +134,7 @@ static char *getgroupname(gid_t gid) // Figure out size of printable entry fields for display indent/wrap -static void entrylen(struct dirtree *dt, unsigned *len) +static void entrylen(struct dirtree *dt, unsigned *len, unsigned *totals) { struct stat *st = &(dt->st); unsigned flags = toys.optflags; @@ -143,7 +143,14 @@ static void entrylen(struct dirtree *dt, unsigned *len) if (endtype(st)) ++*len; if (flags & FLAG_m) ++*len; - if (flags & FLAG_i) *len += (len[1] = numlen(st->st_ino)); + if (flags & FLAG_i) { + len[1] = numlen(st->st_ino) + 1;//+1 stands for additional space displayed + if (totals) + *len += totals[1]; + else + *len += len[1]; + } + if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) { unsigned fn = flags & FLAG_n; len[2] = numlen(st->st_nlink); @@ -157,7 +164,14 @@ static void entrylen(struct dirtree *dt, unsigned *len) len[5] = numlen(major(st->st_rdev))+5; } else len[5] = numlen(st->st_size); } - if (flags & FLAG_s) *len += (len[6] = numlen(st->st_blocks)); + + if (flags & FLAG_s) { + len[6] = numlen(st->st_blocks) + 1;//+1 stands for additional space + if (totals) + *len += totals[6]; + else + *len += len[6]; + } } static int compare(void *a, void *b) @@ -261,9 +275,10 @@ int color_from_mode(mode_t mode) static void listfiles(int dirfd, struct dirtree *indir) { + const unsigned LEN_MAX = 8; struct dirtree *dt, **sort = 0; unsigned long dtlen = 0, ul = 0; - unsigned width, flags = toys.optflags, totals[7], len[7], + unsigned width, flags = toys.optflags, totals[LEN_MAX], len[LEN_MAX], *colsizes = (unsigned *)(toybuf+260), columns = (sizeof(toybuf)-260)/4; memset(totals, 0, sizeof(totals)); @@ -309,6 +324,13 @@ static void listfiles(int dirfd, struct dirtree *indir) if (!(flags & FLAG_f)) qsort(sort, dtlen, sizeof(void *), (void *)compare); + for (ul = 0; ul totals[width]) totals[width] = len[width]; + } + // Find largest entry in each field for display alignment if (flags & (FLAG_C|FLAG_x)) { @@ -323,7 +345,7 @@ static void listfiles(int dirfd, struct dirtree *indir) memset(colsizes, 0, columns*sizeof(unsigned)); for (ul=0; ul colsizes[c]) { @@ -331,8 +353,6 @@ static void listfiles(int dirfd, struct dirtree *indir) colsizes[c] = *len; if (totlen > TT.screen_width) break; } - for (width=0; width<7; width++) - if (len[width] > totals[width]) totals[width] = len[width]; } // If it fit, stop here if (ul == dtlen) break; @@ -340,14 +360,6 @@ static void listfiles(int dirfd, struct dirtree *indir) } else if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) { unsigned long blocks = 0; - for (ul = 0; ul totals[width]) totals[width] = len[width]; - blocks += sort[ul]->st.st_blocks; - } - if (indir->parent) xprintf("total %lu\n", blocks); } @@ -366,7 +378,7 @@ static void listfiles(int dirfd, struct dirtree *indir) TT.nl_title=1; // Handle padding and wrapping for display purposes - entrylen(sort[next], len); + entrylen(sort[next], len, totals); if (ul) { if (flags & FLAG_m) xputc(','); if (flags & (FLAG_C|FLAG_x)) { -- 2.7.4