Home | History | Annotate | Line # | Download | only in du
du.c revision 1.7
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Chris Newcomb.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 char copyright[] =
     39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     40  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /*static char sccsid[] = "from: @(#)du.c	5.17 (Berkeley) 5/20/92";*/
     45 static char rcsid[] = "$Id: du.c,v 1.7 1994/01/27 20:32:37 cgd Exp $";
     46 #endif /* not lint */
     47 
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <errno.h>
     52 #include <sys/types.h>
     53 #include <sys/stat.h>
     54 #include <dirent.h>
     55 #include <unistd.h>
     56 #include <fts.h>
     57 #include <err.h>
     58 
     59 int	 linkchk __P((FTSENT *));
     60 void	 usage __P((void));
     61 
     62 main(argc, argv)
     63 	int argc;
     64 	char *argv[];
     65 {
     66 	register FTS *fts;
     67 	register FTSENT *p;
     68 	register int listdirs, listfiles;
     69 	long blocksize;
     70 	int aflag, ch, ftsoptions, notused, sflag, kflag;
     71 	char **save;
     72 
     73 	ftsoptions = FTS_PHYSICAL;
     74 	save = argv;
     75 	aflag = kflag = sflag = 0;
     76 	while ((ch = getopt(argc, argv, "aksx")) != EOF)
     77 		switch(ch) {
     78 		case 'a':
     79 			aflag = 1;
     80 			break;
     81 		case 'k':
     82 			blocksize = 1024;
     83 			kflag = 1;
     84 			break;
     85 		case 's':
     86 			sflag = 1;
     87 			break;
     88 		case 'x':
     89 			ftsoptions |= FTS_XDEV;
     90 			break;
     91 		case '?':
     92 		default:
     93 			usage();
     94 		}
     95 	argv += optind;
     96 
     97 	if (aflag) {
     98 		if (sflag)
     99 			usage();
    100 		listdirs = listfiles = 1;
    101 	} else if (sflag)
    102 		listdirs = listfiles = 0;
    103 	else {
    104 		listfiles = 0;
    105 		listdirs = 1;
    106 	}
    107 
    108 	if (!*argv) {
    109 		argv = save;
    110 		argv[0] = ".";
    111 		argv[1] = NULL;
    112 	}
    113 
    114 	if (!kflag)
    115 		(void)getbsize(&notused, &blocksize);
    116 	blocksize /= 512;
    117 
    118 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
    119 		err(1, NULL);
    120 
    121 	while (p = fts_read(fts))
    122 		switch(p->fts_info) {
    123 		case FTS_D:
    124 			break;
    125 		case FTS_DP:
    126 			p->fts_parent->fts_number +=
    127 			    p->fts_number += p->fts_statp->st_blocks;
    128 			/*
    129 			 * If listing each directory, or not listing files
    130 			 * or directories and this is post-order of the
    131 			 * root of a traversal, display the total.
    132 			 */
    133 			if (listdirs || !listfiles && !p->fts_level)
    134 				(void)printf("%ld\t%s\n",
    135 				    howmany(p->fts_number, blocksize),
    136 				    p->fts_path);
    137 			break;
    138 		case FTS_DNR:
    139 		case FTS_ERR:
    140 		case FTS_NS:
    141 			warn("%s", p->fts_path);
    142 			break;
    143 		case FTS_SL:
    144 			if (p->fts_level == FTS_ROOTLEVEL) {
    145 				(void)fts_set(fts, p, FTS_FOLLOW);
    146 				break;
    147 			}
    148 			/* FALLTHROUGH */
    149 		default:
    150 			if (p->fts_statp->st_nlink > 1 && linkchk(p))
    151 				break;
    152 			/*
    153 			 * If listing each file, or a non-directory file was
    154 			 * the root of a traversal, display the total.
    155 			 */
    156 			if (listfiles || !p->fts_level)
    157 				(void)printf("%ld\t%s\n",
    158 				    howmany(p->fts_statp->st_blocks, blocksize),
    159 				    p->fts_path);
    160 			p->fts_parent->fts_number += p->fts_statp->st_blocks;
    161 		}
    162 	if (errno)
    163 		err(1, NULL);
    164 	exit(0);
    165 }
    166 
    167 typedef struct _ID {
    168 	dev_t	dev;
    169 	ino_t	inode;
    170 } ID;
    171 
    172 int
    173 linkchk(p)
    174 	register FTSENT *p;
    175 {
    176 	static ID *files;
    177 	static int maxfiles, nfiles;
    178 	register ID *fp, *start;
    179 	register ino_t ino;
    180 	register dev_t dev;
    181 
    182 	ino = p->fts_statp->st_ino;
    183 	dev = p->fts_statp->st_dev;
    184 	if (start = files)
    185 		for (fp = start + nfiles - 1; fp >= start; --fp)
    186 			if (ino == fp->inode && dev == fp->dev)
    187 				return(1);
    188 
    189 	if (nfiles == maxfiles && (files = realloc((char *)files,
    190 	    (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
    191 		err(1, NULL);
    192 	files[nfiles].inode = ino;
    193 	files[nfiles].dev = dev;
    194 	++nfiles;
    195 	return(0);
    196 }
    197 
    198 void
    199 usage()
    200 {
    201 	(void)fprintf(stderr, "usage: du [-a | -s] [-x] [file ...]\n");
    202 	exit(1);
    203 }
    204