1 /* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. 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 static const char copyright[] = 39 "@(#) Copyright (c) 1989, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95"; 46 #endif 47 static const char rcsid[] = 48 "$FreeBSD$"; 49 #endif /* not lint */ 50 51 52 #include <sys/param.h> 53 #include <sys/queue.h> 54 #include <sys/stat.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <fnmatch.h> 59 #include <fts.h> 60 #include <math.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <sysexits.h> 65 #include <unistd.h> 66 67 #define KILO_SZ(n) (n) 68 #define MEGA_SZ(n) ((n) * (n)) 69 #define GIGA_SZ(n) ((n) * (n) * (n)) 70 #define TERA_SZ(n) ((n) * (n) * (n) * (n)) 71 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) 72 73 #define KILO_2_SZ (KILO_SZ(1024ULL)) 74 #define MEGA_2_SZ (MEGA_SZ(1024ULL)) 75 #define GIGA_2_SZ (GIGA_SZ(1024ULL)) 76 #define TERA_2_SZ (TERA_SZ(1024ULL)) 77 #define PETA_2_SZ (PETA_SZ(1024ULL)) 78 79 #define KILO_SI_SZ (KILO_SZ(1000ULL)) 80 #define MEGA_SI_SZ (MEGA_SZ(1000ULL)) 81 #define GIGA_SI_SZ (GIGA_SZ(1000ULL)) 82 #define TERA_SI_SZ (TERA_SZ(1000ULL)) 83 #define PETA_SI_SZ (PETA_SZ(1000ULL)) 84 85 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ}; 86 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ}; 87 unsigned long long *valp; 88 89 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t; 90 91 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA }; 92 93 SLIST_HEAD(ignhead, ignentry) ignores; 94 struct ignentry { 95 char *mask; 96 SLIST_ENTRY(ignentry) next; 97 }; 98 99 int linkchk __P((FTSENT *)); 100 static void usage __P((void)); 101 void prthumanval __P((double)); 102 unit_t unit_adjust __P((double *)); 103 void ignoreadd __P((const char *)); 104 void ignoreclean __P((void)); 105 int ignorep __P((FTSENT *)); 106 107 int 108 main(argc, argv) 109 int argc; 110 char *argv[]; 111 { 112 FTS *fts; 113 FTSENT *p; 114 long blocksize, savednumber = 0; 115 int ftsoptions; 116 int listall; 117 int depth; 118 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval; 119 char **save; 120 121 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0; 122 123 save = argv; 124 ftsoptions = 0; 125 depth = INT_MAX; 126 SLIST_INIT(&ignores); 127 128 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1) 129 switch (ch) { 130 case 'H': 131 Hflag = 1; 132 break; 133 case 'I': 134 ignoreadd(optarg); 135 break; 136 case 'L': 137 if (Pflag) 138 usage(); 139 Lflag = 1; 140 break; 141 case 'P': 142 if (Lflag) 143 usage(); 144 Pflag = 1; 145 break; 146 case 'a': 147 aflag = 1; 148 break; 149 case 's': 150 sflag = 1; 151 break; 152 case 'd': 153 dflag = 1; 154 errno = 0; 155 depth = atoi(optarg); 156 if (errno == ERANGE || depth < 0) { 157 warnx("invalid argument to option d: %s", optarg); 158 usage(); 159 } 160 break; 161 case 'c': 162 cflag = 1; 163 break; 164 case 'h': 165 putenv("BLOCKSIZE=512"); 166 hflag = 1; 167 valp = vals_base2; 168 break; 169 case 'k': 170 if (!hflag) 171 putenv("BLOCKSIZE=1024"); 172 break; 173 case 'r': /* Compatibility. */ 174 break; 175 case 'x': 176 ftsoptions |= FTS_XDEV; 177 break; 178 case '?': 179 default: 180 usage(); 181 } 182 183 argc -= optind; 184 argv += optind; 185 186 /* 187 * XXX 188 * Because of the way that fts(3) works, logical walks will not count 189 * the blocks actually used by symbolic links. We rationalize this by 190 * noting that users computing logical sizes are likely to do logical 191 * copies, so not counting the links is correct. The real reason is 192 * that we'd have to re-implement the kernel's symbolic link traversing 193 * algorithm to get this right. If, for example, you have relative 194 * symbolic links referencing other relative symbolic links, it gets 195 * very nasty, very fast. The bottom line is that it's documented in 196 * the man page, so it's a feature. 197 */ 198 199 if (Hflag + Lflag + Pflag > 1) 200 usage(); 201 202 if (Hflag + Lflag + Pflag == 0) 203 Pflag = 1; /* -P (physical) is default */ 204 205 if (Hflag) 206 ftsoptions |= FTS_COMFOLLOW; 207 208 if (Lflag) 209 ftsoptions |= FTS_LOGICAL; 210 211 if (Pflag) 212 ftsoptions |= FTS_PHYSICAL; 213 214 listall = 0; 215 216 if (aflag) { 217 if (sflag || dflag) 218 usage(); 219 listall = 1; 220 } else if (sflag) { 221 if (dflag) 222 usage(); 223 depth = 0; 224 } 225 226 if (!*argv) { 227 argv = save; 228 argv[0] = "."; 229 argv[1] = NULL; 230 } 231 232 (void) getbsize(¬used, &blocksize); 233 blocksize /= 512; 234 235 rval = 0; 236 237 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) 238 err(1, "fts_open"); 239 240 while ((p = fts_read(fts)) != NULL) { 241 switch (p->fts_info) { 242 case FTS_D: /* Ignore. */ 243 if (ignorep(p)) 244 fts_set(fts, p, FTS_SKIP); 245 break; 246 case FTS_DP: 247 if (ignorep(p)) 248 break; 249 250 p->fts_parent->fts_number += 251 p->fts_number += p->fts_statp->st_blocks; 252 253 if (p->fts_level <= depth) { 254 if (hflag) { 255 (void) prthumanval(howmany(p->fts_number, blocksize)); 256 (void) printf("\t%s\n", p->fts_path); 257 } else { 258 (void) printf("%ld\t%s\n", 259 howmany(p->fts_number, blocksize), 260 p->fts_path); 261 } 262 } 263 break; 264 case FTS_DC: /* Ignore. */ 265 break; 266 case FTS_DNR: /* Warn, continue. */ 267 case FTS_ERR: 268 case FTS_NS: 269 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 270 rval = 1; 271 break; 272 default: 273 if (ignorep(p)) 274 break; 275 276 if (p->fts_statp->st_nlink > 1 && linkchk(p)) 277 break; 278 279 if (listall || p->fts_level == 0) { 280 if (hflag) { 281 (void) prthumanval(howmany(p->fts_statp->st_blocks, 282 blocksize)); 283 (void) printf("\t%s\n", p->fts_path); 284 } else { 285 (void) printf("%qd\t%s\n", 286 howmany(p->fts_statp->st_blocks, blocksize), 287 p->fts_path); 288 } 289 } 290 291 p->fts_parent->fts_number += p->fts_statp->st_blocks; 292 } 293 savednumber = p->fts_parent->fts_number; 294 } 295 296 if (errno) 297 err(1, "fts_read"); 298 299 if (cflag) { 300 if (hflag) { 301 (void) prthumanval(howmany(savednumber, blocksize)); 302 (void) printf("\ttotal\n"); 303 } else { 304 (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize)); 305 } 306 } 307 308 ignoreclean(); 309 exit(rval); 310 } 311 312 313 typedef struct _ID { 314 dev_t dev; 315 ino_t inode; 316 } ID; 317 318 319 int 320 linkchk(p) 321 FTSENT *p; 322 { 323 static ID *files; 324 static int maxfiles, nfiles; 325 ID *fp, *start; 326 ino_t ino; 327 dev_t dev; 328 329 ino = p->fts_statp->st_ino; 330 dev = p->fts_statp->st_dev; 331 if ((start = files) != NULL) 332 for (fp = start + nfiles - 1; fp >= start; --fp) 333 if (ino == fp->inode && dev == fp->dev) 334 return (1); 335 336 if (nfiles == maxfiles && (files = realloc((char *)files, 337 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL) 338 errx(1, "can't allocate memory"); 339 files[nfiles].inode = ino; 340 files[nfiles].dev = dev; 341 ++nfiles; 342 return (0); 343 } 344 345 /* 346 * Output in "human-readable" format. Uses 3 digits max and puts 347 * unit suffixes at the end. Makes output compact and easy to read, 348 * especially on huge disks. 349 * 350 */ 351 unit_t 352 unit_adjust(val) 353 double *val; 354 { 355 double abval; 356 unit_t unit; 357 unsigned int unit_sz; 358 359 abval = fabs(*val); 360 361 unit_sz = abval ? ilogb(abval) / 10 : 0; 362 363 if (unit_sz >= UNIT_MAX) { 364 unit = NONE; 365 } else { 366 unit = unitp[unit_sz]; 367 *val /= (double)valp[unit_sz]; 368 } 369 370 return (unit); 371 } 372 373 void 374 prthumanval(bytes) 375 double bytes; 376 { 377 unit_t unit; 378 379 bytes *= 512; 380 unit = unit_adjust(&bytes); 381 382 if (bytes == 0) 383 (void)printf(" 0B"); 384 else if (bytes > 10) 385 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]); 386 else 387 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]); 388 } 389 390 static void 391 usage() 392 { 393 (void)fprintf(stderr, 394 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n"); 395 exit(EX_USAGE); 396 } 397 398 void 399 ignoreadd(mask) 400 const char *mask; 401 { 402 struct ignentry *ign; 403 404 ign = calloc(1, sizeof(*ign)); 405 if (ign == NULL) 406 errx(1, "cannot allocate memory"); 407 ign->mask = strdup(mask); 408 if (ign->mask == NULL) 409 errx(1, "cannot allocate memory"); 410 SLIST_INSERT_HEAD(&ignores, ign, next); 411 } 412 413 void 414 ignoreclean() 415 { 416 struct ignentry *ign; 417 418 while (!SLIST_EMPTY(&ignores)) { 419 ign = SLIST_FIRST(&ignores); 420 SLIST_REMOVE_HEAD(&ignores, next); 421 free(ign->mask); 422 free(ign); 423 } 424 } 425 426 int 427 ignorep(ent) 428 FTSENT *ent; 429 { 430 struct ignentry *ign; 431 432 SLIST_FOREACH(ign, &ignores, next) 433 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH) 434 return 1; 435 return 0; 436 } 437