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

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