xref: /titanic_52/usr/src/cmd/fs.d/df.c (revision 9df258ac654d4990e45ba8bff1661d2d0d3b0d2a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All Rights Reserved  	*/
23 
24 
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <dlfcn.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <stdlib.h>
40 #include <ftw.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <sys/statvfs.h>
45 #include <sys/stat.h>
46 #include <sys/param.h>
47 #include <sys/mnttab.h>
48 #include <sys/mntent.h>
49 #include <sys/vfstab.h>
50 #include <sys/wait.h>
51 #include <sys/mkdev.h>
52 #include <sys/int_limits.h>
53 #include <sys/zone.h>
54 #include <libzfs.h>
55 
56 #include "fslib.h"
57 
58 extern char *default_fstype(char *);
59 
60 /*
61  * General notice:
62  * String pointers in this code may point to statically allocated memory
63  * or dynamically allocated memory. Furthermore, a dynamically allocated
64  * string may be pointed to by more than one pointer. This does not pose
65  * a problem because malloc'ed memory is never free'd (so we don't need
66  * to remember which pointers point to malloc'ed memory).
67  */
68 
69 /*
70  * TRANSLATION_NOTE
71  * Only strings passed as arguments to the TRANSLATE macro need to
72  * be translated.
73  */
74 
75 #ifndef MNTTYPE_LOFS
76 #define	MNTTYPE_LOFS		"lofs"
77 #endif
78 
79 #define	EQ(s1, s2)		(strcmp(s1, s2) == 0)
80 #define	NEW(type)		xmalloc(sizeof (type))
81 #define	CLEAR(var)		(void) memset(&(var), 0, sizeof (var))
82 #define	MAX(a, b)		((a) > (b) ? (a) : (b))
83 #define	MAX3(a, b, c)		MAX(a, MAX(b, c))
84 #define	TRANSLATE(s)		new_string(gettext(s))
85 
86 #define	MAX_OPTIONS		36
87 #define	N_FSTYPES		20
88 #define	MOUNT_TABLE_ENTRIES	40	/* initial allocation */
89 #define	MSGBUF_SIZE		1024
90 #define	LINEBUF_SIZE		256	/* either input or output lines */
91 
92 #define	BLOCK_SIZE		512	/* when reporting in terms of blocks */
93 
94 #define	DEVNM_CMD		"devnm"
95 #define	FS_LIBPATH		"/usr/lib/fs/"
96 #define	MOUNT_TAB		"/etc/mnttab"
97 #define	VFS_TAB			"/etc/vfstab"
98 #define	REMOTE_FS		"/etc/dfs/fstypes"
99 
100 #define	NUL			'\0'
101 #define	FALSE			0
102 #define	TRUE			1
103 
104 /*
105  * Formatting constants
106  */
107 #define	IBCS2_FILESYSTEM_WIDTH	15	/* Truncate to match ISC/SCO */
108 #define	IBCS2_MOUNT_POINT_WIDTH	10	/* Truncate to match ISC/SCO */
109 #define	FILESYSTEM_WIDTH	20
110 #define	MOUNT_POINT_WIDTH	19
111 #define	SPECIAL_DEVICE_WIDTH	18
112 #define	FSTYPE_WIDTH		8
113 #define	BLOCK_WIDTH		8
114 #define	NFILES_WIDTH		8
115 #ifdef XPG4
116 #define	KBYTE_WIDTH		11
117 #define	AVAILABLE_WIDTH		10
118 #else
119 #define	KBYTE_WIDTH		7
120 #define	AVAILABLE_WIDTH		6
121 #endif
122 #define	SCALED_WIDTH		6
123 #define	CAPACITY_WIDTH		9
124 #define	BSIZE_WIDTH		6
125 #define	FRAGSIZE_WIDTH		7
126 #define	FSID_WIDTH		7
127 #define	FLAG_WIDTH		8
128 #define	NAMELEN_WIDTH		7
129 #define	MNT_SPEC_WIDTH		MOUNT_POINT_WIDTH + SPECIAL_DEVICE_WIDTH + 2
130 
131 /*
132  * Flags for the errmsg() function
133  */
134 #define	ERR_NOFLAGS		0x0
135 #define	ERR_NONAME		0x1	/* don't include the program name */
136 					/* as a prefix */
137 #define	ERR_FATAL		0x2	/* call exit after printing the */
138 					/* message */
139 #define	ERR_PERROR		0x4	/* append an errno explanation to */
140 					/* the message */
141 #define	ERR_USAGE		0x8	/* print the usage line after the */
142 					/* message */
143 
144 #define	NUMBER_WIDTH		40
145 
146 /*
147  * A numbuf_t is used when converting a number to a string representation
148  */
149 typedef char numbuf_t[ NUMBER_WIDTH ];
150 
151 /*
152  * We use bool_int instead of int to make clear which variables are
153  * supposed to be boolean
154  */
155 typedef int bool_int;
156 
157 struct mtab_entry {
158 	bool_int	mte_dev_is_valid;
159 	dev_t		mte_dev;
160 	bool_int	mte_ignore;	/* the "ignore" option was set */
161 	struct extmnttab	*mte_mount;
162 };
163 
164 
165 struct df_request {
166 	bool_int		dfr_valid;
167 	char			*dfr_cmd_arg;	/* what the user specified */
168 	struct mtab_entry	*dfr_mte;
169 	char			*dfr_fstype;
170 	int			dfr_index;	/* to make qsort stable	*/
171 };
172 
173 #define	DFR_MOUNT_POINT(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_mountp
174 #define	DFR_SPECIAL(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_special
175 #define	DFR_FSTYPE(dfrp)	(dfrp)->dfr_mte->mte_mount->mnt_fstype
176 #define	DFR_ISMOUNTEDFS(dfrp)	((dfrp)->dfr_mte != NULL)
177 
178 #define	DFRP(p)			((struct df_request *)(p))
179 
180 typedef void (*output_func)(struct df_request *, struct statvfs64 *);
181 
182 struct df_output {
183 	output_func	dfo_func;	/* function that will do the output */
184 	int		dfo_flags;
185 };
186 
187 /*
188  * Output flags
189  */
190 #define	DFO_NOFLAGS	0x0
191 #define	DFO_HEADER	0x1		/* output preceded by header */
192 #define	DFO_STATVFS	0x2		/* must do a statvfs64(2) */
193 
194 
195 static char	*program_name;
196 static char	df_options[MAX_OPTIONS] = "-";
197 static size_t	df_options_len = 1;
198 static char	*o_option_arg;			/* arg to the -o option */
199 static char	*FSType;
200 static char	*remote_fstypes[N_FSTYPES+1];	/* allocate an extra one */
201 						/* to use as a terminator */
202 
203 /*
204  * The following three variables support an in-memory copy of the mount table
205  * to speedup searches.
206  */
207 static struct mtab_entry	*mount_table;	/* array of mtab_entry's */
208 static size_t			mount_table_entries;
209 static size_t			mount_table_allocated_entries;
210 
211 static bool_int		F_option;
212 static bool_int		V_option;
213 static bool_int		P_option;	/* Added for XCU4 compliance */
214 static bool_int		Z_option;
215 static bool_int		v_option;
216 #ifdef	_iBCS2
217 char			*sysv3_set;
218 #endif /* _iBCS2 */
219 static bool_int		a_option;
220 static bool_int		b_option;
221 static bool_int		e_option;
222 static bool_int		g_option;
223 static bool_int		h_option;
224 static bool_int		k_option;
225 static bool_int		l_option;
226 static bool_int		n_option;
227 static bool_int		t_option;
228 static bool_int		o_option;
229 
230 static bool_int		tty_output;
231 static bool_int		use_scaling;
232 static int		scale;
233 
234 static void usage(void);
235 static void do_devnm(int, char **);
236 static void do_df(int, char **)	__NORETURN;
237 static void parse_options(int, char **);
238 static char *basename(char *);
239 
240 static libzfs_handle_t *(*_libzfs_init)(boolean_t);
241 static zfs_handle_t *(*_zfs_open)(libzfs_handle_t *, const char *, int);
242 static void (*_zfs_close)(zfs_handle_t *);
243 static uint64_t (*_zfs_prop_get_int)(zfs_handle_t *, zfs_prop_t);
244 static libzfs_handle_t *g_zfs;
245 
246 /*
247  * Dynamically check for libzfs, in case the user hasn't installed the SUNWzfs
248  * packages.  A basic utility such as df shouldn't depend on optional
249  * filesystems.
250  */
251 static boolean_t
252 load_libzfs(void)
253 {
254 	void *hdl;
255 
256 	if (_libzfs_init != NULL)
257 		return (g_zfs != NULL);
258 
259 	if ((hdl = dlopen("libzfs.so", RTLD_LAZY)) != NULL) {
260 		_libzfs_init = (libzfs_handle_t *(*)(boolean_t))dlsym(hdl,
261 		    "libzfs_init");
262 		_zfs_open = (zfs_handle_t *(*)())dlsym(hdl, "zfs_open");
263 		_zfs_close = (void (*)())dlsym(hdl, "zfs_close");
264 		_zfs_prop_get_int = (uint64_t (*)())
265 		    dlsym(hdl, "zfs_prop_get_int");
266 
267 		if (_libzfs_init != NULL) {
268 			assert(_zfs_open != NULL);
269 			assert(_zfs_close != NULL);
270 			assert(_zfs_prop_get_int != NULL);
271 
272 			g_zfs = _libzfs_init(B_FALSE);
273 		}
274 	}
275 
276 	return (g_zfs != NULL);
277 }
278 
279 int
280 main(int argc, char *argv[])
281 {
282 	(void) setlocale(LC_ALL, "");
283 
284 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
285 #define	TEXT_DOMAIN "SYS_TEST"
286 #endif
287 	(void) textdomain(TEXT_DOMAIN);
288 
289 	program_name = basename(argv[0]);
290 
291 #ifdef	_iBCS2
292 	sysv3_set = getenv("SYSV3");
293 #endif	/* _iBCS2 */
294 
295 	if (EQ(program_name, DEVNM_CMD))
296 		do_devnm(argc, argv);
297 
298 	parse_options(argc, argv);
299 
300 	/*
301 	 * The k_option implies SunOS 4.x compatibility: when the special
302 	 * device name is too long the line will be split except when the
303 	 * output has been redirected.
304 	 * This is also valid for the -h option.
305 	 */
306 
307 	if (use_scaling || k_option || P_option || v_option)
308 		tty_output = isatty(1);
309 
310 	do_df(argc - optind, &argv[optind]);
311 	/* NOTREACHED */
312 }
313 
314 
315 /*
316  * Prints an error message to stderr.
317  */
318 /* VARARGS2 */
319 static void
320 errmsg(int flags, char *fmt, ...)
321 {
322 	char buf[MSGBUF_SIZE];
323 	va_list ap;
324 	int cc;
325 	int offset;
326 
327 	if (flags & ERR_NONAME)
328 		offset = 0;
329 	else
330 		offset = sprintf(buf, "%s: ", program_name);
331 
332 	va_start(ap, fmt);
333 	cc = vsprintf(&buf[offset], gettext(fmt), ap);
334 	offset += cc;
335 	va_end(ap);
336 
337 	if (flags & ERR_PERROR) {
338 		if (buf[offset-1] != ' ')
339 			(void) strcat(buf, " ");
340 		(void) strcat(buf, strerror(errno));
341 	}
342 	(void) fprintf(stderr, "%s\n", buf);
343 	if (flags & ERR_USAGE)
344 		usage();
345 	if (flags & ERR_FATAL)
346 		exit(1);
347 }
348 
349 
350 static void
351 usage(void)
352 {
353 #ifdef  XPG4
354 	errmsg(ERR_NONAME,
355 	"Usage: %s [-F FSType] [-abeghklntPVZ] [-o FSType-specific_options]"
356 		" [directory | block_device | resource]", program_name);
357 #else
358 	errmsg(ERR_NONAME,
359 	"Usage: %s [-F FSType] [-abeghklntVvZ] [-o FSType-specific_options]"
360 		" [directory | block_device | resource]", program_name);
361 #endif
362 	exit(1);
363 	/* NOTREACHED */
364 }
365 
366 
367 static char *
368 new_string(char *s)
369 {
370 	char *p = NULL;
371 
372 	if (s) {
373 		p = strdup(s);
374 		if (p)
375 			return (p);
376 		errmsg(ERR_FATAL, "out of memory");
377 		/* NOTREACHED */
378 	}
379 	return (p);
380 }
381 
382 
383 /*
384  * Allocate memory using malloc but terminate if the allocation fails
385  */
386 static void *
387 xmalloc(size_t size)
388 {
389 	void *p = malloc(size);
390 
391 	if (p)
392 		return (p);
393 	errmsg(ERR_FATAL, "out of memory");
394 	/* NOTREACHED */
395 	return (NULL);
396 }
397 
398 
399 /*
400  * Allocate memory using realloc but terminate if the allocation fails
401  */
402 static void *
403 xrealloc(void *ptr, size_t size)
404 {
405 	void *p = realloc(ptr, size);
406 
407 	if (p)
408 		return (p);
409 	errmsg(ERR_FATAL, "out of memory");
410 	/* NOTREACHED */
411 	return (NULL);
412 }
413 
414 
415 /*
416  * fopen the specified file for reading but terminate if the fopen fails
417  */
418 static FILE *
419 xfopen(char *file)
420 {
421 	FILE *fp = fopen(file, "r");
422 
423 	if (fp == NULL)
424 		errmsg(ERR_FATAL + ERR_PERROR, "failed to open %s:", file);
425 	return (fp);
426 }
427 
428 
429 /*
430  * Read remote file system types from REMOTE_FS into the
431  * remote_fstypes array.
432  */
433 static void
434 init_remote_fs(void)
435 {
436 	FILE	*fp;
437 	char	line_buf[LINEBUF_SIZE];
438 	size_t	fstype_index = 0;
439 
440 	if ((fp = fopen(REMOTE_FS, "r")) == NULL) {
441 		errmsg(ERR_NOFLAGS,
442 			"Warning: can't open %s, ignored", REMOTE_FS);
443 		return;
444 	}
445 
446 	while (fgets(line_buf, sizeof (line_buf), fp) != NULL) {
447 		char buf[LINEBUF_SIZE];
448 
449 		(void) sscanf(line_buf, "%s", buf);
450 		remote_fstypes[fstype_index++] = new_string(buf);
451 
452 		if (fstype_index == N_FSTYPES)
453 			break;
454 	}
455 	(void) fclose(fp);
456 }
457 
458 
459 /*
460  * Returns TRUE if fstype is a remote file system type;
461  * otherwise, returns FALSE.
462  */
463 static int
464 is_remote_fs(char *fstype)
465 {
466 	char **p;
467 	static bool_int remote_fs_initialized;
468 
469 	if (! remote_fs_initialized) {
470 		init_remote_fs();
471 		remote_fs_initialized = TRUE;
472 	}
473 
474 	for (p = remote_fstypes; *p; p++)
475 		if (EQ(fstype, *p))
476 			return (TRUE);
477 	return (FALSE);
478 }
479 
480 
481 static char *
482 basename(char *s)
483 {
484 	char *p = strrchr(s, '/');
485 
486 	return (p ? p+1 : s);
487 }
488 
489 
490 /*
491  * Create a new "struct extmnttab" and make sure that its fields point
492  * to malloc'ed memory
493  */
494 static struct extmnttab *
495 mntdup(struct extmnttab *old)
496 {
497 	struct extmnttab *new = NEW(struct extmnttab);
498 
499 	new->mnt_special = new_string(old->mnt_special);
500 	new->mnt_mountp  = new_string(old->mnt_mountp);
501 	new->mnt_fstype  = new_string(old->mnt_fstype);
502 	new->mnt_mntopts = new_string(old->mnt_mntopts);
503 	new->mnt_time    = new_string(old->mnt_time);
504 	new->mnt_major   = old->mnt_major;
505 	new->mnt_minor   = old->mnt_minor;
506 	return (new);
507 }
508 
509 
510 static void
511 mtab_error(char *mtab_file, int status)
512 {
513 	if (status == MNT_TOOLONG)
514 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
515 			mtab_file, MNT_LINE_MAX);
516 	else if (status == MNT_TOOMANY)
517 		errmsg(ERR_NOFLAGS,
518 			"a line in %s has too many fields", mtab_file);
519 	else if (status == MNT_TOOFEW)
520 		errmsg(ERR_NOFLAGS,
521 			"a line in %s has too few fields", mtab_file);
522 	else
523 		errmsg(ERR_NOFLAGS,
524 			"error while reading %s: %d", mtab_file, status);
525 	exit(1);
526 	/* NOTREACHED */
527 }
528 
529 
530 /*
531  * Read the mount table from the specified file.
532  * We keep the table in memory for faster lookups.
533  */
534 static void
535 mtab_read_file(void)
536 {
537 	char		*mtab_file = MOUNT_TAB;
538 	FILE		*fp;
539 	struct extmnttab	mtab;
540 	int		status;
541 
542 	fp = xfopen(mtab_file);
543 
544 	resetmnttab(fp);
545 	mount_table_allocated_entries = MOUNT_TABLE_ENTRIES;
546 	mount_table_entries = 0;
547 	mount_table = xmalloc(
548 		mount_table_allocated_entries * sizeof (struct mtab_entry));
549 
550 	while ((status = getextmntent(fp, &mtab, sizeof (struct extmnttab)))
551 		== 0) {
552 		struct mtab_entry *mtep;
553 
554 		if (mount_table_entries == mount_table_allocated_entries) {
555 			mount_table_allocated_entries += MOUNT_TABLE_ENTRIES;
556 			mount_table = xrealloc(mount_table,
557 				mount_table_allocated_entries *
558 					sizeof (struct mtab_entry));
559 		}
560 		mtep = &mount_table[mount_table_entries++];
561 		mtep->mte_mount = mntdup(&mtab);
562 		mtep->mte_dev_is_valid = FALSE;
563 		mtep->mte_ignore = (hasmntopt((struct mnttab *)&mtab,
564 			MNTOPT_IGNORE) != NULL);
565 	}
566 
567 	(void) fclose(fp);
568 
569 	if (status == -1)			/* reached EOF */
570 		return;
571 	mtab_error(mtab_file, status);
572 	/* NOTREACHED */
573 }
574 
575 
576 /*
577  * We use this macro when we want to record the option for the purpose of
578  * passing it to the FS-specific df
579  */
580 #define	SET_OPTION(opt)		opt##_option = TRUE, \
581 				df_options[df_options_len++] = arg
582 
583 static void
584 parse_options(int argc, char *argv[])
585 {
586 	int arg;
587 
588 	opterr = 0;	/* getopt shouldn't complain about unknown options */
589 
590 #ifdef XPG4
591 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlPZ")) != EOF) {
592 #else
593 	while ((arg = getopt(argc, argv, "F:o:abehkVtgnlvZ")) != EOF) {
594 #endif
595 		if (arg == 'F') {
596 			if (F_option)
597 				errmsg(ERR_FATAL + ERR_USAGE,
598 					"more than one FSType specified");
599 			F_option = 1;
600 			FSType = optarg;
601 		} else if (arg == 'V' && ! V_option) {
602 			V_option = TRUE;
603 		} else if (arg == 'v' && ! v_option) {
604 			v_option = TRUE;
605 #ifdef XPG4
606 		} else if (arg == 'P' && ! P_option) {
607 			SET_OPTION(P);
608 #endif
609 		} else if (arg == 'a' && ! a_option) {
610 			SET_OPTION(a);
611 		} else if (arg == 'b' && ! b_option) {
612 			SET_OPTION(b);
613 		} else if (arg == 'e' && ! e_option) {
614 			SET_OPTION(e);
615 		} else if (arg == 'g' && ! g_option) {
616 			SET_OPTION(g);
617 		} else if (arg == 'h') {
618 			use_scaling = TRUE;
619 			scale = 1024;
620 		} else if (arg == 'k' && ! k_option) {
621 			SET_OPTION(k);
622 		} else if (arg == 'l' && ! l_option) {
623 			SET_OPTION(l);
624 		} else if (arg == 'n' && ! n_option) {
625 			SET_OPTION(n);
626 		} else if (arg == 't' && ! t_option) {
627 			SET_OPTION(t);
628 		} else if (arg == 'o') {
629 			if (o_option)
630 				errmsg(ERR_FATAL + ERR_USAGE,
631 				"the -o option can only be specified once");
632 			o_option = TRUE;
633 			o_option_arg = optarg;
634 		} else if (arg == 'Z') {
635 			SET_OPTION(Z);
636 		} else if (arg == '?') {
637 			errmsg(ERR_USAGE, "unknown option: %c", optopt);
638 		}
639 	}
640 
641 	/*
642 	 * Option sanity checks
643 	 */
644 	if (g_option && o_option)
645 		errmsg(ERR_FATAL, "-o and -g options are incompatible");
646 	if (l_option && o_option)
647 		errmsg(ERR_FATAL, "-o and -l options are incompatible");
648 	if (n_option && o_option)
649 		errmsg(ERR_FATAL, "-o and -n options are incompatible");
650 	if (use_scaling && o_option)
651 		errmsg(ERR_FATAL, "-o and -h options are incompatible");
652 }
653 
654 
655 
656 /*
657  * Check if the user-specified argument is a resource name.
658  * A resource name is whatever is placed in the mnt_special field of
659  * struct mnttab. In the case of NFS, a resource name has the form
660  * hostname:pathname
661  * We try to find an exact match between the user-specified argument
662  * and the mnt_special field of a mount table entry.
663  * We also use the heuristic of removing the basename from the user-specified
664  * argument and repeating the test until we get a match. This works
665  * fine for NFS but may fail for other remote file system types. However,
666  * it is guaranteed that the function will not fail if the user specifies
667  * the exact resource name.
668  * If successful, this function sets the 'dfr_mte' field of '*dfrp'
669  */
670 static void
671 resource_mount_entry(struct df_request *dfrp)
672 {
673 	char *name;
674 
675 	/*
676 	 * We need our own copy since we will modify the string
677 	 */
678 	name = new_string(dfrp->dfr_cmd_arg);
679 
680 	for (;;) {
681 		char *p;
682 		int i;
683 
684 		/*
685 		 * Compare against all known mount points.
686 		 * We start from the most recent mount, which is at the
687 		 * end of the array.
688 		 */
689 		for (i = mount_table_entries - 1; i >= 0; i--) {
690 			struct mtab_entry *mtep = &mount_table[i];
691 
692 			if (EQ(name, mtep->mte_mount->mnt_special)) {
693 				dfrp->dfr_mte = mtep;
694 				break;
695 			}
696 		}
697 
698 		/*
699 		 * Remove the last component of the pathname.
700 		 * If there is no such component, this is not a resource name.
701 		 */
702 		p = strrchr(name, '/');
703 		if (p == NULL)
704 			break;
705 		*p = NUL;
706 	}
707 }
708 
709 
710 
711 /*
712  * Try to match the command line argument which is a block special device
713  * with the special device of one of the mounted file systems.
714  * If one is found, set the appropriate field of 'dfrp' to the mount
715  * table entry.
716  */
717 static void
718 bdev_mount_entry(struct df_request *dfrp)
719 {
720 	int i;
721 	char *special = dfrp->dfr_cmd_arg;
722 
723 	/*
724 	 * Compare against all known mount points.
725 	 * We start from the most recent mount, which is at the
726 	 * end of the array.
727 	 */
728 	for (i = mount_table_entries - 1; i >= 0; i--) {
729 		struct mtab_entry *mtep = &mount_table[i];
730 
731 		if (EQ(special, mtep->mte_mount->mnt_special)) {
732 			dfrp->dfr_mte = mtep;
733 			break;
734 		}
735 	}
736 }
737 
738 static struct mtab_entry *
739 devid_matches(int i, dev_t devno)
740 {
741 	struct mtab_entry	*mtep = &mount_table[i];
742 	struct extmnttab	*mtp = mtep->mte_mount;
743 	/* int	len = strlen(mtp->mnt_mountp); */
744 
745 	if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
746 		return (NULL);
747 	/*
748 	 * check if device numbers match. If there is a cached device number
749 	 * in the mtab_entry, use it, otherwise get the device number
750 	 * either from the mnttab entry or by stat'ing the mount point.
751 	 */
752 	if (! mtep->mte_dev_is_valid) {
753 		struct stat64 st;
754 		dev_t dev = NODEV;
755 
756 		dev = makedev(mtp->mnt_major, mtp->mnt_minor);
757 		if (dev == 0)
758 			dev = NODEV;
759 		if (dev == NODEV) {
760 			if (stat64(mtp->mnt_mountp, &st) == -1) {
761 				return (NULL);
762 			} else {
763 				dev = st.st_dev;
764 			}
765 		}
766 		mtep->mte_dev = dev;
767 		mtep->mte_dev_is_valid = TRUE;
768 	}
769 	if (mtep->mte_dev == devno) {
770 		return (mtep);
771 	}
772 	return (NULL);
773 }
774 
775 /*
776  * Find the mount point under which the user-specified path resides
777  * and set the 'dfr_mte' field of '*dfrp' to point to the mount table entry.
778  */
779 static void
780 path_mount_entry(struct df_request *dfrp, dev_t devno)
781 {
782 	char			dirpath[MAXPATHLEN];
783 	char			*dir = dfrp->dfr_cmd_arg;
784 	struct mtab_entry	*match, *tmatch;
785 	int i;
786 
787 	/*
788 	 * Expand the given path to get a canonical version (i.e. an absolute
789 	 * path without symbolic links).
790 	 */
791 	if (realpath(dir, dirpath) == NULL) {
792 		errmsg(ERR_PERROR, "cannot canonicalize %s:", dir);
793 		return;
794 	}
795 	/*
796 	 * If the mnt point is lofs, search from the top of entries from
797 	 * /etc/mnttab and return the first entry that matches the devid
798 	 * For non-lofs mount points, return the first entry from the bottom
799 	 * of the entries in /etc/mnttab that matches on the devid field
800 	 */
801 	match = NULL;
802 	if (dfrp->dfr_fstype && EQ(dfrp->dfr_fstype, MNTTYPE_LOFS)) {
803 		for (i = 0; i < mount_table_entries; i++) {
804 			if (match = devid_matches(i, devno))
805 				break;
806 		}
807 	} else {
808 		for (i = mount_table_entries - 1; i >= 0; i--) {
809 			if (tmatch = devid_matches(i, devno)) {
810 				/*
811 				 * If executing in a zone, there might be lofs
812 				 * mounts for which the real mount point is
813 				 * invisible; accept the "best fit" for this
814 				 * devid.
815 				 */
816 				match = tmatch;
817 				if (!EQ(match->mte_mount->mnt_fstype,
818 					MNTTYPE_LOFS)) {
819 					break;
820 				}
821 			}
822 		}
823 	}
824 	if (! match) {
825 		errmsg(ERR_NOFLAGS,
826 			"Could not find mount point for %s", dir);
827 		return;
828 	}
829 	dfrp->dfr_mte = match;
830 }
831 
832 /*
833  * Execute a single FS-specific df command for all given requests
834  * Return 0 if successful, 1 otherwise.
835  */
836 static int
837 run_fs_specific_df(struct df_request request_list[], int entries)
838 {
839 	int	i;
840 	int	argv_index;
841 	char	**argv;
842 	size_t	size;
843 	pid_t	pid;
844 	int	status;
845 	char	cmd_path[MAXPATHLEN];
846 	char	*fstype;
847 
848 	if (entries == 0)
849 		return (0);
850 
851 	fstype = request_list[0].dfr_fstype;
852 
853 	if (F_option && ! EQ(FSType, fstype))
854 		return (0);
855 
856 	(void) sprintf(cmd_path, "%s%s/df", FS_LIBPATH, fstype);
857 	/*
858 	 * Argv entries:
859 	 *		1 for the path
860 	 *		2 for -o <options>
861 	 *		1 for the generic options that we propagate
862 	 *		1 for the terminating NULL pointer
863 	 *		n for the number of user-specified arguments
864 	 */
865 	size = (5 + entries) * sizeof (char *);
866 	argv = xmalloc(size);
867 	(void) memset(argv, 0, size);
868 
869 	argv[0] = cmd_path;
870 	argv_index = 1;
871 	if (o_option) {
872 		argv[argv_index++] = "-o";
873 		argv[argv_index++] = o_option_arg;
874 	}
875 
876 	/*
877 	 * Check if we need to propagate any generic options
878 	 */
879 	if (df_options_len > 1)
880 		argv[argv_index++] = df_options;
881 
882 	/*
883 	 * If there is a user-specified path, we pass that to the
884 	 * FS-specific df. Otherwise, we are guaranteed to have a mount
885 	 * point, since a request without a user path implies that
886 	 * we are reporting only on mounted file systems.
887 	 */
888 	for (i = 0; i < entries; i++) {
889 		struct df_request *dfrp = &request_list[i];
890 
891 		argv[argv_index++] = (dfrp->dfr_cmd_arg == NULL)
892 						? DFR_MOUNT_POINT(dfrp)
893 						: dfrp->dfr_cmd_arg;
894 	}
895 
896 	if (V_option) {
897 		for (i = 0; i < argv_index-1; i++)
898 			(void) printf("%s ", argv[i]);
899 		(void) printf("%s\n", argv[i]);
900 		return (0);
901 	}
902 
903 	pid = fork();
904 
905 	if (pid == -1) {
906 		errmsg(ERR_PERROR, "cannot fork process:");
907 		return (1);
908 	} else if (pid == 0) {
909 		(void) execv(cmd_path, argv);
910 		if (errno == ENOENT)
911 			errmsg(ERR_NOFLAGS,
912 				"operation not applicable for FSType %s",
913 					fstype);
914 		else
915 			errmsg(ERR_PERROR, "cannot execute %s:", cmd_path);
916 		exit(2);
917 	}
918 
919 	/*
920 	 * Reap the child
921 	 */
922 	for (;;) {
923 		pid_t wpid = waitpid(pid, &status, 0);
924 
925 		if (wpid == -1)
926 			if (errno == EINTR)
927 				continue;
928 			else {
929 				errmsg(ERR_PERROR, "waitpid error:");
930 				return (1);
931 			}
932 		else
933 			break;
934 	}
935 
936 	return ((WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : 1);
937 }
938 
939 
940 
941 /*
942  * Remove from the request list all requests that do not apply.
943  * Notice that the subsequent processing of the requests depends on
944  * the sanity checking performed by this function.
945  */
946 static int
947 prune_list(struct df_request request_list[],
948 		size_t n_requests,
949 		size_t *valid_requests)
950 {
951 	size_t	i;
952 	size_t	n_valid = 0;
953 	int	errors = 0;
954 
955 	for (i = 0; i < n_requests; i++) {
956 		struct df_request *dfrp = &request_list[i];
957 
958 		/*
959 		 * Skip file systems that are not mounted if either the
960 		 * -l or -n options were specified. If none of these options
961 		 * are present, the appropriate FS-specific df will be invoked.
962 		 */
963 		if (! DFR_ISMOUNTEDFS(dfrp)) {
964 			if (l_option || n_option) {
965 				errmsg(ERR_NOFLAGS,
966 		"%s option incompatible with unmounted special device (%s)",
967 			l_option ? "-l" : "-n", dfrp->dfr_cmd_arg);
968 				dfrp->dfr_valid = FALSE;
969 				errors++;
970 			}
971 			else
972 				n_valid++;
973 			continue;
974 		}
975 
976 		/*
977 		 * Check for inconsistency between the argument of -F and
978 		 * the actual file system type.
979 		 * If there is an inconsistency and the user specified a
980 		 * path, this is an error since we are asked to interpret
981 		 * the path using the wrong file system type. If there is
982 		 * no path associated with this request, we quietly ignore it.
983 		 */
984 		if (F_option && ! EQ(dfrp->dfr_fstype, FSType)) {
985 			dfrp->dfr_valid = FALSE;
986 			if (dfrp->dfr_cmd_arg != NULL) {
987 				errmsg(ERR_NOFLAGS,
988 				"Warning: %s mounted as a %s file system",
989 					dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
990 				errors++;
991 			}
992 			continue;
993 		}
994 
995 		/*
996 		 * Skip remote file systems if the -l option is present
997 		 */
998 		if (l_option && is_remote_fs(dfrp->dfr_fstype)) {
999 			if (dfrp->dfr_cmd_arg != NULL) {
1000 				errmsg(ERR_NOFLAGS,
1001 				"Warning: %s is not a local file system",
1002 					dfrp->dfr_cmd_arg);
1003 				errors++;
1004 			}
1005 			dfrp->dfr_valid = FALSE;
1006 			continue;
1007 		}
1008 
1009 		/*
1010 		 * Skip file systems mounted as "ignore" unless the -a option
1011 		 * is present, or the user explicitly specified them on
1012 		 * the command line.
1013 		 */
1014 		if (dfrp->dfr_mte->mte_ignore &&
1015 			! (a_option || dfrp->dfr_cmd_arg)) {
1016 			dfrp->dfr_valid = FALSE;
1017 			continue;
1018 		}
1019 
1020 		n_valid++;
1021 	}
1022 	*valid_requests = n_valid;
1023 	return (errors);
1024 }
1025 
1026 
1027 /*
1028  * Print the appropriate header for the requested output format.
1029  * Options are checked in order of their precedence.
1030  */
1031 static void
1032 print_header(void)
1033 {
1034 	if (use_scaling) { /* this comes from the -h option */
1035 		int arg = 'h';
1036 
1037 		(void) printf("%-*s %*s %*s %*s %-*s %s\n",
1038 			FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1039 #ifdef XPG4
1040 			SCALED_WIDTH, TRANSLATE("Size"),
1041 			SCALED_WIDTH, TRANSLATE("Used"),
1042 			AVAILABLE_WIDTH, TRANSLATE("Available"),
1043 			CAPACITY_WIDTH, TRANSLATE("Capacity"),
1044 #else
1045 			SCALED_WIDTH, TRANSLATE("size"),
1046 			SCALED_WIDTH, TRANSLATE("used"),
1047 			AVAILABLE_WIDTH, TRANSLATE("avail"),
1048 			CAPACITY_WIDTH, TRANSLATE("capacity"),
1049 #endif
1050 			TRANSLATE("Mounted on"));
1051 		SET_OPTION(h);
1052 		return;
1053 	}
1054 	if (k_option) {
1055 		int arg = 'h';
1056 
1057 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
1058 			FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1059 #ifdef XPG4
1060 			KBYTE_WIDTH, TRANSLATE("1024-blocks"),
1061 			KBYTE_WIDTH, TRANSLATE("Used"),
1062 			KBYTE_WIDTH, TRANSLATE("Available"),
1063 			CAPACITY_WIDTH, TRANSLATE("Capacity"),
1064 #else
1065 			KBYTE_WIDTH, TRANSLATE("kbytes"),
1066 			KBYTE_WIDTH, TRANSLATE("used"),
1067 			KBYTE_WIDTH, TRANSLATE("avail"),
1068 			CAPACITY_WIDTH, TRANSLATE("capacity"),
1069 #endif
1070 			TRANSLATE("Mounted on"));
1071 		SET_OPTION(h);
1072 		return;
1073 	}
1074 	/* Added for XCU4 compliance */
1075 	if (P_option) {
1076 		int arg = 'h';
1077 
1078 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
1079 			FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1080 			KBYTE_WIDTH, TRANSLATE("512-blocks"),
1081 			KBYTE_WIDTH, TRANSLATE("Used"),
1082 			KBYTE_WIDTH, TRANSLATE("Available"),
1083 			CAPACITY_WIDTH, TRANSLATE("Capacity"),
1084 			TRANSLATE("Mounted on"));
1085 
1086 		SET_OPTION(h);
1087 		return;
1088 	}
1089 	/* End XCU4 */
1090 	if (v_option) {
1091 		(void) printf("%-*s %-*s %*s %*s %*s %-*s\n",
1092 			IBCS2_MOUNT_POINT_WIDTH, TRANSLATE("Mount Dir"),
1093 			IBCS2_FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1094 			BLOCK_WIDTH, TRANSLATE("blocks"),
1095 			BLOCK_WIDTH, TRANSLATE("used"),
1096 			BLOCK_WIDTH, TRANSLATE("free"),
1097 			CAPACITY_WIDTH, TRANSLATE(" %used"));
1098 		return;
1099 	}
1100 	if (e_option) {
1101 		(void) printf(gettext("%-*s %*s\n"),
1102 			FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1103 			BLOCK_WIDTH, TRANSLATE("ifree"));
1104 		return;
1105 	}
1106 	if (b_option) {
1107 		(void) printf(gettext("%-*s %*s\n"),
1108 			FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1109 			BLOCK_WIDTH, TRANSLATE("avail"));
1110 		return;
1111 	}
1112 }
1113 
1114 
1115 /*
1116  * Convert an unsigned long long to a string representation and place the
1117  * result in the caller-supplied buffer.
1118  * The given number is in units of "unit_from" size, but the
1119  * converted number will be in units of "unit_to" size. The unit sizes
1120  * must be powers of 2.
1121  * The value "(unsigned long long)-1" is a special case and is always
1122  * converted to "-1".
1123  * Returns a pointer to the caller-supplied buffer.
1124  */
1125 static char *
1126 number_to_string(
1127 			char *buf,		/* put the result here */
1128 			unsigned long long number, /* convert this number */
1129 			int unit_from,		/* from units of this size */
1130 			int unit_to)		/* to units of this size */
1131 {
1132 	if ((long long)number == (long long)-1)
1133 		(void) strcpy(buf, "-1");
1134 	else {
1135 		if (unit_from == unit_to)
1136 			(void) sprintf(buf, "%llu", number);
1137 		else if (unit_from < unit_to)
1138 			(void) sprintf(buf, "%llu",
1139 			    number / (unsigned long long)(unit_to / unit_from));
1140 		else
1141 			(void) sprintf(buf, "%llu",
1142 			    number * (unsigned long long)(unit_from / unit_to));
1143 	}
1144 	return (buf);
1145 }
1146 
1147 /*
1148  * Convert an unsigned long long to a string representation and place the
1149  * result in the caller-supplied buffer.
1150  * The given number is in units of "unit_from" size,
1151  * this will first be converted to a number in 1024 or 1000 byte size,
1152  * depending on the scaling factor.
1153  * Then the number is scaled down until it is small enough to be in a good
1154  * human readable format i.e. in the range 0 thru scale-1.
1155  * If it's smaller than 10 there's room enough to provide one decimal place.
1156  * The value "(unsigned long long)-1" is a special case and is always
1157  * converted to "-1".
1158  * Returns a pointer to the caller-supplied buffer.
1159  */
1160 static char *
1161 number_to_scaled_string(
1162 			numbuf_t buf,		/* put the result here */
1163 			unsigned long long number, /* convert this number */
1164 			int unit_from,
1165 			int scale)
1166 {
1167 	unsigned long long save = 0;
1168 	char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */
1169 	char *uom = M;    /* unit of measurement, initially 'K' (=M[0]) */
1170 
1171 	if ((long long)number == (long long)-1) {
1172 		(void) strcpy(buf, "-1");
1173 		return (buf);
1174 	}
1175 
1176 	/*
1177 	 * Convert number from unit_from to given scale (1024 or 1000).
1178 	 * This means multiply number by unit_from and divide by scale.
1179 	 *
1180 	 * Would like to multiply by unit_from and then divide by scale,
1181 	 * but if the first multiplication would overflow, then need to
1182 	 * divide by scale and then multiply by unit_from.
1183 	 */
1184 	if (number > (UINT64_MAX / (unsigned long long)unit_from)) {
1185 		number = (number / (unsigned long long)scale) *
1186 		    (unsigned long long)unit_from;
1187 	} else {
1188 		number = (number * (unsigned long long)unit_from) /
1189 		    (unsigned long long)scale;
1190 	}
1191 
1192 	/*
1193 	 * Now we have number as a count of scale units.
1194 	 * Stop scaling when we reached exa bytes, then something is
1195 	 * probably wrong with our number.
1196 	 */
1197 
1198 	while ((number >= scale) && (*uom != 'E')) {
1199 		uom++; /* next unit of measurement */
1200 		save = number;
1201 		number = (number + (scale / 2)) / scale;
1202 	}
1203 	/* check if we should output a decimal place after the point */
1204 	if (save && ((save / scale) < 10)) {
1205 		/* sprintf() will round for us */
1206 		float fnum = (float)save / scale;
1207 		(void) sprintf(buf, "%2.1f%c", fnum, *uom);
1208 	} else {
1209 		(void) sprintf(buf, "%4llu%c", number, *uom);
1210 	}
1211 	return (buf);
1212 }
1213 
1214 /*
1215  * The statvfs() implementation allows us to return only two values, the total
1216  * number of blocks and the number of blocks free.  The equation 'used = total -
1217  * free' will not work for ZFS filesystems, due to the nature of pooled storage.
1218  * We choose to return values in the statvfs structure that will produce correct
1219  * results for 'used' and 'available', but not 'total'.  This function will open
1220  * the underlying ZFS dataset if necessary and get the real value.
1221  */
1222 static void
1223 adjust_total_blocks(struct df_request *dfrp, fsblkcnt64_t *total,
1224     uint64_t blocksize)
1225 {
1226 	zfs_handle_t	*zhp;
1227 	char *dataset, *slash;
1228 	uint64_t quota;
1229 
1230 	if (strcmp(DFR_FSTYPE(dfrp), MNTTYPE_ZFS) != 0 ||
1231 	    !load_libzfs())
1232 		return;
1233 
1234 	/*
1235 	 * We want to get the total size for this filesystem as bounded by any
1236 	 * quotas. In order to do this, we start at the current filesystem and
1237 	 * work upwards until we find a dataset with a quota.  If we reach the
1238 	 * pool itself, then the total space is the amount used plus the amount
1239 	 * available.
1240 	 */
1241 	if ((dataset = strdup(DFR_SPECIAL(dfrp))) == NULL)
1242 		return;
1243 
1244 	slash = dataset + strlen(dataset);
1245 	do {
1246 		*slash = '\0';
1247 
1248 		if ((zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_ANY)) == NULL) {
1249 			free(dataset);
1250 			return;
1251 		}
1252 
1253 		if ((quota = _zfs_prop_get_int(zhp, ZFS_PROP_QUOTA)) != 0) {
1254 			*total = quota / blocksize;
1255 			_zfs_close(zhp);
1256 			free(dataset);
1257 			return;
1258 		}
1259 
1260 		_zfs_close(zhp);
1261 
1262 	} while ((slash = strrchr(dataset, '/')) != NULL);
1263 
1264 
1265 	if ((zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_ANY)) == NULL) {
1266 		free(dataset);
1267 		return;
1268 	}
1269 
1270 	*total = (_zfs_prop_get_int(zhp, ZFS_PROP_USED) +
1271 	    _zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE)) / blocksize;
1272 
1273 	_zfs_close(zhp);
1274 	free(dataset);
1275 }
1276 
1277 /*
1278  * The output will appear properly columnized regardless of the names of
1279  * the various fields
1280  */
1281 static void
1282 g_output(struct df_request *dfrp, struct statvfs64 *fsp)
1283 {
1284 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
1285 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
1286 	numbuf_t	total_blocks_buf;
1287 	numbuf_t	total_files_buf;
1288 	numbuf_t	free_blocks_buf;
1289 	numbuf_t	available_blocks_buf;
1290 	numbuf_t	free_files_buf;
1291 	numbuf_t	fname_buf;
1292 	char		*temp_buf;
1293 
1294 #define	DEFINE_STR_LEN(var)			\
1295 	static char *var##_str;			\
1296 	static size_t var##_len
1297 
1298 #define	SET_STR_LEN(name, var)\
1299 	if (! var##_str) {\
1300 		var##_str = TRANSLATE(name); \
1301 		var##_len = strlen(var##_str); \
1302 	}
1303 
1304 	DEFINE_STR_LEN(block_size);
1305 	DEFINE_STR_LEN(frag_size);
1306 	DEFINE_STR_LEN(total_blocks);
1307 	DEFINE_STR_LEN(free_blocks);
1308 	DEFINE_STR_LEN(available);
1309 	DEFINE_STR_LEN(total_files);
1310 	DEFINE_STR_LEN(free_files);
1311 	DEFINE_STR_LEN(fstype);
1312 	DEFINE_STR_LEN(fsys_id);
1313 	DEFINE_STR_LEN(fname);
1314 	DEFINE_STR_LEN(flag);
1315 
1316 	/*
1317 	 * TRANSLATION_NOTE
1318 	 * The first argument of each of the following macro invocations is a
1319 	 * string that needs to be translated.
1320 	 */
1321 	SET_STR_LEN("block size", block_size);
1322 	SET_STR_LEN("frag size", frag_size);
1323 	SET_STR_LEN("total blocks", total_blocks);
1324 	SET_STR_LEN("free blocks", free_blocks);
1325 	SET_STR_LEN("available", available);
1326 	SET_STR_LEN("total files", total_files);
1327 	SET_STR_LEN("free files", free_files);
1328 	SET_STR_LEN("fstype", fstype);
1329 	SET_STR_LEN("filesys id", fsys_id);
1330 	SET_STR_LEN("filename length", fname);
1331 	SET_STR_LEN("flag", flag);
1332 
1333 #define	NCOL1_WIDTH	(int)MAX3(BLOCK_WIDTH, NFILES_WIDTH, FSTYPE_WIDTH)
1334 #define	NCOL2_WIDTH	(int)MAX3(BLOCK_WIDTH, FSID_WIDTH, FLAG_WIDTH) + 2
1335 #define	NCOL3_WIDTH	(int)MAX3(BSIZE_WIDTH, BLOCK_WIDTH, NAMELEN_WIDTH)
1336 #define	NCOL4_WIDTH	(int)MAX(FRAGSIZE_WIDTH, NFILES_WIDTH)
1337 
1338 #define	SCOL1_WIDTH	(int)MAX3(total_blocks_len, free_files_len, fstype_len)
1339 #define	SCOL2_WIDTH	(int)MAX3(free_blocks_len, fsys_id_len, flag_len)
1340 #define	SCOL3_WIDTH	(int)MAX3(block_size_len, available_len, fname_len)
1341 #define	SCOL4_WIDTH	(int)MAX(frag_size_len, total_files_len)
1342 
1343 	temp_buf = xmalloc(
1344 	    MAX(MOUNT_POINT_WIDTH, strlen(DFR_MOUNT_POINT(dfrp)))
1345 	    + MAX(SPECIAL_DEVICE_WIDTH, strlen(DFR_SPECIAL(dfrp)))
1346 	    + 20); /* plus slop - nulls & formatting */
1347 	(void) sprintf(temp_buf, "%-*s(%-*s):",
1348 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1349 		SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp));
1350 
1351 	(void) printf("%-*s %*lu %-*s %*lu %-*s\n",
1352 	NCOL1_WIDTH + 1 + SCOL1_WIDTH + 1 + NCOL2_WIDTH + 1 +  SCOL2_WIDTH,
1353 		temp_buf,
1354 	NCOL3_WIDTH, fsp->f_bsize, SCOL3_WIDTH, block_size_str,
1355 	NCOL4_WIDTH, fsp->f_frsize, SCOL4_WIDTH, frag_size_str);
1356 	free(temp_buf);
1357 
1358 	/*
1359 	 * Adjust available_blocks value -  it can be less than 0 on
1360 	 * a 4.x file system. Reset it to 0 in order to avoid printing
1361 	 * negative numbers.
1362 	 */
1363 	if ((long long)available_blocks < (long long)0)
1364 		available_blocks = (fsblkcnt64_t)0;
1365 
1366 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1367 
1368 	(void) printf("%*s %-*s %*s %-*s %*s %-*s %*s %-*s\n",
1369 		NCOL1_WIDTH, number_to_string(total_blocks_buf,
1370 					total_blocks, fsp->f_frsize, 512),
1371 			SCOL1_WIDTH, total_blocks_str,
1372 		NCOL2_WIDTH, number_to_string(free_blocks_buf,
1373 					fsp->f_bfree, fsp->f_frsize, 512),
1374 			SCOL2_WIDTH, free_blocks_str,
1375 		NCOL3_WIDTH, number_to_string(available_blocks_buf,
1376 					available_blocks, fsp->f_frsize, 512),
1377 			SCOL3_WIDTH, available_str,
1378 		NCOL4_WIDTH, number_to_string(total_files_buf,
1379 					fsp->f_files, 1, 1),
1380 			SCOL4_WIDTH, total_files_str);
1381 
1382 	(void) printf("%*s %-*s %*lu %-*s %s\n",
1383 		NCOL1_WIDTH, number_to_string(free_files_buf,
1384 					fsp->f_ffree, 1, 1),
1385 			SCOL1_WIDTH, free_files_str,
1386 		NCOL2_WIDTH, fsp->f_fsid, SCOL2_WIDTH, fsys_id_str,
1387 		fsp->f_fstr);
1388 
1389 	(void) printf("%*s %-*s %#*.*lx %-*s %*s %-*s\n\n",
1390 		NCOL1_WIDTH, fsp->f_basetype, SCOL1_WIDTH, fstype_str,
1391 		NCOL2_WIDTH, NCOL2_WIDTH-2, fsp->f_flag, SCOL2_WIDTH, flag_str,
1392 		NCOL3_WIDTH, number_to_string(fname_buf,
1393 			(unsigned long long)fsp->f_namemax, 1, 1),
1394 			SCOL3_WIDTH, fname_str);
1395 }
1396 
1397 
1398 static void
1399 k_output(struct df_request *dfrp, struct statvfs64 *fsp)
1400 {
1401 	fsblkcnt64_t total_blocks		= fsp->f_blocks;
1402 	fsblkcnt64_t	free_blocks		= fsp->f_bfree;
1403 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
1404 	fsblkcnt64_t	used_blocks;
1405 	char 		*file_system		= DFR_SPECIAL(dfrp);
1406 	numbuf_t	total_blocks_buf;
1407 	numbuf_t	used_blocks_buf;
1408 	numbuf_t	available_blocks_buf;
1409 	char 		capacity_buf[LINEBUF_SIZE];
1410 
1411 	/*
1412 	 * If the free block count is -1, don't trust anything but the total
1413 	 * number of blocks.
1414 	 */
1415 	if (free_blocks == (fsblkcnt64_t)-1) {
1416 		used_blocks = (fsblkcnt64_t)-1;
1417 		(void) strcpy(capacity_buf, "  100%");
1418 	} else {
1419 		fsblkcnt64_t reserved_blocks = free_blocks - available_blocks;
1420 
1421 		used_blocks	= total_blocks - free_blocks;
1422 
1423 		/*
1424 		 * The capacity estimation is bogus when available_blocks is 0
1425 		 * and the super-user has allocated more space. The reason
1426 		 * is that reserved_blocks is inaccurate in that case, because
1427 		 * when the super-user allocates space, free_blocks is updated
1428 		 * but available_blocks is not (since it can't drop below 0).
1429 		 *
1430 		 * XCU4 and POSIX.2 require that any fractional result of the
1431 		 * capacity estimation be rounded to the next highest integer,
1432 		 * hence the addition of 0.5.
1433 		 */
1434 		(void) sprintf(capacity_buf, "%5.0f%%",
1435 			(total_blocks == 0) ? 0.0 :
1436 			((double)used_blocks /
1437 				(double)(total_blocks - reserved_blocks))
1438 					* 100.0 + 0.5);
1439 	}
1440 
1441 	/*
1442 	 * The available_blocks can be less than 0 on a 4.x file system.
1443 	 * Reset it to 0 in order to avoid printing negative numbers.
1444 	 */
1445 	if ((long long)available_blocks < (long long)0)
1446 		available_blocks = (fsblkcnt64_t)0;
1447 	/*
1448 	 * Print long special device names (usually NFS mounts) in a line
1449 	 * by themselves when the output is directed to a terminal.
1450 	 */
1451 	if (tty_output && strlen(file_system) > (size_t)FILESYSTEM_WIDTH) {
1452 		(void) printf("%s\n", file_system);
1453 		file_system = "";
1454 	}
1455 
1456 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1457 
1458 	if (use_scaling) { /* comes from the -h option */
1459 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1460 		FILESYSTEM_WIDTH, file_system,
1461 		SCALED_WIDTH, number_to_scaled_string(total_blocks_buf,
1462 					total_blocks, fsp->f_frsize, scale),
1463 		SCALED_WIDTH, number_to_scaled_string(used_blocks_buf,
1464 					used_blocks, fsp->f_frsize, scale),
1465 		AVAILABLE_WIDTH, number_to_scaled_string(available_blocks_buf,
1466 					available_blocks, fsp->f_frsize, scale),
1467 		CAPACITY_WIDTH, capacity_buf,
1468 		DFR_MOUNT_POINT(dfrp));
1469 		return;
1470 	}
1471 
1472 	if (v_option) {
1473 	(void) printf("%-*.*s %-*.*s %*lld %*lld %*lld %-.*s\n",
1474 		IBCS2_MOUNT_POINT_WIDTH, IBCS2_MOUNT_POINT_WIDTH,
1475 		DFR_MOUNT_POINT(dfrp),
1476 		IBCS2_FILESYSTEM_WIDTH, IBCS2_FILESYSTEM_WIDTH, file_system,
1477 		BLOCK_WIDTH, total_blocks,
1478 		BLOCK_WIDTH, used_blocks,
1479 		BLOCK_WIDTH, available_blocks,
1480 		CAPACITY_WIDTH,	capacity_buf);
1481 		return;
1482 	}
1483 
1484 	if (P_option && !k_option) {
1485 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1486 		FILESYSTEM_WIDTH, file_system,
1487 		KBYTE_WIDTH, number_to_string(total_blocks_buf,
1488 					total_blocks, fsp->f_frsize, 512),
1489 		KBYTE_WIDTH, number_to_string(used_blocks_buf,
1490 					used_blocks, fsp->f_frsize, 512),
1491 		KBYTE_WIDTH, number_to_string(available_blocks_buf,
1492 					available_blocks, fsp->f_frsize, 512),
1493 		CAPACITY_WIDTH, capacity_buf,
1494 		DFR_MOUNT_POINT(dfrp));
1495 	} else {
1496 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1497 		FILESYSTEM_WIDTH, file_system,
1498 		KBYTE_WIDTH, number_to_string(total_blocks_buf,
1499 					total_blocks, fsp->f_frsize, 1024),
1500 		KBYTE_WIDTH, number_to_string(used_blocks_buf,
1501 					used_blocks, fsp->f_frsize, 1024),
1502 		KBYTE_WIDTH, number_to_string(available_blocks_buf,
1503 					available_blocks, fsp->f_frsize, 1024),
1504 		CAPACITY_WIDTH,	capacity_buf,
1505 		DFR_MOUNT_POINT(dfrp));
1506 	}
1507 }
1508 
1509 /*
1510  * The following is for internationalization support.
1511  */
1512 static bool_int strings_initialized;
1513 static char 	*files_str;
1514 static char	*blocks_str;
1515 static char	*total_str;
1516 static char	*kilobytes_str;
1517 
1518 static void
1519 strings_init(void)
1520 {
1521 	total_str = TRANSLATE("total");
1522 #ifdef	_iBCS2
1523 	/* ISC/SCO print i-nodes instead of files */
1524 	if (sysv3_set)
1525 		files_str = TRANSLATE("i-nodes");
1526 	else
1527 #endif	/* _iBCS2 */
1528 		files_str = TRANSLATE("files");
1529 	blocks_str = TRANSLATE("blocks");
1530 	kilobytes_str = TRANSLATE("kilobytes");
1531 	strings_initialized = TRUE;
1532 }
1533 
1534 #define	STRINGS_INIT()		if (!strings_initialized) strings_init()
1535 
1536 
1537 static void
1538 t_output(struct df_request *dfrp, struct statvfs64 *fsp)
1539 {
1540 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
1541 	numbuf_t	total_blocks_buf;
1542 	numbuf_t	total_files_buf;
1543 	numbuf_t	free_blocks_buf;
1544 	numbuf_t	free_files_buf;
1545 
1546 	STRINGS_INIT();
1547 
1548 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1549 
1550 	(void) printf("%-*s(%-*s): %*s %s %*s %s\n",
1551 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1552 		SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1553 		BLOCK_WIDTH, number_to_string(free_blocks_buf,
1554 			fsp->f_bfree, fsp->f_frsize, 512),
1555 			blocks_str,
1556 		NFILES_WIDTH, number_to_string(free_files_buf,
1557 			fsp->f_ffree, 1, 1),
1558 		files_str);
1559 	/*
1560 	 * The total column used to use the same space as the mnt pt & special
1561 	 * dev fields. However, this doesn't work with massive special dev
1562 	 * fields * (eg > 500 chars) causing an enormous amount of white space
1563 	 * before the total column (see bug 4100411). So the code was
1564 	 * simplified to set the total column at the usual gap.
1565 	 * This had the side effect of fixing a bug where the previously
1566 	 * used static buffer was overflowed by the same massive special dev.
1567 	 */
1568 	(void) printf("%*s: %*s %s %*s %s\n",
1569 		MNT_SPEC_WIDTH, total_str,
1570 		BLOCK_WIDTH, number_to_string(total_blocks_buf,
1571 				total_blocks, fsp->f_frsize, 512),
1572 		blocks_str,
1573 		NFILES_WIDTH, number_to_string(total_files_buf,
1574 				fsp->f_files, 1, 1),
1575 		files_str);
1576 }
1577 
1578 
1579 static void
1580 eb_output(struct df_request *dfrp, struct statvfs64 *fsp)
1581 {
1582 	numbuf_t free_files_buf;
1583 	numbuf_t free_kbytes_buf;
1584 
1585 	STRINGS_INIT();
1586 
1587 	(void) printf("%-*s(%-*s): %*s %s\n",
1588 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1589 		SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1590 		MAX(KBYTE_WIDTH, NFILES_WIDTH),
1591 			number_to_string(free_kbytes_buf,
1592 			fsp->f_bfree, fsp->f_frsize, 1024),
1593 		kilobytes_str);
1594 	(void) printf("%-*s(%-*s): %*s %s\n",
1595 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1596 		SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1597 		MAX(NFILES_WIDTH, NFILES_WIDTH),
1598 			number_to_string(free_files_buf, fsp->f_ffree, 1, 1),
1599 		files_str);
1600 }
1601 
1602 
1603 static void
1604 e_output(struct df_request *dfrp, struct statvfs64 *fsp)
1605 {
1606 	numbuf_t free_files_buf;
1607 
1608 	(void) printf("%-*s %*s\n",
1609 		FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
1610 		NFILES_WIDTH,
1611 			number_to_string(free_files_buf, fsp->f_ffree, 1, 1));
1612 }
1613 
1614 
1615 static void
1616 b_output(struct df_request *dfrp, struct statvfs64 *fsp)
1617 {
1618 	numbuf_t free_blocks_buf;
1619 
1620 	(void) printf("%-*s %*s\n",
1621 		FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
1622 		BLOCK_WIDTH, number_to_string(free_blocks_buf,
1623 				fsp->f_bfree, fsp->f_frsize, 1024));
1624 }
1625 
1626 
1627 /* ARGSUSED */
1628 static void
1629 n_output(struct df_request *dfrp, struct statvfs64 *fsp)
1630 {
1631 	(void) printf("%-*s: %-*s\n",
1632 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1633 		FSTYPE_WIDTH, dfrp->dfr_fstype);
1634 }
1635 
1636 
1637 static void
1638 default_output(struct df_request *dfrp, struct statvfs64 *fsp)
1639 {
1640 	numbuf_t free_blocks_buf;
1641 	numbuf_t free_files_buf;
1642 
1643 	STRINGS_INIT();
1644 
1645 	(void) printf("%-*s(%-*s):%*s %s %*s %s\n",
1646 		MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1647 		SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1648 		BLOCK_WIDTH, number_to_string(free_blocks_buf,
1649 			fsp->f_bfree, fsp->f_frsize, 512),
1650 		blocks_str,
1651 		NFILES_WIDTH, number_to_string(free_files_buf,
1652 			fsp->f_ffree, 1, 1),
1653 		files_str);
1654 }
1655 
1656 
1657 /* ARGSUSED */
1658 static void
1659 V_output(struct df_request *dfrp, struct statvfs64 *fsp)
1660 {
1661 	char temp_buf[LINEBUF_SIZE];
1662 
1663 	if (df_options_len > 1)
1664 		(void) strcat(strcpy(temp_buf, df_options), " ");
1665 	else
1666 		temp_buf[0] = NUL;
1667 
1668 	(void) printf("%s -F %s %s%s\n",
1669 		program_name, dfrp->dfr_fstype, temp_buf,
1670 		dfrp->dfr_cmd_arg ? dfrp->dfr_cmd_arg: DFR_SPECIAL(dfrp));
1671 }
1672 
1673 
1674 /*
1675  * This function is used to sort the array of df_requests according to fstype
1676  */
1677 static int
1678 df_reqcomp(const void *p1, const void *p2)
1679 {
1680 	int v = strcmp(DFRP(p1)->dfr_fstype, DFRP(p2)->dfr_fstype);
1681 
1682 	if (v != 0)
1683 		return (v);
1684 	else
1685 		return (DFRP(p1)->dfr_index - DFRP(p2)->dfr_index);
1686 }
1687 
1688 
1689 static void
1690 vfs_error(char *file, int status)
1691 {
1692 	if (status == VFS_TOOLONG)
1693 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
1694 			file, MNT_LINE_MAX);
1695 	else if (status == VFS_TOOMANY)
1696 		errmsg(ERR_NOFLAGS, "a line in %s has too many fields", file);
1697 	else if (status == VFS_TOOFEW)
1698 		errmsg(ERR_NOFLAGS, "a line in %s has too few fields", file);
1699 	else
1700 		errmsg(ERR_NOFLAGS, "error while reading %s: %d", file, status);
1701 }
1702 
1703 
1704 /*
1705  * Try to determine the fstype for the specified block device.
1706  * Return in order of decreasing preference:
1707  *	file system type from vfstab
1708  *	file system type as specified by -F option
1709  *	default file system type
1710  */
1711 static char *
1712 find_fstype(char *special)
1713 {
1714 	struct vfstab	vtab;
1715 	FILE		*fp;
1716 	int		status;
1717 	char		*vfstab_file = VFS_TAB;
1718 
1719 	fp = xfopen(vfstab_file);
1720 	status = getvfsspec(fp, &vtab, special);
1721 	(void) fclose(fp);
1722 	if (status > 0)
1723 		vfs_error(vfstab_file, status);
1724 
1725 	if (status == 0) {
1726 		if (F_option && ! EQ(FSType, vtab.vfs_fstype))
1727 			errmsg(ERR_NOFLAGS,
1728 			"warning: %s is of type %s", special, vtab.vfs_fstype);
1729 		return (new_string(vtab.vfs_fstype));
1730 	}
1731 	else
1732 		return (F_option ? FSType : default_fstype(special));
1733 }
1734 
1735 /*
1736  * When this function returns, the following fields are filled for all
1737  * valid entries in the requests[] array:
1738  *		dfr_mte		(if the file system is mounted)
1739  *		dfr_fstype
1740  *		dfr_index
1741  *
1742  * The function returns the number of errors that occurred while building
1743  * the request list.
1744  */
1745 static int
1746 create_request_list(
1747 			int argc,
1748 			char *argv[],
1749 			struct df_request *requests_p[],
1750 			size_t *request_count)
1751 {
1752 	struct df_request	*requests;
1753 	struct df_request	*dfrp;
1754 	size_t			size;
1755 	size_t 			i;
1756 	size_t 			request_index = 0;
1757 	size_t			max_requests;
1758 	int			errors = 0;
1759 
1760 	/*
1761 	 * If no args, use the mounted file systems, otherwise use the
1762 	 * user-specified arguments.
1763 	 */
1764 	if (argc == 0) {
1765 		mtab_read_file();
1766 		max_requests = mount_table_entries;
1767 	} else
1768 		max_requests = argc;
1769 
1770 	size = max_requests * sizeof (struct df_request);
1771 	requests = xmalloc(size);
1772 	(void) memset(requests, 0, size);
1773 
1774 	if (argc == 0) {
1775 		/*
1776 		 * If -Z wasn't specified, we skip mounts in other
1777 		 * zones.  This obviously is a noop in a non-global
1778 		 * zone.
1779 		 */
1780 		boolean_t showall = (getzoneid() != GLOBAL_ZONEID) || Z_option;
1781 		struct zone_summary *zsp;
1782 
1783 		if (!showall) {
1784 			zsp = fs_get_zone_summaries();
1785 			if (zsp == NULL)
1786 				errmsg(ERR_FATAL,
1787 				    "unable to retrieve list of zones");
1788 		}
1789 
1790 		for (i = 0; i < mount_table_entries; i++) {
1791 			struct extmnttab *mtp = mount_table[i].mte_mount;
1792 
1793 			if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
1794 				continue;
1795 
1796 			if (!showall) {
1797 				if (fs_mount_in_other_zone(zsp,
1798 				    mtp->mnt_mountp))
1799 					continue;
1800 			}
1801 			dfrp = &requests[request_index++];
1802 			dfrp->dfr_mte		= &mount_table[i];
1803 			dfrp->dfr_fstype	= mtp->mnt_fstype;
1804 			dfrp->dfr_index		= i;
1805 			dfrp->dfr_valid		= TRUE;
1806 		}
1807 	} else {
1808 		struct stat64 *arg_stat; /* array of stat structures	*/
1809 		bool_int *valid_stat;	/* which structures are valid	*/
1810 
1811 		arg_stat = xmalloc(argc * sizeof (struct stat64));
1812 		valid_stat = xmalloc(argc * sizeof (bool_int));
1813 
1814 		/*
1815 		 * Obtain stat64 information for each argument before
1816 		 * constructing the list of mounted file systems. By
1817 		 * touching all these places we force the automounter
1818 		 * to establish any mounts required to access the arguments,
1819 		 * so that the corresponding mount table entries will exist
1820 		 * when we look for them.
1821 		 * It is still possible that the automounter may timeout
1822 		 * mounts between the time we read the mount table and the
1823 		 * time we process the request. Even in that case, when
1824 		 * we issue the statvfs64(2) for the mount point, the file
1825 		 * system will be mounted again. The only problem will
1826 		 * occur if the automounter maps change in the meantime
1827 		 * and the mount point is eliminated.
1828 		 */
1829 		for (i = 0; i < argc; i++)
1830 			valid_stat[i] = (stat64(argv[i], &arg_stat[i]) == 0);
1831 
1832 		mtab_read_file();
1833 
1834 		for (i = 0; i < argc; i++) {
1835 			char *arg = argv[i];
1836 
1837 			dfrp = &requests[request_index];
1838 
1839 			dfrp->dfr_index = request_index;
1840 			dfrp->dfr_cmd_arg = arg;
1841 
1842 			if (valid_stat[i]) {
1843 				if (S_ISBLK(arg_stat[i].st_mode)) {
1844 					bdev_mount_entry(dfrp);
1845 					dfrp->dfr_valid = TRUE;
1846 				} else if (S_ISDIR(arg_stat[i].st_mode) ||
1847 					S_ISREG(arg_stat[i].st_mode) ||
1848 					S_ISFIFO(arg_stat[i].st_mode)) {
1849 					path_mount_entry(dfrp,
1850 						arg_stat[i].st_dev);
1851 					if (! DFR_ISMOUNTEDFS(dfrp)) {
1852 						errors++;
1853 						continue;
1854 					}
1855 					dfrp->dfr_valid = TRUE;
1856 				}
1857 			} else {
1858 				resource_mount_entry(dfrp);
1859 				dfrp->dfr_valid = DFR_ISMOUNTEDFS(dfrp);
1860 			}
1861 
1862 			/*
1863 			 * If we haven't managed to verify that the request
1864 			 * is valid, we must have gotten a bad argument.
1865 			 */
1866 			if (!dfrp->dfr_valid) {
1867 				errmsg(ERR_NOFLAGS,
1868 		"(%-10s) not a block device, directory or mounted resource",
1869 					arg);
1870 				errors++;
1871 				continue;
1872 			}
1873 
1874 			/*
1875 			 * Determine the file system type.
1876 			 */
1877 			if (DFR_ISMOUNTEDFS(dfrp))
1878 				dfrp->dfr_fstype =
1879 					dfrp->dfr_mte->mte_mount->mnt_fstype;
1880 			else
1881 				dfrp->dfr_fstype =
1882 					find_fstype(dfrp->dfr_cmd_arg);
1883 
1884 			request_index++;
1885 		}
1886 	}
1887 	*requests_p = requests;
1888 	*request_count = request_index;
1889 	return (errors);
1890 }
1891 
1892 
1893 /*
1894  * Select the appropriate function and flags to use for output.
1895  * Notice that using both -e and -b options produces a different form of
1896  * output than either of those two options alone; this is the behavior of
1897  * the SVR4 df.
1898  */
1899 static struct df_output *
1900 select_output(void)
1901 {
1902 	static struct df_output dfo;
1903 
1904 	/*
1905 	 * The order of checking options follows the option precedence
1906 	 * rules as they are listed in the man page.
1907 	 */
1908 	if (use_scaling) { /* comes from the -h option */
1909 		dfo.dfo_func = k_output;
1910 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1911 	} else if (V_option) {
1912 		dfo.dfo_func = V_output;
1913 		dfo.dfo_flags = DFO_NOFLAGS;
1914 	} else if (g_option) {
1915 		dfo.dfo_func = g_output;
1916 		dfo.dfo_flags = DFO_STATVFS;
1917 	} else if (k_option || P_option || v_option) {
1918 		dfo.dfo_func = k_output;
1919 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1920 	} else if (t_option) {
1921 		dfo.dfo_func = t_output;
1922 		dfo.dfo_flags = DFO_STATVFS;
1923 	} else if (b_option && e_option) {
1924 		dfo.dfo_func = eb_output;
1925 		dfo.dfo_flags = DFO_STATVFS;
1926 	} else if (b_option) {
1927 		dfo.dfo_func = b_output;
1928 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1929 	} else if (e_option) {
1930 		dfo.dfo_func = e_output;
1931 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1932 	} else if (n_option) {
1933 		dfo.dfo_func = n_output;
1934 		dfo.dfo_flags = DFO_NOFLAGS;
1935 	} else {
1936 		dfo.dfo_func = default_output;
1937 		dfo.dfo_flags = DFO_STATVFS;
1938 	}
1939 	return (&dfo);
1940 }
1941 
1942 
1943 /*
1944  * The (argc,argv) pair contains all the non-option arguments
1945  */
1946 static void
1947 do_df(int argc, char *argv[])
1948 {
1949 	size_t			i;
1950 	struct df_request	*requests;		/* array of requests */
1951 	size_t			n_requests;
1952 	struct df_request	*dfrp;
1953 	int			errors;
1954 
1955 	errors = create_request_list(argc, argv, &requests, &n_requests);
1956 
1957 	if (n_requests == 0)
1958 		exit(errors);
1959 
1960 	/*
1961 	 * If we are going to run the FSType-specific df command,
1962 	 * rearrange the requests so that we can issue a single command
1963 	 * per file system type.
1964 	 */
1965 	if (o_option) {
1966 		size_t j;
1967 
1968 		/*
1969 		 * qsort is not a stable sorting method (i.e. requests of
1970 		 * the same file system type may be swapped, and hence appear
1971 		 * in the output in a different order from the one in which
1972 		 * they were listed in the command line). In order to force
1973 		 * stability, we use the dfr_index field which is unique
1974 		 * for each request.
1975 		 */
1976 		qsort(requests,
1977 			n_requests, sizeof (struct df_request), df_reqcomp);
1978 		for (i = 0; i < n_requests; i = j) {
1979 			char *fstype = requests[i].dfr_fstype;
1980 
1981 			for (j = i+1; j < n_requests; j++)
1982 				if (! EQ(fstype, requests[j].dfr_fstype))
1983 					break;
1984 
1985 			/*
1986 			 * At this point, requests in the range [i,j) are
1987 			 * of the same type.
1988 			 *
1989 			 * If the -F option was used, and the user specified
1990 			 * arguments, the filesystem types must match
1991 			 *
1992 			 * XXX: the alternative of doing this check here is to
1993 			 * 	invoke prune_list, but then we have to
1994 			 *	modify this code to ignore invalid requests.
1995 			 */
1996 			if (F_option && ! EQ(fstype, FSType)) {
1997 				size_t k;
1998 
1999 				for (k = i; k < j; k++) {
2000 					dfrp = &requests[k];
2001 					if (dfrp->dfr_cmd_arg != NULL) {
2002 						errmsg(ERR_NOFLAGS,
2003 				"Warning: %s mounted as a %s file system",
2004 					dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
2005 						errors++;
2006 					}
2007 				}
2008 			} else
2009 				errors += run_fs_specific_df(&requests[i], j-i);
2010 		}
2011 	} else {
2012 		size_t valid_requests;
2013 
2014 		/*
2015 		 * We have to prune the request list to avoid printing a header
2016 		 * if there are no valid requests
2017 		 */
2018 		errors += prune_list(requests, n_requests, &valid_requests);
2019 
2020 		if (valid_requests) {
2021 			struct df_output *dfop = select_output();
2022 
2023 			/* indicates if we already printed out a header line */
2024 			int printed_header = 0;
2025 
2026 			for (i = 0; i < n_requests; i++) {
2027 				dfrp = &requests[i];
2028 				if (! dfrp->dfr_valid)
2029 					continue;
2030 
2031 				/*
2032 				 * If we don't have a mount point,
2033 				 * this must be a block device.
2034 				 */
2035 				if (DFR_ISMOUNTEDFS(dfrp)) {
2036 					struct statvfs64 stvfs;
2037 
2038 					if ((dfop->dfo_flags & DFO_STATVFS) &&
2039 						statvfs64(DFR_MOUNT_POINT(dfrp),
2040 							&stvfs) == -1) {
2041 						errmsg(ERR_PERROR,
2042 							"cannot statvfs %s:",
2043 							DFR_MOUNT_POINT(dfrp));
2044 						errors++;
2045 						continue;
2046 					}
2047 					if ((!printed_header) &&
2048 					    (dfop->dfo_flags & DFO_HEADER)) {
2049 						print_header();
2050 						printed_header = 1;
2051 					}
2052 
2053 					(*dfop->dfo_func)(dfrp, &stvfs);
2054 				} else {
2055 					/*
2056 					 *  -h option only works for
2057 					 *  mounted filesystems
2058 					 */
2059 					if (use_scaling) {
2060 						errmsg(ERR_NOFLAGS,
2061 		"-h option incompatible with unmounted special device (%s)",
2062 						    dfrp->dfr_cmd_arg);
2063 						errors++;
2064 						continue;
2065 					}
2066 					errors += run_fs_specific_df(dfrp, 1);
2067 				}
2068 			}
2069 		}
2070 	}
2071 	exit(errors);
2072 }
2073 
2074 
2075 /*
2076  * The rest of this file implements the devnm command
2077  */
2078 
2079 static char *
2080 find_dev_name(char *file, dev_t dev)
2081 {
2082 	struct df_request dfreq;
2083 
2084 	dfreq.dfr_cmd_arg = file;
2085 	dfreq.dfr_fstype = 0;
2086 	dfreq.dfr_mte = NULL;
2087 	path_mount_entry(&dfreq, dev);
2088 	return (DFR_ISMOUNTEDFS(&dfreq) ? DFR_SPECIAL(&dfreq) : NULL);
2089 }
2090 
2091 
2092 static void
2093 do_devnm(int argc, char *argv[])
2094 {
2095 	int arg;
2096 	int errors = 0;
2097 	char *dev_name;
2098 
2099 	if (argc == 1)
2100 		errmsg(ERR_NONAME, "Usage: %s name ...", DEVNM_CMD);
2101 
2102 	mtab_read_file();
2103 
2104 	for (arg = 1; arg < argc; arg++) {
2105 		char *file = argv[arg];
2106 		struct stat64 st;
2107 
2108 		if (stat64(file, &st) == -1) {
2109 			errmsg(ERR_PERROR, "%s: ", file);
2110 			errors++;
2111 			continue;
2112 		}
2113 
2114 		if (! is_remote_fs(st.st_fstype) &&
2115 			! EQ(st.st_fstype, MNTTYPE_TMPFS) &&
2116 				(dev_name = find_dev_name(file, st.st_dev)))
2117 			(void) printf("%s %s\n", dev_name, file);
2118 		else
2119 			errmsg(ERR_NOFLAGS,
2120 				"%s not found", file);
2121 	}
2122 	exit(errors);
2123 	/* NOTREACHED */
2124 }
2125