Home | History | Annotate | Line # | Download | only in du
du.c revision 1.17.2.1
      1 /*	$NetBSD: du.c,v 1.17.2.1 2003/06/19 00:21:50 grant 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.17.2.1 2003/06/19 00:21:50 grant 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, notused;
     78 	int Hflag, Lflag, Pflag, aflag, ch, cflag, kmflag, rval, sflag;
     79 	char *noargv[2];
     80 
     81 	Hflag = Lflag = Pflag = aflag = cflag = kmflag = sflag = 0;
     82 	totalblocks = 0;
     83 	ftsoptions = FTS_PHYSICAL;
     84 	while ((ch = getopt(argc, argv, "HLPackmrsx")) != -1)
     85 		switch (ch) {
     86 		case 'H':
     87 			Hflag = 1;
     88 			Lflag = Pflag = 0;
     89 			break;
     90 		case 'L':
     91 			Lflag = 1;
     92 			Hflag = Pflag = 0;
     93 			break;
     94 		case 'P':
     95 			Pflag = 1;
     96 			Hflag = Lflag = 0;
     97 			break;
     98 		case 'a':
     99 			aflag = 1;
    100 			break;
    101 		case 'c':
    102 			cflag = 1;
    103 			break;
    104 		case 'k':
    105 			blocksize = 1024;
    106 			kmflag = 1;
    107 			break;
    108 		case 'm':
    109 			blocksize = 1024 * 1024;
    110 			kmflag = 1;
    111 			break;
    112 		case 'r':
    113 			break;
    114 		case 's':
    115 			sflag = 1;
    116 			break;
    117 		case 'x':
    118 			ftsoptions |= FTS_XDEV;
    119 			break;
    120 		case '?':
    121 		default:
    122 			usage();
    123 		}
    124 	argc -= optind;
    125 	argv += optind;
    126 
    127 	/*
    128 	 * XXX
    129 	 * Because of the way that fts(3) works, logical walks will not count
    130 	 * the blocks actually used by symbolic links.  We rationalize this by
    131 	 * noting that users computing logical sizes are likely to do logical
    132 	 * copies, so not counting the links is correct.  The real reason is
    133 	 * that we'd have to re-implement the kernel's symbolic link traversing
    134 	 * algorithm to get this right.  If, for example, you have relative
    135 	 * symbolic links referencing other relative symbolic links, it gets
    136 	 * very nasty, very fast.  The bottom line is that it's documented in
    137 	 * the man page, so it's a feature.
    138 	 */
    139 	if (Hflag)
    140 		ftsoptions |= FTS_COMFOLLOW;
    141 	if (Lflag) {
    142 		ftsoptions &= ~FTS_PHYSICAL;
    143 		ftsoptions |= FTS_LOGICAL;
    144 	}
    145 
    146 	if (aflag) {
    147 		if (sflag)
    148 			usage();
    149 		listdirs = listfiles = 1;
    150 	} else if (sflag)
    151 		listdirs = listfiles = 0;
    152 	else {
    153 		listfiles = 0;
    154 		listdirs = 1;
    155 	}
    156 
    157 	if (!*argv) {
    158 		noargv[0] = ".";
    159 		noargv[1] = NULL;
    160 		argv = noargv;
    161 	}
    162 
    163 	if (!kmflag)
    164 		(void)getbsize(&notused, &blocksize);
    165 	blocksize /= 512;
    166 
    167 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
    168 		err(1, "fts_open `%s'", *argv);
    169 
    170 	for (rval = 0; (p = fts_read(fts)) != NULL;)
    171 		switch (p->fts_info) {
    172 		case FTS_D:			/* Ignore. */
    173 			break;
    174 		case FTS_DP:
    175 			p->fts_parent->fts_number +=
    176 			    p->fts_number += p->fts_statp->st_blocks;
    177 			if (cflag)
    178 				totalblocks += p->fts_statp->st_blocks;
    179 			/*
    180 			 * If listing each directory, or not listing files
    181 			 * or directories and this is post-order of the
    182 			 * root of a traversal, display the total.
    183 			 */
    184 			if (listdirs || (!listfiles && !p->fts_level))
    185 				(void)printf("%ld\t%s\n",
    186 				    howmany(p->fts_number, blocksize),
    187 				    p->fts_path);
    188 			break;
    189 		case FTS_DC:			/* Ignore. */
    190 			break;
    191 		case FTS_DNR:			/* Warn, continue. */
    192 		case FTS_ERR:
    193 		case FTS_NS:
    194 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
    195 			rval = 1;
    196 			break;
    197 		default:
    198 			if (p->fts_statp->st_nlink > 1 && linkchk(p))
    199 				break;
    200 			/*
    201 			 * If listing each file, or a non-directory file was
    202 			 * the root of a traversal, display the total.
    203 			 */
    204 			if (listfiles || !p->fts_level)
    205 				(void)printf("%lld\t%s\n", (long long)
    206 				    howmany(p->fts_statp->st_blocks, blocksize),
    207 				    p->fts_path);
    208 			p->fts_parent->fts_number += p->fts_statp->st_blocks;
    209 			if (cflag)
    210 				totalblocks += p->fts_statp->st_blocks;
    211 		}
    212 	if (errno)
    213 		err(1, "fts_read");
    214 	if (cflag)
    215 		(void)printf("%ld\ttotal\n",
    216 		    howmany(totalblocks, blocksize));
    217 	exit(0);
    218 }
    219 
    220 typedef struct _ID {
    221 	dev_t	dev;
    222 	ino_t	inode;
    223 } ID;
    224 
    225 int
    226 linkchk(p)
    227 	FTSENT *p;
    228 {
    229 	static ID *files;
    230 	static int maxfiles, nfiles;
    231 	ID *fp, *start;
    232 	ino_t ino;
    233 	dev_t dev;
    234 
    235 	ino = p->fts_statp->st_ino;
    236 	dev = p->fts_statp->st_dev;
    237 	if ((start = files) != NULL)
    238 		for (fp = start + nfiles - 1; fp >= start; --fp)
    239 			if (ino == fp->inode && dev == fp->dev)
    240 				return (1);
    241 
    242 	if (nfiles == maxfiles && (files = realloc((char *)files,
    243 	    (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
    244 		err(1, "realloc");
    245 	files[nfiles].inode = ino;
    246 	files[nfiles].dev = dev;
    247 	++nfiles;
    248 	return (0);
    249 }
    250 
    251 void
    252 usage()
    253 {
    254 
    255 	(void)fprintf(stderr,
    256 		"usage: du [-H | -L | -P] [-a | -s] [-ckmrx] [file ...]\n");
    257 	exit(1);
    258 }
    259