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

1.1     ! deraadt     1: /*     $NetBSD: du.c,v 1.10 1995/09/28 06:19:56 perry Exp $    */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1989, 1993, 1994
        !             5:  *     The Regents of the University of California.  All rights reserved.
        !             6:  *
        !             7:  * This code is derived from software contributed to Berkeley by
        !             8:  * Chris Newcomb.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  * 3. All advertising materials mentioning features or use of this software
        !            19:  *    must display the following acknowledgement:
        !            20:  *     This product includes software developed by the University of
        !            21:  *     California, Berkeley and its contributors.
        !            22:  * 4. Neither the name of the University nor the names of its contributors
        !            23:  *    may be used to endorse or promote products derived from this software
        !            24:  *    without specific prior written permission.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            36:  * SUCH DAMAGE.
        !            37:  */
        !            38:
        !            39: #ifndef lint
        !            40: static char copyright[] =
        !            41: "@(#) Copyright (c) 1989, 1993, 1994\n\
        !            42:        The Regents of the University of California.  All rights reserved.\n";
        !            43: #endif /* not lint */
        !            44:
        !            45: #ifndef lint
        !            46: #if 0
        !            47: static char sccsid[] = "@(#)du.c       8.5 (Berkeley) 5/4/95";
        !            48: #else
        !            49: static char rcsid[] = "$NetBSD: du.c,v 1.10 1995/09/28 06:19:56 perry Exp $";
        !            50: #endif
        !            51: #endif /* not lint */
        !            52:
        !            53: #include <sys/types.h>
        !            54: #include <sys/stat.h>
        !            55:
        !            56: #include <dirent.h>
        !            57: #include <err.h>
        !            58: #include <errno.h>
        !            59: #include <fts.h>
        !            60: #include <stdio.h>
        !            61: #include <stdlib.h>
        !            62: #include <string.h>
        !            63: #include <unistd.h>
        !            64:
        !            65: int     linkchk __P((FTSENT *));
        !            66: void    usage __P((void));
        !            67:
        !            68: int
        !            69: main(argc, argv)
        !            70:        int argc;
        !            71:        char *argv[];
        !            72: {
        !            73:        FTS *fts;
        !            74:        FTSENT *p;
        !            75:        long blocksize;
        !            76:        int ftsoptions, listdirs, listfiles;
        !            77:        int Hflag, Lflag, Pflag, aflag, ch, kflag, notused, rval, sflag;
        !            78:        char **save;
        !            79:
        !            80:        save = argv;
        !            81:        Hflag = Lflag = Pflag = aflag = kflag = sflag = 0;
        !            82:        ftsoptions = FTS_PHYSICAL;
        !            83:        while ((ch = getopt(argc, argv, "HLPaksx")) != EOF)
        !            84:                switch (ch) {
        !            85:                case 'H':
        !            86:                        Hflag = 1;
        !            87:                        Lflag = Pflag = 0;
        !            88:                        break;
        !            89:                case 'L':
        !            90:                        Lflag = 1;
        !            91:                        Hflag = Pflag = 0;
        !            92:                        break;
        !            93:                case 'P':
        !            94:                        Pflag = 1;
        !            95:                        Hflag = Lflag = 0;
        !            96:                        break;
        !            97:                case 'a':
        !            98:                        aflag = 1;
        !            99:                        break;
        !           100:                case 'k':
        !           101:                        blocksize = 1024;
        !           102:                        kflag = 1;
        !           103:                        break;
        !           104:                case 's':
        !           105:                        sflag = 1;
        !           106:                        break;
        !           107:                case 'x':
        !           108:                        ftsoptions |= FTS_XDEV;
        !           109:                        break;
        !           110:                case '?':
        !           111:                default:
        !           112:                        usage();
        !           113:                }
        !           114:        argc -= optind;
        !           115:        argv += optind;
        !           116:
        !           117:        /*
        !           118:         * XXX
        !           119:         * Because of the way that fts(3) works, logical walks will not count
        !           120:         * the blocks actually used by symbolic links.  We rationalize this by
        !           121:         * noting that users computing logical sizes are likely to do logical
        !           122:         * copies, so not counting the links is correct.  The real reason is
        !           123:         * that we'd have to re-implement the kernel's symbolic link traversing
        !           124:         * algorithm to get this right.  If, for example, you have relative
        !           125:         * symbolic links referencing other relative symbolic links, it gets
        !           126:         * very nasty, very fast.  The bottom line is that it's documented in
        !           127:         * the man page, so it's a feature.
        !           128:         */
        !           129:        if (Hflag)
        !           130:                ftsoptions |= FTS_COMFOLLOW;
        !           131:        if (Lflag) {
        !           132:                ftsoptions &= ~FTS_PHYSICAL;
        !           133:                ftsoptions |= FTS_LOGICAL;
        !           134:        }
        !           135:
        !           136:        if (aflag) {
        !           137:                if (sflag)
        !           138:                        usage();
        !           139:                listdirs = listfiles = 1;
        !           140:        } else if (sflag)
        !           141:                listdirs = listfiles = 0;
        !           142:        else {
        !           143:                listfiles = 0;
        !           144:                listdirs = 1;
        !           145:        }
        !           146:
        !           147:        if (!*argv) {
        !           148:                argv = save;
        !           149:                argv[0] = ".";
        !           150:                argv[1] = NULL;
        !           151:        }
        !           152:
        !           153:        if (!kflag)
        !           154:                (void)getbsize(&notused, &blocksize);
        !           155:        blocksize /= 512;
        !           156:
        !           157:        if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
        !           158:                err(1, NULL);
        !           159:
        !           160:        for (rval = 0; (p = fts_read(fts)) != NULL;)
        !           161:                switch (p->fts_info) {
        !           162:                case FTS_D:                     /* Ignore. */
        !           163:                        break;
        !           164:                case FTS_DP:
        !           165:                        p->fts_parent->fts_number +=
        !           166:                            p->fts_number += p->fts_statp->st_blocks;
        !           167:                        /*
        !           168:                         * If listing each directory, or not listing files
        !           169:                         * or directories and this is post-order of the
        !           170:                         * root of a traversal, display the total.
        !           171:                         */
        !           172:                        if (listdirs || !listfiles && !p->fts_level)
        !           173:                                (void)printf("%ld\t%s\n",
        !           174:                                    howmany(p->fts_number, blocksize),
        !           175:                                    p->fts_path);
        !           176:                        break;
        !           177:                case FTS_DC:                    /* Ignore. */
        !           178:                        break;
        !           179:                case FTS_DNR:                   /* Warn, continue. */
        !           180:                case FTS_ERR:
        !           181:                case FTS_NS:
        !           182:                        warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
        !           183:                        rval = 1;
        !           184:                        break;
        !           185:                default:
        !           186:                        if (p->fts_statp->st_nlink > 1 && linkchk(p))
        !           187:                                break;
        !           188:                        /*
        !           189:                         * If listing each file, or a non-directory file was
        !           190:                         * the root of a traversal, display the total.
        !           191:                         */
        !           192:                        if (listfiles || !p->fts_level)
        !           193:                                (void)printf("%qd\t%s\n",
        !           194:                                    howmany(p->fts_statp->st_blocks, blocksize),
        !           195:                                    p->fts_path);
        !           196:                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
        !           197:                }
        !           198:        if (errno)
        !           199:                err(1, "fts_read");
        !           200:        exit(0);
        !           201: }
        !           202:
        !           203: typedef struct _ID {
        !           204:        dev_t   dev;
        !           205:        ino_t   inode;
        !           206: } ID;
        !           207:
        !           208: int
        !           209: linkchk(p)
        !           210:        FTSENT *p;
        !           211: {
        !           212:        static ID *files;
        !           213:        static int maxfiles, nfiles;
        !           214:        ID *fp, *start;
        !           215:        ino_t ino;
        !           216:        dev_t dev;
        !           217:
        !           218:        ino = p->fts_statp->st_ino;
        !           219:        dev = p->fts_statp->st_dev;
        !           220:        if ((start = files) != NULL)
        !           221:                for (fp = start + nfiles - 1; fp >= start; --fp)
        !           222:                        if (ino == fp->inode && dev == fp->dev)
        !           223:                                return (1);
        !           224:
        !           225:        if (nfiles == maxfiles && (files = realloc((char *)files,
        !           226:            (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
        !           227:                err(1, "");
        !           228:        files[nfiles].inode = ino;
        !           229:        files[nfiles].dev = dev;
        !           230:        ++nfiles;
        !           231:        return (0);
        !           232: }
        !           233:
        !           234: void
        !           235: usage()
        !           236: {
        !           237:
        !           238:        (void)fprintf(stderr,
        !           239:                "usage: du [-H | -L | -P] [-a | -s] [-kx] [file ...]\n");
        !           240:        exit(1);
        !           241: }