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