Print this page
7967 Want apparent size option for du(1)
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Peter Tribble <peter.tribble@gmail.com>
Reviewed by: Dan McDonald <danmcd@omniti.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/du/du.c
          +++ new/usr/src/cmd/du/du.c
↓ open down ↓ 11 lines elided ↑ open up ↑
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
       22 + * Copyright 2017 OmniTI Computer Consulting, Inc.  All rights reserved.
  22   23   * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  23   24   * Use is subject to license terms.
  24   25   */
  25   26  
  26   27  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  27   28  /*        All Rights Reserved   */
  28   29  
  29      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  30      -
  31   30  /*
  32   31   * du -- summarize disk usage
  33      - *      du [-dorx] [-a|-s] [-h|-k|-m] [-H|-L] [file...]
       32 + *      du [-Adorx] [-a|-s] [-h|-k|-m] [-H|-L] [file...]
  34   33   */
  35   34  
  36   35  #include <sys/types.h>
  37   36  #include <sys/stat.h>
  38   37  #include <sys/avl.h>
  39   38  #include <fcntl.h>
  40   39  #include <dirent.h>
  41   40  #include <limits.h>
  42   41  #include <stdio.h>
  43   42  #include <stdlib.h>
↓ open down ↓ 4 lines elided ↑ open up ↑
  48   47  
  49   48  
  50   49  static int              aflg = 0;
  51   50  static int              rflg = 0;
  52   51  static int              sflg = 0;
  53   52  static int              kflg = 0;
  54   53  static int              mflg = 0;
  55   54  static int              oflg = 0;
  56   55  static int              dflg = 0;
  57   56  static int              hflg = 0;
       57 +static int              Aflg = 0;
  58   58  static int              Hflg = 0;
  59   59  static int              Lflg = 0;
  60   60  static int              cmdarg = 0;     /* Command line argument */
  61   61  static char             *dot = ".";
  62   62  static int              level = 0;      /* Level of recursion */
  63   63  
  64   64  static char             *base;
  65   65  static char             *name;
  66   66  static size_t           base_len = PATH_MAX + 1;    /* # of chars for base */
  67   67  static size_t           name_len = PATH_MAX + 1;    /* # of chars for name */
  68   68  
  69   69  #define NUMBER_WIDTH    64
  70   70  typedef char            numbuf_t[NUMBER_WIDTH];
  71   71  
  72   72  /*
  73      - * Output formats.  Solaris uses a tab as separator, XPG4 a space.
       73 + * Output formats. illumos uses a tab as separator, XPG4 a space.
  74   74   */
  75   75  #ifdef XPG4
  76   76  #define FORMAT1 "%s %s\n"
  77   77  #define FORMAT2 "%lld %s\n"
  78   78  #else
  79   79  #define FORMAT1 "%s\t%s\n"
  80   80  #define FORMAT2 "%lld\t%s\n"
  81   81  #endif
  82   82  
  83   83  /*
↓ open down ↓ 26 lines elided ↑ open up ↑
 110  110          (void) setlocale(LC_ALL, "");
 111  111  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 112  112  #define TEXT_DOMAIN     "SYS_TEST"      /* Use this only if it weren't */
 113  113  #endif
 114  114          (void) textdomain(TEXT_DOMAIN);
 115  115  
 116  116  #ifdef XPG4
 117  117          rflg++;         /* "-r" is not an option but ON always */
 118  118  #endif
 119  119  
 120      -        while ((c = getopt(argc, argv, "adhHkLmorsx")) != EOF)
      120 +        while ((c = getopt(argc, argv, "aAdhHkLmorsx")) != EOF)
 121  121                  switch (c) {
 122  122  
 123  123                  case 'a':
 124  124                          aflg++;
 125  125                          continue;
 126  126  
 127  127                  case 'h':
 128  128                          hflg++;
 129  129                          kflg = 0;
 130  130                          mflg = 0;
↓ open down ↓ 24 lines elided ↑ open up ↑
 155  155                          continue;
 156  156  
 157  157                  case 'd':
 158  158                          dflg++;
 159  159                          continue;
 160  160  
 161  161                  case 'x':
 162  162                          dflg++;
 163  163                          continue;
 164  164  
      165 +                case 'A':
      166 +                        Aflg++;
      167 +                        continue;
      168 +
 165  169                  case 'H':
 166  170                          Hflg++;
 167  171                          /* -H and -L are mutually exclusive */
 168  172                          Lflg = 0;
 169  173                          cmdarg++;
 170  174                          continue;
 171  175  
 172  176                  case 'L':
 173  177                          Lflg++;
 174  178                          /* -H and -L are mutually exclusive */
 175  179                          Hflg = 0;
 176  180                          cmdarg = 0;
 177  181                          continue;
 178  182                  case '?':
 179  183                          (void) fprintf(stderr, gettext(
 180      -                            "usage: du [-dorx] [-a|-s] [-h|-k|-m] [-H|-L] "
      184 +                            "usage: du [-Adorx] [-a|-s] [-h|-k|-m] [-H|-L] "
 181  185                              "[file...]\n"));
 182  186                          exit(2);
 183  187                  }
 184  188          if (optind == argc) {
 185  189                  argv = &dot;
 186  190                  argc = 1;
 187  191                  optind = 0;
 188  192          }
 189  193  
 190  194          /* "-o" and "-s" don't make any sense together. */
↓ open down ↓ 181 lines elided ↑ open up ↑
 372  376                                   * An error occurred while trying to add the
 373  377                                   * node to the tree.
 374  378                                   */
 375  379                                  if (rflg) {
 376  380                                          perror("du");
 377  381                                  }
 378  382                                  exitdu(1);
 379  383                          }
 380  384                  }
 381  385          }
 382      -        blocks = stb.st_blocks;
      386 +        blocks = Aflg ? stb.st_size : stb.st_blocks;
      387 +
 383  388          /*
 384  389           * If there are extended attributes on the current file, add their
 385  390           * block usage onto the block count.  Note: Since pathconf() always
 386  391           * follows symlinks, only test for extended attributes using pathconf()
 387  392           * if we are following symlinks or the current file is not a symlink.
 388  393           */
 389  394          if (curname && (follow_symlinks ||
 390  395              ((stb.st_mode & S_IFMT) != S_IFLNK)) &&
 391  396              pathconf(curname, _PC_XATTR_EXISTS) == 1) {
 392  397                  if ((fd = attropen(curname, ".", O_RDONLY)) < 0) {
↓ open down ↓ 195 lines elided ↑ open up ↑
 588  593                  (void) sprintf(buf, "%4.1f%c", fnum, *uom);
 589  594          } else {
 590  595                  (void) sprintf(buf, "%4llu%c", number, *uom);
 591  596          }
 592  597          return (buf);
 593  598  }
 594  599  
 595  600  static void
 596  601  printsize(blkcnt_t blocks, char *path)
 597  602  {
      603 +        u_longlong_t bsize;
      604 +
      605 +        bsize = Aflg ? 1 : DEV_BSIZE;
      606 +
 598  607          if (hflg) {
 599  608                  numbuf_t numbuf;
 600  609                  unsigned long long scale = 1024L;
 601  610                  (void) printf(FORMAT1,
 602      -                    number_to_scaled_string(numbuf, blocks, DEV_BSIZE, scale),
      611 +                    number_to_scaled_string(numbuf, blocks, bsize, scale),
 603  612                      path);
 604  613          } else if (kflg) {
 605  614                  (void) printf(FORMAT2, (long long)kb(blocks), path);
 606  615          } else if (mflg) {
 607  616                  (void) printf(FORMAT2, (long long)mb(blocks), path);
 608  617          } else {
 609  618                  (void) printf(FORMAT2, (long long)blocks, path);
 610  619          }
 611  620  }
 612  621  
 613  622  static void
 614  623  exitdu(int exitcode)
 615  624  {
 616  625          free(base);
 617  626          free(name);
 618  627          exit(exitcode);
 619  628  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX