xref: /titanic_52/usr/src/cmd/du/du.c (revision 24430d07c1f5f389fff144c8deb136ebea2ed2c4)
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*24430d07SDale Ghent  * Copyright 2017 OmniTI Computer Consulting, Inc.  All rights reserved.
2315deec58Sceastha  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
271979231eSceastha /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
281979231eSceastha /*	  All Rights Reserved  	*/
291979231eSceastha 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * du -- summarize disk usage
32*24430d07SDale Ghent  *	du [-Adorx] [-a|-s] [-h|-k|-m] [-H|-L] [file...]
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/avl.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <dirent.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int		aflg = 0;
507c478bd9Sstevel@tonic-gate static int		rflg = 0;
517c478bd9Sstevel@tonic-gate static int		sflg = 0;
527c478bd9Sstevel@tonic-gate static int		kflg = 0;
531979231eSceastha static int		mflg = 0;
547c478bd9Sstevel@tonic-gate static int		oflg = 0;
557c478bd9Sstevel@tonic-gate static int		dflg = 0;
567c478bd9Sstevel@tonic-gate static int		hflg = 0;
57*24430d07SDale Ghent static int		Aflg = 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 /*
73*24430d07SDale Ghent  * Output formats. illumos 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 
120*24430d07SDale Ghent 	while ((c = getopt(argc, argv, "aAdhHkLmorsx")) != 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 
165*24430d07SDale Ghent 		case 'A':
166*24430d07SDale Ghent 			Aflg++;
167*24430d07SDale Ghent 			continue;
168*24430d07SDale Ghent 
1697c478bd9Sstevel@tonic-gate 		case 'H':
1707c478bd9Sstevel@tonic-gate 			Hflg++;
1717c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
1727c478bd9Sstevel@tonic-gate 			Lflg = 0;
1737c478bd9Sstevel@tonic-gate 			cmdarg++;
1747c478bd9Sstevel@tonic-gate 			continue;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case 'L':
1777c478bd9Sstevel@tonic-gate 			Lflg++;
1787c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
1797c478bd9Sstevel@tonic-gate 			Hflg = 0;
1807c478bd9Sstevel@tonic-gate 			cmdarg = 0;
1817c478bd9Sstevel@tonic-gate 			continue;
1827c478bd9Sstevel@tonic-gate 		case '?':
1837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
184*24430d07SDale Ghent 			    "usage: du [-Adorx] [-a|-s] [-h|-k|-m] [-H|-L] "
1857c478bd9Sstevel@tonic-gate 			    "[file...]\n"));
1867c478bd9Sstevel@tonic-gate 			exit(2);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	if (optind == argc) {
1897c478bd9Sstevel@tonic-gate 		argv = &dot;
1907c478bd9Sstevel@tonic-gate 		argc = 1;
1917c478bd9Sstevel@tonic-gate 		optind = 0;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* "-o" and "-s" don't make any sense together. */
1957c478bd9Sstevel@tonic-gate 	if (oflg && sflg)
1967c478bd9Sstevel@tonic-gate 		oflg = 0;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if ((base = (char *)calloc(base_len, sizeof (char))) == NULL) {
1997c478bd9Sstevel@tonic-gate 		perror("du");
2007c478bd9Sstevel@tonic-gate 		exit(1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	if ((name = (char *)calloc(name_len, sizeof (char))) == NULL) {
2037c478bd9Sstevel@tonic-gate 		perror("du");
2047c478bd9Sstevel@tonic-gate 		free(base);
2057c478bd9Sstevel@tonic-gate 		exit(1);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	do {
2087c478bd9Sstevel@tonic-gate 		if (optind < argc - 1) {
2097c478bd9Sstevel@tonic-gate 			pid = fork();
2107c478bd9Sstevel@tonic-gate 			if (pid == (pid_t)-1) {
2117c478bd9Sstevel@tonic-gate 				perror(gettext("du: No more processes"));
2127c478bd9Sstevel@tonic-gate 				exitdu(1);
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 			if (pid != 0) {
2157c478bd9Sstevel@tonic-gate 				while ((wpid = wait(&status)) != pid &&
2167c478bd9Sstevel@tonic-gate 				    wpid != (pid_t)-1)
2177c478bd9Sstevel@tonic-gate 					;
2187c478bd9Sstevel@tonic-gate 				if (pid != (pid_t)-1 && status != 0)
2197c478bd9Sstevel@tonic-gate 					retcode = 1;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 		if (optind == argc - 1 || pid == 0) {
2237c478bd9Sstevel@tonic-gate 			while (base_len < (strlen(argv[optind]) + 1)) {
2247c478bd9Sstevel@tonic-gate 				base_len = base_len * 2;
2257c478bd9Sstevel@tonic-gate 				if ((base = (char *)realloc(base, base_len *
2267c478bd9Sstevel@tonic-gate 				    sizeof (char))) == NULL) {
2277c478bd9Sstevel@tonic-gate 					if (rflg) {
2287c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2297c478bd9Sstevel@tonic-gate 						    "du: can't process %s"),
2307c478bd9Sstevel@tonic-gate 						    argv[optind]);
2317c478bd9Sstevel@tonic-gate 						perror("");
2327c478bd9Sstevel@tonic-gate 					}
2337c478bd9Sstevel@tonic-gate 					exitdu(1);
2347c478bd9Sstevel@tonic-gate 				}
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			if (base_len > name_len) {
2377c478bd9Sstevel@tonic-gate 				name_len = base_len;
2387c478bd9Sstevel@tonic-gate 				if ((name = (char *)realloc(name, name_len *
2397c478bd9Sstevel@tonic-gate 				    sizeof (char))) == NULL) {
2407c478bd9Sstevel@tonic-gate 					if (rflg) {
2417c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2427c478bd9Sstevel@tonic-gate 						    "du: can't process %s"),
2437c478bd9Sstevel@tonic-gate 						    argv[optind]);
2447c478bd9Sstevel@tonic-gate 						perror("");
2457c478bd9Sstevel@tonic-gate 					}
2467c478bd9Sstevel@tonic-gate 					exitdu(1);
2477c478bd9Sstevel@tonic-gate 				}
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			(void) strcpy(base, argv[optind]);
2507c478bd9Sstevel@tonic-gate 			(void) strcpy(name, argv[optind]);
2517c478bd9Sstevel@tonic-gate 			if (np = strrchr(name, '/')) {
2527c478bd9Sstevel@tonic-gate 				*np++ = '\0';
2537c478bd9Sstevel@tonic-gate 				if (chdir(*name ? name : "/") < 0) {
2547c478bd9Sstevel@tonic-gate 					if (rflg) {
2557c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "du: ");
2567c478bd9Sstevel@tonic-gate 						perror(*name ? name : "/");
2577c478bd9Sstevel@tonic-gate 						exitdu(1);
2587c478bd9Sstevel@tonic-gate 					}
2597c478bd9Sstevel@tonic-gate 					exitdu(0);
2607c478bd9Sstevel@tonic-gate 				}
2617c478bd9Sstevel@tonic-gate 			} else
2627c478bd9Sstevel@tonic-gate 				np = base;
2637c478bd9Sstevel@tonic-gate 			blocks = descend(*np ? np : ".", 0, &retcode,
2647c478bd9Sstevel@tonic-gate 			    (dev_t)0);
2657c478bd9Sstevel@tonic-gate 			if (sflg)
2667c478bd9Sstevel@tonic-gate 				printsize(blocks, base);
2677c478bd9Sstevel@tonic-gate 			if (optind < argc - 1)
2687c478bd9Sstevel@tonic-gate 				exitdu(retcode);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		optind++;
2717c478bd9Sstevel@tonic-gate 	} while (optind < argc);
2727c478bd9Sstevel@tonic-gate 	exitdu(retcode);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (retcode);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * descend recursively, adding up the allocated blocks.
2797c478bd9Sstevel@tonic-gate  * If curname is NULL, curfd is used.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static u_longlong_t
2827c478bd9Sstevel@tonic-gate descend(char *curname, int curfd, int *retcode, dev_t device)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	static DIR		*dirp = NULL;
2857c478bd9Sstevel@tonic-gate 	char			*ebase0, *ebase;
2867c478bd9Sstevel@tonic-gate 	struct stat		stb, stb1;
2877c478bd9Sstevel@tonic-gate 	int			i, j, ret, fd, tmpflg;
28815deec58Sceastha 	int			follow_symlinks;
2897c478bd9Sstevel@tonic-gate 	blkcnt_t		blocks = 0;
2907c478bd9Sstevel@tonic-gate 	off_t			curoff = 0;
2917c478bd9Sstevel@tonic-gate 	ptrdiff_t		offset;
2927c478bd9Sstevel@tonic-gate 	ptrdiff_t		offset0;
2937c478bd9Sstevel@tonic-gate 	struct dirent		*dp;
2947c478bd9Sstevel@tonic-gate 	char			dirbuf[PATH_MAX + 1];
2957c478bd9Sstevel@tonic-gate 	u_longlong_t		retval;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	ebase0 = ebase = strchr(base, 0);
2987c478bd9Sstevel@tonic-gate 	if (ebase > base && ebase[-1] == '/')
2997c478bd9Sstevel@tonic-gate 		ebase--;
3007c478bd9Sstevel@tonic-gate 	offset = ebase - base;
3017c478bd9Sstevel@tonic-gate 	offset0 = ebase0 - base;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (curname)
3047c478bd9Sstevel@tonic-gate 		curfd = AT_FDCWD;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If neither a -L or a -H was specified, don't follow symlinks.
3087c478bd9Sstevel@tonic-gate 	 * If a -H was specified, don't follow symlinks if the file is
3097c478bd9Sstevel@tonic-gate 	 * not a command line argument.
3107c478bd9Sstevel@tonic-gate 	 */
31115deec58Sceastha 	follow_symlinks = (Lflg || (Hflg && cmdarg));
31215deec58Sceastha 	if (follow_symlinks) {
3137c478bd9Sstevel@tonic-gate 		i = fstatat(curfd, curname, &stb, 0);
3147c478bd9Sstevel@tonic-gate 		j = fstatat(curfd, curname, &stb1, AT_SYMLINK_NOFOLLOW);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		/*
3177c478bd9Sstevel@tonic-gate 		 * Make sure any files encountered while traversing the
3187c478bd9Sstevel@tonic-gate 		 * hierarchy are not considered command line arguments.
3197c478bd9Sstevel@tonic-gate 		 */
3207c478bd9Sstevel@tonic-gate 		if (Hflg) {
3217c478bd9Sstevel@tonic-gate 			cmdarg = 0;
3227c478bd9Sstevel@tonic-gate 		}
32315deec58Sceastha 	} else {
32415deec58Sceastha 		i = fstatat(curfd, curname, &stb, AT_SYMLINK_NOFOLLOW);
32515deec58Sceastha 		j = 0;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if ((i < 0) || (j < 0)) {
3297c478bd9Sstevel@tonic-gate 		if (rflg) {
3307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
3317c478bd9Sstevel@tonic-gate 			perror(base);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		/*
3357c478bd9Sstevel@tonic-gate 		 * POSIX states that non-zero status codes are only set
3367c478bd9Sstevel@tonic-gate 		 * when an error message is printed out on stderr
3377c478bd9Sstevel@tonic-gate 		 */
3387c478bd9Sstevel@tonic-gate 		*retcode = (rflg ? 1 : 0);
3397c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
3407c478bd9Sstevel@tonic-gate 		return (0);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	if (device) {
3437c478bd9Sstevel@tonic-gate 		if (dflg && stb.st_dev != device) {
3447c478bd9Sstevel@tonic-gate 			*ebase0 = 0;
3457c478bd9Sstevel@tonic-gate 			return (0);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	else
3497c478bd9Sstevel@tonic-gate 		device = stb.st_dev;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * If following links (-L) we need to keep track of all inodes
3537c478bd9Sstevel@tonic-gate 	 * visited so they are only visited/reported once and cycles
3547c478bd9Sstevel@tonic-gate 	 * are avoided.  Otherwise, only keep track of files which are
3557c478bd9Sstevel@tonic-gate 	 * hard links so they only get reported once, and of directories
3567c478bd9Sstevel@tonic-gate 	 * so we don't report a directory and its hierarchy more than
3577c478bd9Sstevel@tonic-gate 	 * once in the special case in which it lies under the
3587c478bd9Sstevel@tonic-gate 	 * hierarchy of a directory which is a hard link.
3597c478bd9Sstevel@tonic-gate 	 * Note:  Files with multiple links should only be counted
3607c478bd9Sstevel@tonic-gate 	 * once.  Since each inode could possibly be referenced by a
3617c478bd9Sstevel@tonic-gate 	 * symbolic link, we need to keep track of all inodes when -L
3627c478bd9Sstevel@tonic-gate 	 * is specified.
3637c478bd9Sstevel@tonic-gate 	 */
36415deec58Sceastha 	if (Lflg || ((stb.st_mode & S_IFMT) == S_IFDIR) ||
3657c478bd9Sstevel@tonic-gate 	    (stb.st_nlink > 1)) {
3667c478bd9Sstevel@tonic-gate 		int rc;
3677c478bd9Sstevel@tonic-gate 		if ((rc = add_tnode(&tree, stb.st_dev, stb.st_ino)) != 1) {
3687c478bd9Sstevel@tonic-gate 			if (rc == 0) {
3697c478bd9Sstevel@tonic-gate 				/*
3707c478bd9Sstevel@tonic-gate 				 * This hierarchy, or file with multiple
3717c478bd9Sstevel@tonic-gate 				 * links, has already been visited/reported.
3727c478bd9Sstevel@tonic-gate 				 */
3737c478bd9Sstevel@tonic-gate 				return (0);
3747c478bd9Sstevel@tonic-gate 			} else {
3757c478bd9Sstevel@tonic-gate 				/*
3767c478bd9Sstevel@tonic-gate 				 * An error occurred while trying to add the
3777c478bd9Sstevel@tonic-gate 				 * node to the tree.
3787c478bd9Sstevel@tonic-gate 				 */
3797c478bd9Sstevel@tonic-gate 				if (rflg) {
3807c478bd9Sstevel@tonic-gate 					perror("du");
3817c478bd9Sstevel@tonic-gate 				}
3827c478bd9Sstevel@tonic-gate 				exitdu(1);
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
386*24430d07SDale Ghent 	blocks = Aflg ? stb.st_size : stb.st_blocks;
387*24430d07SDale Ghent 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * If there are extended attributes on the current file, add their
39015deec58Sceastha 	 * block usage onto the block count.  Note: Since pathconf() always
39115deec58Sceastha 	 * follows symlinks, only test for extended attributes using pathconf()
39215deec58Sceastha 	 * if we are following symlinks or the current file is not a symlink.
3937c478bd9Sstevel@tonic-gate 	 */
39415deec58Sceastha 	if (curname && (follow_symlinks ||
39515deec58Sceastha 	    ((stb.st_mode & S_IFMT) != S_IFLNK)) &&
39615deec58Sceastha 	    pathconf(curname, _PC_XATTR_EXISTS) == 1) {
3977c478bd9Sstevel@tonic-gate 		if ((fd = attropen(curname, ".", O_RDONLY)) < 0) {
3987c478bd9Sstevel@tonic-gate 			if (rflg)
3997c478bd9Sstevel@tonic-gate 				perror(gettext(
4007c478bd9Sstevel@tonic-gate 				    "du: can't access extended attributes"));
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		else
4037c478bd9Sstevel@tonic-gate 		{
4047c478bd9Sstevel@tonic-gate 			tmpflg = sflg;
4057c478bd9Sstevel@tonic-gate 			sflg = 1;
4067c478bd9Sstevel@tonic-gate 			blocks += descend(NULL, fd, retcode, device);
4077c478bd9Sstevel@tonic-gate 			sflg = tmpflg;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	if ((stb.st_mode & S_IFMT) != S_IFDIR) {
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * Don't print twice: if sflg, file will get printed in main().
4137c478bd9Sstevel@tonic-gate 		 * Otherwise, level == 0 means this file is listed on the
4147c478bd9Sstevel@tonic-gate 		 * command line, so print here; aflg means print all files.
4157c478bd9Sstevel@tonic-gate 		 */
4167c478bd9Sstevel@tonic-gate 		if (sflg == 0 && (aflg || level == 0))
4177c478bd9Sstevel@tonic-gate 			printsize(blocks, base);
4187c478bd9Sstevel@tonic-gate 		return (blocks);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	if (dirp != NULL)
4217c478bd9Sstevel@tonic-gate 		/*
4227c478bd9Sstevel@tonic-gate 		 * Close the parent directory descriptor, we will reopen
4237c478bd9Sstevel@tonic-gate 		 * the directory when we pop up from this level of the
4247c478bd9Sstevel@tonic-gate 		 * recursion.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
4277c478bd9Sstevel@tonic-gate 	if (curname == NULL)
4287c478bd9Sstevel@tonic-gate 		dirp = fdopendir(curfd);
4297c478bd9Sstevel@tonic-gate 	else
4307c478bd9Sstevel@tonic-gate 		dirp = opendir(curname);
4317c478bd9Sstevel@tonic-gate 	if (dirp == NULL) {
4327c478bd9Sstevel@tonic-gate 		if (rflg) {
4337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
4347c478bd9Sstevel@tonic-gate 			perror(base);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		*retcode = 1;
4377c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
4387c478bd9Sstevel@tonic-gate 		return (0);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	level++;
4417c478bd9Sstevel@tonic-gate 	if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode))) {
4427c478bd9Sstevel@tonic-gate 		if (getcwd(dirbuf, PATH_MAX) == NULL) {
4437c478bd9Sstevel@tonic-gate 			if (rflg) {
4447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "du: ");
4457c478bd9Sstevel@tonic-gate 				perror(base);
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 			exitdu(1);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	if ((curname ? (chdir(curname) < 0) : (fchdir(curfd) < 0))) {
4517c478bd9Sstevel@tonic-gate 		if (rflg) {
4527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
4537c478bd9Sstevel@tonic-gate 			perror(base);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		*retcode = 1;
4567c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
4577c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
4587c478bd9Sstevel@tonic-gate 		dirp = NULL;
4597c478bd9Sstevel@tonic-gate 		level--;
4607c478bd9Sstevel@tonic-gate 		return (0);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
4637c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
4647c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
4657c478bd9Sstevel@tonic-gate 			continue;
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * we're about to append "/" + dp->d_name
4687c478bd9Sstevel@tonic-gate 		 * onto end of base; make sure there's enough
4697c478bd9Sstevel@tonic-gate 		 * space
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		while ((offset + strlen(dp->d_name) + 2) > base_len) {
4727c478bd9Sstevel@tonic-gate 			base_len = base_len * 2;
4737c478bd9Sstevel@tonic-gate 			if ((base = (char *)realloc(base,
4747c478bd9Sstevel@tonic-gate 			    base_len * sizeof (char))) == NULL) {
4757c478bd9Sstevel@tonic-gate 				if (rflg) {
4767c478bd9Sstevel@tonic-gate 					perror("du");
4777c478bd9Sstevel@tonic-gate 				}
4787c478bd9Sstevel@tonic-gate 				exitdu(1);
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 			ebase = base + offset;
4817c478bd9Sstevel@tonic-gate 			ebase0 = base + offset0;
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
4847c478bd9Sstevel@tonic-gate 		(void) sprintf(ebase, "/%s", dp->d_name);
4857c478bd9Sstevel@tonic-gate 		curoff = telldir(dirp);
4867c478bd9Sstevel@tonic-gate 		retval = descend(ebase + 1, 0, retcode, device);
4877c478bd9Sstevel@tonic-gate 			/* base may have been moved via realloc in descend() */
4887c478bd9Sstevel@tonic-gate 		ebase = base + offset;
4897c478bd9Sstevel@tonic-gate 		ebase0 = base + offset0;
4907c478bd9Sstevel@tonic-gate 		*ebase = 0;
4917c478bd9Sstevel@tonic-gate 		blocks += retval;
4927c478bd9Sstevel@tonic-gate 		if (dirp == NULL) {
4937c478bd9Sstevel@tonic-gate 			if ((dirp = opendir(".")) == NULL) {
4947c478bd9Sstevel@tonic-gate 				if (rflg) {
4957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4967c478bd9Sstevel@tonic-gate 					    gettext("du: Can't reopen in "));
4977c478bd9Sstevel@tonic-gate 					perror(base);
4987c478bd9Sstevel@tonic-gate 				}
4997c478bd9Sstevel@tonic-gate 				*retcode = 1;
5007c478bd9Sstevel@tonic-gate 				level--;
5017c478bd9Sstevel@tonic-gate 				return (0);
5027c478bd9Sstevel@tonic-gate 			}
5037c478bd9Sstevel@tonic-gate 			seekdir(dirp, curoff);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5077c478bd9Sstevel@tonic-gate 	level--;
5087c478bd9Sstevel@tonic-gate 	dirp = NULL;
5097c478bd9Sstevel@tonic-gate 	if (sflg == 0)
5107c478bd9Sstevel@tonic-gate 		printsize(blocks, base);
5117c478bd9Sstevel@tonic-gate 	if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode)))
5127c478bd9Sstevel@tonic-gate 		ret = chdir(dirbuf);
5137c478bd9Sstevel@tonic-gate 	else
5147c478bd9Sstevel@tonic-gate 		ret = chdir("..");
5157c478bd9Sstevel@tonic-gate 	if (ret < 0) {
5167c478bd9Sstevel@tonic-gate 		if (rflg) {
5177c478bd9Sstevel@tonic-gate 			(void) sprintf(strchr(base, '\0'), "/..");
5187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5197c478bd9Sstevel@tonic-gate 			    gettext("du: Can't change dir to '..' in "));
5207c478bd9Sstevel@tonic-gate 			perror(base);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		exitdu(1);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	*ebase0 = 0;
5257c478bd9Sstevel@tonic-gate 	if (oflg)
5267c478bd9Sstevel@tonic-gate 		return (0);
5277c478bd9Sstevel@tonic-gate 	else
5287c478bd9Sstevel@tonic-gate 		return (blocks);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
5337c478bd9Sstevel@tonic-gate  * result in the caller-supplied buffer.
5347c478bd9Sstevel@tonic-gate  * The given number is in units of "unit_from" size,
5357c478bd9Sstevel@tonic-gate  * this will first be converted to a number in 1024 or 1000 byte size,
5367c478bd9Sstevel@tonic-gate  * depending on the scaling factor.
5377c478bd9Sstevel@tonic-gate  * Then the number is scaled down until it is small enough to be in a good
5387c478bd9Sstevel@tonic-gate  * human readable format i.e. in the range 0 thru scale-1.
5397c478bd9Sstevel@tonic-gate  * If it's smaller than 10 there's room enough to provide one decimal place.
5407c478bd9Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
5417c478bd9Sstevel@tonic-gate  * converted to "-1".
5427c478bd9Sstevel@tonic-gate  * Returns a pointer to the caller-supplied buffer.
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate static char *
5457c478bd9Sstevel@tonic-gate number_to_scaled_string(
5467c478bd9Sstevel@tonic-gate 	numbuf_t buf,			/* put the result here */
5477c478bd9Sstevel@tonic-gate 	unsigned long long number,	/* convert this number */
5487c478bd9Sstevel@tonic-gate 	unsigned long long unit_from,	/* number of bytes per input unit */
5497c478bd9Sstevel@tonic-gate 	unsigned long long scale)	/* 1024 (-h)  or 1000 (-H) */
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	unsigned long long save = 0;
5527c478bd9Sstevel@tonic-gate 	char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */
5537c478bd9Sstevel@tonic-gate 	char *uom = M;    /* unit of measurement, initially 'K' (=M[0]) */
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
5567c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, "-1");
5577c478bd9Sstevel@tonic-gate 		return (buf);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Convert number from unit_from to given scale (1024 or 1000)
5627c478bd9Sstevel@tonic-gate 	 * This means multiply number with unit_from and divide by scale.
5637c478bd9Sstevel@tonic-gate 	 * if number is large enough, we first divide and then multiply
5647c478bd9Sstevel@tonic-gate 	 * 	to avoid an overflow
5657c478bd9Sstevel@tonic-gate 	 * 	(large enough here means 100 (rather arbitrary value)
5667c478bd9Sstevel@tonic-gate 	 *	times scale in order to reduce rounding errors)
5677c478bd9Sstevel@tonic-gate 	 * otherwise, we first multiply and then divide
5687c478bd9Sstevel@tonic-gate 	 * 	to avoid an underflow
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if (number >= 100L * scale) {
5717c478bd9Sstevel@tonic-gate 		number = number / scale;
5727c478bd9Sstevel@tonic-gate 		number = number * unit_from;
5737c478bd9Sstevel@tonic-gate 	} else {
5747c478bd9Sstevel@tonic-gate 		number = number * unit_from;
5757c478bd9Sstevel@tonic-gate 		number = number / scale;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
5807c478bd9Sstevel@tonic-gate 	 * Stop scaling when we reached exa bytes, then something is
5817c478bd9Sstevel@tonic-gate 	 * probably wrong with our number.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	while ((number >= scale) && (*uom != 'E')) {
5847c478bd9Sstevel@tonic-gate 		uom++; /* next unit of measurement */
5857c478bd9Sstevel@tonic-gate 		save = number;
5867c478bd9Sstevel@tonic-gate 		number = (number + (scale / 2)) / scale;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
5907c478bd9Sstevel@tonic-gate 	if (save && ((save / scale) < 10)) {
5917c478bd9Sstevel@tonic-gate 		/* sprintf() will round for us */
5927c478bd9Sstevel@tonic-gate 		float fnum = (float)save / scale;
5937c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%4.1f%c", fnum, *uom);
5947c478bd9Sstevel@tonic-gate 	} else {
5957c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "%4llu%c", number, *uom);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	return (buf);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate static void
6017c478bd9Sstevel@tonic-gate printsize(blkcnt_t blocks, char *path)
6027c478bd9Sstevel@tonic-gate {
603*24430d07SDale Ghent 	u_longlong_t bsize;
604*24430d07SDale Ghent 
605*24430d07SDale Ghent 	bsize = Aflg ? 1 : DEV_BSIZE;
606*24430d07SDale Ghent 
6077c478bd9Sstevel@tonic-gate 	if (hflg) {
6087c478bd9Sstevel@tonic-gate 		numbuf_t numbuf;
6097c478bd9Sstevel@tonic-gate 		unsigned long long scale = 1024L;
6101979231eSceastha 		(void) printf(FORMAT1,
611*24430d07SDale Ghent 		    number_to_scaled_string(numbuf, blocks, bsize, scale),
6127c478bd9Sstevel@tonic-gate 		    path);
6137c478bd9Sstevel@tonic-gate 	} else if (kflg) {
6141979231eSceastha 		(void) printf(FORMAT2, (long long)kb(blocks), path);
6151979231eSceastha 	} else if (mflg) {
6161979231eSceastha 		(void) printf(FORMAT2, (long long)mb(blocks), path);
6177c478bd9Sstevel@tonic-gate 	} else {
6181979231eSceastha 		(void) printf(FORMAT2, (long long)blocks, path);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate static void
6237c478bd9Sstevel@tonic-gate exitdu(int exitcode)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	free(base);
6267c478bd9Sstevel@tonic-gate 	free(name);
6277c478bd9Sstevel@tonic-gate 	exit(exitcode);
6287c478bd9Sstevel@tonic-gate }
629