Home | History | Annotate | Line # | Download | only in du
du.c revision 1.16
      1 /*	$NetBSD: du.c,v 1.16 2000/12/30 16:00:33 hubertf 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 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: du.c,v 1.16 2000/12/30 16:00:33 hubertf 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 int	main __P((int, char **));
     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;
     76 	long blocksize, totalblocks;
     77 	int ftsoptions, listdirs, listfiles;
     78 	int Hflag, Lflag, Pflag, aflag, ch, cflag, kmflag, notused, rval, sflag;
     79 	char **save;
     80 
     81 	save = argv;
     82 	Hflag = Lflag = Pflag = aflag = cflag = kmflag = sflag = 0;
     83 	totalblocks = 0;
     84 	ftsoptions = FTS_PHYSICAL;
     85 	while ((ch = getopt(argc, argv, "HLPackmrsx")) != -1)
     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;
    102 		case 'c':
    103 			cflag = 1;
    104 			break;
    105 		case 'k':
    106 			blocksize = 1024;
    107 			kmflag = 1;
    108 			break;
    109 		case 'm':
    110 			blocksize = 1024 * 1024;
    111 			kmflag = 1;
    112 			break;
    113 		case 'r':
    114 			break;
    115 		case 's':
    116 			sflag = 1;
    117 			break;
    118 		case 'x':
    119 			ftsoptions |= FTS_XDEV;
    120 			break;
    121 		case '?':
    122 		default:
    123 			usage();
    124 		}
    125 	argc -= optind;
    126 	argv += optind;
    127 
    128 	/*
    129 	 * XXX
    130 	 * Because of the way that fts(3) works, logical walks will not count
    131 	 * the blocks actually used by symbolic links.  We rationalize this by
    132 	 * noting that users computing logical sizes are likely to do logical
    133 	 * copies, so not counting the links is correct.  The real reason is
    134 	 * that we'd have to re-implement the kernel's symbolic link traversing
    135 	 * algorithm to get this right.  If, for example, you have relative
    136 	 * symbolic links referencing other relative symbolic links, it gets
    137 	 * very nasty, very fast.  The bottom line is that it's documented in
    138 	 * the man page, so it's a feature.
    139 	 */
    140 	if (Hflag)
    141 		ftsoptions |= FTS_COMFOLLOW;
    142 	if (Lflag) {
    143 		ftsoptions &= ~FTS_PHYSICAL;
    144 		ftsoptions |= FTS_LOGICAL;
    145 	}
    146 
    147 	if (aflag) {
    148 		if (sflag)
    149 			usage();
    150 		listdirs = listfiles = 1;
    151 	} else if (sflag)
    152 		listdirs = listfiles = 0;
    153 	else {
    154 		listfiles = 0;
    155 		listdirs = 1;
    156 	}
    157 
    158 	if (!*argv) {
    159 		argv = save;
    160 		argv[0] = ".";
    161 		argv[1] = NULL;
    162 	}
    163 
    164 	if (!kmflag)
    165 		(void)getbsize(&notused, &blocksize);
    166 	blocksize /= 512;
    167 
    168 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
    169 		err(1, "fts_open `%s'", *argv);
    170 
    171 	for (rval = 0; (p = fts_read(fts)) != NULL;)
    172 		switch (p->fts_info) {
    173 		case FTS_D:			/* Ignore. */
    174 			break;
    175 		case FTS_DP:
    176 			p->fts_parent->fts_number +=
    177 			    p->fts_number += p->fts_statp->st_blocks;
    178 			if (cflag)
    179 				totalblocks += p->fts_statp->st_blocks;
    180 			/*
    181 			 * If listing each directory, or not listing files
    182 			 * or directories and this is post-order of the
    183 			 * root of a traversal, display the total.
    184 			 */
    185 			if (listdirs || (!listfiles && !p->fts_level))
    186 				(void)printf("%ld\t%s\n",
    187 				    howmany(p->fts_number, blocksize),
    188 				    p->fts_path);
    189 			break;
    190 		case FTS_DC:			/* Ignore. */
    191 			break;
    192 		case FTS_DNR:			/* Warn, continue. */
    193 		case FTS_ERR:
    194 		case FTS_NS:
    195 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
    196 			rval = 1;
    197 			break;
    198 		default:
    199 			if (p->fts_statp->st_nlink > 1 && linkchk(p))
    200 				break;
    201 			/*
    202 			 * If listing each file, or a non-directory file was
    203 			 * the root of a traversal, display the total.
    204 			 */
    205 			if (listfiles || !p->fts_level)
    206 				(void)printf("%qd\t%s\n", (long long)
    207 				    howmany(p->fts_statp->st_blocks, blocksize),
    208 				    p->fts_path);
    209 			p->fts_parent->fts_number += p->fts_statp->st_blocks;
    210 			if (cflag)
    211 				totalblocks += p->fts_statp->st_blocks;
    212 		}
    213 	if (errno)
    214 		err(1, "fts_read");
    215 	if (cflag)
    216 		(void)printf("%ld\ttotal\n",
    217 		    howmany(totalblocks, blocksize));
    218 	exit(0);
    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)
    245 		err(1, "realloc");
    246 	files[nfiles].inode = ino;
    247 	files[nfiles].dev = dev;
    248 	++nfiles;
    249 	return (0);
    250 }
    251 
    252 void
    253 usage()
    254 {
    255 
    256 	(void)fprintf(stderr,
    257 		"usage: du [-H | -L | -P] [-a | -s] [-ckmrx] [file ...]\n");
    258 	exit(1);
    259 }
    260