xref: /titanic_52/usr/src/cmd/du/du.c (revision fc30d4661fdf50c4c69dacbc572af35439c28cd8)
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 /*
2224430d07SDale 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.
25*fc30d466SJason King  * Copyright 2017 Jason King
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
281979231eSceastha /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
291979231eSceastha /*	  All Rights Reserved  	*/
301979231eSceastha 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * du -- summarize disk usage
3324430d07SDale Ghent  *	du [-Adorx] [-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;
5824430d07SDale Ghent static int		Aflg = 0;
597c478bd9Sstevel@tonic-gate static int		Hflg = 0;
607c478bd9Sstevel@tonic-gate static int		Lflg = 0;
617c478bd9Sstevel@tonic-gate static int		cmdarg = 0;	/* Command line argument */
627c478bd9Sstevel@tonic-gate static char		*dot = ".";
637c478bd9Sstevel@tonic-gate static int		level = 0;	/* Level of recursion */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char		*base;
667c478bd9Sstevel@tonic-gate static char		*name;
677c478bd9Sstevel@tonic-gate static size_t		base_len = PATH_MAX + 1;    /* # of chars for base */
687c478bd9Sstevel@tonic-gate static size_t		name_len = PATH_MAX + 1;    /* # of chars for name */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
7124430d07SDale Ghent  * Output formats. illumos uses a tab as separator, XPG4 a space.
721979231eSceastha  */
731979231eSceastha #ifdef XPG4
741979231eSceastha #define	FORMAT1	"%s %s\n"
751979231eSceastha #define	FORMAT2	"%lld %s\n"
761979231eSceastha #else
771979231eSceastha #define	FORMAT1	"%s\t%s\n"
781979231eSceastha #define	FORMAT2	"%lld\t%s\n"
791979231eSceastha #endif
801979231eSceastha 
811979231eSceastha /*
827c478bd9Sstevel@tonic-gate  * convert DEV_BSIZE blocks to K blocks
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate #define	DEV_BSIZE	512
857c478bd9Sstevel@tonic-gate #define	DEV_KSHIFT	1
861979231eSceastha #define	DEV_MSHIFT	11
877c478bd9Sstevel@tonic-gate #define	kb(n)		(((u_longlong_t)(n)) >> DEV_KSHIFT)
881979231eSceastha #define	mb(n)		(((u_longlong_t)(n)) >> DEV_MSHIFT)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate long	wait();
917c478bd9Sstevel@tonic-gate static u_longlong_t 	descend(char *curname, int curfd, int *retcode,
927c478bd9Sstevel@tonic-gate 			    dev_t device);
937c478bd9Sstevel@tonic-gate static void		printsize(blkcnt_t blocks, char *path);
947c478bd9Sstevel@tonic-gate static void		exitdu(int exitcode);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static avl_tree_t	*tree = NULL;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate int
997c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	blkcnt_t	blocks = 0;
1027c478bd9Sstevel@tonic-gate 	int		c;
1037c478bd9Sstevel@tonic-gate 	extern int	optind;
1047c478bd9Sstevel@tonic-gate 	char		*np;
1057c478bd9Sstevel@tonic-gate 	pid_t		pid, wpid;
1067c478bd9Sstevel@tonic-gate 	int		status, retcode = 0;
1077c478bd9Sstevel@tonic-gate 	setbuf(stderr, NULL);
1087c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1097c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1107c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef XPG4
1157c478bd9Sstevel@tonic-gate 	rflg++;		/* "-r" is not an option but ON always */
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate 
11824430d07SDale Ghent 	while ((c = getopt(argc, argv, "aAdhHkLmorsx")) != EOF)
1197c478bd9Sstevel@tonic-gate 		switch (c) {
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		case 'a':
1227c478bd9Sstevel@tonic-gate 			aflg++;
1237c478bd9Sstevel@tonic-gate 			continue;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		case 'h':
1267c478bd9Sstevel@tonic-gate 			hflg++;
1271979231eSceastha 			kflg = 0;
1281979231eSceastha 			mflg = 0;
1297c478bd9Sstevel@tonic-gate 			continue;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		case 'r':
1327c478bd9Sstevel@tonic-gate 			rflg++;
1337c478bd9Sstevel@tonic-gate 			continue;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		case 's':
1367c478bd9Sstevel@tonic-gate 			sflg++;
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		case 'k':
1407c478bd9Sstevel@tonic-gate 			kflg++;
1411979231eSceastha 			hflg = 0;
1421979231eSceastha 			mflg = 0;
1431979231eSceastha 			continue;
1441979231eSceastha 
1451979231eSceastha 		case 'm':
1461979231eSceastha 			mflg++;
1471979231eSceastha 			hflg = 0;
1481979231eSceastha 			kflg = 0;
1497c478bd9Sstevel@tonic-gate 			continue;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		case 'o':
1527c478bd9Sstevel@tonic-gate 			oflg++;
1537c478bd9Sstevel@tonic-gate 			continue;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		case 'd':
1567c478bd9Sstevel@tonic-gate 			dflg++;
1577c478bd9Sstevel@tonic-gate 			continue;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		case 'x':
1607c478bd9Sstevel@tonic-gate 			dflg++;
1617c478bd9Sstevel@tonic-gate 			continue;
1627c478bd9Sstevel@tonic-gate 
16324430d07SDale Ghent 		case 'A':
16424430d07SDale Ghent 			Aflg++;
16524430d07SDale Ghent 			continue;
16624430d07SDale Ghent 
1677c478bd9Sstevel@tonic-gate 		case 'H':
1687c478bd9Sstevel@tonic-gate 			Hflg++;
1697c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
1707c478bd9Sstevel@tonic-gate 			Lflg = 0;
1717c478bd9Sstevel@tonic-gate 			cmdarg++;
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		case 'L':
1757c478bd9Sstevel@tonic-gate 			Lflg++;
1767c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
1777c478bd9Sstevel@tonic-gate 			Hflg = 0;
1787c478bd9Sstevel@tonic-gate 			cmdarg = 0;
1797c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		case '?':
1817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
18224430d07SDale Ghent 			    "usage: du [-Adorx] [-a|-s] [-h|-k|-m] [-H|-L] "
1837c478bd9Sstevel@tonic-gate 			    "[file...]\n"));
1847c478bd9Sstevel@tonic-gate 			exit(2);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 	if (optind == argc) {
1877c478bd9Sstevel@tonic-gate 		argv = &dot;
1887c478bd9Sstevel@tonic-gate 		argc = 1;
1897c478bd9Sstevel@tonic-gate 		optind = 0;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* "-o" and "-s" don't make any sense together. */
1937c478bd9Sstevel@tonic-gate 	if (oflg && sflg)
1947c478bd9Sstevel@tonic-gate 		oflg = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if ((base = (char *)calloc(base_len, sizeof (char))) == NULL) {
1977c478bd9Sstevel@tonic-gate 		perror("du");
1987c478bd9Sstevel@tonic-gate 		exit(1);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	if ((name = (char *)calloc(name_len, sizeof (char))) == NULL) {
2017c478bd9Sstevel@tonic-gate 		perror("du");
2027c478bd9Sstevel@tonic-gate 		free(base);
2037c478bd9Sstevel@tonic-gate 		exit(1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	do {
2067c478bd9Sstevel@tonic-gate 		if (optind < argc - 1) {
2077c478bd9Sstevel@tonic-gate 			pid = fork();
2087c478bd9Sstevel@tonic-gate 			if (pid == (pid_t)-1) {
2097c478bd9Sstevel@tonic-gate 				perror(gettext("du: No more processes"));
2107c478bd9Sstevel@tonic-gate 				exitdu(1);
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 			if (pid != 0) {
2137c478bd9Sstevel@tonic-gate 				while ((wpid = wait(&status)) != pid &&
2147c478bd9Sstevel@tonic-gate 				    wpid != (pid_t)-1)
2157c478bd9Sstevel@tonic-gate 					;
2167c478bd9Sstevel@tonic-gate 				if (pid != (pid_t)-1 && status != 0)
2177c478bd9Sstevel@tonic-gate 					retcode = 1;
2187c478bd9Sstevel@tonic-gate 			}
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		if (optind == argc - 1 || pid == 0) {
2217c478bd9Sstevel@tonic-gate 			while (base_len < (strlen(argv[optind]) + 1)) {
2227c478bd9Sstevel@tonic-gate 				base_len = base_len * 2;
2237c478bd9Sstevel@tonic-gate 				if ((base = (char *)realloc(base, base_len *
2247c478bd9Sstevel@tonic-gate 				    sizeof (char))) == NULL) {
2257c478bd9Sstevel@tonic-gate 					if (rflg) {
2267c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2277c478bd9Sstevel@tonic-gate 						    "du: can't process %s"),
2287c478bd9Sstevel@tonic-gate 						    argv[optind]);
2297c478bd9Sstevel@tonic-gate 						perror("");
2307c478bd9Sstevel@tonic-gate 					}
2317c478bd9Sstevel@tonic-gate 					exitdu(1);
2327c478bd9Sstevel@tonic-gate 				}
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 			if (base_len > name_len) {
2357c478bd9Sstevel@tonic-gate 				name_len = base_len;
2367c478bd9Sstevel@tonic-gate 				if ((name = (char *)realloc(name, name_len *
2377c478bd9Sstevel@tonic-gate 				    sizeof (char))) == NULL) {
2387c478bd9Sstevel@tonic-gate 					if (rflg) {
2397c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2407c478bd9Sstevel@tonic-gate 						    "du: can't process %s"),
2417c478bd9Sstevel@tonic-gate 						    argv[optind]);
2427c478bd9Sstevel@tonic-gate 						perror("");
2437c478bd9Sstevel@tonic-gate 					}
2447c478bd9Sstevel@tonic-gate 					exitdu(1);
2457c478bd9Sstevel@tonic-gate 				}
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 			(void) strcpy(base, argv[optind]);
2487c478bd9Sstevel@tonic-gate 			(void) strcpy(name, argv[optind]);
2497c478bd9Sstevel@tonic-gate 			if (np = strrchr(name, '/')) {
2507c478bd9Sstevel@tonic-gate 				*np++ = '\0';
2517c478bd9Sstevel@tonic-gate 				if (chdir(*name ? name : "/") < 0) {
2527c478bd9Sstevel@tonic-gate 					if (rflg) {
2537c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "du: ");
2547c478bd9Sstevel@tonic-gate 						perror(*name ? name : "/");
2557c478bd9Sstevel@tonic-gate 						exitdu(1);
2567c478bd9Sstevel@tonic-gate 					}
2577c478bd9Sstevel@tonic-gate 					exitdu(0);
2587c478bd9Sstevel@tonic-gate 				}
2597c478bd9Sstevel@tonic-gate 			} else
2607c478bd9Sstevel@tonic-gate 				np = base;
2617c478bd9Sstevel@tonic-gate 			blocks = descend(*np ? np : ".", 0, &retcode,
2627c478bd9Sstevel@tonic-gate 			    (dev_t)0);
2637c478bd9Sstevel@tonic-gate 			if (sflg)
2647c478bd9Sstevel@tonic-gate 				printsize(blocks, base);
2657c478bd9Sstevel@tonic-gate 			if (optind < argc - 1)
2667c478bd9Sstevel@tonic-gate 				exitdu(retcode);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		optind++;
2697c478bd9Sstevel@tonic-gate 	} while (optind < argc);
2707c478bd9Sstevel@tonic-gate 	exitdu(retcode);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	return (retcode);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * descend recursively, adding up the allocated blocks.
2777c478bd9Sstevel@tonic-gate  * If curname is NULL, curfd is used.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate static u_longlong_t
2807c478bd9Sstevel@tonic-gate descend(char *curname, int curfd, int *retcode, dev_t device)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	static DIR		*dirp = NULL;
2837c478bd9Sstevel@tonic-gate 	char			*ebase0, *ebase;
2847c478bd9Sstevel@tonic-gate 	struct stat		stb, stb1;
2857c478bd9Sstevel@tonic-gate 	int			i, j, ret, fd, tmpflg;
28615deec58Sceastha 	int			follow_symlinks;
2877c478bd9Sstevel@tonic-gate 	blkcnt_t		blocks = 0;
2887c478bd9Sstevel@tonic-gate 	off_t			curoff = 0;
2897c478bd9Sstevel@tonic-gate 	ptrdiff_t		offset;
2907c478bd9Sstevel@tonic-gate 	ptrdiff_t		offset0;
2917c478bd9Sstevel@tonic-gate 	struct dirent		*dp;
2927c478bd9Sstevel@tonic-gate 	char			dirbuf[PATH_MAX + 1];
2937c478bd9Sstevel@tonic-gate 	u_longlong_t		retval;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	ebase0 = ebase = strchr(base, 0);
2967c478bd9Sstevel@tonic-gate 	if (ebase > base && ebase[-1] == '/')
2977c478bd9Sstevel@tonic-gate 		ebase--;
2987c478bd9Sstevel@tonic-gate 	offset = ebase - base;
2997c478bd9Sstevel@tonic-gate 	offset0 = ebase0 - base;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (curname)
3027c478bd9Sstevel@tonic-gate 		curfd = AT_FDCWD;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * If neither a -L or a -H was specified, don't follow symlinks.
3067c478bd9Sstevel@tonic-gate 	 * If a -H was specified, don't follow symlinks if the file is
3077c478bd9Sstevel@tonic-gate 	 * not a command line argument.
3087c478bd9Sstevel@tonic-gate 	 */
30915deec58Sceastha 	follow_symlinks = (Lflg || (Hflg && cmdarg));
31015deec58Sceastha 	if (follow_symlinks) {
3117c478bd9Sstevel@tonic-gate 		i = fstatat(curfd, curname, &stb, 0);
3127c478bd9Sstevel@tonic-gate 		j = fstatat(curfd, curname, &stb1, AT_SYMLINK_NOFOLLOW);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Make sure any files encountered while traversing the
3167c478bd9Sstevel@tonic-gate 		 * hierarchy are not considered command line arguments.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if (Hflg) {
3197c478bd9Sstevel@tonic-gate 			cmdarg = 0;
3207c478bd9Sstevel@tonic-gate 		}
32115deec58Sceastha 	} else {
32215deec58Sceastha 		i = fstatat(curfd, curname, &stb, AT_SYMLINK_NOFOLLOW);
32315deec58Sceastha 		j = 0;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if ((i < 0) || (j < 0)) {
3277c478bd9Sstevel@tonic-gate 		if (rflg) {
3287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
3297c478bd9Sstevel@tonic-gate 			perror(base);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		/*
3337c478bd9Sstevel@tonic-gate 		 * POSIX states that non-zero status codes are only set
3347c478bd9Sstevel@tonic-gate 		 * when an error message is printed out on stderr
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		*retcode = (rflg ? 1 : 0);
3377c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
3387c478bd9Sstevel@tonic-gate 		return (0);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	if (device) {
3417c478bd9Sstevel@tonic-gate 		if (dflg && stb.st_dev != device) {
3427c478bd9Sstevel@tonic-gate 			*ebase0 = 0;
3437c478bd9Sstevel@tonic-gate 			return (0);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	else
3477c478bd9Sstevel@tonic-gate 		device = stb.st_dev;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * If following links (-L) we need to keep track of all inodes
3517c478bd9Sstevel@tonic-gate 	 * visited so they are only visited/reported once and cycles
3527c478bd9Sstevel@tonic-gate 	 * are avoided.  Otherwise, only keep track of files which are
3537c478bd9Sstevel@tonic-gate 	 * hard links so they only get reported once, and of directories
3547c478bd9Sstevel@tonic-gate 	 * so we don't report a directory and its hierarchy more than
3557c478bd9Sstevel@tonic-gate 	 * once in the special case in which it lies under the
3567c478bd9Sstevel@tonic-gate 	 * hierarchy of a directory which is a hard link.
3577c478bd9Sstevel@tonic-gate 	 * Note:  Files with multiple links should only be counted
3587c478bd9Sstevel@tonic-gate 	 * once.  Since each inode could possibly be referenced by a
3597c478bd9Sstevel@tonic-gate 	 * symbolic link, we need to keep track of all inodes when -L
3607c478bd9Sstevel@tonic-gate 	 * is specified.
3617c478bd9Sstevel@tonic-gate 	 */
36215deec58Sceastha 	if (Lflg || ((stb.st_mode & S_IFMT) == S_IFDIR) ||
3637c478bd9Sstevel@tonic-gate 	    (stb.st_nlink > 1)) {
3647c478bd9Sstevel@tonic-gate 		int rc;
3657c478bd9Sstevel@tonic-gate 		if ((rc = add_tnode(&tree, stb.st_dev, stb.st_ino)) != 1) {
3667c478bd9Sstevel@tonic-gate 			if (rc == 0) {
3677c478bd9Sstevel@tonic-gate 				/*
3687c478bd9Sstevel@tonic-gate 				 * This hierarchy, or file with multiple
3697c478bd9Sstevel@tonic-gate 				 * links, has already been visited/reported.
3707c478bd9Sstevel@tonic-gate 				 */
3717c478bd9Sstevel@tonic-gate 				return (0);
3727c478bd9Sstevel@tonic-gate 			} else {
3737c478bd9Sstevel@tonic-gate 				/*
3747c478bd9Sstevel@tonic-gate 				 * An error occurred while trying to add the
3757c478bd9Sstevel@tonic-gate 				 * node to the tree.
3767c478bd9Sstevel@tonic-gate 				 */
3777c478bd9Sstevel@tonic-gate 				if (rflg) {
3787c478bd9Sstevel@tonic-gate 					perror("du");
3797c478bd9Sstevel@tonic-gate 				}
3807c478bd9Sstevel@tonic-gate 				exitdu(1);
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
38424430d07SDale Ghent 	blocks = Aflg ? stb.st_size : stb.st_blocks;
38524430d07SDale Ghent 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * If there are extended attributes on the current file, add their
38815deec58Sceastha 	 * block usage onto the block count.  Note: Since pathconf() always
38915deec58Sceastha 	 * follows symlinks, only test for extended attributes using pathconf()
39015deec58Sceastha 	 * if we are following symlinks or the current file is not a symlink.
3917c478bd9Sstevel@tonic-gate 	 */
39215deec58Sceastha 	if (curname && (follow_symlinks ||
39315deec58Sceastha 	    ((stb.st_mode & S_IFMT) != S_IFLNK)) &&
39415deec58Sceastha 	    pathconf(curname, _PC_XATTR_EXISTS) == 1) {
3957c478bd9Sstevel@tonic-gate 		if ((fd = attropen(curname, ".", O_RDONLY)) < 0) {
3967c478bd9Sstevel@tonic-gate 			if (rflg)
3977c478bd9Sstevel@tonic-gate 				perror(gettext(
3987c478bd9Sstevel@tonic-gate 				    "du: can't access extended attributes"));
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		else
4017c478bd9Sstevel@tonic-gate 		{
4027c478bd9Sstevel@tonic-gate 			tmpflg = sflg;
4037c478bd9Sstevel@tonic-gate 			sflg = 1;
4047c478bd9Sstevel@tonic-gate 			blocks += descend(NULL, fd, retcode, device);
4057c478bd9Sstevel@tonic-gate 			sflg = tmpflg;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	if ((stb.st_mode & S_IFMT) != S_IFDIR) {
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * Don't print twice: if sflg, file will get printed in main().
4117c478bd9Sstevel@tonic-gate 		 * Otherwise, level == 0 means this file is listed on the
4127c478bd9Sstevel@tonic-gate 		 * command line, so print here; aflg means print all files.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		if (sflg == 0 && (aflg || level == 0))
4157c478bd9Sstevel@tonic-gate 			printsize(blocks, base);
4167c478bd9Sstevel@tonic-gate 		return (blocks);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	if (dirp != NULL)
4197c478bd9Sstevel@tonic-gate 		/*
4207c478bd9Sstevel@tonic-gate 		 * Close the parent directory descriptor, we will reopen
4217c478bd9Sstevel@tonic-gate 		 * the directory when we pop up from this level of the
4227c478bd9Sstevel@tonic-gate 		 * recursion.
4237c478bd9Sstevel@tonic-gate 		 */
4247c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
4257c478bd9Sstevel@tonic-gate 	if (curname == NULL)
4267c478bd9Sstevel@tonic-gate 		dirp = fdopendir(curfd);
4277c478bd9Sstevel@tonic-gate 	else
4287c478bd9Sstevel@tonic-gate 		dirp = opendir(curname);
4297c478bd9Sstevel@tonic-gate 	if (dirp == NULL) {
4307c478bd9Sstevel@tonic-gate 		if (rflg) {
4317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
4327c478bd9Sstevel@tonic-gate 			perror(base);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		*retcode = 1;
4357c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
4367c478bd9Sstevel@tonic-gate 		return (0);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	level++;
4397c478bd9Sstevel@tonic-gate 	if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode))) {
4407c478bd9Sstevel@tonic-gate 		if (getcwd(dirbuf, PATH_MAX) == NULL) {
4417c478bd9Sstevel@tonic-gate 			if (rflg) {
4427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "du: ");
4437c478bd9Sstevel@tonic-gate 				perror(base);
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 			exitdu(1);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	if ((curname ? (chdir(curname) < 0) : (fchdir(curfd) < 0))) {
4497c478bd9Sstevel@tonic-gate 		if (rflg) {
4507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "du: ");
4517c478bd9Sstevel@tonic-gate 			perror(base);
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate 		*retcode = 1;
4547c478bd9Sstevel@tonic-gate 		*ebase0 = 0;
4557c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
4567c478bd9Sstevel@tonic-gate 		dirp = NULL;
4577c478bd9Sstevel@tonic-gate 		level--;
4587c478bd9Sstevel@tonic-gate 		return (0);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
4617c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
4627c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
4637c478bd9Sstevel@tonic-gate 			continue;
4647c478bd9Sstevel@tonic-gate 		/*
4657c478bd9Sstevel@tonic-gate 		 * we're about to append "/" + dp->d_name
4667c478bd9Sstevel@tonic-gate 		 * onto end of base; make sure there's enough
4677c478bd9Sstevel@tonic-gate 		 * space
4687c478bd9Sstevel@tonic-gate 		 */
4697c478bd9Sstevel@tonic-gate 		while ((offset + strlen(dp->d_name) + 2) > base_len) {
4707c478bd9Sstevel@tonic-gate 			base_len = base_len * 2;
4717c478bd9Sstevel@tonic-gate 			if ((base = (char *)realloc(base,
4727c478bd9Sstevel@tonic-gate 			    base_len * sizeof (char))) == NULL) {
4737c478bd9Sstevel@tonic-gate 				if (rflg) {
4747c478bd9Sstevel@tonic-gate 					perror("du");
4757c478bd9Sstevel@tonic-gate 				}
4767c478bd9Sstevel@tonic-gate 				exitdu(1);
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 			ebase = base + offset;
4797c478bd9Sstevel@tonic-gate 			ebase0 = base + offset0;
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
4827c478bd9Sstevel@tonic-gate 		(void) sprintf(ebase, "/%s", dp->d_name);
4837c478bd9Sstevel@tonic-gate 		curoff = telldir(dirp);
4847c478bd9Sstevel@tonic-gate 		retval = descend(ebase + 1, 0, retcode, device);
4857c478bd9Sstevel@tonic-gate 			/* base may have been moved via realloc in descend() */
4867c478bd9Sstevel@tonic-gate 		ebase = base + offset;
4877c478bd9Sstevel@tonic-gate 		ebase0 = base + offset0;
4887c478bd9Sstevel@tonic-gate 		*ebase = 0;
4897c478bd9Sstevel@tonic-gate 		blocks += retval;
4907c478bd9Sstevel@tonic-gate 		if (dirp == NULL) {
4917c478bd9Sstevel@tonic-gate 			if ((dirp = opendir(".")) == NULL) {
4927c478bd9Sstevel@tonic-gate 				if (rflg) {
4937c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4947c478bd9Sstevel@tonic-gate 					    gettext("du: Can't reopen in "));
4957c478bd9Sstevel@tonic-gate 					perror(base);
4967c478bd9Sstevel@tonic-gate 				}
4977c478bd9Sstevel@tonic-gate 				*retcode = 1;
4987c478bd9Sstevel@tonic-gate 				level--;
4997c478bd9Sstevel@tonic-gate 				return (0);
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			seekdir(dirp, curoff);
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5057c478bd9Sstevel@tonic-gate 	level--;
5067c478bd9Sstevel@tonic-gate 	dirp = NULL;
5077c478bd9Sstevel@tonic-gate 	if (sflg == 0)
5087c478bd9Sstevel@tonic-gate 		printsize(blocks, base);
5097c478bd9Sstevel@tonic-gate 	if (curname == NULL || (Lflg && S_ISLNK(stb1.st_mode)))
5107c478bd9Sstevel@tonic-gate 		ret = chdir(dirbuf);
5117c478bd9Sstevel@tonic-gate 	else
5127c478bd9Sstevel@tonic-gate 		ret = chdir("..");
5137c478bd9Sstevel@tonic-gate 	if (ret < 0) {
5147c478bd9Sstevel@tonic-gate 		if (rflg) {
5157c478bd9Sstevel@tonic-gate 			(void) sprintf(strchr(base, '\0'), "/..");
5167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5177c478bd9Sstevel@tonic-gate 			    gettext("du: Can't change dir to '..' in "));
5187c478bd9Sstevel@tonic-gate 			perror(base);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		exitdu(1);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	*ebase0 = 0;
5237c478bd9Sstevel@tonic-gate 	if (oflg)
5247c478bd9Sstevel@tonic-gate 		return (0);
5257c478bd9Sstevel@tonic-gate 	else
5267c478bd9Sstevel@tonic-gate 		return (blocks);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate printsize(blkcnt_t blocks, char *path)
5317c478bd9Sstevel@tonic-gate {
53224430d07SDale Ghent 	u_longlong_t bsize;
53324430d07SDale Ghent 
53424430d07SDale Ghent 	bsize = Aflg ? 1 : DEV_BSIZE;
53524430d07SDale Ghent 
5367c478bd9Sstevel@tonic-gate 	if (hflg) {
537*fc30d466SJason King 	char buf[NN_NUMBUF_SZ] = { 0 };
538*fc30d466SJason King 
539*fc30d466SJason King 	nicenum_scale(blocks, bsize, buf, sizeof (buf), 0);
540*fc30d466SJason King 	(void) printf(FORMAT1, buf, path);
5417c478bd9Sstevel@tonic-gate 	} else if (kflg) {
5421979231eSceastha 		(void) printf(FORMAT2, (long long)kb(blocks), path);
5431979231eSceastha 	} else if (mflg) {
5441979231eSceastha 		(void) printf(FORMAT2, (long long)mb(blocks), path);
5457c478bd9Sstevel@tonic-gate 	} else {
5461979231eSceastha 		(void) printf(FORMAT2, (long long)blocks, path);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static void
5517c478bd9Sstevel@tonic-gate exitdu(int exitcode)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	free(base);
5547c478bd9Sstevel@tonic-gate 	free(name);
5557c478bd9Sstevel@tonic-gate 	exit(exitcode);
5567c478bd9Sstevel@tonic-gate }
557