[BACK]Return to du.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / du

Annotation of src/usr.bin/du/du.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: du.c,v 1.11 2002/02/16 21:27:45 millert Exp $ */
1.3       millert     2: /*     $NetBSD: du.c,v 1.11 1996/10/18 07:20:35 thorpej Exp $  */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Chris Newcomb.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.12    ! millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1989, 1993, 1994\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: #if 0
                     44: static char sccsid[] = "@(#)du.c       8.5 (Berkeley) 5/4/95";
                     45: #else
1.12    ! millert    46: static char rcsid[] = "$OpenBSD: du.c,v 1.11 2002/02/16 21:27:45 millert Exp $";
1.1       deraadt    47: #endif
                     48: #endif /* not lint */
                     49:
                     50: #include <sys/types.h>
                     51: #include <sys/stat.h>
                     52:
                     53: #include <dirent.h>
                     54: #include <err.h>
                     55: #include <errno.h>
                     56: #include <fts.h>
1.7       pjanzen    57: #include <math.h>
1.1       deraadt    58: #include <stdio.h>
                     59: #include <stdlib.h>
                     60: #include <string.h>
                     61: #include <unistd.h>
                     62:
1.11      millert    63: int     linkchk(FTSENT *);
                     64: void    prtout(quad_t, char *, int);
                     65: void    usage(void);
1.1       deraadt    66:
                     67: int
                     68: main(argc, argv)
                     69:        int argc;
                     70:        char *argv[];
                     71: {
                     72:        FTS *fts;
                     73:        FTSENT *p;
1.7       pjanzen    74:        long blocksize;
                     75:        quad_t totalblocks;
1.1       deraadt    76:        int ftsoptions, listdirs, listfiles;
1.7       pjanzen    77:        int Hflag, Lflag, Pflag, aflag, cflag, hflag, kflag, sflag;
                     78:        int ch, notused, rval;
1.1       deraadt    79:        char **save;
                     80:
                     81:        save = argv;
1.7       pjanzen    82:        Hflag = Lflag = Pflag = aflag = cflag = hflag = kflag = sflag = 0;
1.3       millert    83:        totalblocks = 0;
1.1       deraadt    84:        ftsoptions = FTS_PHYSICAL;
1.7       pjanzen    85:        while ((ch = getopt(argc, argv, "HLPachksxr")) != -1)
1.1       deraadt    86:                switch (ch) {
                     87:                case 'H':
                     88:                        Hflag = 1;
                     89:                        Lflag = Pflag = 0;
                     90:                        break;
                     91:                case 'L':
                     92:                        Lflag = 1;
                     93:                        Hflag = Pflag = 0;
                     94:                        break;
                     95:                case 'P':
                     96:                        Pflag = 1;
                     97:                        Hflag = Lflag = 0;
                     98:                        break;
                     99:                case 'a':
                    100:                        aflag = 1;
                    101:                        break;
1.3       millert   102:                case 'c':
                    103:                        cflag = 1;
                    104:                        break;
1.7       pjanzen   105:                case 'h':
                    106:                        hflag = 1;
                    107:                        break;
1.1       deraadt   108:                case 'k':
                    109:                        kflag = 1;
                    110:                        break;
                    111:                case 's':
                    112:                        sflag = 1;
                    113:                        break;
1.5       deraadt   114:                case 'r':
                    115:                        break;
1.1       deraadt   116:                case 'x':
                    117:                        ftsoptions |= FTS_XDEV;
                    118:                        break;
                    119:                case '?':
                    120:                default:
                    121:                        usage();
                    122:                }
                    123:        argc -= optind;
                    124:        argv += optind;
                    125:
                    126:        /*
                    127:         * XXX
                    128:         * Because of the way that fts(3) works, logical walks will not count
                    129:         * the blocks actually used by symbolic links.  We rationalize this by
                    130:         * noting that users computing logical sizes are likely to do logical
                    131:         * copies, so not counting the links is correct.  The real reason is
                    132:         * that we'd have to re-implement the kernel's symbolic link traversing
                    133:         * algorithm to get this right.  If, for example, you have relative
                    134:         * symbolic links referencing other relative symbolic links, it gets
                    135:         * very nasty, very fast.  The bottom line is that it's documented in
                    136:         * the man page, so it's a feature.
                    137:         */
                    138:        if (Hflag)
                    139:                ftsoptions |= FTS_COMFOLLOW;
                    140:        if (Lflag) {
                    141:                ftsoptions &= ~FTS_PHYSICAL;
                    142:                ftsoptions |= FTS_LOGICAL;
                    143:        }
                    144:
                    145:        if (aflag) {
                    146:                if (sflag)
                    147:                        usage();
                    148:                listdirs = listfiles = 1;
                    149:        } else if (sflag)
                    150:                listdirs = listfiles = 0;
                    151:        else {
                    152:                listfiles = 0;
                    153:                listdirs = 1;
                    154:        }
                    155:
                    156:        if (!*argv) {
                    157:                argv = save;
                    158:                argv[0] = ".";
                    159:                argv[1] = NULL;
                    160:        }
                    161:
1.8       pjanzen   162:        if (hflag)
                    163:                blocksize = 512;
                    164:        else if (kflag)
                    165:                blocksize = 1024;
                    166:        else
1.1       deraadt   167:                (void)getbsize(&notused, &blocksize);
                    168:        blocksize /= 512;
                    169:
                    170:        if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
1.3       millert   171:                err(1, "fts_open");
1.1       deraadt   172:
                    173:        for (rval = 0; (p = fts_read(fts)) != NULL;)
                    174:                switch (p->fts_info) {
                    175:                case FTS_D:                     /* Ignore. */
                    176:                        break;
                    177:                case FTS_DP:
                    178:                        p->fts_parent->fts_number +=
                    179:                            p->fts_number += p->fts_statp->st_blocks;
1.3       millert   180:                        if (cflag)
                    181:                                totalblocks += p->fts_statp->st_blocks;
1.1       deraadt   182:                        /*
                    183:                         * If listing each directory, or not listing files
                    184:                         * or directories and this is post-order of the
                    185:                         * root of a traversal, display the total.
                    186:                         */
1.3       millert   187:                        if (listdirs || (!listfiles && !p->fts_level))
1.7       pjanzen   188:                                prtout((quad_t)howmany(p->fts_number, blocksize),
                    189:                                    p->fts_path, hflag);
1.1       deraadt   190:                        break;
                    191:                case FTS_DC:                    /* Ignore. */
                    192:                        break;
                    193:                case FTS_DNR:                   /* Warn, continue. */
                    194:                case FTS_ERR:
                    195:                case FTS_NS:
                    196:                        warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
                    197:                        rval = 1;
                    198:                        break;
                    199:                default:
                    200:                        if (p->fts_statp->st_nlink > 1 && linkchk(p))
                    201:                                break;
                    202:                        /*
                    203:                         * If listing each file, or a non-directory file was
                    204:                         * the root of a traversal, display the total.
                    205:                         */
                    206:                        if (listfiles || !p->fts_level)
1.7       pjanzen   207:                                prtout(howmany(p->fts_statp->st_blocks, blocksize),
                    208:                                    p->fts_path, hflag);
1.1       deraadt   209:                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
1.3       millert   210:                        if (cflag)
                    211:                                totalblocks += p->fts_statp->st_blocks;
1.1       deraadt   212:                }
                    213:        if (errno)
                    214:                err(1, "fts_read");
1.7       pjanzen   215:        if (cflag) {
                    216:                prtout((quad_t)howmany(totalblocks, blocksize), "total", hflag);
                    217:        }
1.6       ericj     218:        exit(rval);
1.1       deraadt   219: }
                    220:
                    221: typedef struct _ID {
                    222:        dev_t   dev;
                    223:        ino_t   inode;
                    224: } ID;
                    225:
                    226: int
                    227: linkchk(p)
                    228:        FTSENT *p;
                    229: {
                    230:        static ID *files;
                    231:        static int maxfiles, nfiles;
                    232:        ID *fp, *start;
                    233:        ino_t ino;
                    234:        dev_t dev;
                    235:
                    236:        ino = p->fts_statp->st_ino;
                    237:        dev = p->fts_statp->st_dev;
                    238:        if ((start = files) != NULL)
                    239:                for (fp = start + nfiles - 1; fp >= start; --fp)
                    240:                        if (ino == fp->inode && dev == fp->dev)
                    241:                                return (1);
                    242:
                    243:        if (nfiles == maxfiles && (files = realloc((char *)files,
                    244:            (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
1.3       millert   245:                err(1, "can't allocate memory");
1.1       deraadt   246:        files[nfiles].inode = ino;
                    247:        files[nfiles].dev = dev;
                    248:        ++nfiles;
                    249:        return (0);
                    250: }
                    251:
1.7       pjanzen   252: /*
                    253:  * "human-readable" output: use 3 digits max.--put unit suffixes at
                    254:  * the end.  Makes output compact and easy-to-read.
                    255:  */
                    256:
                    257: typedef enum { NONE = 0, KILO, MEGA, GIGA, TERA, PETA /* , EXA */ } unit_t;
                    258:
                    259: unit_t
                    260: unit_adjust(val)
                    261:        double *val;
                    262: {
                    263:        double abval;
                    264:        unit_t unit;
                    265:
                    266:        abval = fabs(*val);
                    267:        if (abval < 1024)
                    268:                unit = NONE;
                    269:        else if (abval < 1048576ULL) {
                    270:                unit = KILO;
                    271:                *val /= 1024;
                    272:        } else if (abval < 1073741824ULL) {
                    273:                unit = MEGA;
                    274:                *val /= 1048576;
                    275:        } else if (abval < 1099511627776ULL) {
                    276:                unit = GIGA;
                    277:                *val /= 1073741824ULL;
                    278:        } else if (abval < 1125899906842624ULL) {
                    279:                unit = TERA;
                    280:                *val /= 1099511627776ULL;
                    281:        } else /* if (abval < 1152921504606846976ULL) */ {
                    282:                unit = PETA;
                    283:                *val /= 1125899906842624ULL;
                    284:        }
                    285:        return (unit);
                    286: }
                    287:
                    288: void
                    289: prtout(size, path, hflag)
                    290:        quad_t size;
                    291:        char *path;
                    292:        int hflag;
                    293: {
                    294:        unit_t unit;
                    295:        double bytes;
                    296:
                    297:        if (!hflag)
1.9       deraadt   298:                (void)printf("%lld\t%s\n", (long long)size, path);
1.7       pjanzen   299:        else {
                    300:                bytes = (double)size * 512.0;
                    301:                unit = unit_adjust(&bytes);
                    302:
                    303:                if (bytes == 0)
1.10      deraadt   304:                        (void)printf("0B\t%s\n", path);
1.7       pjanzen   305:                else if (bytes > 10)
                    306:                        (void)printf("%.0f%c\t%s\n", bytes, "BKMGTPE"[unit], path);
                    307:                else
                    308:                        (void)printf("%.1f%c\t%s\n", bytes, "BKMGTPE"[unit], path);
                    309:        }
                    310: }
                    311:
1.1       deraadt   312: void
                    313: usage()
                    314: {
                    315:
                    316:        (void)fprintf(stderr,
1.7       pjanzen   317:                "usage: du [-H | -L | -P] [-a | -s] [-chkrx] [file ...]\n");
1.1       deraadt   318:        exit(1);
                    319: }