print totals for -s flag, as well as -l
[unix-history] / usr / src / bin / ls / print.c
index 164bb41..3069701 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)print.c    5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)print.c    5.11 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -45,12 +45,12 @@ printlong(stats, num)
        LS *stats;
        register int num;
 {
        LS *stats;
        register int num;
 {
-       (void)printf("total %lu\n", stats[0].lstat.st_flags);
+       (void)printf("total %lu\n", stats[0].lstat.st_btotal);
        for (; num--; ++stats) {
                if (f_inode)
                        (void)printf("%6lu ", stats->lstat.st_ino);
                if (f_size)
        for (; num--; ++stats) {
                if (f_inode)
                        (void)printf("%6lu ", stats->lstat.st_ino);
                if (f_size)
-                       (void)printf("%4ld ", stats->lstat.st_blocks / 2);
+                       (void)printf("%4ld ", stats->lstat.st_blocks);
                printperms(stats->lstat.st_mode);
                (void)printf("%3d ", stats->lstat.st_nlink);
                printowner(stats->lstat.st_uid);
                printperms(stats->lstat.st_mode);
                (void)printf("%3d ", stats->lstat.st_nlink);
                printowner(stats->lstat.st_uid);
@@ -77,50 +77,45 @@ printlong(stats, num)
        }
 }
 
        }
 }
 
+#define        TAB     8
+
 printcol(stats, num)
        LS *stats;
 printcol(stats, num)
        LS *stats;
-       register int num;
+       int num;
 {
        extern int termwidth;
 {
        extern int termwidth;
-       int i;                  /* subscript to stats */
-       int maxlen;             /* length of longest name string */
-       int colwidth;           /* width of a printing column */
-       int numcols;            /* number of columns */
-       int collength;          /* lines in longest column */
-       int base;               /* subscript for leftmost column */
-       int offset;             /* delta from base to next column */
-       int chcnt;              /* character count printed */
+       register int base, chcnt, cnt, col, colwidth;
+       int endcol, numcols, numrows, row;
 
 
-       maxlen = stats[0].lstat.st_flags;
+       colwidth = stats[0].lstat.st_maxlen;
        if (f_inode)
        if (f_inode)
-               maxlen += 6;
+               colwidth += 6;
        if (f_size)
        if (f_size)
-               maxlen += 5;
+               colwidth += 5;
        if (f_type)
        if (f_type)
-               maxlen += 1;
-#define        TAB     8
-       colwidth = maxlen + TAB + 1 & ~(TAB - 1);
-       numcols = (termwidth + colwidth - maxlen) / colwidth;
-       collength = (int)((float)num / (float)numcols + 0.999);
+               colwidth += 1;
+       colwidth = (colwidth + TAB) & ~(TAB - 1);
+
+       numcols = termwidth / colwidth;
+       numrows = num / numcols;
+       if (num % numcols)
+               ++numrows;
 
 
-       for (base = 0; base < collength; base++) {
-               for (offset = 0, i = 0; i < numcols; ++i, offset += collength) {
-                       if (base + offset >= num)
+       if (f_size)
+               (void)printf("total %lu\n", stats[0].lstat.st_btotal);
+       for (row = 0; row < numrows; ++row) {
+               endcol = colwidth;
+               for (base = row, chcnt = col = 0; col < numcols; ++col) {
+                       chcnt += printaname(stats + base);
+                       if ((base += numrows) >= num)
                                break;
                                break;
-                       chcnt = printaname(&stats[base + offset]);
-                       if (base + offset + collength < num) {
-                               while (chcnt + 8 < colwidth) {
-                                       (void)putchar('\t');
-                                       chcnt += 8;
-                               }
-                               if (chcnt < colwidth)
-                                       (void)putchar('\t');
-                               chcnt = (chcnt + 8) & ~0x7;
+                       while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
+                               (void)putchar('\t');
+                               chcnt = cnt;
                        }
                        }
+                       endcol += colwidth;
                }
                }
-               if (base + offset < num)
-                       (void)printaname(&stats[base + offset]);
-               (void)printf("\n");
+               putchar('\n');
        }
 }
 
        }
 }
 
@@ -137,7 +132,7 @@ printaname(lp)
        if (f_inode)
                chcnt += printf("%5lu ", lp->lstat.st_ino);
        if (f_size)
        if (f_inode)
                chcnt += printf("%5lu ", lp->lstat.st_ino);
        if (f_size)
-               chcnt += printf("%4ld ", lp->lstat.st_blocks / 2);
+               chcnt += printf("%4ld ", lp->lstat.st_blocks);
        chcnt += printf("%s", lp->name);
        if (f_type)
                chcnt += printtype(lp->lstat.st_mode);
        chcnt += printf("%s", lp->name);
        if (f_type)
                chcnt += printtype(lp->lstat.st_mode);