[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.29

1.29    ! schwarze    1: /*     $OpenBSD: du.c,v 1.28 2014/10/19 18:24:58 jmc 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: #include <sys/types.h>
                     37: #include <sys/stat.h>
                     38:
                     39: #include <dirent.h>
                     40: #include <err.h>
                     41: #include <errno.h>
                     42: #include <fts.h>
1.26      schwarze   43: #include <limits.h>
1.1       deraadt    44: #include <stdio.h>
                     45: #include <stdlib.h>
                     46: #include <string.h>
1.17      otto       47: #include <sys/tree.h>
1.1       deraadt    48: #include <unistd.h>
1.16      otto       49: #include <util.h>
1.1       deraadt    50:
1.14      deraadt    51:
1.11      millert    52: int     linkchk(FTSENT *);
                     53: void    prtout(quad_t, char *, int);
                     54: void    usage(void);
1.1       deraadt    55:
                     56: int
1.13      deraadt    57: main(int argc, char *argv[])
1.1       deraadt    58: {
                     59:        FTS *fts;
                     60:        FTSENT *p;
1.7       pjanzen    61:        long blocksize;
                     62:        quad_t totalblocks;
1.26      schwarze   63:        int ftsoptions, listfiles, maxdepth;
                     64:        int Hflag, Lflag, cflag, hflag, kflag;
1.7       pjanzen    65:        int ch, notused, rval;
1.1       deraadt    66:        char **save;
1.26      schwarze   67:        const char *errstr;
1.1       deraadt    68:
                     69:        save = argv;
1.26      schwarze   70:        Hflag = Lflag = cflag = hflag = kflag = listfiles = 0;
1.3       millert    71:        totalblocks = 0;
1.1       deraadt    72:        ftsoptions = FTS_PHYSICAL;
1.26      schwarze   73:        maxdepth = -1;
                     74:        while ((ch = getopt(argc, argv, "HLPacd:hkrsx")) != -1)
1.1       deraadt    75:                switch (ch) {
                     76:                case 'H':
                     77:                        Hflag = 1;
1.19      tedu       78:                        Lflag = 0;
1.1       deraadt    79:                        break;
                     80:                case 'L':
                     81:                        Lflag = 1;
1.19      tedu       82:                        Hflag = 0;
1.1       deraadt    83:                        break;
                     84:                case 'P':
                     85:                        Hflag = Lflag = 0;
                     86:                        break;
                     87:                case 'a':
1.26      schwarze   88:                        listfiles = 1;
1.1       deraadt    89:                        break;
1.3       millert    90:                case 'c':
                     91:                        cflag = 1;
                     92:                        break;
1.26      schwarze   93:                case 'd':
                     94:                        maxdepth = strtonum(optarg, 0, INT_MAX, &errstr);
                     95:                        if (errstr) {
                     96:                                warnx("max depth %s: %s", optarg, errstr);
                     97:                                usage();
                     98:                        }
                     99:                        break;
1.7       pjanzen   100:                case 'h':
                    101:                        hflag = 1;
1.20      millert   102:                        kflag = 0;
1.7       pjanzen   103:                        break;
1.1       deraadt   104:                case 'k':
                    105:                        kflag = 1;
1.20      millert   106:                        hflag = 0;
1.1       deraadt   107:                        break;
                    108:                case 's':
1.26      schwarze  109:                        maxdepth = 0;
1.1       deraadt   110:                        break;
1.5       deraadt   111:                case 'r':
                    112:                        break;
1.1       deraadt   113:                case 'x':
                    114:                        ftsoptions |= FTS_XDEV;
                    115:                        break;
                    116:                case '?':
                    117:                default:
                    118:                        usage();
                    119:                }
                    120:        argc -= optind;
                    121:        argv += optind;
                    122:
                    123:        /*
                    124:         * XXX
                    125:         * Because of the way that fts(3) works, logical walks will not count
                    126:         * the blocks actually used by symbolic links.  We rationalize this by
                    127:         * noting that users computing logical sizes are likely to do logical
                    128:         * copies, so not counting the links is correct.  The real reason is
                    129:         * that we'd have to re-implement the kernel's symbolic link traversing
                    130:         * algorithm to get this right.  If, for example, you have relative
                    131:         * symbolic links referencing other relative symbolic links, it gets
                    132:         * very nasty, very fast.  The bottom line is that it's documented in
                    133:         * the man page, so it's a feature.
                    134:         */
                    135:        if (Hflag)
                    136:                ftsoptions |= FTS_COMFOLLOW;
                    137:        if (Lflag) {
                    138:                ftsoptions &= ~FTS_PHYSICAL;
                    139:                ftsoptions |= FTS_LOGICAL;
                    140:        }
                    141:
1.26      schwarze  142:        if (maxdepth == -1)
                    143:                maxdepth = INT_MAX;
1.1       deraadt   144:
                    145:        if (!*argv) {
                    146:                argv = save;
                    147:                argv[0] = ".";
                    148:                argv[1] = NULL;
                    149:        }
                    150:
1.8       pjanzen   151:        if (hflag)
                    152:                blocksize = 512;
                    153:        else if (kflag)
                    154:                blocksize = 1024;
                    155:        else
1.1       deraadt   156:                (void)getbsize(&notused, &blocksize);
                    157:        blocksize /= 512;
                    158:
                    159:        if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
1.3       millert   160:                err(1, "fts_open");
1.1       deraadt   161:
                    162:        for (rval = 0; (p = fts_read(fts)) != NULL;)
                    163:                switch (p->fts_info) {
                    164:                case FTS_D:                     /* Ignore. */
                    165:                        break;
                    166:                case FTS_DP:
                    167:                        p->fts_parent->fts_number +=
                    168:                            p->fts_number += p->fts_statp->st_blocks;
1.3       millert   169:                        if (cflag)
                    170:                                totalblocks += p->fts_statp->st_blocks;
1.1       deraadt   171:                        /*
                    172:                         * If listing each directory, or not listing files
                    173:                         * or directories and this is post-order of the
                    174:                         * root of a traversal, display the total.
                    175:                         */
1.26      schwarze  176:                        if (p->fts_level <= maxdepth)
1.23      sobrado   177:                                prtout((quad_t)howmany(p->fts_number,
                    178:                                    (unsigned long)blocksize), p->fts_path,
                    179:                                    hflag);
1.1       deraadt   180:                        break;
                    181:                case FTS_DC:                    /* Ignore. */
                    182:                        break;
                    183:                case FTS_DNR:                   /* Warn, continue. */
                    184:                case FTS_ERR:
                    185:                case FTS_NS:
1.25      guenther  186:                        warnc(p->fts_errno, "%s", p->fts_path);
1.1       deraadt   187:                        rval = 1;
                    188:                        break;
                    189:                default:
                    190:                        if (p->fts_statp->st_nlink > 1 && linkchk(p))
                    191:                                break;
                    192:                        /*
                    193:                         * If listing each file, or a non-directory file was
                    194:                         * the root of a traversal, display the total.
                    195:                         */
1.29    ! schwarze  196:                        if ((listfiles && p->fts_level <= maxdepth) ||
        !           197:                            p->fts_level == FTS_ROOTLEVEL)
1.23      sobrado   198:                                prtout(howmany(p->fts_statp->st_blocks,
                    199:                                    blocksize), p->fts_path, hflag);
1.1       deraadt   200:                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
1.3       millert   201:                        if (cflag)
                    202:                                totalblocks += p->fts_statp->st_blocks;
1.1       deraadt   203:                }
                    204:        if (errno)
                    205:                err(1, "fts_read");
1.7       pjanzen   206:        if (cflag) {
                    207:                prtout((quad_t)howmany(totalblocks, blocksize), "total", hflag);
                    208:        }
1.6       ericj     209:        exit(rval);
1.1       deraadt   210: }
                    211:
1.17      otto      212:
                    213: struct links_entry {
                    214:        RB_ENTRY(links_entry) entry;
                    215:        struct links_entry *fnext;
                    216:        int      links;
                    217:        dev_t    dev;
                    218:        ino_t    ino;
                    219: };
                    220:
1.24      deraadt   221: static int
1.17      otto      222: links_cmp(struct links_entry *e1, struct links_entry *e2)
                    223: {
                    224:        if (e1->dev == e2->dev) {
                    225:                if (e1->ino == e2->ino)
                    226:                        return (0);
                    227:                else
                    228:                        return (e1->ino < e2->ino ? -1 : 1);
                    229:        }
                    230:        else
                    231:                return (e1->dev < e2->dev ? -1 : 1);
                    232: }
                    233:
                    234: RB_HEAD(ltree, links_entry) links = RB_INITIALIZER(&links);
                    235:
1.24      deraadt   236: RB_GENERATE_STATIC(ltree, links_entry, entry, links_cmp);
1.17      otto      237:
                    238:
1.1       deraadt   239: int
1.13      deraadt   240: linkchk(FTSENT *p)
1.1       deraadt   241: {
1.17      otto      242:        static struct links_entry *free_list = NULL;
                    243:        static int stop_allocating = 0;
                    244:        struct links_entry ltmp, *le;
1.16      otto      245:        struct stat *st;
                    246:
                    247:        st = p->fts_statp;
                    248:
1.17      otto      249:        ltmp.ino = st->st_ino;
                    250:        ltmp.dev = st->st_dev;
1.16      otto      251:
1.17      otto      252:        le = RB_FIND(ltree, &links, &ltmp);
                    253:        if (le != NULL) {
                    254:                /*
                    255:                 * Save memory by releasing an entry when we've seen
                    256:                 * all of it's links.
                    257:                 */
                    258:                if (--le->links <= 0) {
                    259:                        RB_REMOVE(ltree, &links, le);
                    260:                        /* Recycle this node through the free list */
                    261:                        if (stop_allocating) {
1.16      otto      262:                                free(le);
1.17      otto      263:                        } else {
                    264:                                le->fnext = free_list;
                    265:                                free_list = le;
1.16      otto      266:                        }
                    267:                }
1.17      otto      268:                return (1);
1.16      otto      269:        }
1.7       pjanzen   270:
1.16      otto      271:        if (stop_allocating)
                    272:                return (0);
1.7       pjanzen   273:
1.16      otto      274:        /* Add this entry to the links cache. */
                    275:        if (free_list != NULL) {
                    276:                /* Pull a node from the free list if we can. */
                    277:                le = free_list;
1.17      otto      278:                free_list = le->fnext;
1.16      otto      279:        } else
                    280:                /* Malloc one if we have to. */
                    281:                le = malloc(sizeof(struct links_entry));
1.17      otto      282:
1.16      otto      283:        if (le == NULL) {
                    284:                stop_allocating = 1;
                    285:                warnx("No more memory for tracking hard links");
                    286:                return (0);
1.7       pjanzen   287:        }
1.17      otto      288:
1.16      otto      289:        le->dev = st->st_dev;
                    290:        le->ino = st->st_ino;
                    291:        le->links = st->st_nlink - 1;
1.17      otto      292:        le->fnext = NULL;
                    293:
                    294:        RB_INSERT(ltree, &links, le);
                    295:
1.16      otto      296:        return (0);
1.7       pjanzen   297: }
                    298:
                    299: void
1.13      deraadt   300: prtout(quad_t size, char *path, int hflag)
1.7       pjanzen   301: {
                    302:        if (!hflag)
1.9       deraadt   303:                (void)printf("%lld\t%s\n", (long long)size, path);
1.7       pjanzen   304:        else {
1.16      otto      305:                char buf[FMT_SCALED_STRSIZE];
1.7       pjanzen   306:
1.16      otto      307:                if (fmt_scaled(size * 512, buf) == 0)
                    308:                        (void)printf("%s\t%s\n", buf, path);
1.7       pjanzen   309:                else
1.16      otto      310:                        (void)printf("%lld\t%s\n", (long long)size, path);
1.7       pjanzen   311:        }
                    312: }
                    313:
1.1       deraadt   314: void
1.13      deraadt   315: usage(void)
1.1       deraadt   316: {
                    317:
                    318:        (void)fprintf(stderr,
1.28      jmc       319:            "usage: du [-achkrsx] [-H | -L | -P] [-d depth] [file ...]\n");
1.1       deraadt   320:        exit(1);
                    321: }