xref: /onnv-gate/usr/src/cmd/fs.d/df.c (revision 11443:1b9e01f058d2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*11443SSanjeev.Bagewadi@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
31789Sahrens #include <dlfcn.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <libintl.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <ftw.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <sys/statvfs.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/param.h>
450Sstevel@tonic-gate #include <sys/mnttab.h>
460Sstevel@tonic-gate #include <sys/mntent.h>
470Sstevel@tonic-gate #include <sys/vfstab.h>
480Sstevel@tonic-gate #include <sys/wait.h>
490Sstevel@tonic-gate #include <sys/mkdev.h>
500Sstevel@tonic-gate #include <sys/int_limits.h>
510Sstevel@tonic-gate #include <sys/zone.h>
52789Sahrens #include <libzfs.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include "fslib.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern char *default_fstype(char *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * General notice:
600Sstevel@tonic-gate  * String pointers in this code may point to statically allocated memory
610Sstevel@tonic-gate  * or dynamically allocated memory. Furthermore, a dynamically allocated
620Sstevel@tonic-gate  * string may be pointed to by more than one pointer. This does not pose
630Sstevel@tonic-gate  * a problem because malloc'ed memory is never free'd (so we don't need
640Sstevel@tonic-gate  * to remember which pointers point to malloc'ed memory).
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * TRANSLATION_NOTE
690Sstevel@tonic-gate  * Only strings passed as arguments to the TRANSLATE macro need to
700Sstevel@tonic-gate  * be translated.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifndef MNTTYPE_LOFS
740Sstevel@tonic-gate #define	MNTTYPE_LOFS		"lofs"
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	EQ(s1, s2)		(strcmp(s1, s2) == 0)
780Sstevel@tonic-gate #define	NEW(type)		xmalloc(sizeof (type))
790Sstevel@tonic-gate #define	CLEAR(var)		(void) memset(&(var), 0, sizeof (var))
800Sstevel@tonic-gate #define	MAX(a, b)		((a) > (b) ? (a) : (b))
810Sstevel@tonic-gate #define	MAX3(a, b, c)		MAX(a, MAX(b, c))
820Sstevel@tonic-gate #define	TRANSLATE(s)		new_string(gettext(s))
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	MAX_OPTIONS		36
850Sstevel@tonic-gate #define	N_FSTYPES		20
860Sstevel@tonic-gate #define	MOUNT_TABLE_ENTRIES	40	/* initial allocation */
870Sstevel@tonic-gate #define	MSGBUF_SIZE		1024
880Sstevel@tonic-gate #define	LINEBUF_SIZE		256	/* either input or output lines */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #define	BLOCK_SIZE		512	/* when reporting in terms of blocks */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	DEVNM_CMD		"devnm"
930Sstevel@tonic-gate #define	FS_LIBPATH		"/usr/lib/fs/"
940Sstevel@tonic-gate #define	MOUNT_TAB		"/etc/mnttab"
950Sstevel@tonic-gate #define	VFS_TAB			"/etc/vfstab"
960Sstevel@tonic-gate #define	REMOTE_FS		"/etc/dfs/fstypes"
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	NUL			'\0'
990Sstevel@tonic-gate #define	FALSE			0
1000Sstevel@tonic-gate #define	TRUE			1
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Formatting constants
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate #define	IBCS2_FILESYSTEM_WIDTH	15	/* Truncate to match ISC/SCO */
1060Sstevel@tonic-gate #define	IBCS2_MOUNT_POINT_WIDTH	10	/* Truncate to match ISC/SCO */
1070Sstevel@tonic-gate #define	FILESYSTEM_WIDTH	20
1080Sstevel@tonic-gate #define	MOUNT_POINT_WIDTH	19
1090Sstevel@tonic-gate #define	SPECIAL_DEVICE_WIDTH	18
1100Sstevel@tonic-gate #define	FSTYPE_WIDTH		8
1110Sstevel@tonic-gate #define	BLOCK_WIDTH		8
1120Sstevel@tonic-gate #define	NFILES_WIDTH		8
1130Sstevel@tonic-gate #ifdef XPG4
1140Sstevel@tonic-gate #define	KBYTE_WIDTH		11
1150Sstevel@tonic-gate #define	AVAILABLE_WIDTH		10
1160Sstevel@tonic-gate #else
1170Sstevel@tonic-gate #define	KBYTE_WIDTH		7
1180Sstevel@tonic-gate #define	AVAILABLE_WIDTH		6
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate #define	SCALED_WIDTH		6
1210Sstevel@tonic-gate #define	CAPACITY_WIDTH		9
1220Sstevel@tonic-gate #define	BSIZE_WIDTH		6
1230Sstevel@tonic-gate #define	FRAGSIZE_WIDTH		7
1240Sstevel@tonic-gate #define	FSID_WIDTH		7
1250Sstevel@tonic-gate #define	FLAG_WIDTH		8
1260Sstevel@tonic-gate #define	NAMELEN_WIDTH		7
1270Sstevel@tonic-gate #define	MNT_SPEC_WIDTH		MOUNT_POINT_WIDTH + SPECIAL_DEVICE_WIDTH + 2
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Flags for the errmsg() function
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate #define	ERR_NOFLAGS		0x0
1330Sstevel@tonic-gate #define	ERR_NONAME		0x1	/* don't include the program name */
1340Sstevel@tonic-gate 					/* as a prefix */
1350Sstevel@tonic-gate #define	ERR_FATAL		0x2	/* call exit after printing the */
1360Sstevel@tonic-gate 					/* message */
1370Sstevel@tonic-gate #define	ERR_PERROR		0x4	/* append an errno explanation to */
1380Sstevel@tonic-gate 					/* the message */
1390Sstevel@tonic-gate #define	ERR_USAGE		0x8	/* print the usage line after the */
1400Sstevel@tonic-gate 					/* message */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #define	NUMBER_WIDTH		40
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * A numbuf_t is used when converting a number to a string representation
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate typedef char numbuf_t[ NUMBER_WIDTH ];
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * We use bool_int instead of int to make clear which variables are
1510Sstevel@tonic-gate  * supposed to be boolean
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate typedef int bool_int;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate struct mtab_entry {
1560Sstevel@tonic-gate 	bool_int	mte_dev_is_valid;
1570Sstevel@tonic-gate 	dev_t		mte_dev;
1580Sstevel@tonic-gate 	bool_int	mte_ignore;	/* the "ignore" option was set */
1590Sstevel@tonic-gate 	struct extmnttab	*mte_mount;
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate struct df_request {
1640Sstevel@tonic-gate 	bool_int		dfr_valid;
1650Sstevel@tonic-gate 	char			*dfr_cmd_arg;	/* what the user specified */
1660Sstevel@tonic-gate 	struct mtab_entry	*dfr_mte;
1670Sstevel@tonic-gate 	char			*dfr_fstype;
1680Sstevel@tonic-gate 	int			dfr_index;	/* to make qsort stable	*/
1690Sstevel@tonic-gate };
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate #define	DFR_MOUNT_POINT(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_mountp
1720Sstevel@tonic-gate #define	DFR_SPECIAL(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_special
173789Sahrens #define	DFR_FSTYPE(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_fstype
1740Sstevel@tonic-gate #define	DFR_ISMOUNTEDFS(dfrp)	((dfrp)->dfr_mte != NULL)
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate #define	DFRP(p)			((struct df_request *)(p))
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate typedef void (*output_func)(struct df_request *, struct statvfs64 *);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate struct df_output {
1810Sstevel@tonic-gate 	output_func	dfo_func;	/* function that will do the output */
1820Sstevel@tonic-gate 	int		dfo_flags;
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Output flags
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate #define	DFO_NOFLAGS	0x0
1890Sstevel@tonic-gate #define	DFO_HEADER	0x1		/* output preceded by header */
1900Sstevel@tonic-gate #define	DFO_STATVFS	0x2		/* must do a statvfs64(2) */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static char	*program_name;
1940Sstevel@tonic-gate static char	df_options[MAX_OPTIONS] = "-";
1950Sstevel@tonic-gate static size_t	df_options_len = 1;
1960Sstevel@tonic-gate static char	*o_option_arg;			/* arg to the -o option */
1970Sstevel@tonic-gate static char	*FSType;
1980Sstevel@tonic-gate static char	*remote_fstypes[N_FSTYPES+1];	/* allocate an extra one */
1990Sstevel@tonic-gate 						/* to use as a terminator */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * The following three variables support an in-memory copy of the mount table
2030Sstevel@tonic-gate  * to speedup searches.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static struct mtab_entry	*mount_table;	/* array of mtab_entry's */
2060Sstevel@tonic-gate static size_t			mount_table_entries;
2070Sstevel@tonic-gate static size_t			mount_table_allocated_entries;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate static bool_int		F_option;
2100Sstevel@tonic-gate static bool_int		V_option;
2110Sstevel@tonic-gate static bool_int		P_option;	/* Added for XCU4 compliance */
2120Sstevel@tonic-gate static bool_int		Z_option;
2130Sstevel@tonic-gate static bool_int		v_option;
2140Sstevel@tonic-gate #ifdef	_iBCS2
2150Sstevel@tonic-gate char			*sysv3_set;
2160Sstevel@tonic-gate #endif /* _iBCS2 */
2170Sstevel@tonic-gate static bool_int		a_option;
2180Sstevel@tonic-gate static bool_int		b_option;
2190Sstevel@tonic-gate static bool_int		e_option;
2200Sstevel@tonic-gate static bool_int		g_option;
2210Sstevel@tonic-gate static bool_int		h_option;
2220Sstevel@tonic-gate static bool_int		k_option;
2230Sstevel@tonic-gate static bool_int		l_option;
2240Sstevel@tonic-gate static bool_int		n_option;
2250Sstevel@tonic-gate static bool_int		t_option;
2260Sstevel@tonic-gate static bool_int		o_option;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static bool_int		tty_output;
2290Sstevel@tonic-gate static bool_int		use_scaling;
2300Sstevel@tonic-gate static int		scale;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static void usage(void);
2330Sstevel@tonic-gate static void do_devnm(int, char **);
234821Sdh145677 static void do_df(int, char **)	__NORETURN;
2350Sstevel@tonic-gate static void parse_options(int, char **);
2360Sstevel@tonic-gate static char *basename(char *);
2370Sstevel@tonic-gate 
2382082Seschrock static libzfs_handle_t *(*_libzfs_init)(boolean_t);
2392082Seschrock static zfs_handle_t *(*_zfs_open)(libzfs_handle_t *, const char *, int);
240789Sahrens static void (*_zfs_close)(zfs_handle_t *);
241789Sahrens static uint64_t (*_zfs_prop_get_int)(zfs_handle_t *, zfs_prop_t);
2422082Seschrock static libzfs_handle_t *g_zfs;
243789Sahrens 
2441544Seschrock /*
2451544Seschrock  * Dynamically check for libzfs, in case the user hasn't installed the SUNWzfs
2461544Seschrock  * packages.  A basic utility such as df shouldn't depend on optional
2471544Seschrock  * filesystems.
2481544Seschrock  */
2492082Seschrock static boolean_t
2501544Seschrock load_libzfs(void)
2510Sstevel@tonic-gate {
252789Sahrens 	void *hdl;
253789Sahrens 
2542082Seschrock 	if (_libzfs_init != NULL)
2552082Seschrock 		return (g_zfs != NULL);
2560Sstevel@tonic-gate 
257789Sahrens 	if ((hdl = dlopen("libzfs.so", RTLD_LAZY)) != NULL) {
2582082Seschrock 		_libzfs_init = (libzfs_handle_t *(*)(boolean_t))dlsym(hdl,
2592082Seschrock 		    "libzfs_init");
260789Sahrens 		_zfs_open = (zfs_handle_t *(*)())dlsym(hdl, "zfs_open");
261789Sahrens 		_zfs_close = (void (*)())dlsym(hdl, "zfs_close");
262789Sahrens 		_zfs_prop_get_int = (uint64_t (*)())
263789Sahrens 		    dlsym(hdl, "zfs_prop_get_int");
264789Sahrens 
2652082Seschrock 		if (_libzfs_init != NULL) {
266789Sahrens 			assert(_zfs_open != NULL);
267789Sahrens 			assert(_zfs_close != NULL);
268789Sahrens 			assert(_zfs_prop_get_int != NULL);
269789Sahrens 
2702082Seschrock 			g_zfs = _libzfs_init(B_FALSE);
271789Sahrens 		}
272789Sahrens 	}
273789Sahrens 
2742082Seschrock 	return (g_zfs != NULL);
2751544Seschrock }
2761544Seschrock 
2771544Seschrock int
2781544Seschrock main(int argc, char *argv[])
2791544Seschrock {
2801544Seschrock 	(void) setlocale(LC_ALL, "");
2811544Seschrock 
2821544Seschrock #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
2831544Seschrock #define	TEXT_DOMAIN "SYS_TEST"
2841544Seschrock #endif
2851544Seschrock 	(void) textdomain(TEXT_DOMAIN);
2861544Seschrock 
2871544Seschrock 	program_name = basename(argv[0]);
2881544Seschrock 
2891544Seschrock #ifdef	_iBCS2
2901544Seschrock 	sysv3_set = getenv("SYSV3");
2911544Seschrock #endif	/* _iBCS2 */
2921544Seschrock 
2930Sstevel@tonic-gate 	if (EQ(program_name, DEVNM_CMD))
2940Sstevel@tonic-gate 		do_devnm(argc, argv);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	parse_options(argc, argv);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * The k_option implies SunOS 4.x compatibility: when the special
3000Sstevel@tonic-gate 	 * device name is too long the line will be split except when the
3010Sstevel@tonic-gate 	 * output has been redirected.
3020Sstevel@tonic-gate 	 * This is also valid for the -h option.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if (use_scaling || k_option || P_option || v_option)
3060Sstevel@tonic-gate 		tty_output = isatty(1);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	do_df(argc - optind, &argv[optind]);
3090Sstevel@tonic-gate 	/* NOTREACHED */
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * Prints an error message to stderr.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate /* VARARGS2 */
3170Sstevel@tonic-gate static void
3180Sstevel@tonic-gate errmsg(int flags, char *fmt, ...)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	char buf[MSGBUF_SIZE];
3210Sstevel@tonic-gate 	va_list ap;
3220Sstevel@tonic-gate 	int cc;
3230Sstevel@tonic-gate 	int offset;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (flags & ERR_NONAME)
3260Sstevel@tonic-gate 		offset = 0;
3270Sstevel@tonic-gate 	else
3280Sstevel@tonic-gate 		offset = sprintf(buf, "%s: ", program_name);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	va_start(ap, fmt);
3310Sstevel@tonic-gate 	cc = vsprintf(&buf[offset], gettext(fmt), ap);
3320Sstevel@tonic-gate 	offset += cc;
3330Sstevel@tonic-gate 	va_end(ap);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (flags & ERR_PERROR) {
3360Sstevel@tonic-gate 		if (buf[offset-1] != ' ')
3370Sstevel@tonic-gate 			(void) strcat(buf, " ");
3380Sstevel@tonic-gate 		(void) strcat(buf, strerror(errno));
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n", buf);
3410Sstevel@tonic-gate 	if (flags & ERR_USAGE)
3420Sstevel@tonic-gate 		usage();
3430Sstevel@tonic-gate 	if (flags & ERR_FATAL)
3440Sstevel@tonic-gate 		exit(1);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate static void
349821Sdh145677 usage(void)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate #ifdef  XPG4
3520Sstevel@tonic-gate 	errmsg(ERR_NONAME,
3535094Slling 	    "Usage: %s [-F FSType] [-abeghklntPVZ] [-o FSType-specific_options]"
3545094Slling 	    " [directory | block_device | resource]", program_name);
3550Sstevel@tonic-gate #else
3560Sstevel@tonic-gate 	errmsg(ERR_NONAME,
3575094Slling 	    "Usage: %s [-F FSType] [-abeghklntVvZ] [-o FSType-specific_options]"
3585094Slling 	    " [directory | block_device | resource]", program_name);
3590Sstevel@tonic-gate #endif
3600Sstevel@tonic-gate 	exit(1);
3610Sstevel@tonic-gate 	/* NOTREACHED */
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate static char *
3660Sstevel@tonic-gate new_string(char *s)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	char *p = NULL;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (s) {
3710Sstevel@tonic-gate 		p = strdup(s);
3720Sstevel@tonic-gate 		if (p)
3730Sstevel@tonic-gate 			return (p);
3740Sstevel@tonic-gate 		errmsg(ERR_FATAL, "out of memory");
3750Sstevel@tonic-gate 		/* NOTREACHED */
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	return (p);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate  * Allocate memory using malloc but terminate if the allocation fails
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate static void *
3850Sstevel@tonic-gate xmalloc(size_t size)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	void *p = malloc(size);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (p)
3900Sstevel@tonic-gate 		return (p);
3910Sstevel@tonic-gate 	errmsg(ERR_FATAL, "out of memory");
3920Sstevel@tonic-gate 	/* NOTREACHED */
393821Sdh145677 	return (NULL);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Allocate memory using realloc but terminate if the allocation fails
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate static void *
4010Sstevel@tonic-gate xrealloc(void *ptr, size_t size)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	void *p = realloc(ptr, size);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (p)
4060Sstevel@tonic-gate 		return (p);
4070Sstevel@tonic-gate 	errmsg(ERR_FATAL, "out of memory");
4080Sstevel@tonic-gate 	/* NOTREACHED */
409821Sdh145677 	return (NULL);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * fopen the specified file for reading but terminate if the fopen fails
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate static FILE *
4170Sstevel@tonic-gate xfopen(char *file)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	FILE *fp = fopen(file, "r");
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (fp == NULL)
4220Sstevel@tonic-gate 		errmsg(ERR_FATAL + ERR_PERROR, "failed to open %s:", file);
4230Sstevel@tonic-gate 	return (fp);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate  * Read remote file system types from REMOTE_FS into the
4290Sstevel@tonic-gate  * remote_fstypes array.
4300Sstevel@tonic-gate  */
4310Sstevel@tonic-gate static void
432821Sdh145677 init_remote_fs(void)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	FILE	*fp;
4350Sstevel@tonic-gate 	char	line_buf[LINEBUF_SIZE];
4360Sstevel@tonic-gate 	size_t	fstype_index = 0;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
4390Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS,
4405094Slling 		    "Warning: can't open %s, ignored", REMOTE_FS);
4410Sstevel@tonic-gate 		return;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
4450Sstevel@tonic-gate 		char buf[LINEBUF_SIZE];
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		(void) sscanf(line_buf, "%s", buf);
4480Sstevel@tonic-gate 		remote_fstypes[fstype_index++] = new_string(buf);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		if (fstype_index == N_FSTYPES)
4510Sstevel@tonic-gate 			break;
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	(void) fclose(fp);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * Returns TRUE if fstype is a remote file system type;
4590Sstevel@tonic-gate  * otherwise, returns FALSE.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate static int
4620Sstevel@tonic-gate is_remote_fs(char *fstype)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	char **p;
4650Sstevel@tonic-gate 	static bool_int remote_fs_initialized;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (! remote_fs_initialized) {
4680Sstevel@tonic-gate 		init_remote_fs();
4690Sstevel@tonic-gate 		remote_fs_initialized = TRUE;
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	for (p = remote_fstypes; *p; p++)
4730Sstevel@tonic-gate 		if (EQ(fstype, *p))
4740Sstevel@tonic-gate 			return (TRUE);
4750Sstevel@tonic-gate 	return (FALSE);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static char *
4800Sstevel@tonic-gate basename(char *s)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	char *p = strrchr(s, '/');
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	return (p ? p+1 : s);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Create a new "struct extmnttab" and make sure that its fields point
4900Sstevel@tonic-gate  * to malloc'ed memory
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static struct extmnttab *
4930Sstevel@tonic-gate mntdup(struct extmnttab *old)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	struct extmnttab *new = NEW(struct extmnttab);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	new->mnt_special = new_string(old->mnt_special);
4980Sstevel@tonic-gate 	new->mnt_mountp  = new_string(old->mnt_mountp);
4990Sstevel@tonic-gate 	new->mnt_fstype  = new_string(old->mnt_fstype);
5000Sstevel@tonic-gate 	new->mnt_mntopts = new_string(old->mnt_mntopts);
5010Sstevel@tonic-gate 	new->mnt_time    = new_string(old->mnt_time);
5020Sstevel@tonic-gate 	new->mnt_major   = old->mnt_major;
5030Sstevel@tonic-gate 	new->mnt_minor   = old->mnt_minor;
5040Sstevel@tonic-gate 	return (new);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate static void
5090Sstevel@tonic-gate mtab_error(char *mtab_file, int status)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	if (status == MNT_TOOLONG)
5120Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
5135094Slling 		    mtab_file, MNT_LINE_MAX);
5140Sstevel@tonic-gate 	else if (status == MNT_TOOMANY)
5150Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS,
5165094Slling 		    "a line in %s has too many fields", mtab_file);
5170Sstevel@tonic-gate 	else if (status == MNT_TOOFEW)
5180Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS,
5195094Slling 		    "a line in %s has too few fields", mtab_file);
5200Sstevel@tonic-gate 	else
5210Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS,
5225094Slling 		    "error while reading %s: %d", mtab_file, status);
5230Sstevel@tonic-gate 	exit(1);
5240Sstevel@tonic-gate 	/* NOTREACHED */
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * Read the mount table from the specified file.
5300Sstevel@tonic-gate  * We keep the table in memory for faster lookups.
5310Sstevel@tonic-gate  */
5320Sstevel@tonic-gate static void
533821Sdh145677 mtab_read_file(void)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	char		*mtab_file = MOUNT_TAB;
5360Sstevel@tonic-gate 	FILE		*fp;
5370Sstevel@tonic-gate 	struct extmnttab	mtab;
5380Sstevel@tonic-gate 	int		status;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	fp = xfopen(mtab_file);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	resetmnttab(fp);
5430Sstevel@tonic-gate 	mount_table_allocated_entries = MOUNT_TABLE_ENTRIES;
5440Sstevel@tonic-gate 	mount_table_entries = 0;
5450Sstevel@tonic-gate 	mount_table = xmalloc(
5465094Slling 	    mount_table_allocated_entries * sizeof (struct mtab_entry));
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	while ((status = getextmntent(fp, &mtab, sizeof (struct extmnttab)))
5495094Slling 	    == 0) {
5500Sstevel@tonic-gate 		struct mtab_entry *mtep;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		if (mount_table_entries == mount_table_allocated_entries) {
5530Sstevel@tonic-gate 			mount_table_allocated_entries += MOUNT_TABLE_ENTRIES;
5540Sstevel@tonic-gate 			mount_table = xrealloc(mount_table,
5555094Slling 			    mount_table_allocated_entries *
5565094Slling 			    sizeof (struct mtab_entry));
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		mtep = &mount_table[mount_table_entries++];
5590Sstevel@tonic-gate 		mtep->mte_mount = mntdup(&mtab);
5600Sstevel@tonic-gate 		mtep->mte_dev_is_valid = FALSE;
5610Sstevel@tonic-gate 		mtep->mte_ignore = (hasmntopt((struct mnttab *)&mtab,
5625094Slling 		    MNTOPT_IGNORE) != NULL);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	(void) fclose(fp);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (status == -1)			/* reached EOF */
5680Sstevel@tonic-gate 		return;
5690Sstevel@tonic-gate 	mtab_error(mtab_file, status);
5700Sstevel@tonic-gate 	/* NOTREACHED */
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * We use this macro when we want to record the option for the purpose of
5760Sstevel@tonic-gate  * passing it to the FS-specific df
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate #define	SET_OPTION(opt)		opt##_option = TRUE, \
5790Sstevel@tonic-gate 				df_options[df_options_len++] = arg
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate static void
5820Sstevel@tonic-gate parse_options(int argc, char *argv[])
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	int arg;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	opterr = 0;	/* getopt shouldn't complain about unknown options */
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate #ifdef XPG4
5890Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlPZ")) != EOF) {
5900Sstevel@tonic-gate #else
5910Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlvZ")) != EOF) {
5920Sstevel@tonic-gate #endif
5930Sstevel@tonic-gate 		if (arg == 'F') {
5940Sstevel@tonic-gate 			if (F_option)
5950Sstevel@tonic-gate 				errmsg(ERR_FATAL + ERR_USAGE,
5965094Slling 				    "more than one FSType specified");
5970Sstevel@tonic-gate 			F_option = 1;
5980Sstevel@tonic-gate 			FSType = optarg;
5990Sstevel@tonic-gate 		} else if (arg == 'V' && ! V_option) {
6000Sstevel@tonic-gate 			V_option = TRUE;
6010Sstevel@tonic-gate 		} else if (arg == 'v' && ! v_option) {
6020Sstevel@tonic-gate 			v_option = TRUE;
6030Sstevel@tonic-gate #ifdef XPG4
6040Sstevel@tonic-gate 		} else if (arg == 'P' && ! P_option) {
6050Sstevel@tonic-gate 			SET_OPTION(P);
6060Sstevel@tonic-gate #endif
6070Sstevel@tonic-gate 		} else if (arg == 'a' && ! a_option) {
6080Sstevel@tonic-gate 			SET_OPTION(a);
6090Sstevel@tonic-gate 		} else if (arg == 'b' && ! b_option) {
6100Sstevel@tonic-gate 			SET_OPTION(b);
6110Sstevel@tonic-gate 		} else if (arg == 'e' && ! e_option) {
6120Sstevel@tonic-gate 			SET_OPTION(e);
6130Sstevel@tonic-gate 		} else if (arg == 'g' && ! g_option) {
6140Sstevel@tonic-gate 			SET_OPTION(g);
6150Sstevel@tonic-gate 		} else if (arg == 'h') {
6160Sstevel@tonic-gate 			use_scaling = TRUE;
6170Sstevel@tonic-gate 			scale = 1024;
6180Sstevel@tonic-gate 		} else if (arg == 'k' && ! k_option) {
6190Sstevel@tonic-gate 			SET_OPTION(k);
6200Sstevel@tonic-gate 		} else if (arg == 'l' && ! l_option) {
6210Sstevel@tonic-gate 			SET_OPTION(l);
6220Sstevel@tonic-gate 		} else if (arg == 'n' && ! n_option) {
6230Sstevel@tonic-gate 			SET_OPTION(n);
6240Sstevel@tonic-gate 		} else if (arg == 't' && ! t_option) {
6250Sstevel@tonic-gate 			SET_OPTION(t);
6260Sstevel@tonic-gate 		} else if (arg == 'o') {
6270Sstevel@tonic-gate 			if (o_option)
6280Sstevel@tonic-gate 				errmsg(ERR_FATAL + ERR_USAGE,
6290Sstevel@tonic-gate 				"the -o option can only be specified once");
6300Sstevel@tonic-gate 			o_option = TRUE;
6310Sstevel@tonic-gate 			o_option_arg = optarg;
6320Sstevel@tonic-gate 		} else if (arg == 'Z') {
6330Sstevel@tonic-gate 			SET_OPTION(Z);
6340Sstevel@tonic-gate 		} else if (arg == '?') {
6350Sstevel@tonic-gate 			errmsg(ERR_USAGE, "unknown option: %c", optopt);
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/*
6400Sstevel@tonic-gate 	 * Option sanity checks
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	if (g_option && o_option)
6430Sstevel@tonic-gate 		errmsg(ERR_FATAL, "-o and -g options are incompatible");
6440Sstevel@tonic-gate 	if (l_option && o_option)
6450Sstevel@tonic-gate 		errmsg(ERR_FATAL, "-o and -l options are incompatible");
6460Sstevel@tonic-gate 	if (n_option && o_option)
6470Sstevel@tonic-gate 		errmsg(ERR_FATAL, "-o and -n options are incompatible");
6480Sstevel@tonic-gate 	if (use_scaling && o_option)
6490Sstevel@tonic-gate 		errmsg(ERR_FATAL, "-o and -h options are incompatible");
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Check if the user-specified argument is a resource name.
6560Sstevel@tonic-gate  * A resource name is whatever is placed in the mnt_special field of
6570Sstevel@tonic-gate  * struct mnttab. In the case of NFS, a resource name has the form
6580Sstevel@tonic-gate  * hostname:pathname
6590Sstevel@tonic-gate  * We try to find an exact match between the user-specified argument
6600Sstevel@tonic-gate  * and the mnt_special field of a mount table entry.
6610Sstevel@tonic-gate  * We also use the heuristic of removing the basename from the user-specified
6620Sstevel@tonic-gate  * argument and repeating the test until we get a match. This works
6630Sstevel@tonic-gate  * fine for NFS but may fail for other remote file system types. However,
6640Sstevel@tonic-gate  * it is guaranteed that the function will not fail if the user specifies
6650Sstevel@tonic-gate  * the exact resource name.
6660Sstevel@tonic-gate  * If successful, this function sets the 'dfr_mte' field of '*dfrp'
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate static void
6690Sstevel@tonic-gate resource_mount_entry(struct df_request *dfrp)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	char *name;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * We need our own copy since we will modify the string
6750Sstevel@tonic-gate 	 */
6760Sstevel@tonic-gate 	name = new_string(dfrp->dfr_cmd_arg);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	for (;;) {
6790Sstevel@tonic-gate 		char *p;
6800Sstevel@tonic-gate 		int i;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * Compare against all known mount points.
6840Sstevel@tonic-gate 		 * We start from the most recent mount, which is at the
6850Sstevel@tonic-gate 		 * end of the array.
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		for (i = mount_table_entries - 1; i >= 0; i--) {
6880Sstevel@tonic-gate 			struct mtab_entry *mtep = &mount_table[i];
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 			if (EQ(name, mtep->mte_mount->mnt_special)) {
6910Sstevel@tonic-gate 				dfrp->dfr_mte = mtep;
6920Sstevel@tonic-gate 				break;
6930Sstevel@tonic-gate 			}
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * Remove the last component of the pathname.
6980Sstevel@tonic-gate 		 * If there is no such component, this is not a resource name.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		p = strrchr(name, '/');
7010Sstevel@tonic-gate 		if (p == NULL)
7020Sstevel@tonic-gate 			break;
7030Sstevel@tonic-gate 		*p = NUL;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate  * Try to match the command line argument which is a block special device
7110Sstevel@tonic-gate  * with the special device of one of the mounted file systems.
7120Sstevel@tonic-gate  * If one is found, set the appropriate field of 'dfrp' to the mount
7130Sstevel@tonic-gate  * table entry.
7140Sstevel@tonic-gate  */
7150Sstevel@tonic-gate static void
7160Sstevel@tonic-gate bdev_mount_entry(struct df_request *dfrp)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	int i;
7190Sstevel@tonic-gate 	char *special = dfrp->dfr_cmd_arg;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/*
7220Sstevel@tonic-gate 	 * Compare against all known mount points.
7230Sstevel@tonic-gate 	 * We start from the most recent mount, which is at the
7240Sstevel@tonic-gate 	 * end of the array.
7250Sstevel@tonic-gate 	 */
7260Sstevel@tonic-gate 	for (i = mount_table_entries - 1; i >= 0; i--) {
7270Sstevel@tonic-gate 		struct mtab_entry *mtep = &mount_table[i];
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (EQ(special, mtep->mte_mount->mnt_special)) {
7300Sstevel@tonic-gate 			dfrp->dfr_mte = mtep;
7310Sstevel@tonic-gate 			break;
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static struct mtab_entry *
7370Sstevel@tonic-gate devid_matches(int i, dev_t devno)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	struct mtab_entry	*mtep = &mount_table[i];
7400Sstevel@tonic-gate 	struct extmnttab	*mtp = mtep->mte_mount;
7410Sstevel@tonic-gate 	/* int	len = strlen(mtp->mnt_mountp); */
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
7440Sstevel@tonic-gate 		return (NULL);
7450Sstevel@tonic-gate 	/*
7460Sstevel@tonic-gate 	 * check if device numbers match. If there is a cached device number
7470Sstevel@tonic-gate 	 * in the mtab_entry, use it, otherwise get the device number
7480Sstevel@tonic-gate 	 * either from the mnttab entry or by stat'ing the mount point.
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	if (! mtep->mte_dev_is_valid) {
7510Sstevel@tonic-gate 		struct stat64 st;
7520Sstevel@tonic-gate 		dev_t dev = NODEV;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		dev = makedev(mtp->mnt_major, mtp->mnt_minor);
7550Sstevel@tonic-gate 		if (dev == 0)
7560Sstevel@tonic-gate 			dev = NODEV;
7570Sstevel@tonic-gate 		if (dev == NODEV) {
7580Sstevel@tonic-gate 			if (stat64(mtp->mnt_mountp, &st) == -1) {
7590Sstevel@tonic-gate 				return (NULL);
7600Sstevel@tonic-gate 			} else {
7610Sstevel@tonic-gate 				dev = st.st_dev;
7620Sstevel@tonic-gate 			}
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 		mtep->mte_dev = dev;
7650Sstevel@tonic-gate 		mtep->mte_dev_is_valid = TRUE;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 	if (mtep->mte_dev == devno) {
7680Sstevel@tonic-gate 		return (mtep);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	return (NULL);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * Find the mount point under which the user-specified path resides
7750Sstevel@tonic-gate  * and set the 'dfr_mte' field of '*dfrp' to point to the mount table entry.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static void
7780Sstevel@tonic-gate path_mount_entry(struct df_request *dfrp, dev_t devno)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	char			dirpath[MAXPATHLEN];
7810Sstevel@tonic-gate 	char			*dir = dfrp->dfr_cmd_arg;
7820Sstevel@tonic-gate 	struct mtab_entry	*match, *tmatch;
7830Sstevel@tonic-gate 	int i;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * Expand the given path to get a canonical version (i.e. an absolute
7870Sstevel@tonic-gate 	 * path without symbolic links).
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	if (realpath(dir, dirpath) == NULL) {
7900Sstevel@tonic-gate 		errmsg(ERR_PERROR, "cannot canonicalize %s:", dir);
7910Sstevel@tonic-gate 		return;
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * If the mnt point is lofs, search from the top of entries from
7950Sstevel@tonic-gate 	 * /etc/mnttab and return the first entry that matches the devid
7960Sstevel@tonic-gate 	 * For non-lofs mount points, return the first entry from the bottom
7970Sstevel@tonic-gate 	 * of the entries in /etc/mnttab that matches on the devid field
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	match = NULL;
8000Sstevel@tonic-gate 	if (dfrp->dfr_fstype && EQ(dfrp->dfr_fstype, MNTTYPE_LOFS)) {
8010Sstevel@tonic-gate 		for (i = 0; i < mount_table_entries; i++) {
8020Sstevel@tonic-gate 			if (match = devid_matches(i, devno))
8030Sstevel@tonic-gate 				break;
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 	} else {
8060Sstevel@tonic-gate 		for (i = mount_table_entries - 1; i >= 0; i--) {
8070Sstevel@tonic-gate 			if (tmatch = devid_matches(i, devno)) {
8080Sstevel@tonic-gate 				/*
8090Sstevel@tonic-gate 				 * If executing in a zone, there might be lofs
8100Sstevel@tonic-gate 				 * mounts for which the real mount point is
8110Sstevel@tonic-gate 				 * invisible; accept the "best fit" for this
8120Sstevel@tonic-gate 				 * devid.
8130Sstevel@tonic-gate 				 */
8140Sstevel@tonic-gate 				match = tmatch;
8150Sstevel@tonic-gate 				if (!EQ(match->mte_mount->mnt_fstype,
8165094Slling 				    MNTTYPE_LOFS)) {
8170Sstevel@tonic-gate 					break;
8180Sstevel@tonic-gate 				}
8190Sstevel@tonic-gate 			}
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 	if (! match) {
8230Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS,
8245094Slling 		    "Could not find mount point for %s", dir);
8250Sstevel@tonic-gate 		return;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 	dfrp->dfr_mte = match;
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Execute a single FS-specific df command for all given requests
8320Sstevel@tonic-gate  * Return 0 if successful, 1 otherwise.
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate static int
8350Sstevel@tonic-gate run_fs_specific_df(struct df_request request_list[], int entries)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	int	i;
8380Sstevel@tonic-gate 	int	argv_index;
8390Sstevel@tonic-gate 	char	**argv;
8400Sstevel@tonic-gate 	size_t	size;
8410Sstevel@tonic-gate 	pid_t	pid;
8420Sstevel@tonic-gate 	int	status;
8430Sstevel@tonic-gate 	char	cmd_path[MAXPATHLEN];
8440Sstevel@tonic-gate 	char	*fstype;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	if (entries == 0)
8470Sstevel@tonic-gate 		return (0);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	fstype = request_list[0].dfr_fstype;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (F_option && ! EQ(FSType, fstype))
8520Sstevel@tonic-gate 		return (0);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	(void) sprintf(cmd_path, "%s%s/df", FS_LIBPATH, fstype);
8550Sstevel@tonic-gate 	/*
8560Sstevel@tonic-gate 	 * Argv entries:
8570Sstevel@tonic-gate 	 *		1 for the path
8580Sstevel@tonic-gate 	 *		2 for -o <options>
8590Sstevel@tonic-gate 	 *		1 for the generic options that we propagate
8600Sstevel@tonic-gate 	 *		1 for the terminating NULL pointer
8610Sstevel@tonic-gate 	 *		n for the number of user-specified arguments
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	size = (5 + entries) * sizeof (char *);
8640Sstevel@tonic-gate 	argv = xmalloc(size);
8650Sstevel@tonic-gate 	(void) memset(argv, 0, size);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	argv[0] = cmd_path;
8680Sstevel@tonic-gate 	argv_index = 1;
8690Sstevel@tonic-gate 	if (o_option) {
8700Sstevel@tonic-gate 		argv[argv_index++] = "-o";
8710Sstevel@tonic-gate 		argv[argv_index++] = o_option_arg;
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * Check if we need to propagate any generic options
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	if (df_options_len > 1)
8780Sstevel@tonic-gate 		argv[argv_index++] = df_options;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * If there is a user-specified path, we pass that to the
8820Sstevel@tonic-gate 	 * FS-specific df. Otherwise, we are guaranteed to have a mount
8830Sstevel@tonic-gate 	 * point, since a request without a user path implies that
8840Sstevel@tonic-gate 	 * we are reporting only on mounted file systems.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	for (i = 0; i < entries; i++) {
8870Sstevel@tonic-gate 		struct df_request *dfrp = &request_list[i];
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		argv[argv_index++] = (dfrp->dfr_cmd_arg == NULL)
8905094Slling 		    ? DFR_MOUNT_POINT(dfrp)
8915094Slling 		    : dfrp->dfr_cmd_arg;
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	if (V_option) {
8950Sstevel@tonic-gate 		for (i = 0; i < argv_index-1; i++)
8960Sstevel@tonic-gate 			(void) printf("%s ", argv[i]);
8970Sstevel@tonic-gate 		(void) printf("%s\n", argv[i]);
8980Sstevel@tonic-gate 		return (0);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	pid = fork();
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (pid == -1) {
9040Sstevel@tonic-gate 		errmsg(ERR_PERROR, "cannot fork process:");
9050Sstevel@tonic-gate 		return (1);
9060Sstevel@tonic-gate 	} else if (pid == 0) {
9070Sstevel@tonic-gate 		(void) execv(cmd_path, argv);
9080Sstevel@tonic-gate 		if (errno == ENOENT)
9090Sstevel@tonic-gate 			errmsg(ERR_NOFLAGS,
9105094Slling 			    "operation not applicable for FSType %s",
9115094Slling 			    fstype);
9120Sstevel@tonic-gate 		else
9130Sstevel@tonic-gate 			errmsg(ERR_PERROR, "cannot execute %s:", cmd_path);
9140Sstevel@tonic-gate 		exit(2);
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	/*
9180Sstevel@tonic-gate 	 * Reap the child
9190Sstevel@tonic-gate 	 */
9200Sstevel@tonic-gate 	for (;;) {
9210Sstevel@tonic-gate 		pid_t wpid = waitpid(pid, &status, 0);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		if (wpid == -1)
9240Sstevel@tonic-gate 			if (errno == EINTR)
9250Sstevel@tonic-gate 				continue;
9260Sstevel@tonic-gate 			else {
9270Sstevel@tonic-gate 				errmsg(ERR_PERROR, "waitpid error:");
9280Sstevel@tonic-gate 				return (1);
9290Sstevel@tonic-gate 			}
9300Sstevel@tonic-gate 		else
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	return ((WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : 1);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * Remove from the request list all requests that do not apply.
9410Sstevel@tonic-gate  * Notice that the subsequent processing of the requests depends on
9420Sstevel@tonic-gate  * the sanity checking performed by this function.
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate static int
9450Sstevel@tonic-gate prune_list(struct df_request request_list[],
9460Sstevel@tonic-gate 		size_t n_requests,
9470Sstevel@tonic-gate 		size_t *valid_requests)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	size_t	i;
9500Sstevel@tonic-gate 	size_t	n_valid = 0;
9510Sstevel@tonic-gate 	int	errors = 0;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	for (i = 0; i < n_requests; i++) {
9540Sstevel@tonic-gate 		struct df_request *dfrp = &request_list[i];
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		/*
9570Sstevel@tonic-gate 		 * Skip file systems that are not mounted if either the
9580Sstevel@tonic-gate 		 * -l or -n options were specified. If none of these options
9590Sstevel@tonic-gate 		 * are present, the appropriate FS-specific df will be invoked.
9600Sstevel@tonic-gate 		 */
9610Sstevel@tonic-gate 		if (! DFR_ISMOUNTEDFS(dfrp)) {
9620Sstevel@tonic-gate 			if (l_option || n_option) {
9630Sstevel@tonic-gate 				errmsg(ERR_NOFLAGS,
9645094Slling 				    "%s option incompatible with unmounted "
9655094Slling 				    "special device (%s)",
9665094Slling 				    l_option ? "-l" : "-n", dfrp->dfr_cmd_arg);
9670Sstevel@tonic-gate 				dfrp->dfr_valid = FALSE;
9680Sstevel@tonic-gate 				errors++;
9690Sstevel@tonic-gate 			}
9700Sstevel@tonic-gate 			else
9710Sstevel@tonic-gate 				n_valid++;
9720Sstevel@tonic-gate 			continue;
9730Sstevel@tonic-gate 		}
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		/*
9760Sstevel@tonic-gate 		 * Check for inconsistency between the argument of -F and
9770Sstevel@tonic-gate 		 * the actual file system type.
9780Sstevel@tonic-gate 		 * If there is an inconsistency and the user specified a
9790Sstevel@tonic-gate 		 * path, this is an error since we are asked to interpret
9800Sstevel@tonic-gate 		 * the path using the wrong file system type. If there is
9810Sstevel@tonic-gate 		 * no path associated with this request, we quietly ignore it.
9820Sstevel@tonic-gate 		 */
9830Sstevel@tonic-gate 		if (F_option && ! EQ(dfrp->dfr_fstype, FSType)) {
9840Sstevel@tonic-gate 			dfrp->dfr_valid = FALSE;
9850Sstevel@tonic-gate 			if (dfrp->dfr_cmd_arg != NULL) {
9860Sstevel@tonic-gate 				errmsg(ERR_NOFLAGS,
9870Sstevel@tonic-gate 				"Warning: %s mounted as a %s file system",
9885094Slling 				    dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
9890Sstevel@tonic-gate 				errors++;
9900Sstevel@tonic-gate 			}
9910Sstevel@tonic-gate 			continue;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		/*
9950Sstevel@tonic-gate 		 * Skip remote file systems if the -l option is present
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		if (l_option && is_remote_fs(dfrp->dfr_fstype)) {
9980Sstevel@tonic-gate 			if (dfrp->dfr_cmd_arg != NULL) {
9990Sstevel@tonic-gate 				errmsg(ERR_NOFLAGS,
10005094Slling 				    "Warning: %s is not a local file system",
10015094Slling 				    dfrp->dfr_cmd_arg);
10020Sstevel@tonic-gate 				errors++;
10030Sstevel@tonic-gate 			}
10040Sstevel@tonic-gate 			dfrp->dfr_valid = FALSE;
10050Sstevel@tonic-gate 			continue;
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		/*
10090Sstevel@tonic-gate 		 * Skip file systems mounted as "ignore" unless the -a option
10100Sstevel@tonic-gate 		 * is present, or the user explicitly specified them on
10110Sstevel@tonic-gate 		 * the command line.
10120Sstevel@tonic-gate 		 */
10130Sstevel@tonic-gate 		if (dfrp->dfr_mte->mte_ignore &&
10145094Slling 		    ! (a_option || dfrp->dfr_cmd_arg)) {
10150Sstevel@tonic-gate 			dfrp->dfr_valid = FALSE;
10160Sstevel@tonic-gate 			continue;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 		n_valid++;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 	*valid_requests = n_valid;
10220Sstevel@tonic-gate 	return (errors);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate  * Print the appropriate header for the requested output format.
10280Sstevel@tonic-gate  * Options are checked in order of their precedence.
10290Sstevel@tonic-gate  */
10300Sstevel@tonic-gate static void
1031821Sdh145677 print_header(void)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	if (use_scaling) { /* this comes from the -h option */
10340Sstevel@tonic-gate 		int arg = 'h';
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		(void) printf("%-*s %*s %*s %*s %-*s %s\n",
10375094Slling 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
10380Sstevel@tonic-gate #ifdef XPG4
10395094Slling 		    SCALED_WIDTH, TRANSLATE("Size"),
10405094Slling 		    SCALED_WIDTH, TRANSLATE("Used"),
10415094Slling 		    AVAILABLE_WIDTH, TRANSLATE("Available"),
10425094Slling 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
10430Sstevel@tonic-gate #else
10445094Slling 		    SCALED_WIDTH, TRANSLATE("size"),
10455094Slling 		    SCALED_WIDTH, TRANSLATE("used"),
10465094Slling 		    AVAILABLE_WIDTH, TRANSLATE("avail"),
10475094Slling 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
10480Sstevel@tonic-gate #endif
10495094Slling 		    TRANSLATE("Mounted on"));
10500Sstevel@tonic-gate 		SET_OPTION(h);
10510Sstevel@tonic-gate 		return;
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	if (k_option) {
10540Sstevel@tonic-gate 		int arg = 'h';
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
10575094Slling 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
10580Sstevel@tonic-gate #ifdef XPG4
10595094Slling 		    KBYTE_WIDTH, TRANSLATE("1024-blocks"),
10605094Slling 		    KBYTE_WIDTH, TRANSLATE("Used"),
10615094Slling 		    KBYTE_WIDTH, TRANSLATE("Available"),
10625094Slling 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
10630Sstevel@tonic-gate #else
10645094Slling 		    KBYTE_WIDTH, TRANSLATE("kbytes"),
10655094Slling 		    KBYTE_WIDTH, TRANSLATE("used"),
10665094Slling 		    KBYTE_WIDTH, TRANSLATE("avail"),
10675094Slling 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
10680Sstevel@tonic-gate #endif
10695094Slling 		    TRANSLATE("Mounted on"));
10700Sstevel@tonic-gate 		SET_OPTION(h);
10710Sstevel@tonic-gate 		return;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 	/* Added for XCU4 compliance */
10740Sstevel@tonic-gate 	if (P_option) {
10750Sstevel@tonic-gate 		int arg = 'h';
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
10785094Slling 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
10795094Slling 		    KBYTE_WIDTH, TRANSLATE("512-blocks"),
10805094Slling 		    KBYTE_WIDTH, TRANSLATE("Used"),
10815094Slling 		    KBYTE_WIDTH, TRANSLATE("Available"),
10825094Slling 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
10835094Slling 		    TRANSLATE("Mounted on"));
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		SET_OPTION(h);
10860Sstevel@tonic-gate 		return;
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 	/* End XCU4 */
10890Sstevel@tonic-gate 	if (v_option) {
10900Sstevel@tonic-gate 		(void) printf("%-*s %-*s %*s %*s %*s %-*s\n",
10915094Slling 		    IBCS2_MOUNT_POINT_WIDTH, TRANSLATE("Mount Dir"),
10925094Slling 		    IBCS2_FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
10935094Slling 		    BLOCK_WIDTH, TRANSLATE("blocks"),
10945094Slling 		    BLOCK_WIDTH, TRANSLATE("used"),
10955094Slling 		    BLOCK_WIDTH, TRANSLATE("free"),
10965094Slling 		    CAPACITY_WIDTH, TRANSLATE(" %used"));
10970Sstevel@tonic-gate 		return;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 	if (e_option) {
11000Sstevel@tonic-gate 		(void) printf(gettext("%-*s %*s\n"),
11015094Slling 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
11025094Slling 		    BLOCK_WIDTH, TRANSLATE("ifree"));
11030Sstevel@tonic-gate 		return;
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 	if (b_option) {
11060Sstevel@tonic-gate 		(void) printf(gettext("%-*s %*s\n"),
11075094Slling 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
11085094Slling 		    BLOCK_WIDTH, TRANSLATE("avail"));
11090Sstevel@tonic-gate 		return;
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
11160Sstevel@tonic-gate  * result in the caller-supplied buffer.
11170Sstevel@tonic-gate  * The given number is in units of "unit_from" size, but the
11180Sstevel@tonic-gate  * converted number will be in units of "unit_to" size. The unit sizes
11190Sstevel@tonic-gate  * must be powers of 2.
11200Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
11210Sstevel@tonic-gate  * converted to "-1".
11220Sstevel@tonic-gate  * Returns a pointer to the caller-supplied buffer.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate static char *
11250Sstevel@tonic-gate number_to_string(
11260Sstevel@tonic-gate 			char *buf,		/* put the result here */
11270Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
11280Sstevel@tonic-gate 			int unit_from,		/* from units of this size */
11290Sstevel@tonic-gate 			int unit_to)		/* to units of this size */
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	if ((long long)number == (long long)-1)
11320Sstevel@tonic-gate 		(void) strcpy(buf, "-1");
11330Sstevel@tonic-gate 	else {
11340Sstevel@tonic-gate 		if (unit_from == unit_to)
11350Sstevel@tonic-gate 			(void) sprintf(buf, "%llu", number);
11360Sstevel@tonic-gate 		else if (unit_from < unit_to)
11370Sstevel@tonic-gate 			(void) sprintf(buf, "%llu",
11380Sstevel@tonic-gate 			    number / (unsigned long long)(unit_to / unit_from));
11390Sstevel@tonic-gate 		else
11400Sstevel@tonic-gate 			(void) sprintf(buf, "%llu",
11410Sstevel@tonic-gate 			    number * (unsigned long long)(unit_from / unit_to));
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	return (buf);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate /*
11470Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
11480Sstevel@tonic-gate  * result in the caller-supplied buffer.
11490Sstevel@tonic-gate  * The given number is in units of "unit_from" size,
11500Sstevel@tonic-gate  * this will first be converted to a number in 1024 or 1000 byte size,
11510Sstevel@tonic-gate  * depending on the scaling factor.
11520Sstevel@tonic-gate  * Then the number is scaled down until it is small enough to be in a good
11530Sstevel@tonic-gate  * human readable format i.e. in the range 0 thru scale-1.
11540Sstevel@tonic-gate  * If it's smaller than 10 there's room enough to provide one decimal place.
11550Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
11560Sstevel@tonic-gate  * converted to "-1".
11570Sstevel@tonic-gate  * Returns a pointer to the caller-supplied buffer.
11580Sstevel@tonic-gate  */
11590Sstevel@tonic-gate static char *
11600Sstevel@tonic-gate number_to_scaled_string(
11610Sstevel@tonic-gate 			numbuf_t buf,		/* put the result here */
11620Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
11630Sstevel@tonic-gate 			int unit_from,
11640Sstevel@tonic-gate 			int scale)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	unsigned long long save = 0;
11670Sstevel@tonic-gate 	char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */
11680Sstevel@tonic-gate 	char *uom = M;    /* unit of measurement, initially 'K' (=M[0]) */
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
11710Sstevel@tonic-gate 		(void) strcpy(buf, "-1");
11720Sstevel@tonic-gate 		return (buf);
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * Convert number from unit_from to given scale (1024 or 1000).
11770Sstevel@tonic-gate 	 * This means multiply number by unit_from and divide by scale.
11780Sstevel@tonic-gate 	 *
11790Sstevel@tonic-gate 	 * Would like to multiply by unit_from and then divide by scale,
11800Sstevel@tonic-gate 	 * but if the first multiplication would overflow, then need to
11810Sstevel@tonic-gate 	 * divide by scale and then multiply by unit_from.
11820Sstevel@tonic-gate 	 */
11830Sstevel@tonic-gate 	if (number > (UINT64_MAX / (unsigned long long)unit_from)) {
11840Sstevel@tonic-gate 		number = (number / (unsigned long long)scale) *
11850Sstevel@tonic-gate 		    (unsigned long long)unit_from;
11860Sstevel@tonic-gate 	} else {
11870Sstevel@tonic-gate 		number = (number * (unsigned long long)unit_from) /
11880Sstevel@tonic-gate 		    (unsigned long long)scale;
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
11930Sstevel@tonic-gate 	 * Stop scaling when we reached exa bytes, then something is
11940Sstevel@tonic-gate 	 * probably wrong with our number.
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	while ((number >= scale) && (*uom != 'E')) {
11980Sstevel@tonic-gate 		uom++; /* next unit of measurement */
11990Sstevel@tonic-gate 		save = number;
12000Sstevel@tonic-gate 		number = (number + (scale / 2)) / scale;
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
12030Sstevel@tonic-gate 	if (save && ((save / scale) < 10)) {
12040Sstevel@tonic-gate 		/* sprintf() will round for us */
12050Sstevel@tonic-gate 		float fnum = (float)save / scale;
12060Sstevel@tonic-gate 		(void) sprintf(buf, "%2.1f%c", fnum, *uom);
12070Sstevel@tonic-gate 	} else {
12080Sstevel@tonic-gate 		(void) sprintf(buf, "%4llu%c", number, *uom);
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 	return (buf);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
1213789Sahrens /*
1214789Sahrens  * The statvfs() implementation allows us to return only two values, the total
1215789Sahrens  * number of blocks and the number of blocks free.  The equation 'used = total -
1216789Sahrens  * free' will not work for ZFS filesystems, due to the nature of pooled storage.
1217789Sahrens  * We choose to return values in the statvfs structure that will produce correct
1218789Sahrens  * results for 'used' and 'available', but not 'total'.  This function will open
1219789Sahrens  * the underlying ZFS dataset if necessary and get the real value.
1220789Sahrens  */
1221789Sahrens static void
1222789Sahrens adjust_total_blocks(struct df_request *dfrp, fsblkcnt64_t *total,
1223789Sahrens     uint64_t blocksize)
1224789Sahrens {
1225789Sahrens 	char *dataset, *slash;
12265378Sck153898 	boolean_t first = TRUE;
12275378Sck153898 	uint64_t quota = 0;
1228789Sahrens 
12295378Sck153898 	if (strcmp(DFR_FSTYPE(dfrp), MNTTYPE_ZFS) != 0 || !load_libzfs())
1230789Sahrens 		return;
1231789Sahrens 
1232789Sahrens 	/*
1233789Sahrens 	 * We want to get the total size for this filesystem as bounded by any
1234789Sahrens 	 * quotas. In order to do this, we start at the current filesystem and
12355378Sck153898 	 * work upwards looking for the smallest quota.  When we reach the
12365378Sck153898 	 * pool itself, the quota is the amount used plus the amount
1237789Sahrens 	 * available.
1238789Sahrens 	 */
1239789Sahrens 	if ((dataset = strdup(DFR_SPECIAL(dfrp))) == NULL)
1240789Sahrens 		return;
1241789Sahrens 
1242789Sahrens 	slash = dataset + strlen(dataset);
12435378Sck153898 	while (slash != NULL) {
12445378Sck153898 		zfs_handle_t *zhp;
12455378Sck153898 		uint64_t this_quota;
12465378Sck153898 
1247789Sahrens 		*slash = '\0';
1248789Sahrens 
12495378Sck153898 		zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET);
12505378Sck153898 		if (zhp == NULL)
12515378Sck153898 			break;
12525378Sck153898 
12535378Sck153898 		/* true at first iteration of loop */
12545378Sck153898 		if (first) {
12555378Sck153898 			quota = _zfs_prop_get_int(zhp, ZFS_PROP_REFQUOTA);
12565378Sck153898 			if (quota == 0)
12575378Sck153898 				quota = UINT64_MAX;
12585378Sck153898 			first = FALSE;
1259789Sahrens 		}
1260789Sahrens 
12615378Sck153898 		this_quota = _zfs_prop_get_int(zhp, ZFS_PROP_QUOTA);
12625378Sck153898 		if (this_quota && this_quota < quota)
12635378Sck153898 			quota = this_quota;
12645378Sck153898 
12655378Sck153898 		/* true at last iteration of loop */
12665378Sck153898 		if ((slash = strrchr(dataset, '/')) == NULL) {
12675378Sck153898 			uint64_t size;
12685378Sck153898 
12695378Sck153898 			size = _zfs_prop_get_int(zhp, ZFS_PROP_USED) +
12705378Sck153898 			    _zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
12715378Sck153898 			if (size < quota)
12725378Sck153898 				quota = size;
1273789Sahrens 		}
1274789Sahrens 
1275789Sahrens 		_zfs_close(zhp);
1276789Sahrens 	}
1277789Sahrens 
1278*11443SSanjeev.Bagewadi@Sun.COM 	/*
1279*11443SSanjeev.Bagewadi@Sun.COM 	 * Modify total only if we managed to get some stats from libzfs.
1280*11443SSanjeev.Bagewadi@Sun.COM 	 */
1281*11443SSanjeev.Bagewadi@Sun.COM 	if (quota != 0)
1282*11443SSanjeev.Bagewadi@Sun.COM 		*total = quota / blocksize;
1283789Sahrens 	free(dataset);
1284789Sahrens }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * The output will appear properly columnized regardless of the names of
12880Sstevel@tonic-gate  * the various fields
12890Sstevel@tonic-gate  */
12900Sstevel@tonic-gate static void
12910Sstevel@tonic-gate g_output(struct df_request *dfrp, struct statvfs64 *fsp)
12920Sstevel@tonic-gate {
12930Sstevel@tonic-gate 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
1294789Sahrens 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
12950Sstevel@tonic-gate 	numbuf_t	total_blocks_buf;
12960Sstevel@tonic-gate 	numbuf_t	total_files_buf;
12970Sstevel@tonic-gate 	numbuf_t	free_blocks_buf;
12980Sstevel@tonic-gate 	numbuf_t	available_blocks_buf;
12990Sstevel@tonic-gate 	numbuf_t	free_files_buf;
13000Sstevel@tonic-gate 	numbuf_t	fname_buf;
13010Sstevel@tonic-gate 	char		*temp_buf;
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate #define	DEFINE_STR_LEN(var)			\
13040Sstevel@tonic-gate 	static char *var##_str;			\
13050Sstevel@tonic-gate 	static size_t var##_len
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate #define	SET_STR_LEN(name, var)\
13080Sstevel@tonic-gate 	if (! var##_str) {\
13090Sstevel@tonic-gate 		var##_str = TRANSLATE(name); \
13100Sstevel@tonic-gate 		var##_len = strlen(var##_str); \
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	DEFINE_STR_LEN(block_size);
13140Sstevel@tonic-gate 	DEFINE_STR_LEN(frag_size);
13150Sstevel@tonic-gate 	DEFINE_STR_LEN(total_blocks);
13160Sstevel@tonic-gate 	DEFINE_STR_LEN(free_blocks);
13170Sstevel@tonic-gate 	DEFINE_STR_LEN(available);
13180Sstevel@tonic-gate 	DEFINE_STR_LEN(total_files);
13190Sstevel@tonic-gate 	DEFINE_STR_LEN(free_files);
13200Sstevel@tonic-gate 	DEFINE_STR_LEN(fstype);
13210Sstevel@tonic-gate 	DEFINE_STR_LEN(fsys_id);
13220Sstevel@tonic-gate 	DEFINE_STR_LEN(fname);
13230Sstevel@tonic-gate 	DEFINE_STR_LEN(flag);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * TRANSLATION_NOTE
13270Sstevel@tonic-gate 	 * The first argument of each of the following macro invocations is a
13280Sstevel@tonic-gate 	 * string that needs to be translated.
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	SET_STR_LEN("block size", block_size);
13310Sstevel@tonic-gate 	SET_STR_LEN("frag size", frag_size);
13320Sstevel@tonic-gate 	SET_STR_LEN("total blocks", total_blocks);
13330Sstevel@tonic-gate 	SET_STR_LEN("free blocks", free_blocks);
13340Sstevel@tonic-gate 	SET_STR_LEN("available", available);
13350Sstevel@tonic-gate 	SET_STR_LEN("total files", total_files);
13360Sstevel@tonic-gate 	SET_STR_LEN("free files", free_files);
13370Sstevel@tonic-gate 	SET_STR_LEN("fstype", fstype);
13380Sstevel@tonic-gate 	SET_STR_LEN("filesys id", fsys_id);
13390Sstevel@tonic-gate 	SET_STR_LEN("filename length", fname);
13400Sstevel@tonic-gate 	SET_STR_LEN("flag", flag);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate #define	NCOL1_WIDTH	(int)MAX3(BLOCK_WIDTH, NFILES_WIDTH, FSTYPE_WIDTH)
13430Sstevel@tonic-gate #define	NCOL2_WIDTH	(int)MAX3(BLOCK_WIDTH, FSID_WIDTH, FLAG_WIDTH) + 2
13440Sstevel@tonic-gate #define	NCOL3_WIDTH	(int)MAX3(BSIZE_WIDTH, BLOCK_WIDTH, NAMELEN_WIDTH)
13450Sstevel@tonic-gate #define	NCOL4_WIDTH	(int)MAX(FRAGSIZE_WIDTH, NFILES_WIDTH)
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate #define	SCOL1_WIDTH	(int)MAX3(total_blocks_len, free_files_len, fstype_len)
13480Sstevel@tonic-gate #define	SCOL2_WIDTH	(int)MAX3(free_blocks_len, fsys_id_len, flag_len)
13490Sstevel@tonic-gate #define	SCOL3_WIDTH	(int)MAX3(block_size_len, available_len, fname_len)
13500Sstevel@tonic-gate #define	SCOL4_WIDTH	(int)MAX(frag_size_len, total_files_len)
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	temp_buf = xmalloc(
13530Sstevel@tonic-gate 	    MAX(MOUNT_POINT_WIDTH, strlen(DFR_MOUNT_POINT(dfrp)))
13540Sstevel@tonic-gate 	    + MAX(SPECIAL_DEVICE_WIDTH, strlen(DFR_SPECIAL(dfrp)))
13550Sstevel@tonic-gate 	    + 20); /* plus slop - nulls & formatting */
13560Sstevel@tonic-gate 	(void) sprintf(temp_buf, "%-*s(%-*s):",
13575094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
13585094Slling 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp));
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	(void) printf("%-*s %*lu %-*s %*lu %-*s\n",
13615094Slling 	    NCOL1_WIDTH + 1 + SCOL1_WIDTH + 1 + NCOL2_WIDTH + 1 +  SCOL2_WIDTH,
13625094Slling 	    temp_buf,
13635094Slling 	    NCOL3_WIDTH, fsp->f_bsize, SCOL3_WIDTH, block_size_str,
13645094Slling 	    NCOL4_WIDTH, fsp->f_frsize, SCOL4_WIDTH, frag_size_str);
13650Sstevel@tonic-gate 	free(temp_buf);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * Adjust available_blocks value -  it can be less than 0 on
13690Sstevel@tonic-gate 	 * a 4.x file system. Reset it to 0 in order to avoid printing
13700Sstevel@tonic-gate 	 * negative numbers.
13710Sstevel@tonic-gate 	 */
13720Sstevel@tonic-gate 	if ((long long)available_blocks < (long long)0)
13730Sstevel@tonic-gate 		available_blocks = (fsblkcnt64_t)0;
13740Sstevel@tonic-gate 
1375789Sahrens 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1376789Sahrens 
13770Sstevel@tonic-gate 	(void) printf("%*s %-*s %*s %-*s %*s %-*s %*s %-*s\n",
13785094Slling 	    NCOL1_WIDTH, number_to_string(total_blocks_buf,
13795094Slling 	    total_blocks, fsp->f_frsize, 512),
13805094Slling 	    SCOL1_WIDTH, total_blocks_str,
13815094Slling 	    NCOL2_WIDTH, number_to_string(free_blocks_buf,
13825094Slling 	    fsp->f_bfree, fsp->f_frsize, 512),
13835094Slling 	    SCOL2_WIDTH, free_blocks_str,
13845094Slling 	    NCOL3_WIDTH, number_to_string(available_blocks_buf,
13855094Slling 	    available_blocks, fsp->f_frsize, 512),
13865094Slling 	    SCOL3_WIDTH, available_str,
13875094Slling 	    NCOL4_WIDTH, number_to_string(total_files_buf,
13885094Slling 	    fsp->f_files, 1, 1),
13895094Slling 	    SCOL4_WIDTH, total_files_str);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	(void) printf("%*s %-*s %*lu %-*s %s\n",
13925094Slling 	    NCOL1_WIDTH, number_to_string(free_files_buf,
13935094Slling 	    fsp->f_ffree, 1, 1),
13945094Slling 	    SCOL1_WIDTH, free_files_str,
13955094Slling 	    NCOL2_WIDTH, fsp->f_fsid, SCOL2_WIDTH, fsys_id_str,
13965094Slling 	    fsp->f_fstr);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	(void) printf("%*s %-*s %#*.*lx %-*s %*s %-*s\n\n",
13995094Slling 	    NCOL1_WIDTH, fsp->f_basetype, SCOL1_WIDTH, fstype_str,
14005094Slling 	    NCOL2_WIDTH, NCOL2_WIDTH-2, fsp->f_flag, SCOL2_WIDTH, flag_str,
14015094Slling 	    NCOL3_WIDTH, number_to_string(fname_buf,
14025094Slling 	    (unsigned long long)fsp->f_namemax, 1, 1),
14035094Slling 	    SCOL3_WIDTH, fname_str);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate static void
14080Sstevel@tonic-gate k_output(struct df_request *dfrp, struct statvfs64 *fsp)
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	fsblkcnt64_t total_blocks		= fsp->f_blocks;
14110Sstevel@tonic-gate 	fsblkcnt64_t	free_blocks		= fsp->f_bfree;
14120Sstevel@tonic-gate 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
14130Sstevel@tonic-gate 	fsblkcnt64_t	used_blocks;
14140Sstevel@tonic-gate 	char 		*file_system		= DFR_SPECIAL(dfrp);
14150Sstevel@tonic-gate 	numbuf_t	total_blocks_buf;
14160Sstevel@tonic-gate 	numbuf_t	used_blocks_buf;
14170Sstevel@tonic-gate 	numbuf_t	available_blocks_buf;
14180Sstevel@tonic-gate 	char 		capacity_buf[LINEBUF_SIZE];
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * If the free block count is -1, don't trust anything but the total
14220Sstevel@tonic-gate 	 * number of blocks.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	if (free_blocks == (fsblkcnt64_t)-1) {
14250Sstevel@tonic-gate 		used_blocks = (fsblkcnt64_t)-1;
14260Sstevel@tonic-gate 		(void) strcpy(capacity_buf, "  100%");
14270Sstevel@tonic-gate 	} else {
14280Sstevel@tonic-gate 		fsblkcnt64_t reserved_blocks = free_blocks - available_blocks;
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		used_blocks	= total_blocks - free_blocks;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 		/*
14330Sstevel@tonic-gate 		 * The capacity estimation is bogus when available_blocks is 0
14340Sstevel@tonic-gate 		 * and the super-user has allocated more space. The reason
14350Sstevel@tonic-gate 		 * is that reserved_blocks is inaccurate in that case, because
14360Sstevel@tonic-gate 		 * when the super-user allocates space, free_blocks is updated
14370Sstevel@tonic-gate 		 * but available_blocks is not (since it can't drop below 0).
14380Sstevel@tonic-gate 		 *
14390Sstevel@tonic-gate 		 * XCU4 and POSIX.2 require that any fractional result of the
14400Sstevel@tonic-gate 		 * capacity estimation be rounded to the next highest integer,
14410Sstevel@tonic-gate 		 * hence the addition of 0.5.
14420Sstevel@tonic-gate 		 */
14430Sstevel@tonic-gate 		(void) sprintf(capacity_buf, "%5.0f%%",
14445094Slling 		    (total_blocks == 0) ? 0.0 :
14455094Slling 		    ((double)used_blocks /
14465094Slling 		    (double)(total_blocks - reserved_blocks))
14475094Slling 		    * 100.0 + 0.5);
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * The available_blocks can be less than 0 on a 4.x file system.
14520Sstevel@tonic-gate 	 * Reset it to 0 in order to avoid printing negative numbers.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	if ((long long)available_blocks < (long long)0)
14550Sstevel@tonic-gate 		available_blocks = (fsblkcnt64_t)0;
14560Sstevel@tonic-gate 	/*
14570Sstevel@tonic-gate 	 * Print long special device names (usually NFS mounts) in a line
14580Sstevel@tonic-gate 	 * by themselves when the output is directed to a terminal.
14590Sstevel@tonic-gate 	 */
14600Sstevel@tonic-gate 	if (tty_output && strlen(file_system) > (size_t)FILESYSTEM_WIDTH) {
14610Sstevel@tonic-gate 		(void) printf("%s\n", file_system);
14620Sstevel@tonic-gate 		file_system = "";
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
1465789Sahrens 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1466789Sahrens 
14670Sstevel@tonic-gate 	if (use_scaling) { /* comes from the -h option */
14680Sstevel@tonic-gate 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
14695094Slling 	    FILESYSTEM_WIDTH, file_system,
14705094Slling 	    SCALED_WIDTH, number_to_scaled_string(total_blocks_buf,
14715094Slling 	    total_blocks, fsp->f_frsize, scale),
14725094Slling 	    SCALED_WIDTH, number_to_scaled_string(used_blocks_buf,
14735094Slling 	    used_blocks, fsp->f_frsize, scale),
14745094Slling 	    AVAILABLE_WIDTH, number_to_scaled_string(available_blocks_buf,
14755094Slling 	    available_blocks, fsp->f_frsize, scale),
14765094Slling 	    CAPACITY_WIDTH, capacity_buf,
14775094Slling 	    DFR_MOUNT_POINT(dfrp));
14780Sstevel@tonic-gate 		return;
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	if (v_option) {
14820Sstevel@tonic-gate 	(void) printf("%-*.*s %-*.*s %*lld %*lld %*lld %-.*s\n",
14835094Slling 	    IBCS2_MOUNT_POINT_WIDTH, IBCS2_MOUNT_POINT_WIDTH,
14845094Slling 	    DFR_MOUNT_POINT(dfrp),
14855094Slling 	    IBCS2_FILESYSTEM_WIDTH, IBCS2_FILESYSTEM_WIDTH, file_system,
14865094Slling 	    BLOCK_WIDTH, total_blocks,
14875094Slling 	    BLOCK_WIDTH, used_blocks,
14885094Slling 	    BLOCK_WIDTH, available_blocks,
14895094Slling 	    CAPACITY_WIDTH,	capacity_buf);
14900Sstevel@tonic-gate 		return;
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if (P_option && !k_option) {
14940Sstevel@tonic-gate 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
14955094Slling 	    FILESYSTEM_WIDTH, file_system,
14965094Slling 	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
14975094Slling 	    total_blocks, fsp->f_frsize, 512),
14985094Slling 	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
14995094Slling 	    used_blocks, fsp->f_frsize, 512),
15005094Slling 	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
15015094Slling 	    available_blocks, fsp->f_frsize, 512),
15025094Slling 	    CAPACITY_WIDTH, capacity_buf,
15035094Slling 	    DFR_MOUNT_POINT(dfrp));
15040Sstevel@tonic-gate 	} else {
15050Sstevel@tonic-gate 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
15065094Slling 	    FILESYSTEM_WIDTH, file_system,
15075094Slling 	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
15085094Slling 	    total_blocks, fsp->f_frsize, 1024),
15095094Slling 	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
15105094Slling 	    used_blocks, fsp->f_frsize, 1024),
15115094Slling 	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
15125094Slling 	    available_blocks, fsp->f_frsize, 1024),
15135094Slling 	    CAPACITY_WIDTH,	capacity_buf,
15145094Slling 	    DFR_MOUNT_POINT(dfrp));
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate /*
15190Sstevel@tonic-gate  * The following is for internationalization support.
15200Sstevel@tonic-gate  */
15210Sstevel@tonic-gate static bool_int strings_initialized;
15220Sstevel@tonic-gate static char 	*files_str;
15230Sstevel@tonic-gate static char	*blocks_str;
15240Sstevel@tonic-gate static char	*total_str;
15250Sstevel@tonic-gate static char	*kilobytes_str;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate static void
1528821Sdh145677 strings_init(void)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	total_str = TRANSLATE("total");
15310Sstevel@tonic-gate #ifdef	_iBCS2
15320Sstevel@tonic-gate 	/* ISC/SCO print i-nodes instead of files */
15330Sstevel@tonic-gate 	if (sysv3_set)
15340Sstevel@tonic-gate 		files_str = TRANSLATE("i-nodes");
15350Sstevel@tonic-gate 	else
15360Sstevel@tonic-gate #endif	/* _iBCS2 */
15370Sstevel@tonic-gate 		files_str = TRANSLATE("files");
15380Sstevel@tonic-gate 	blocks_str = TRANSLATE("blocks");
15390Sstevel@tonic-gate 	kilobytes_str = TRANSLATE("kilobytes");
15400Sstevel@tonic-gate 	strings_initialized = TRUE;
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate #define	STRINGS_INIT()		if (!strings_initialized) strings_init()
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate static void
15470Sstevel@tonic-gate t_output(struct df_request *dfrp, struct statvfs64 *fsp)
15480Sstevel@tonic-gate {
1549789Sahrens 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
15500Sstevel@tonic-gate 	numbuf_t	total_blocks_buf;
15510Sstevel@tonic-gate 	numbuf_t	total_files_buf;
15520Sstevel@tonic-gate 	numbuf_t	free_blocks_buf;
15530Sstevel@tonic-gate 	numbuf_t	free_files_buf;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	STRINGS_INIT();
15560Sstevel@tonic-gate 
1557789Sahrens 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1558789Sahrens 
15590Sstevel@tonic-gate 	(void) printf("%-*s(%-*s): %*s %s %*s %s\n",
15605094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
15615094Slling 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
15625094Slling 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
15635094Slling 	    fsp->f_bfree, fsp->f_frsize, 512),
15645094Slling 	    blocks_str,
15655094Slling 	    NFILES_WIDTH, number_to_string(free_files_buf,
15665094Slling 	    fsp->f_ffree, 1, 1),
15675094Slling 	    files_str);
15680Sstevel@tonic-gate 	/*
15690Sstevel@tonic-gate 	 * The total column used to use the same space as the mnt pt & special
15700Sstevel@tonic-gate 	 * dev fields. However, this doesn't work with massive special dev
15710Sstevel@tonic-gate 	 * fields * (eg > 500 chars) causing an enormous amount of white space
15720Sstevel@tonic-gate 	 * before the total column (see bug 4100411). So the code was
15730Sstevel@tonic-gate 	 * simplified to set the total column at the usual gap.
15740Sstevel@tonic-gate 	 * This had the side effect of fixing a bug where the previously
15750Sstevel@tonic-gate 	 * used static buffer was overflowed by the same massive special dev.
15760Sstevel@tonic-gate 	 */
15770Sstevel@tonic-gate 	(void) printf("%*s: %*s %s %*s %s\n",
15785094Slling 	    MNT_SPEC_WIDTH, total_str,
15795094Slling 	    BLOCK_WIDTH, number_to_string(total_blocks_buf,
15805094Slling 	    total_blocks, fsp->f_frsize, 512),
15815094Slling 	    blocks_str,
15825094Slling 	    NFILES_WIDTH, number_to_string(total_files_buf,
15835094Slling 	    fsp->f_files, 1, 1),
15845094Slling 	    files_str);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate static void
15890Sstevel@tonic-gate eb_output(struct df_request *dfrp, struct statvfs64 *fsp)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	numbuf_t free_files_buf;
15920Sstevel@tonic-gate 	numbuf_t free_kbytes_buf;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	STRINGS_INIT();
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	(void) printf("%-*s(%-*s): %*s %s\n",
15975094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
15985094Slling 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
15995094Slling 	    MAX(KBYTE_WIDTH, NFILES_WIDTH),
16005094Slling 	    number_to_string(free_kbytes_buf,
16015094Slling 	    fsp->f_bfree, fsp->f_frsize, 1024),
16025094Slling 	    kilobytes_str);
16030Sstevel@tonic-gate 	(void) printf("%-*s(%-*s): %*s %s\n",
16045094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
16055094Slling 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
16065094Slling 	    MAX(NFILES_WIDTH, NFILES_WIDTH),
16075094Slling 	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1),
16085094Slling 	    files_str);
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate static void
16130Sstevel@tonic-gate e_output(struct df_request *dfrp, struct statvfs64 *fsp)
16140Sstevel@tonic-gate {
16150Sstevel@tonic-gate 	numbuf_t free_files_buf;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	(void) printf("%-*s %*s\n",
16185094Slling 	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
16195094Slling 	    NFILES_WIDTH,
16205094Slling 	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1));
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate static void
16250Sstevel@tonic-gate b_output(struct df_request *dfrp, struct statvfs64 *fsp)
16260Sstevel@tonic-gate {
16270Sstevel@tonic-gate 	numbuf_t free_blocks_buf;
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	(void) printf("%-*s %*s\n",
16305094Slling 	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
16315094Slling 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
16325094Slling 	    fsp->f_bfree, fsp->f_frsize, 1024));
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate /* ARGSUSED */
16370Sstevel@tonic-gate static void
16380Sstevel@tonic-gate n_output(struct df_request *dfrp, struct statvfs64 *fsp)
16390Sstevel@tonic-gate {
16400Sstevel@tonic-gate 	(void) printf("%-*s: %-*s\n",
16415094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
16425094Slling 	    FSTYPE_WIDTH, dfrp->dfr_fstype);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate static void
16470Sstevel@tonic-gate default_output(struct df_request *dfrp, struct statvfs64 *fsp)
16480Sstevel@tonic-gate {
16490Sstevel@tonic-gate 	numbuf_t free_blocks_buf;
16500Sstevel@tonic-gate 	numbuf_t free_files_buf;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	STRINGS_INIT();
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	(void) printf("%-*s(%-*s):%*s %s %*s %s\n",
16555094Slling 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
16565094Slling 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
16575094Slling 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
16585094Slling 	    fsp->f_bfree, fsp->f_frsize, 512),
16595094Slling 	    blocks_str,
16605094Slling 	    NFILES_WIDTH, number_to_string(free_files_buf,
16615094Slling 	    fsp->f_ffree, 1, 1),
16625094Slling 	    files_str);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate /* ARGSUSED */
16670Sstevel@tonic-gate static void
16680Sstevel@tonic-gate V_output(struct df_request *dfrp, struct statvfs64 *fsp)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	char temp_buf[LINEBUF_SIZE];
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	if (df_options_len > 1)
16730Sstevel@tonic-gate 		(void) strcat(strcpy(temp_buf, df_options), " ");
16740Sstevel@tonic-gate 	else
16750Sstevel@tonic-gate 		temp_buf[0] = NUL;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	(void) printf("%s -F %s %s%s\n",
16785094Slling 	    program_name, dfrp->dfr_fstype, temp_buf,
16795094Slling 	    dfrp->dfr_cmd_arg ? dfrp->dfr_cmd_arg: DFR_SPECIAL(dfrp));
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate /*
16840Sstevel@tonic-gate  * This function is used to sort the array of df_requests according to fstype
16850Sstevel@tonic-gate  */
16860Sstevel@tonic-gate static int
16870Sstevel@tonic-gate df_reqcomp(const void *p1, const void *p2)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	int v = strcmp(DFRP(p1)->dfr_fstype, DFRP(p2)->dfr_fstype);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	if (v != 0)
16920Sstevel@tonic-gate 		return (v);
16930Sstevel@tonic-gate 	else
16940Sstevel@tonic-gate 		return (DFRP(p1)->dfr_index - DFRP(p2)->dfr_index);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate static void
16990Sstevel@tonic-gate vfs_error(char *file, int status)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	if (status == VFS_TOOLONG)
17020Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
17035094Slling 		    file, MNT_LINE_MAX);
17040Sstevel@tonic-gate 	else if (status == VFS_TOOMANY)
17050Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS, "a line in %s has too many fields", file);
17060Sstevel@tonic-gate 	else if (status == VFS_TOOFEW)
17070Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS, "a line in %s has too few fields", file);
17080Sstevel@tonic-gate 	else
17090Sstevel@tonic-gate 		errmsg(ERR_NOFLAGS, "error while reading %s: %d", file, status);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate /*
17140Sstevel@tonic-gate  * Try to determine the fstype for the specified block device.
17150Sstevel@tonic-gate  * Return in order of decreasing preference:
17160Sstevel@tonic-gate  *	file system type from vfstab
17170Sstevel@tonic-gate  *	file system type as specified by -F option
17180Sstevel@tonic-gate  *	default file system type
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate static char *
17210Sstevel@tonic-gate find_fstype(char *special)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	struct vfstab	vtab;
17240Sstevel@tonic-gate 	FILE		*fp;
17250Sstevel@tonic-gate 	int		status;
17260Sstevel@tonic-gate 	char		*vfstab_file = VFS_TAB;
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	fp = xfopen(vfstab_file);
17290Sstevel@tonic-gate 	status = getvfsspec(fp, &vtab, special);
17300Sstevel@tonic-gate 	(void) fclose(fp);
17310Sstevel@tonic-gate 	if (status > 0)
17320Sstevel@tonic-gate 		vfs_error(vfstab_file, status);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	if (status == 0) {
17350Sstevel@tonic-gate 		if (F_option && ! EQ(FSType, vtab.vfs_fstype))
17360Sstevel@tonic-gate 			errmsg(ERR_NOFLAGS,
17370Sstevel@tonic-gate 			"warning: %s is of type %s", special, vtab.vfs_fstype);
17380Sstevel@tonic-gate 		return (new_string(vtab.vfs_fstype));
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 	else
17410Sstevel@tonic-gate 		return (F_option ? FSType : default_fstype(special));
17420Sstevel@tonic-gate }
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate /*
17450Sstevel@tonic-gate  * When this function returns, the following fields are filled for all
17460Sstevel@tonic-gate  * valid entries in the requests[] array:
17470Sstevel@tonic-gate  *		dfr_mte		(if the file system is mounted)
17480Sstevel@tonic-gate  *		dfr_fstype
17490Sstevel@tonic-gate  *		dfr_index
17500Sstevel@tonic-gate  *
17510Sstevel@tonic-gate  * The function returns the number of errors that occurred while building
17520Sstevel@tonic-gate  * the request list.
17530Sstevel@tonic-gate  */
17540Sstevel@tonic-gate static int
17550Sstevel@tonic-gate create_request_list(
17560Sstevel@tonic-gate 			int argc,
17570Sstevel@tonic-gate 			char *argv[],
17580Sstevel@tonic-gate 			struct df_request *requests_p[],
17590Sstevel@tonic-gate 			size_t *request_count)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	struct df_request	*requests;
17620Sstevel@tonic-gate 	struct df_request	*dfrp;
17630Sstevel@tonic-gate 	size_t			size;
17640Sstevel@tonic-gate 	size_t 			i;
17650Sstevel@tonic-gate 	size_t 			request_index = 0;
17660Sstevel@tonic-gate 	size_t			max_requests;
17670Sstevel@tonic-gate 	int			errors = 0;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	/*
17700Sstevel@tonic-gate 	 * If no args, use the mounted file systems, otherwise use the
17710Sstevel@tonic-gate 	 * user-specified arguments.
17720Sstevel@tonic-gate 	 */
17730Sstevel@tonic-gate 	if (argc == 0) {
17740Sstevel@tonic-gate 		mtab_read_file();
17750Sstevel@tonic-gate 		max_requests = mount_table_entries;
17760Sstevel@tonic-gate 	} else
17770Sstevel@tonic-gate 		max_requests = argc;
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	size = max_requests * sizeof (struct df_request);
17800Sstevel@tonic-gate 	requests = xmalloc(size);
17810Sstevel@tonic-gate 	(void) memset(requests, 0, size);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	if (argc == 0) {
17840Sstevel@tonic-gate 		/*
17850Sstevel@tonic-gate 		 * If -Z wasn't specified, we skip mounts in other
17860Sstevel@tonic-gate 		 * zones.  This obviously is a noop in a non-global
17870Sstevel@tonic-gate 		 * zone.
17880Sstevel@tonic-gate 		 */
17890Sstevel@tonic-gate 		boolean_t showall = (getzoneid() != GLOBAL_ZONEID) || Z_option;
17900Sstevel@tonic-gate 		struct zone_summary *zsp;
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		if (!showall) {
17930Sstevel@tonic-gate 			zsp = fs_get_zone_summaries();
17940Sstevel@tonic-gate 			if (zsp == NULL)
17950Sstevel@tonic-gate 				errmsg(ERR_FATAL,
17960Sstevel@tonic-gate 				    "unable to retrieve list of zones");
17970Sstevel@tonic-gate 		}
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 		for (i = 0; i < mount_table_entries; i++) {
18000Sstevel@tonic-gate 			struct extmnttab *mtp = mount_table[i].mte_mount;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 			if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
18030Sstevel@tonic-gate 				continue;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 			if (!showall) {
18060Sstevel@tonic-gate 				if (fs_mount_in_other_zone(zsp,
18070Sstevel@tonic-gate 				    mtp->mnt_mountp))
18080Sstevel@tonic-gate 					continue;
18090Sstevel@tonic-gate 			}
18100Sstevel@tonic-gate 			dfrp = &requests[request_index++];
18110Sstevel@tonic-gate 			dfrp->dfr_mte		= &mount_table[i];
18120Sstevel@tonic-gate 			dfrp->dfr_fstype	= mtp->mnt_fstype;
18130Sstevel@tonic-gate 			dfrp->dfr_index		= i;
18140Sstevel@tonic-gate 			dfrp->dfr_valid		= TRUE;
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 	} else {
18170Sstevel@tonic-gate 		struct stat64 *arg_stat; /* array of stat structures	*/
18180Sstevel@tonic-gate 		bool_int *valid_stat;	/* which structures are valid	*/
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 		arg_stat = xmalloc(argc * sizeof (struct stat64));
18210Sstevel@tonic-gate 		valid_stat = xmalloc(argc * sizeof (bool_int));
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 		/*
18240Sstevel@tonic-gate 		 * Obtain stat64 information for each argument before
18250Sstevel@tonic-gate 		 * constructing the list of mounted file systems. By
18260Sstevel@tonic-gate 		 * touching all these places we force the automounter
18270Sstevel@tonic-gate 		 * to establish any mounts required to access the arguments,
18280Sstevel@tonic-gate 		 * so that the corresponding mount table entries will exist
18290Sstevel@tonic-gate 		 * when we look for them.
18300Sstevel@tonic-gate 		 * It is still possible that the automounter may timeout
18310Sstevel@tonic-gate 		 * mounts between the time we read the mount table and the
18320Sstevel@tonic-gate 		 * time we process the request. Even in that case, when
18330Sstevel@tonic-gate 		 * we issue the statvfs64(2) for the mount point, the file
18340Sstevel@tonic-gate 		 * system will be mounted again. The only problem will
18350Sstevel@tonic-gate 		 * occur if the automounter maps change in the meantime
18360Sstevel@tonic-gate 		 * and the mount point is eliminated.
18370Sstevel@tonic-gate 		 */
18380Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
18390Sstevel@tonic-gate 			valid_stat[i] = (stat64(argv[i], &arg_stat[i]) == 0);
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 		mtab_read_file();
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
18440Sstevel@tonic-gate 			char *arg = argv[i];
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 			dfrp = &requests[request_index];
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 			dfrp->dfr_index = request_index;
18490Sstevel@tonic-gate 			dfrp->dfr_cmd_arg = arg;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			if (valid_stat[i]) {
18520Sstevel@tonic-gate 				if (S_ISBLK(arg_stat[i].st_mode)) {
18530Sstevel@tonic-gate 					bdev_mount_entry(dfrp);
18540Sstevel@tonic-gate 					dfrp->dfr_valid = TRUE;
18550Sstevel@tonic-gate 				} else if (S_ISDIR(arg_stat[i].st_mode) ||
18565094Slling 				    S_ISREG(arg_stat[i].st_mode) ||
18575094Slling 				    S_ISFIFO(arg_stat[i].st_mode)) {
18580Sstevel@tonic-gate 					path_mount_entry(dfrp,
18595094Slling 					    arg_stat[i].st_dev);
18600Sstevel@tonic-gate 					if (! DFR_ISMOUNTEDFS(dfrp)) {
18610Sstevel@tonic-gate 						errors++;
18620Sstevel@tonic-gate 						continue;
18630Sstevel@tonic-gate 					}
18640Sstevel@tonic-gate 					dfrp->dfr_valid = TRUE;
18650Sstevel@tonic-gate 				}
18660Sstevel@tonic-gate 			} else {
18670Sstevel@tonic-gate 				resource_mount_entry(dfrp);
18680Sstevel@tonic-gate 				dfrp->dfr_valid = DFR_ISMOUNTEDFS(dfrp);
18690Sstevel@tonic-gate 			}
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 			/*
18720Sstevel@tonic-gate 			 * If we haven't managed to verify that the request
18730Sstevel@tonic-gate 			 * is valid, we must have gotten a bad argument.
18740Sstevel@tonic-gate 			 */
18750Sstevel@tonic-gate 			if (!dfrp->dfr_valid) {
18760Sstevel@tonic-gate 				errmsg(ERR_NOFLAGS,
18775094Slling 				    "(%-10s) not a block device, directory or "
18785094Slling 				    "mounted resource", arg);
18790Sstevel@tonic-gate 				errors++;
18800Sstevel@tonic-gate 				continue;
18810Sstevel@tonic-gate 			}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 			/*
18840Sstevel@tonic-gate 			 * Determine the file system type.
18850Sstevel@tonic-gate 			 */
18860Sstevel@tonic-gate 			if (DFR_ISMOUNTEDFS(dfrp))
18870Sstevel@tonic-gate 				dfrp->dfr_fstype =
18885094Slling 				    dfrp->dfr_mte->mte_mount->mnt_fstype;
18890Sstevel@tonic-gate 			else
18900Sstevel@tonic-gate 				dfrp->dfr_fstype =
18915094Slling 				    find_fstype(dfrp->dfr_cmd_arg);
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 			request_index++;
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate 	*requests_p = requests;
18970Sstevel@tonic-gate 	*request_count = request_index;
18980Sstevel@tonic-gate 	return (errors);
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate  * Select the appropriate function and flags to use for output.
19040Sstevel@tonic-gate  * Notice that using both -e and -b options produces a different form of
19050Sstevel@tonic-gate  * output than either of those two options alone; this is the behavior of
19060Sstevel@tonic-gate  * the SVR4 df.
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate static struct df_output *
1909821Sdh145677 select_output(void)
19100Sstevel@tonic-gate {
19110Sstevel@tonic-gate 	static struct df_output dfo;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	/*
19140Sstevel@tonic-gate 	 * The order of checking options follows the option precedence
19150Sstevel@tonic-gate 	 * rules as they are listed in the man page.
19160Sstevel@tonic-gate 	 */
19170Sstevel@tonic-gate 	if (use_scaling) { /* comes from the -h option */
19180Sstevel@tonic-gate 		dfo.dfo_func = k_output;
19190Sstevel@tonic-gate 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
19200Sstevel@tonic-gate 	} else if (V_option) {
19210Sstevel@tonic-gate 		dfo.dfo_func = V_output;
19220Sstevel@tonic-gate 		dfo.dfo_flags = DFO_NOFLAGS;
19230Sstevel@tonic-gate 	} else if (g_option) {
19240Sstevel@tonic-gate 		dfo.dfo_func = g_output;
19250Sstevel@tonic-gate 		dfo.dfo_flags = DFO_STATVFS;
19260Sstevel@tonic-gate 	} else if (k_option || P_option || v_option) {
19270Sstevel@tonic-gate 		dfo.dfo_func = k_output;
19280Sstevel@tonic-gate 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
19290Sstevel@tonic-gate 	} else if (t_option) {
19300Sstevel@tonic-gate 		dfo.dfo_func = t_output;
19310Sstevel@tonic-gate 		dfo.dfo_flags = DFO_STATVFS;
19320Sstevel@tonic-gate 	} else if (b_option && e_option) {
19330Sstevel@tonic-gate 		dfo.dfo_func = eb_output;
19340Sstevel@tonic-gate 		dfo.dfo_flags = DFO_STATVFS;
19350Sstevel@tonic-gate 	} else if (b_option) {
19360Sstevel@tonic-gate 		dfo.dfo_func = b_output;
19370Sstevel@tonic-gate 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
19380Sstevel@tonic-gate 	} else if (e_option) {
19390Sstevel@tonic-gate 		dfo.dfo_func = e_output;
19400Sstevel@tonic-gate 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
19410Sstevel@tonic-gate 	} else if (n_option) {
19420Sstevel@tonic-gate 		dfo.dfo_func = n_output;
19430Sstevel@tonic-gate 		dfo.dfo_flags = DFO_NOFLAGS;
19440Sstevel@tonic-gate 	} else {
19450Sstevel@tonic-gate 		dfo.dfo_func = default_output;
19460Sstevel@tonic-gate 		dfo.dfo_flags = DFO_STATVFS;
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 	return (&dfo);
19490Sstevel@tonic-gate }
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate /*
19530Sstevel@tonic-gate  * The (argc,argv) pair contains all the non-option arguments
19540Sstevel@tonic-gate  */
19550Sstevel@tonic-gate static void
19560Sstevel@tonic-gate do_df(int argc, char *argv[])
19570Sstevel@tonic-gate {
19580Sstevel@tonic-gate 	size_t			i;
19590Sstevel@tonic-gate 	struct df_request	*requests;		/* array of requests */
19600Sstevel@tonic-gate 	size_t			n_requests;
19610Sstevel@tonic-gate 	struct df_request	*dfrp;
19620Sstevel@tonic-gate 	int			errors;
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	errors = create_request_list(argc, argv, &requests, &n_requests);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	if (n_requests == 0)
19670Sstevel@tonic-gate 		exit(errors);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * If we are going to run the FSType-specific df command,
19710Sstevel@tonic-gate 	 * rearrange the requests so that we can issue a single command
19720Sstevel@tonic-gate 	 * per file system type.
19730Sstevel@tonic-gate 	 */
19740Sstevel@tonic-gate 	if (o_option) {
19750Sstevel@tonic-gate 		size_t j;
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 		/*
19780Sstevel@tonic-gate 		 * qsort is not a stable sorting method (i.e. requests of
19790Sstevel@tonic-gate 		 * the same file system type may be swapped, and hence appear
19800Sstevel@tonic-gate 		 * in the output in a different order from the one in which
19810Sstevel@tonic-gate 		 * they were listed in the command line). In order to force
19820Sstevel@tonic-gate 		 * stability, we use the dfr_index field which is unique
19830Sstevel@tonic-gate 		 * for each request.
19840Sstevel@tonic-gate 		 */
19850Sstevel@tonic-gate 		qsort(requests,
19865094Slling 		    n_requests, sizeof (struct df_request), df_reqcomp);
19870Sstevel@tonic-gate 		for (i = 0; i < n_requests; i = j) {
19880Sstevel@tonic-gate 			char *fstype = requests[i].dfr_fstype;
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 			for (j = i+1; j < n_requests; j++)
19910Sstevel@tonic-gate 				if (! EQ(fstype, requests[j].dfr_fstype))
19920Sstevel@tonic-gate 					break;
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 			/*
19950Sstevel@tonic-gate 			 * At this point, requests in the range [i,j) are
19960Sstevel@tonic-gate 			 * of the same type.
19970Sstevel@tonic-gate 			 *
19980Sstevel@tonic-gate 			 * If the -F option was used, and the user specified
19990Sstevel@tonic-gate 			 * arguments, the filesystem types must match
20000Sstevel@tonic-gate 			 *
20010Sstevel@tonic-gate 			 * XXX: the alternative of doing this check here is to
20020Sstevel@tonic-gate 			 * 	invoke prune_list, but then we have to
20030Sstevel@tonic-gate 			 *	modify this code to ignore invalid requests.
20040Sstevel@tonic-gate 			 */
20050Sstevel@tonic-gate 			if (F_option && ! EQ(fstype, FSType)) {
20060Sstevel@tonic-gate 				size_t k;
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 				for (k = i; k < j; k++) {
20090Sstevel@tonic-gate 					dfrp = &requests[k];
20100Sstevel@tonic-gate 					if (dfrp->dfr_cmd_arg != NULL) {
20110Sstevel@tonic-gate 						errmsg(ERR_NOFLAGS,
20125094Slling 						    "Warning: %s mounted as a "
20135094Slling 						    "%s file system",
20145094Slling 						    dfrp->dfr_cmd_arg,
20155094Slling 						    dfrp->dfr_fstype);
20160Sstevel@tonic-gate 						errors++;
20170Sstevel@tonic-gate 					}
20180Sstevel@tonic-gate 				}
20190Sstevel@tonic-gate 			} else
20200Sstevel@tonic-gate 				errors += run_fs_specific_df(&requests[i], j-i);
20210Sstevel@tonic-gate 		}
20220Sstevel@tonic-gate 	} else {
20230Sstevel@tonic-gate 		size_t valid_requests;
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 		/*
20260Sstevel@tonic-gate 		 * We have to prune the request list to avoid printing a header
20270Sstevel@tonic-gate 		 * if there are no valid requests
20280Sstevel@tonic-gate 		 */
20290Sstevel@tonic-gate 		errors += prune_list(requests, n_requests, &valid_requests);
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 		if (valid_requests) {
20320Sstevel@tonic-gate 			struct df_output *dfop = select_output();
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 			/* indicates if we already printed out a header line */
20350Sstevel@tonic-gate 			int printed_header = 0;
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 			for (i = 0; i < n_requests; i++) {
20380Sstevel@tonic-gate 				dfrp = &requests[i];
20390Sstevel@tonic-gate 				if (! dfrp->dfr_valid)
20400Sstevel@tonic-gate 					continue;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 				/*
20430Sstevel@tonic-gate 				 * If we don't have a mount point,
20440Sstevel@tonic-gate 				 * this must be a block device.
20450Sstevel@tonic-gate 				 */
20460Sstevel@tonic-gate 				if (DFR_ISMOUNTEDFS(dfrp)) {
20470Sstevel@tonic-gate 					struct statvfs64 stvfs;
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 					if ((dfop->dfo_flags & DFO_STATVFS) &&
20505094Slling 					    statvfs64(DFR_MOUNT_POINT(dfrp),
20515094Slling 					    &stvfs) == -1) {
20520Sstevel@tonic-gate 						errmsg(ERR_PERROR,
20535094Slling 						    "cannot statvfs %s:",
20545094Slling 						    DFR_MOUNT_POINT(dfrp));
20550Sstevel@tonic-gate 						errors++;
20560Sstevel@tonic-gate 						continue;
20570Sstevel@tonic-gate 					}
20580Sstevel@tonic-gate 					if ((!printed_header) &&
20590Sstevel@tonic-gate 					    (dfop->dfo_flags & DFO_HEADER)) {
20600Sstevel@tonic-gate 						print_header();
20610Sstevel@tonic-gate 						printed_header = 1;
20620Sstevel@tonic-gate 					}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 					(*dfop->dfo_func)(dfrp, &stvfs);
20650Sstevel@tonic-gate 				} else {
20660Sstevel@tonic-gate 					/*
20670Sstevel@tonic-gate 					 *  -h option only works for
20680Sstevel@tonic-gate 					 *  mounted filesystems
20690Sstevel@tonic-gate 					 */
20700Sstevel@tonic-gate 					if (use_scaling) {
20710Sstevel@tonic-gate 						errmsg(ERR_NOFLAGS,
20720Sstevel@tonic-gate 		"-h option incompatible with unmounted special device (%s)",
20730Sstevel@tonic-gate 						    dfrp->dfr_cmd_arg);
20740Sstevel@tonic-gate 						errors++;
20750Sstevel@tonic-gate 						continue;
20760Sstevel@tonic-gate 					}
20770Sstevel@tonic-gate 					errors += run_fs_specific_df(dfrp, 1);
20780Sstevel@tonic-gate 				}
20790Sstevel@tonic-gate 			}
20800Sstevel@tonic-gate 		}
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 	exit(errors);
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate /*
20870Sstevel@tonic-gate  * The rest of this file implements the devnm command
20880Sstevel@tonic-gate  */
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate static char *
20910Sstevel@tonic-gate find_dev_name(char *file, dev_t dev)
20920Sstevel@tonic-gate {
20930Sstevel@tonic-gate 	struct df_request dfreq;
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	dfreq.dfr_cmd_arg = file;
20960Sstevel@tonic-gate 	dfreq.dfr_fstype = 0;
20970Sstevel@tonic-gate 	dfreq.dfr_mte = NULL;
20980Sstevel@tonic-gate 	path_mount_entry(&dfreq, dev);
20990Sstevel@tonic-gate 	return (DFR_ISMOUNTEDFS(&dfreq) ? DFR_SPECIAL(&dfreq) : NULL);
21000Sstevel@tonic-gate }
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate static void
21040Sstevel@tonic-gate do_devnm(int argc, char *argv[])
21050Sstevel@tonic-gate {
21060Sstevel@tonic-gate 	int arg;
21070Sstevel@tonic-gate 	int errors = 0;
21080Sstevel@tonic-gate 	char *dev_name;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	if (argc == 1)
21110Sstevel@tonic-gate 		errmsg(ERR_NONAME, "Usage: %s name ...", DEVNM_CMD);
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	mtab_read_file();
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	for (arg = 1; arg < argc; arg++) {
21160Sstevel@tonic-gate 		char *file = argv[arg];
21170Sstevel@tonic-gate 		struct stat64 st;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 		if (stat64(file, &st) == -1) {
21200Sstevel@tonic-gate 			errmsg(ERR_PERROR, "%s: ", file);
21210Sstevel@tonic-gate 			errors++;
21220Sstevel@tonic-gate 			continue;
21230Sstevel@tonic-gate 		}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 		if (! is_remote_fs(st.st_fstype) &&
21265094Slling 		    ! EQ(st.st_fstype, MNTTYPE_TMPFS) &&
21275094Slling 		    (dev_name = find_dev_name(file, st.st_dev)))
21280Sstevel@tonic-gate 			(void) printf("%s %s\n", dev_name, file);
21290Sstevel@tonic-gate 		else
21300Sstevel@tonic-gate 			errmsg(ERR_NOFLAGS,
21315094Slling 			    "%s not found", file);
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 	exit(errors);
21340Sstevel@tonic-gate 	/* NOTREACHED */
21350Sstevel@tonic-gate }
2136