17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51979231eSceastha * Common Development and Distribution License (the "License"). 61979231eSceastha * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*15deec58Sceastha * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 261979231eSceastha /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 271979231eSceastha /* All Rights Reserved */ 281979231eSceastha 297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * du -- summarize disk usage 331979231eSceastha * du [-dorx] [-a|-s] [-h|-k|-m] [-H|-L] [file...] 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <sys/avl.h> 397c478bd9Sstevel@tonic-gate #include <fcntl.h> 407c478bd9Sstevel@tonic-gate #include <dirent.h> 417c478bd9Sstevel@tonic-gate #include <limits.h> 427c478bd9Sstevel@tonic-gate #include <stdio.h> 437c478bd9Sstevel@tonic-gate #include <stdlib.h> 447c478bd9Sstevel@tonic-gate #include <string.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <locale.h> 477c478bd9Sstevel@tonic-gate #include <libcmdutils.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate static int aflg = 0; 517c478bd9Sstevel@tonic-gate static int rflg = 0; 527c478bd9Sstevel@tonic-gate static int sflg = 0; 537c478bd9Sstevel@tonic-gate static int kflg = 0; 541979231eSceastha static int mflg = 0; 557c478bd9Sstevel@tonic-gate static int oflg = 0; 567c478bd9Sstevel@tonic-gate static int dflg = 0; 577c478bd9Sstevel@tonic-gate static int hflg = 0; 587c478bd9Sstevel@tonic-gate static int Hflg = 0; 597c478bd9Sstevel@tonic-gate static int Lflg = 0; 607c478bd9Sstevel@tonic-gate static int cmdarg = 0; /* Command line argument */ 617c478bd9Sstevel@tonic-gate static char *dot = "."; 627c478bd9Sstevel@tonic-gate static int level = 0; /* Level of recursion */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static char *base; 657c478bd9Sstevel@tonic-gate static char *name; 667c478bd9Sstevel@tonic-gate static size_t base_len = PATH_MAX + 1; /* # of chars for base */ 677c478bd9Sstevel@tonic-gate static size_t name_len = PATH_MAX + 1; /* # of chars for name */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #define NUMBER_WIDTH 64 707c478bd9Sstevel@tonic-gate typedef char numbuf_t[NUMBER_WIDTH]; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 731979231eSceastha * Output formats. Solaris uses a tab as separator, XPG4 a space. 741979231eSceastha */ 751979231eSceastha #ifdef XPG4 761979231eSceastha #define FORMAT1 "%s %s\n" 771979231eSceastha #define FORMAT2 "%lld %s\n" 781979231eSceastha #else 791979231eSceastha #define FORMAT1 "%s\t%s\n" 801979231eSceastha #define FORMAT2 "%lld\t%s\n" 811979231eSceastha #endif 821979231eSceastha 831979231eSceastha /* 847c478bd9Sstevel@tonic-gate * convert DEV_BSIZE blocks to K blocks 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate #define DEV_BSIZE 512 877c478bd9Sstevel@tonic-gate #define DEV_KSHIFT 1 881979231eSceastha #define DEV_MSHIFT 11 897c478bd9Sstevel@tonic-gate #define kb(n) (((u_longlong_t)(n)) >> DEV_KSHIFT) 901979231eSceastha #define mb(n) (((u_longlong_t)(n)) >> DEV_MSHIFT) 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate long wait(); 937c478bd9Sstevel@tonic-gate static u_longlong_t descend(char *curname, int curfd, int *retcode, 947c478bd9Sstevel@tonic-gate dev_t device); 957c478bd9Sstevel@tonic-gate static void printsize(blkcnt_t blocks, char *path); 967c478bd9Sstevel@tonic-gate static void exitdu(int exitcode); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate static avl_tree_t *tree = NULL; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate int 1017c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate blkcnt_t blocks = 0; 1047c478bd9Sstevel@tonic-gate int c; 1057c478bd9Sstevel@tonic-gate extern int optind; 1067c478bd9Sstevel@tonic-gate char *np; 1077c478bd9Sstevel@tonic-gate pid_t pid, wpid; 1087c478bd9Sstevel@tonic-gate int status, retcode = 0; 1097c478bd9Sstevel@tonic-gate setbuf(stderr, NULL); 1107c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1117c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1127c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1137c478bd9Sstevel@tonic-gate #endif 1147c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate #ifdef XPG4 1177c478bd9Sstevel@tonic-gate rflg++; /* "-r" is not an option but ON always */ 1187c478bd9Sstevel@tonic-gate #endif 1197c478bd9Sstevel@tonic-gate 1201979231eSceastha while ((c = getopt(argc, argv, "adhHkLmorsx")) != EOF) 1217c478bd9Sstevel@tonic-gate switch (c) { 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate case 'a': 1247c478bd9Sstevel@tonic-gate aflg++; 1257c478bd9Sstevel@tonic-gate continue; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate case 'h': 1287c478bd9Sstevel@tonic-gate hflg++; 1291979231eSceastha kflg = 0; 1301979231eSceastha mflg = 0; 1317c478bd9Sstevel@tonic-gate continue; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate case 'r': 1347c478bd9Sstevel@tonic-gate rflg++; 1357c478bd9Sstevel@tonic-gate continue; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate case 's': 1387c478bd9Sstevel@tonic-gate sflg++; 1397c478bd9Sstevel@tonic-gate continue; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate case 'k': 1427c478bd9Sstevel@tonic-gate kflg++; 1431979231eSceastha hflg = 0; 1441979231eSceastha mflg = 0; 1451979231eSceastha continue; 1461979231eSceastha 1471979231eSceastha case 'm': 1481979231eSceastha mflg++; 1491979231eSceastha hflg = 0; 1501979231eSceastha kflg = 0; 1517c478bd9Sstevel@tonic-gate continue; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate case 'o': 1547c478bd9Sstevel@tonic-gate oflg++; 1557c478bd9Sstevel@tonic-gate continue; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate case 'd': 1587c478bd9Sstevel@tonic-gate dflg++; 1597c478bd9Sstevel@tonic-gate continue; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate case 'x': 1627c478bd9Sstevel@tonic-gate dflg++; 1637c478bd9Sstevel@tonic-gate continue; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate case 'H': 1667c478bd9Sstevel@tonic-gate Hflg++; 1677c478bd9Sstevel@tonic-gate /* -H and -L are mutually exclusive */ 1687c478bd9Sstevel@tonic-gate Lflg = 0; 1697c478bd9Sstevel@tonic-gate cmdarg++; 1707c478bd9Sstevel@tonic-gate continue; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate case 'L': 1737c478bd9Sstevel@tonic-gate Lflg++; 1747c478bd9Sstevel@tonic-gate /* -H and -L are mutually exclusive */ 1757c478bd9Sstevel@tonic-gate Hflg = 0; 1767c478bd9Sstevel@tonic-gate cmdarg = 0; 1777c478bd9Sstevel@tonic-gate continue; 1787c478bd9Sstevel@tonic-gate case '?': 1797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1801979231eSceastha "usage: du [-dorx] [-a|-s] [-h|-k|-m] [-H|-L] " 1817c478bd9Sstevel@tonic-gate "[file...]\n")); 1827c478bd9Sstevel@tonic-gate exit(2); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate if (optind == argc) { 1857c478bd9Sstevel@tonic-gate argv = ˙ 1867c478bd9Sstevel@tonic-gate argc = 1; 1877c478bd9Sstevel@tonic-gate optind = 0; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* "-o" and "-s" don't make any sense together. */ 1917c478bd9Sstevel@tonic-gate if (oflg && sflg) 1927c478bd9Sstevel@tonic-gate oflg = 0; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate if ((base = (char *)calloc(base_len, sizeof (char))) == NULL) { 1957c478bd9Sstevel@tonic-gate perror("du"); 1967c478bd9Sstevel@tonic-gate exit(1); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate if ((name = (char *)calloc(name_len, sizeof (char))) == NULL) { 1997c478bd9Sstevel@tonic-gate perror("du"); 2007c478bd9Sstevel@tonic-gate free(base); 2017c478bd9Sstevel@tonic-gate exit(1); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate do { 2047c478bd9Sstevel@tonic-gate if (optind < argc - 1) { 2057c478bd9Sstevel@tonic-gate pid = fork(); 2067c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) { 2077c478bd9Sstevel@tonic-gate perror(gettext("du: No more processes")); 2087c478bd9Sstevel@tonic-gate exitdu(1); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (pid != 0) { 2117c478bd9Sstevel@tonic-gate while ((wpid = wait(&status)) != pid && 2127c478bd9Sstevel@tonic-gate wpid != (pid_t)-1) 2137c478bd9Sstevel@tonic-gate ; 2147c478bd9Sstevel@tonic-gate if (pid != (pid_t)-1 && status != 0) 2157c478bd9Sstevel@tonic-gate retcode = 1; 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate if (optind == argc - 1 || pid == 0) { 2197c478bd9Sstevel@tonic-gate while (base_len < (strlen(argv[optind]) + 1)) { 2207c478bd9Sstevel@tonic-gate base_len = base_len * 2; 2217c478bd9Sstevel@tonic-gate if ((base = (char *)realloc(base, base_len * 2227c478bd9Sstevel@tonic-gate sizeof (char))) == NULL) { 2237c478bd9Sstevel@tonic-gate if (rflg) { 2247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2257c478bd9Sstevel@tonic-gate "du: can't process %s"), 2267c478bd9Sstevel@tonic-gate argv[optind]); 2277c478bd9Sstevel@tonic-gate perror(""); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate exitdu(1); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate if (base_len > name_len) { 2337c478bd9Sstevel@tonic-gate name_len = base_len; 2347c478bd9Sstevel@tonic-gate if ((name = (char *)realloc(name, name_len * 2357c478bd9Sstevel@tonic-gate sizeof (char))) == NULL) { 2367c478bd9Sstevel@tonic-gate if (rflg) { 2377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 2387c478bd9Sstevel@tonic-gate "du: can't process %s"), 2397c478bd9Sstevel@tonic-gate argv[optind]); 2407c478bd9Sstevel@tonic-gate perror(""); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate exitdu(1); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate (void) strcpy(base, argv[optind]); 2467c478bd9Sstevel@tonic-gate (void) strcpy(name, argv[optind]); 2477c478bd9Sstevel@tonic-gate if (np = strrchr(name, '/')) { 2487c478bd9Sstevel@tonic-gate *np++ = '\0'; 2497c478bd9Sstevel@tonic-gate if (chdir(*name ? name : "/") < 0) { 2507c478bd9Sstevel@tonic-gate if (rflg) { 2517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "du: "); 2527c478bd9Sstevel@tonic-gate perror(*name ? name : "/"); 2537c478bd9Sstevel@tonic-gate exitdu(1); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate exitdu(0); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate } else 2587c478bd9Sstevel@tonic-gate np = base; 2597c478bd9Sstevel@tonic-gate blocks = descend(*np ? np : ".", 0, &retcode, 2607c478bd9Sstevel@tonic-gate (dev_t)0); 2617c478bd9Sstevel@tonic-gate if (sflg) 2627c478bd9Sstevel@tonic-gate printsize(blocks, base); 2637c478bd9Sstevel@tonic-gate if (optind < argc - 1) 2647c478bd9Sstevel@tonic-gate exitdu(retcode); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate optind++; 2677c478bd9Sstevel@tonic-gate } while (optind < argc); 2687c478bd9Sstevel@tonic-gate exitdu(retcode); 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate return (retcode); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * descend recursively, adding up the allocated blocks. 2757c478bd9Sstevel@tonic-gate * If curname is NULL, curfd is used. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate static u_longlong_t 2787c478bd9Sstevel@tonic-gate descend(char *curname, int curfd, int *retcode, dev_t device) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate static DIR *dirp = NULL; 2817c478bd9Sstevel@tonic-gate char *ebase0, *ebase; 2827c478bd9Sstevel@tonic-gate struct stat stb, stb1; 2837c478bd9Sstevel@tonic-gate int i, j, ret, fd, tmpflg; 284*15deec58Sceastha int follow_symlinks; 2857c478bd9Sstevel@tonic-gate blkcnt_t blocks = 0; 2867c478bd9Sstevel@tonic-gate off_t curoff = 0; 2877c478bd9Sstevel@tonic-gate ptrdiff_t offset; 2887c478bd9Sstevel@tonic-gate ptrdiff_t offset0; 2897c478bd9Sstevel@tonic-gate struct dirent *dp; 2907c478bd9Sstevel@tonic-gate char dirbuf[PATH_MAX + 1]; 2917c478bd9Sstevel@tonic-gate u_longlong_t retval; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate ebase0 = ebase = strchr(base, 0); 2947c478bd9Sstevel@tonic-gate if (ebase > base && ebase[-1] == '/') 2957c478bd9Sstevel@tonic-gate ebase--; 2967c478bd9Sstevel@tonic-gate offset = ebase - base; 2977c478bd9Sstevel@tonic-gate offset0 = ebase0 - base; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (curname) 3007c478bd9Sstevel@tonic-gate curfd = AT_FDCWD; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate /* 3037c478bd9Sstevel@tonic-gate * If neither a -L or a -H was specified, don't follow symlinks. 3047c478bd9Sstevel@tonic-gate * If a -H was specified, don't follow symlinks if the file is 3057c478bd9Sstevel@tonic-gate * not a command line argument. 3067c478bd9Sstevel@tonic-gate */ 307*15deec58Sceastha follow_symlinks = (Lflg || (Hflg && cmdarg)); 308*15deec58Sceastha if (follow_symlinks) { 3097c478bd9Sstevel@tonic-gate i = fstatat(curfd, curname, &stb, 0); 3107c478bd9Sstevel@tonic-gate j = fstatat(curfd, curname, &stb1, AT_SYMLINK_NOFOLLOW); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * Make sure any files encountered while traversing the 3147c478bd9Sstevel@tonic-gate * hierarchy are not considered command line arguments. 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate if (Hflg) { 3177c478bd9Sstevel@tonic-gate cmdarg = 0; 3187c478bd9Sstevel@tonic-gate } 319*15deec58Sceastha } else { 320*15deec58Sceastha i = fstatat(curfd, curname, &stb, AT_SYMLINK_NOFOLLOW); 321*15deec58Sceastha j = 0; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if ((i < 0) || (j < 0)) { 3257c478bd9Sstevel@tonic-gate if (rflg) { 3267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "du: "); 3277c478bd9Sstevel@tonic-gate perror(base); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* 3317c478bd9Sstevel@tonic-gate * POSIX states that non-zero status codes are only set 3327c478bd9Sstevel@tonic-gate * when an error message is printed out on stderr 3337c478bd9Sstevel@tonic-gate */ 3347c478bd9Sstevel@tonic-gate *retcode = (rflg ? 1 : 0); 3357c478bd9Sstevel@tonic-gate *ebase0 = 0; 3367c478bd9Sstevel@tonic-gate return (0); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate if (device) { 3397c478bd9Sstevel@tonic-gate if (dflg && stb.st_dev != device) { 3407c478bd9Sstevel@tonic-gate *ebase0 = 0; 3417c478bd9Sstevel@tonic-gate return (0); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate else 3457c478bd9Sstevel@tonic-gate device = stb.st_dev; 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * If following links (-L) we need to keep track of all inodes 3497c478bd9Sstevel@tonic-gate * visited so they are only visited/reported once and cycles 3507c478bd9Sstevel@tonic-gate * are avoided. Otherwise, only keep track of files which are 3517c478bd9Sstevel@tonic-gate * hard links so they only get reported once, and of directories 3527c478bd9Sstevel@tonic-gate * so we don't report a directory and its hierarchy more than 3537c478bd9Sstevel@tonic-gate * once in the special case in which it lies under the 3547c478bd9Sstevel@tonic-gate * hierarchy of a directory which is a hard link. 3557c478bd9Sstevel@tonic-gate * Note: Files with multiple links should only be counted 3567c478bd9Sstevel@tonic-gate * once. Since each inode could possibly be referenced by a 3577c478bd9Sstevel@tonic-gate * symbolic link, we need to keep track of all inodes when -L 3587c478bd9Sstevel@tonic-gate * is specified. 3597c478bd9Sstevel@tonic-gate */ 360*15deec58Sceastha if (Lflg || ((stb.st_mode & S_IFMT) == S_IFDIR) || 3617c478bd9Sstevel@tonic-gate (stb.st_nlink > 1)) { 3627c478bd9Sstevel@tonic-gate int rc; 3637c478bd9Sstevel@tonic-gate if ((rc = add_tnode(&tree, stb.st_dev, stb.st_ino)) != 1) { 3647c478bd9Sstevel@tonic-gate if (rc == 0) { 3657c478bd9Sstevel@tonic-gate /* 3667c478bd9Sstevel@tonic-gate * This hierarchy, or file with multiple 3677c478bd9Sstevel@tonic-gate * links, has already been visited/reported. 3687c478bd9Sstevel@tonic-gate */ 3697c478bd9Sstevel@tonic-gate return (0); 3707c478bd9Sstevel@tonic-gate } else { 3717c478bd9Sstevel@tonic-gate /* 3727c478bd9Sstevel@tonic-gate * An error occurred while trying to add the 3737c478bd9Sstevel@tonic-gate * node to the tree. 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate if (rflg) { 3767c478bd9Sstevel@tonic-gate perror("du"); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate exitdu(1); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate blocks = stb.st_blocks; 3837c478bd9Sstevel@tonic-gate /* 3847c478bd9Sstevel@tonic-gate * If there are extended attributes on the current file, add their 385*15deec58Sceastha * block usage onto the block count. Note: Since pathconf() always 386*15deec58Sceastha * follows symlinks, only test for extended attributes using pathconf() 387*15deec58Sceastha * if we are following symlinks or the current file is not a symlink. 3887c478bd9Sstevel@tonic-gate */ 389*15deec58Sceastha if (curname && (follow_symlinks || 390*15deec58Sceastha ((stb.st_mode & S_IFMT) != S_IFLNK)) && 391*15deec58Sceastha pathconf(curname, _PC_XATTR_EXISTS) == 1) { 3927c478bd9Sstevel@tonic-gate if ((fd = attropen(curname, ".", O_RDONLY)) < 0) { 3937c478bd9Sstevel@tonic-gate if (rflg) 3947c478bd9Sstevel@tonic-gate perror(gettext( 3957c478bd9Sstevel@tonic-gate "du: can't access extended attributes")); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate else 3987c478bd9Sstevel@tonic-gate { 3997c478bd9Sstevel@tonic-gate tmpflg = sflg; 4007c478bd9Sstevel@tonic-gate sflg = 1; 4017c478bd9Sstevel@tonic-gate blocks += descend(NULL, fd, retcode, device); 4027c478bd9Sstevel@tonic-gate sflg = tmpflg; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate if ((stb.st_mode & S_IFMT) != S_IFDIR) { 4067c478bd9Sstevel@tonic-gate /* 4077c478bd9Sstevel@tonic-gate * Don't print twice: if sflg, file will get printed in main(). 4087c478bd9Sstevel@tonic-gate * Otherwise, level == 0 means this file is listed on the 4097c478bd9Sstevel@tonic-gate * command line, so print here; aflg means print all files. 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate if (sflg == 0 && (aflg || level == 0)) 4127c478bd9Sstevel@tonic-gate printsize(blocks, base); 4137c478bd9Sstevel@tonic-gate return (blocks); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate if (dirp != NULL) 4167c478bd9Sstevel@tonic-gate /* 4177c478bd9Sstevel@tonic-gate * Close the parent directory descriptor, we will reopen 4187c478bd9Sstevel@tonic-gate * the directory when we pop up from this level of the 4197c478bd9Sstevel@tonic-gate * recursion. 4207c478bd9Sstevel@tonic-gate */ 4217c478bd9Sstevel@tonic-gate (void) closedir(dirp); 4227c478bd9Sstevel@tonic-gate if (curname == NULL) 4237c478bd9Sstevel@tonic-gate dirp = fdopendir(curfd); 4247c478bd9Sstevel@tonic-gate else 4257c478bd9Sstevel@tonic-gate dirp = opendir(curname); 4267c478bd9Sstevel@tonic-gate if (dirp == NULL) { 4277c478bd9Sstevel@tonic-gate if (rflg) { 4287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "du: "); 4297c478bd9Sstevel@tonic-gate perror(base); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate *retcode = 1; 4327c478bd9Sstevel@tonic-gate *ebase0 = 0; 4337c478bd9Sstevel@tonic-gate return (0); 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate level++; 4367c478bd9Sstevel@tonic-gate if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode))) { 4377c478bd9Sstevel@tonic-gate if (getcwd(dirbuf, PATH_MAX) == NULL) { 4387c478bd9Sstevel@tonic-gate if (rflg) { 4397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "du: "); 4407c478bd9Sstevel@tonic-gate perror(base); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate exitdu(1); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate if ((curname ? (chdir(curname) < 0) : (fchdir(curfd) < 0))) { 4467c478bd9Sstevel@tonic-gate if (rflg) { 4477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "du: "); 4487c478bd9Sstevel@tonic-gate perror(base); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate *retcode = 1; 4517c478bd9Sstevel@tonic-gate *ebase0 = 0; 4527c478bd9Sstevel@tonic-gate (void) closedir(dirp); 4537c478bd9Sstevel@tonic-gate dirp = NULL; 4547c478bd9Sstevel@tonic-gate level--; 4557c478bd9Sstevel@tonic-gate return (0); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate while (dp = readdir(dirp)) { 4587c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 4597c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 4607c478bd9Sstevel@tonic-gate continue; 4617c478bd9Sstevel@tonic-gate /* 4627c478bd9Sstevel@tonic-gate * we're about to append "/" + dp->d_name 4637c478bd9Sstevel@tonic-gate * onto end of base; make sure there's enough 4647c478bd9Sstevel@tonic-gate * space 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate while ((offset + strlen(dp->d_name) + 2) > base_len) { 4677c478bd9Sstevel@tonic-gate base_len = base_len * 2; 4687c478bd9Sstevel@tonic-gate if ((base = (char *)realloc(base, 4697c478bd9Sstevel@tonic-gate base_len * sizeof (char))) == NULL) { 4707c478bd9Sstevel@tonic-gate if (rflg) { 4717c478bd9Sstevel@tonic-gate perror("du"); 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate exitdu(1); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate ebase = base + offset; 4767c478bd9Sstevel@tonic-gate ebase0 = base + offset0; 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate /* LINTED - unbounded string specifier */ 4797c478bd9Sstevel@tonic-gate (void) sprintf(ebase, "/%s", dp->d_name); 4807c478bd9Sstevel@tonic-gate curoff = telldir(dirp); 4817c478bd9Sstevel@tonic-gate retval = descend(ebase + 1, 0, retcode, device); 4827c478bd9Sstevel@tonic-gate /* base may have been moved via realloc in descend() */ 4837c478bd9Sstevel@tonic-gate ebase = base + offset; 4847c478bd9Sstevel@tonic-gate ebase0 = base + offset0; 4857c478bd9Sstevel@tonic-gate *ebase = 0; 4867c478bd9Sstevel@tonic-gate blocks += retval; 4877c478bd9Sstevel@tonic-gate if (dirp == NULL) { 4887c478bd9Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) { 4897c478bd9Sstevel@tonic-gate if (rflg) { 4907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4917c478bd9Sstevel@tonic-gate gettext("du: Can't reopen in ")); 4927c478bd9Sstevel@tonic-gate perror(base); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate *retcode = 1; 4957c478bd9Sstevel@tonic-gate level--; 4967c478bd9Sstevel@tonic-gate return (0); 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate seekdir(dirp, curoff); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate (void) closedir(dirp); 5027c478bd9Sstevel@tonic-gate level--; 5037c478bd9Sstevel@tonic-gate dirp = NULL; 5047c478bd9Sstevel@tonic-gate if (sflg == 0) 5057c478bd9Sstevel@tonic-gate printsize(blocks, base); 5067c478bd9Sstevel@tonic-gate if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode))) 5077c478bd9Sstevel@tonic-gate ret = chdir(dirbuf); 5087c478bd9Sstevel@tonic-gate else 5097c478bd9Sstevel@tonic-gate ret = chdir(".."); 5107c478bd9Sstevel@tonic-gate if (ret < 0) { 5117c478bd9Sstevel@tonic-gate if (rflg) { 5127c478bd9Sstevel@tonic-gate (void) sprintf(strchr(base, '\0'), "/.."); 5137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5147c478bd9Sstevel@tonic-gate gettext("du: Can't change dir to '..' in ")); 5157c478bd9Sstevel@tonic-gate perror(base); 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate exitdu(1); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate *ebase0 = 0; 5207c478bd9Sstevel@tonic-gate if (oflg) 5217c478bd9Sstevel@tonic-gate return (0); 5227c478bd9Sstevel@tonic-gate else 5237c478bd9Sstevel@tonic-gate return (blocks); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * Convert an unsigned long long to a string representation and place the 5287c478bd9Sstevel@tonic-gate * result in the caller-supplied buffer. 5297c478bd9Sstevel@tonic-gate * The given number is in units of "unit_from" size, 5307c478bd9Sstevel@tonic-gate * this will first be converted to a number in 1024 or 1000 byte size, 5317c478bd9Sstevel@tonic-gate * depending on the scaling factor. 5327c478bd9Sstevel@tonic-gate * Then the number is scaled down until it is small enough to be in a good 5337c478bd9Sstevel@tonic-gate * human readable format i.e. in the range 0 thru scale-1. 5347c478bd9Sstevel@tonic-gate * If it's smaller than 10 there's room enough to provide one decimal place. 5357c478bd9Sstevel@tonic-gate * The value "(unsigned long long)-1" is a special case and is always 5367c478bd9Sstevel@tonic-gate * converted to "-1". 5377c478bd9Sstevel@tonic-gate * Returns a pointer to the caller-supplied buffer. 5387c478bd9Sstevel@tonic-gate */ 5397c478bd9Sstevel@tonic-gate static char * 5407c478bd9Sstevel@tonic-gate number_to_scaled_string( 5417c478bd9Sstevel@tonic-gate numbuf_t buf, /* put the result here */ 5427c478bd9Sstevel@tonic-gate unsigned long long number, /* convert this number */ 5437c478bd9Sstevel@tonic-gate unsigned long long unit_from, /* number of bytes per input unit */ 5447c478bd9Sstevel@tonic-gate unsigned long long scale) /* 1024 (-h) or 1000 (-H) */ 5457c478bd9Sstevel@tonic-gate { 5467c478bd9Sstevel@tonic-gate unsigned long long save = 0; 5477c478bd9Sstevel@tonic-gate char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */ 5487c478bd9Sstevel@tonic-gate char *uom = M; /* unit of measurement, initially 'K' (=M[0]) */ 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if ((long long)number == (long long)-1) { 5517c478bd9Sstevel@tonic-gate (void) strcpy(buf, "-1"); 5527c478bd9Sstevel@tonic-gate return (buf); 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate /* 5567c478bd9Sstevel@tonic-gate * Convert number from unit_from to given scale (1024 or 1000) 5577c478bd9Sstevel@tonic-gate * This means multiply number with unit_from and divide by scale. 5587c478bd9Sstevel@tonic-gate * if number is large enough, we first divide and then multiply 5597c478bd9Sstevel@tonic-gate * to avoid an overflow 5607c478bd9Sstevel@tonic-gate * (large enough here means 100 (rather arbitrary value) 5617c478bd9Sstevel@tonic-gate * times scale in order to reduce rounding errors) 5627c478bd9Sstevel@tonic-gate * otherwise, we first multiply and then divide 5637c478bd9Sstevel@tonic-gate * to avoid an underflow 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate if (number >= 100L * scale) { 5667c478bd9Sstevel@tonic-gate number = number / scale; 5677c478bd9Sstevel@tonic-gate number = number * unit_from; 5687c478bd9Sstevel@tonic-gate } else { 5697c478bd9Sstevel@tonic-gate number = number * unit_from; 5707c478bd9Sstevel@tonic-gate number = number / scale; 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate /* 5747c478bd9Sstevel@tonic-gate * Now we have number as a count of scale units. 5757c478bd9Sstevel@tonic-gate * Stop scaling when we reached exa bytes, then something is 5767c478bd9Sstevel@tonic-gate * probably wrong with our number. 5777c478bd9Sstevel@tonic-gate */ 5787c478bd9Sstevel@tonic-gate while ((number >= scale) && (*uom != 'E')) { 5797c478bd9Sstevel@tonic-gate uom++; /* next unit of measurement */ 5807c478bd9Sstevel@tonic-gate save = number; 5817c478bd9Sstevel@tonic-gate number = (number + (scale / 2)) / scale; 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate /* check if we should output a decimal place after the point */ 5857c478bd9Sstevel@tonic-gate if (save && ((save / scale) < 10)) { 5867c478bd9Sstevel@tonic-gate /* sprintf() will round for us */ 5877c478bd9Sstevel@tonic-gate float fnum = (float)save / scale; 5887c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%4.1f%c", fnum, *uom); 5897c478bd9Sstevel@tonic-gate } else { 5907c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%4llu%c", number, *uom); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate return (buf); 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate static void 5967c478bd9Sstevel@tonic-gate printsize(blkcnt_t blocks, char *path) 5977c478bd9Sstevel@tonic-gate { 5987c478bd9Sstevel@tonic-gate if (hflg) { 5997c478bd9Sstevel@tonic-gate numbuf_t numbuf; 6007c478bd9Sstevel@tonic-gate unsigned long long scale = 1024L; 6011979231eSceastha (void) printf(FORMAT1, 6027c478bd9Sstevel@tonic-gate number_to_scaled_string(numbuf, blocks, DEV_BSIZE, scale), 6037c478bd9Sstevel@tonic-gate path); 6047c478bd9Sstevel@tonic-gate } else if (kflg) { 6051979231eSceastha (void) printf(FORMAT2, (long long)kb(blocks), path); 6061979231eSceastha } else if (mflg) { 6071979231eSceastha (void) printf(FORMAT2, (long long)mb(blocks), path); 6087c478bd9Sstevel@tonic-gate } else { 6091979231eSceastha (void) printf(FORMAT2, (long long)blocks, path); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate static void 6147c478bd9Sstevel@tonic-gate exitdu(int exitcode) 6157c478bd9Sstevel@tonic-gate { 6167c478bd9Sstevel@tonic-gate free(base); 6177c478bd9Sstevel@tonic-gate free(name); 6187c478bd9Sstevel@tonic-gate exit(exitcode); 6197c478bd9Sstevel@tonic-gate } 620