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