xref: /titanic_44/usr/src/cmd/fs.d/df.c (revision 2cbed7292737821015ab481353eb10e8346b2c05)
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 2007 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 "
967 				    "special device (%s)",
968 				    l_option ? "-l" : "-n", dfrp->dfr_cmd_arg);
969 				dfrp->dfr_valid = FALSE;
970 				errors++;
971 			}
972 			else
973 				n_valid++;
974 			continue;
975 		}
976 
977 		/*
978 		 * Check for inconsistency between the argument of -F and
979 		 * the actual file system type.
980 		 * If there is an inconsistency and the user specified a
981 		 * path, this is an error since we are asked to interpret
982 		 * the path using the wrong file system type. If there is
983 		 * no path associated with this request, we quietly ignore it.
984 		 */
985 		if (F_option && ! EQ(dfrp->dfr_fstype, FSType)) {
986 			dfrp->dfr_valid = FALSE;
987 			if (dfrp->dfr_cmd_arg != NULL) {
988 				errmsg(ERR_NOFLAGS,
989 				"Warning: %s mounted as a %s file system",
990 				    dfrp->dfr_cmd_arg, dfrp->dfr_fstype);
991 				errors++;
992 			}
993 			continue;
994 		}
995 
996 		/*
997 		 * Skip remote file systems if the -l option is present
998 		 */
999 		if (l_option && is_remote_fs(dfrp->dfr_fstype)) {
1000 			if (dfrp->dfr_cmd_arg != NULL) {
1001 				errmsg(ERR_NOFLAGS,
1002 				    "Warning: %s is not a local file system",
1003 				    dfrp->dfr_cmd_arg);
1004 				errors++;
1005 			}
1006 			dfrp->dfr_valid = FALSE;
1007 			continue;
1008 		}
1009 
1010 		/*
1011 		 * Skip file systems mounted as "ignore" unless the -a option
1012 		 * is present, or the user explicitly specified them on
1013 		 * the command line.
1014 		 */
1015 		if (dfrp->dfr_mte->mte_ignore &&
1016 		    ! (a_option || dfrp->dfr_cmd_arg)) {
1017 			dfrp->dfr_valid = FALSE;
1018 			continue;
1019 		}
1020 
1021 		n_valid++;
1022 	}
1023 	*valid_requests = n_valid;
1024 	return (errors);
1025 }
1026 
1027 
1028 /*
1029  * Print the appropriate header for the requested output format.
1030  * Options are checked in order of their precedence.
1031  */
1032 static void
1033 print_header(void)
1034 {
1035 	if (use_scaling) { /* this comes from the -h option */
1036 		int arg = 'h';
1037 
1038 		(void) printf("%-*s %*s %*s %*s %-*s %s\n",
1039 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1040 #ifdef XPG4
1041 		    SCALED_WIDTH, TRANSLATE("Size"),
1042 		    SCALED_WIDTH, TRANSLATE("Used"),
1043 		    AVAILABLE_WIDTH, TRANSLATE("Available"),
1044 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
1045 #else
1046 		    SCALED_WIDTH, TRANSLATE("size"),
1047 		    SCALED_WIDTH, TRANSLATE("used"),
1048 		    AVAILABLE_WIDTH, TRANSLATE("avail"),
1049 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
1050 #endif
1051 		    TRANSLATE("Mounted on"));
1052 		SET_OPTION(h);
1053 		return;
1054 	}
1055 	if (k_option) {
1056 		int arg = 'h';
1057 
1058 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
1059 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1060 #ifdef XPG4
1061 		    KBYTE_WIDTH, TRANSLATE("1024-blocks"),
1062 		    KBYTE_WIDTH, TRANSLATE("Used"),
1063 		    KBYTE_WIDTH, TRANSLATE("Available"),
1064 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
1065 #else
1066 		    KBYTE_WIDTH, TRANSLATE("kbytes"),
1067 		    KBYTE_WIDTH, TRANSLATE("used"),
1068 		    KBYTE_WIDTH, TRANSLATE("avail"),
1069 		    CAPACITY_WIDTH, TRANSLATE("capacity"),
1070 #endif
1071 		    TRANSLATE("Mounted on"));
1072 		SET_OPTION(h);
1073 		return;
1074 	}
1075 	/* Added for XCU4 compliance */
1076 	if (P_option) {
1077 		int arg = 'h';
1078 
1079 		(void) printf(gettext("%-*s %*s %*s %*s %-*s %s\n"),
1080 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1081 		    KBYTE_WIDTH, TRANSLATE("512-blocks"),
1082 		    KBYTE_WIDTH, TRANSLATE("Used"),
1083 		    KBYTE_WIDTH, TRANSLATE("Available"),
1084 		    CAPACITY_WIDTH, TRANSLATE("Capacity"),
1085 		    TRANSLATE("Mounted on"));
1086 
1087 		SET_OPTION(h);
1088 		return;
1089 	}
1090 	/* End XCU4 */
1091 	if (v_option) {
1092 		(void) printf("%-*s %-*s %*s %*s %*s %-*s\n",
1093 		    IBCS2_MOUNT_POINT_WIDTH, TRANSLATE("Mount Dir"),
1094 		    IBCS2_FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1095 		    BLOCK_WIDTH, TRANSLATE("blocks"),
1096 		    BLOCK_WIDTH, TRANSLATE("used"),
1097 		    BLOCK_WIDTH, TRANSLATE("free"),
1098 		    CAPACITY_WIDTH, TRANSLATE(" %used"));
1099 		return;
1100 	}
1101 	if (e_option) {
1102 		(void) printf(gettext("%-*s %*s\n"),
1103 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1104 		    BLOCK_WIDTH, TRANSLATE("ifree"));
1105 		return;
1106 	}
1107 	if (b_option) {
1108 		(void) printf(gettext("%-*s %*s\n"),
1109 		    FILESYSTEM_WIDTH, TRANSLATE("Filesystem"),
1110 		    BLOCK_WIDTH, TRANSLATE("avail"));
1111 		return;
1112 	}
1113 }
1114 
1115 
1116 /*
1117  * Convert an unsigned long long to a string representation and place the
1118  * result in the caller-supplied buffer.
1119  * The given number is in units of "unit_from" size, but the
1120  * converted number will be in units of "unit_to" size. The unit sizes
1121  * must be powers of 2.
1122  * The value "(unsigned long long)-1" is a special case and is always
1123  * converted to "-1".
1124  * Returns a pointer to the caller-supplied buffer.
1125  */
1126 static char *
1127 number_to_string(
1128 			char *buf,		/* put the result here */
1129 			unsigned long long number, /* convert this number */
1130 			int unit_from,		/* from units of this size */
1131 			int unit_to)		/* to units of this size */
1132 {
1133 	if ((long long)number == (long long)-1)
1134 		(void) strcpy(buf, "-1");
1135 	else {
1136 		if (unit_from == unit_to)
1137 			(void) sprintf(buf, "%llu", number);
1138 		else if (unit_from < unit_to)
1139 			(void) sprintf(buf, "%llu",
1140 			    number / (unsigned long long)(unit_to / unit_from));
1141 		else
1142 			(void) sprintf(buf, "%llu",
1143 			    number * (unsigned long long)(unit_from / unit_to));
1144 	}
1145 	return (buf);
1146 }
1147 
1148 /*
1149  * Convert an unsigned long long to a string representation and place the
1150  * result in the caller-supplied buffer.
1151  * The given number is in units of "unit_from" size,
1152  * this will first be converted to a number in 1024 or 1000 byte size,
1153  * depending on the scaling factor.
1154  * Then the number is scaled down until it is small enough to be in a good
1155  * human readable format i.e. in the range 0 thru scale-1.
1156  * If it's smaller than 10 there's room enough to provide one decimal place.
1157  * The value "(unsigned long long)-1" is a special case and is always
1158  * converted to "-1".
1159  * Returns a pointer to the caller-supplied buffer.
1160  */
1161 static char *
1162 number_to_scaled_string(
1163 			numbuf_t buf,		/* put the result here */
1164 			unsigned long long number, /* convert this number */
1165 			int unit_from,
1166 			int scale)
1167 {
1168 	unsigned long long save = 0;
1169 	char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */
1170 	char *uom = M;    /* unit of measurement, initially 'K' (=M[0]) */
1171 
1172 	if ((long long)number == (long long)-1) {
1173 		(void) strcpy(buf, "-1");
1174 		return (buf);
1175 	}
1176 
1177 	/*
1178 	 * Convert number from unit_from to given scale (1024 or 1000).
1179 	 * This means multiply number by unit_from and divide by scale.
1180 	 *
1181 	 * Would like to multiply by unit_from and then divide by scale,
1182 	 * but if the first multiplication would overflow, then need to
1183 	 * divide by scale and then multiply by unit_from.
1184 	 */
1185 	if (number > (UINT64_MAX / (unsigned long long)unit_from)) {
1186 		number = (number / (unsigned long long)scale) *
1187 		    (unsigned long long)unit_from;
1188 	} else {
1189 		number = (number * (unsigned long long)unit_from) /
1190 		    (unsigned long long)scale;
1191 	}
1192 
1193 	/*
1194 	 * Now we have number as a count of scale units.
1195 	 * Stop scaling when we reached exa bytes, then something is
1196 	 * probably wrong with our number.
1197 	 */
1198 
1199 	while ((number >= scale) && (*uom != 'E')) {
1200 		uom++; /* next unit of measurement */
1201 		save = number;
1202 		number = (number + (scale / 2)) / scale;
1203 	}
1204 	/* check if we should output a decimal place after the point */
1205 	if (save && ((save / scale) < 10)) {
1206 		/* sprintf() will round for us */
1207 		float fnum = (float)save / scale;
1208 		(void) sprintf(buf, "%2.1f%c", fnum, *uom);
1209 	} else {
1210 		(void) sprintf(buf, "%4llu%c", number, *uom);
1211 	}
1212 	return (buf);
1213 }
1214 
1215 /*
1216  * The statvfs() implementation allows us to return only two values, the total
1217  * number of blocks and the number of blocks free.  The equation 'used = total -
1218  * free' will not work for ZFS filesystems, due to the nature of pooled storage.
1219  * We choose to return values in the statvfs structure that will produce correct
1220  * results for 'used' and 'available', but not 'total'.  This function will open
1221  * the underlying ZFS dataset if necessary and get the real value.
1222  */
1223 static void
1224 adjust_total_blocks(struct df_request *dfrp, fsblkcnt64_t *total,
1225     uint64_t blocksize)
1226 {
1227 	zfs_handle_t	*zhp;
1228 	char *dataset, *slash;
1229 	uint64_t quota;
1230 
1231 	if (strcmp(DFR_FSTYPE(dfrp), MNTTYPE_ZFS) != 0 ||
1232 	    !load_libzfs())
1233 		return;
1234 
1235 	/*
1236 	 * We want to get the total size for this filesystem as bounded by any
1237 	 * quotas. In order to do this, we start at the current filesystem and
1238 	 * work upwards until we find a dataset with a quota.  If we reach the
1239 	 * pool itself, then the total space is the amount used plus the amount
1240 	 * available.
1241 	 */
1242 	if ((dataset = strdup(DFR_SPECIAL(dfrp))) == NULL)
1243 		return;
1244 
1245 	slash = dataset + strlen(dataset);
1246 	do {
1247 		*slash = '\0';
1248 
1249 		if ((zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET))
1250 		    == NULL) {
1251 			free(dataset);
1252 			return;
1253 		}
1254 
1255 		if ((quota = _zfs_prop_get_int(zhp, ZFS_PROP_QUOTA)) != 0) {
1256 			*total = quota / blocksize;
1257 			_zfs_close(zhp);
1258 			free(dataset);
1259 			return;
1260 		}
1261 
1262 		_zfs_close(zhp);
1263 
1264 	} while ((slash = strrchr(dataset, '/')) != NULL);
1265 
1266 
1267 	if ((zhp = _zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL) {
1268 		free(dataset);
1269 		return;
1270 	}
1271 
1272 	*total = (_zfs_prop_get_int(zhp, ZFS_PROP_USED) +
1273 	    _zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE)) / blocksize;
1274 
1275 	_zfs_close(zhp);
1276 	free(dataset);
1277 }
1278 
1279 /*
1280  * The output will appear properly columnized regardless of the names of
1281  * the various fields
1282  */
1283 static void
1284 g_output(struct df_request *dfrp, struct statvfs64 *fsp)
1285 {
1286 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
1287 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
1288 	numbuf_t	total_blocks_buf;
1289 	numbuf_t	total_files_buf;
1290 	numbuf_t	free_blocks_buf;
1291 	numbuf_t	available_blocks_buf;
1292 	numbuf_t	free_files_buf;
1293 	numbuf_t	fname_buf;
1294 	char		*temp_buf;
1295 
1296 #define	DEFINE_STR_LEN(var)			\
1297 	static char *var##_str;			\
1298 	static size_t var##_len
1299 
1300 #define	SET_STR_LEN(name, var)\
1301 	if (! var##_str) {\
1302 		var##_str = TRANSLATE(name); \
1303 		var##_len = strlen(var##_str); \
1304 	}
1305 
1306 	DEFINE_STR_LEN(block_size);
1307 	DEFINE_STR_LEN(frag_size);
1308 	DEFINE_STR_LEN(total_blocks);
1309 	DEFINE_STR_LEN(free_blocks);
1310 	DEFINE_STR_LEN(available);
1311 	DEFINE_STR_LEN(total_files);
1312 	DEFINE_STR_LEN(free_files);
1313 	DEFINE_STR_LEN(fstype);
1314 	DEFINE_STR_LEN(fsys_id);
1315 	DEFINE_STR_LEN(fname);
1316 	DEFINE_STR_LEN(flag);
1317 
1318 	/*
1319 	 * TRANSLATION_NOTE
1320 	 * The first argument of each of the following macro invocations is a
1321 	 * string that needs to be translated.
1322 	 */
1323 	SET_STR_LEN("block size", block_size);
1324 	SET_STR_LEN("frag size", frag_size);
1325 	SET_STR_LEN("total blocks", total_blocks);
1326 	SET_STR_LEN("free blocks", free_blocks);
1327 	SET_STR_LEN("available", available);
1328 	SET_STR_LEN("total files", total_files);
1329 	SET_STR_LEN("free files", free_files);
1330 	SET_STR_LEN("fstype", fstype);
1331 	SET_STR_LEN("filesys id", fsys_id);
1332 	SET_STR_LEN("filename length", fname);
1333 	SET_STR_LEN("flag", flag);
1334 
1335 #define	NCOL1_WIDTH	(int)MAX3(BLOCK_WIDTH, NFILES_WIDTH, FSTYPE_WIDTH)
1336 #define	NCOL2_WIDTH	(int)MAX3(BLOCK_WIDTH, FSID_WIDTH, FLAG_WIDTH) + 2
1337 #define	NCOL3_WIDTH	(int)MAX3(BSIZE_WIDTH, BLOCK_WIDTH, NAMELEN_WIDTH)
1338 #define	NCOL4_WIDTH	(int)MAX(FRAGSIZE_WIDTH, NFILES_WIDTH)
1339 
1340 #define	SCOL1_WIDTH	(int)MAX3(total_blocks_len, free_files_len, fstype_len)
1341 #define	SCOL2_WIDTH	(int)MAX3(free_blocks_len, fsys_id_len, flag_len)
1342 #define	SCOL3_WIDTH	(int)MAX3(block_size_len, available_len, fname_len)
1343 #define	SCOL4_WIDTH	(int)MAX(frag_size_len, total_files_len)
1344 
1345 	temp_buf = xmalloc(
1346 	    MAX(MOUNT_POINT_WIDTH, strlen(DFR_MOUNT_POINT(dfrp)))
1347 	    + MAX(SPECIAL_DEVICE_WIDTH, strlen(DFR_SPECIAL(dfrp)))
1348 	    + 20); /* plus slop - nulls & formatting */
1349 	(void) sprintf(temp_buf, "%-*s(%-*s):",
1350 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1351 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp));
1352 
1353 	(void) printf("%-*s %*lu %-*s %*lu %-*s\n",
1354 	    NCOL1_WIDTH + 1 + SCOL1_WIDTH + 1 + NCOL2_WIDTH + 1 +  SCOL2_WIDTH,
1355 	    temp_buf,
1356 	    NCOL3_WIDTH, fsp->f_bsize, SCOL3_WIDTH, block_size_str,
1357 	    NCOL4_WIDTH, fsp->f_frsize, SCOL4_WIDTH, frag_size_str);
1358 	free(temp_buf);
1359 
1360 	/*
1361 	 * Adjust available_blocks value -  it can be less than 0 on
1362 	 * a 4.x file system. Reset it to 0 in order to avoid printing
1363 	 * negative numbers.
1364 	 */
1365 	if ((long long)available_blocks < (long long)0)
1366 		available_blocks = (fsblkcnt64_t)0;
1367 
1368 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1369 
1370 	(void) printf("%*s %-*s %*s %-*s %*s %-*s %*s %-*s\n",
1371 	    NCOL1_WIDTH, number_to_string(total_blocks_buf,
1372 	    total_blocks, fsp->f_frsize, 512),
1373 	    SCOL1_WIDTH, total_blocks_str,
1374 	    NCOL2_WIDTH, number_to_string(free_blocks_buf,
1375 	    fsp->f_bfree, fsp->f_frsize, 512),
1376 	    SCOL2_WIDTH, free_blocks_str,
1377 	    NCOL3_WIDTH, number_to_string(available_blocks_buf,
1378 	    available_blocks, fsp->f_frsize, 512),
1379 	    SCOL3_WIDTH, available_str,
1380 	    NCOL4_WIDTH, number_to_string(total_files_buf,
1381 	    fsp->f_files, 1, 1),
1382 	    SCOL4_WIDTH, total_files_str);
1383 
1384 	(void) printf("%*s %-*s %*lu %-*s %s\n",
1385 	    NCOL1_WIDTH, number_to_string(free_files_buf,
1386 	    fsp->f_ffree, 1, 1),
1387 	    SCOL1_WIDTH, free_files_str,
1388 	    NCOL2_WIDTH, fsp->f_fsid, SCOL2_WIDTH, fsys_id_str,
1389 	    fsp->f_fstr);
1390 
1391 	(void) printf("%*s %-*s %#*.*lx %-*s %*s %-*s\n\n",
1392 	    NCOL1_WIDTH, fsp->f_basetype, SCOL1_WIDTH, fstype_str,
1393 	    NCOL2_WIDTH, NCOL2_WIDTH-2, fsp->f_flag, SCOL2_WIDTH, flag_str,
1394 	    NCOL3_WIDTH, number_to_string(fname_buf,
1395 	    (unsigned long long)fsp->f_namemax, 1, 1),
1396 	    SCOL3_WIDTH, fname_str);
1397 }
1398 
1399 
1400 static void
1401 k_output(struct df_request *dfrp, struct statvfs64 *fsp)
1402 {
1403 	fsblkcnt64_t total_blocks		= fsp->f_blocks;
1404 	fsblkcnt64_t	free_blocks		= fsp->f_bfree;
1405 	fsblkcnt64_t	available_blocks	= fsp->f_bavail;
1406 	fsblkcnt64_t	used_blocks;
1407 	char 		*file_system		= DFR_SPECIAL(dfrp);
1408 	numbuf_t	total_blocks_buf;
1409 	numbuf_t	used_blocks_buf;
1410 	numbuf_t	available_blocks_buf;
1411 	char 		capacity_buf[LINEBUF_SIZE];
1412 
1413 	/*
1414 	 * If the free block count is -1, don't trust anything but the total
1415 	 * number of blocks.
1416 	 */
1417 	if (free_blocks == (fsblkcnt64_t)-1) {
1418 		used_blocks = (fsblkcnt64_t)-1;
1419 		(void) strcpy(capacity_buf, "  100%");
1420 	} else {
1421 		fsblkcnt64_t reserved_blocks = free_blocks - available_blocks;
1422 
1423 		used_blocks	= total_blocks - free_blocks;
1424 
1425 		/*
1426 		 * The capacity estimation is bogus when available_blocks is 0
1427 		 * and the super-user has allocated more space. The reason
1428 		 * is that reserved_blocks is inaccurate in that case, because
1429 		 * when the super-user allocates space, free_blocks is updated
1430 		 * but available_blocks is not (since it can't drop below 0).
1431 		 *
1432 		 * XCU4 and POSIX.2 require that any fractional result of the
1433 		 * capacity estimation be rounded to the next highest integer,
1434 		 * hence the addition of 0.5.
1435 		 */
1436 		(void) sprintf(capacity_buf, "%5.0f%%",
1437 		    (total_blocks == 0) ? 0.0 :
1438 		    ((double)used_blocks /
1439 		    (double)(total_blocks - reserved_blocks))
1440 		    * 100.0 + 0.5);
1441 	}
1442 
1443 	/*
1444 	 * The available_blocks can be less than 0 on a 4.x file system.
1445 	 * Reset it to 0 in order to avoid printing negative numbers.
1446 	 */
1447 	if ((long long)available_blocks < (long long)0)
1448 		available_blocks = (fsblkcnt64_t)0;
1449 	/*
1450 	 * Print long special device names (usually NFS mounts) in a line
1451 	 * by themselves when the output is directed to a terminal.
1452 	 */
1453 	if (tty_output && strlen(file_system) > (size_t)FILESYSTEM_WIDTH) {
1454 		(void) printf("%s\n", file_system);
1455 		file_system = "";
1456 	}
1457 
1458 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1459 
1460 	if (use_scaling) { /* comes from the -h option */
1461 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1462 	    FILESYSTEM_WIDTH, file_system,
1463 	    SCALED_WIDTH, number_to_scaled_string(total_blocks_buf,
1464 	    total_blocks, fsp->f_frsize, scale),
1465 	    SCALED_WIDTH, number_to_scaled_string(used_blocks_buf,
1466 	    used_blocks, fsp->f_frsize, scale),
1467 	    AVAILABLE_WIDTH, number_to_scaled_string(available_blocks_buf,
1468 	    available_blocks, fsp->f_frsize, scale),
1469 	    CAPACITY_WIDTH, capacity_buf,
1470 	    DFR_MOUNT_POINT(dfrp));
1471 		return;
1472 	}
1473 
1474 	if (v_option) {
1475 	(void) printf("%-*.*s %-*.*s %*lld %*lld %*lld %-.*s\n",
1476 	    IBCS2_MOUNT_POINT_WIDTH, IBCS2_MOUNT_POINT_WIDTH,
1477 	    DFR_MOUNT_POINT(dfrp),
1478 	    IBCS2_FILESYSTEM_WIDTH, IBCS2_FILESYSTEM_WIDTH, file_system,
1479 	    BLOCK_WIDTH, total_blocks,
1480 	    BLOCK_WIDTH, used_blocks,
1481 	    BLOCK_WIDTH, available_blocks,
1482 	    CAPACITY_WIDTH,	capacity_buf);
1483 		return;
1484 	}
1485 
1486 	if (P_option && !k_option) {
1487 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1488 	    FILESYSTEM_WIDTH, file_system,
1489 	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
1490 	    total_blocks, fsp->f_frsize, 512),
1491 	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
1492 	    used_blocks, fsp->f_frsize, 512),
1493 	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
1494 	    available_blocks, fsp->f_frsize, 512),
1495 	    CAPACITY_WIDTH, capacity_buf,
1496 	    DFR_MOUNT_POINT(dfrp));
1497 	} else {
1498 	(void) printf("%-*s %*s %*s %*s %-*s %-s\n",
1499 	    FILESYSTEM_WIDTH, file_system,
1500 	    KBYTE_WIDTH, number_to_string(total_blocks_buf,
1501 	    total_blocks, fsp->f_frsize, 1024),
1502 	    KBYTE_WIDTH, number_to_string(used_blocks_buf,
1503 	    used_blocks, fsp->f_frsize, 1024),
1504 	    KBYTE_WIDTH, number_to_string(available_blocks_buf,
1505 	    available_blocks, fsp->f_frsize, 1024),
1506 	    CAPACITY_WIDTH,	capacity_buf,
1507 	    DFR_MOUNT_POINT(dfrp));
1508 	}
1509 }
1510 
1511 /*
1512  * The following is for internationalization support.
1513  */
1514 static bool_int strings_initialized;
1515 static char 	*files_str;
1516 static char	*blocks_str;
1517 static char	*total_str;
1518 static char	*kilobytes_str;
1519 
1520 static void
1521 strings_init(void)
1522 {
1523 	total_str = TRANSLATE("total");
1524 #ifdef	_iBCS2
1525 	/* ISC/SCO print i-nodes instead of files */
1526 	if (sysv3_set)
1527 		files_str = TRANSLATE("i-nodes");
1528 	else
1529 #endif	/* _iBCS2 */
1530 		files_str = TRANSLATE("files");
1531 	blocks_str = TRANSLATE("blocks");
1532 	kilobytes_str = TRANSLATE("kilobytes");
1533 	strings_initialized = TRUE;
1534 }
1535 
1536 #define	STRINGS_INIT()		if (!strings_initialized) strings_init()
1537 
1538 
1539 static void
1540 t_output(struct df_request *dfrp, struct statvfs64 *fsp)
1541 {
1542 	fsblkcnt64_t	total_blocks = fsp->f_blocks;
1543 	numbuf_t	total_blocks_buf;
1544 	numbuf_t	total_files_buf;
1545 	numbuf_t	free_blocks_buf;
1546 	numbuf_t	free_files_buf;
1547 
1548 	STRINGS_INIT();
1549 
1550 	adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize);
1551 
1552 	(void) printf("%-*s(%-*s): %*s %s %*s %s\n",
1553 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1554 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1555 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
1556 	    fsp->f_bfree, fsp->f_frsize, 512),
1557 	    blocks_str,
1558 	    NFILES_WIDTH, number_to_string(free_files_buf,
1559 	    fsp->f_ffree, 1, 1),
1560 	    files_str);
1561 	/*
1562 	 * The total column used to use the same space as the mnt pt & special
1563 	 * dev fields. However, this doesn't work with massive special dev
1564 	 * fields * (eg > 500 chars) causing an enormous amount of white space
1565 	 * before the total column (see bug 4100411). So the code was
1566 	 * simplified to set the total column at the usual gap.
1567 	 * This had the side effect of fixing a bug where the previously
1568 	 * used static buffer was overflowed by the same massive special dev.
1569 	 */
1570 	(void) printf("%*s: %*s %s %*s %s\n",
1571 	    MNT_SPEC_WIDTH, total_str,
1572 	    BLOCK_WIDTH, number_to_string(total_blocks_buf,
1573 	    total_blocks, fsp->f_frsize, 512),
1574 	    blocks_str,
1575 	    NFILES_WIDTH, number_to_string(total_files_buf,
1576 	    fsp->f_files, 1, 1),
1577 	    files_str);
1578 }
1579 
1580 
1581 static void
1582 eb_output(struct df_request *dfrp, struct statvfs64 *fsp)
1583 {
1584 	numbuf_t free_files_buf;
1585 	numbuf_t free_kbytes_buf;
1586 
1587 	STRINGS_INIT();
1588 
1589 	(void) printf("%-*s(%-*s): %*s %s\n",
1590 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1591 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1592 	    MAX(KBYTE_WIDTH, NFILES_WIDTH),
1593 	    number_to_string(free_kbytes_buf,
1594 	    fsp->f_bfree, fsp->f_frsize, 1024),
1595 	    kilobytes_str);
1596 	(void) printf("%-*s(%-*s): %*s %s\n",
1597 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1598 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1599 	    MAX(NFILES_WIDTH, NFILES_WIDTH),
1600 	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1),
1601 	    files_str);
1602 }
1603 
1604 
1605 static void
1606 e_output(struct df_request *dfrp, struct statvfs64 *fsp)
1607 {
1608 	numbuf_t free_files_buf;
1609 
1610 	(void) printf("%-*s %*s\n",
1611 	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
1612 	    NFILES_WIDTH,
1613 	    number_to_string(free_files_buf, fsp->f_ffree, 1, 1));
1614 }
1615 
1616 
1617 static void
1618 b_output(struct df_request *dfrp, struct statvfs64 *fsp)
1619 {
1620 	numbuf_t free_blocks_buf;
1621 
1622 	(void) printf("%-*s %*s\n",
1623 	    FILESYSTEM_WIDTH, DFR_SPECIAL(dfrp),
1624 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
1625 	    fsp->f_bfree, fsp->f_frsize, 1024));
1626 }
1627 
1628 
1629 /* ARGSUSED */
1630 static void
1631 n_output(struct df_request *dfrp, struct statvfs64 *fsp)
1632 {
1633 	(void) printf("%-*s: %-*s\n",
1634 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1635 	    FSTYPE_WIDTH, dfrp->dfr_fstype);
1636 }
1637 
1638 
1639 static void
1640 default_output(struct df_request *dfrp, struct statvfs64 *fsp)
1641 {
1642 	numbuf_t free_blocks_buf;
1643 	numbuf_t free_files_buf;
1644 
1645 	STRINGS_INIT();
1646 
1647 	(void) printf("%-*s(%-*s):%*s %s %*s %s\n",
1648 	    MOUNT_POINT_WIDTH, DFR_MOUNT_POINT(dfrp),
1649 	    SPECIAL_DEVICE_WIDTH, DFR_SPECIAL(dfrp),
1650 	    BLOCK_WIDTH, number_to_string(free_blocks_buf,
1651 	    fsp->f_bfree, fsp->f_frsize, 512),
1652 	    blocks_str,
1653 	    NFILES_WIDTH, number_to_string(free_files_buf,
1654 	    fsp->f_ffree, 1, 1),
1655 	    files_str);
1656 }
1657 
1658 
1659 /* ARGSUSED */
1660 static void
1661 V_output(struct df_request *dfrp, struct statvfs64 *fsp)
1662 {
1663 	char temp_buf[LINEBUF_SIZE];
1664 
1665 	if (df_options_len > 1)
1666 		(void) strcat(strcpy(temp_buf, df_options), " ");
1667 	else
1668 		temp_buf[0] = NUL;
1669 
1670 	(void) printf("%s -F %s %s%s\n",
1671 	    program_name, dfrp->dfr_fstype, temp_buf,
1672 	    dfrp->dfr_cmd_arg ? dfrp->dfr_cmd_arg: DFR_SPECIAL(dfrp));
1673 }
1674 
1675 
1676 /*
1677  * This function is used to sort the array of df_requests according to fstype
1678  */
1679 static int
1680 df_reqcomp(const void *p1, const void *p2)
1681 {
1682 	int v = strcmp(DFRP(p1)->dfr_fstype, DFRP(p2)->dfr_fstype);
1683 
1684 	if (v != 0)
1685 		return (v);
1686 	else
1687 		return (DFRP(p1)->dfr_index - DFRP(p2)->dfr_index);
1688 }
1689 
1690 
1691 static void
1692 vfs_error(char *file, int status)
1693 {
1694 	if (status == VFS_TOOLONG)
1695 		errmsg(ERR_NOFLAGS, "a line in %s exceeds %d characters",
1696 		    file, MNT_LINE_MAX);
1697 	else if (status == VFS_TOOMANY)
1698 		errmsg(ERR_NOFLAGS, "a line in %s has too many fields", file);
1699 	else if (status == VFS_TOOFEW)
1700 		errmsg(ERR_NOFLAGS, "a line in %s has too few fields", file);
1701 	else
1702 		errmsg(ERR_NOFLAGS, "error while reading %s: %d", file, status);
1703 }
1704 
1705 
1706 /*
1707  * Try to determine the fstype for the specified block device.
1708  * Return in order of decreasing preference:
1709  *	file system type from vfstab
1710  *	file system type as specified by -F option
1711  *	default file system type
1712  */
1713 static char *
1714 find_fstype(char *special)
1715 {
1716 	struct vfstab	vtab;
1717 	FILE		*fp;
1718 	int		status;
1719 	char		*vfstab_file = VFS_TAB;
1720 
1721 	fp = xfopen(vfstab_file);
1722 	status = getvfsspec(fp, &vtab, special);
1723 	(void) fclose(fp);
1724 	if (status > 0)
1725 		vfs_error(vfstab_file, status);
1726 
1727 	if (status == 0) {
1728 		if (F_option && ! EQ(FSType, vtab.vfs_fstype))
1729 			errmsg(ERR_NOFLAGS,
1730 			"warning: %s is of type %s", special, vtab.vfs_fstype);
1731 		return (new_string(vtab.vfs_fstype));
1732 	}
1733 	else
1734 		return (F_option ? FSType : default_fstype(special));
1735 }
1736 
1737 /*
1738  * When this function returns, the following fields are filled for all
1739  * valid entries in the requests[] array:
1740  *		dfr_mte		(if the file system is mounted)
1741  *		dfr_fstype
1742  *		dfr_index
1743  *
1744  * The function returns the number of errors that occurred while building
1745  * the request list.
1746  */
1747 static int
1748 create_request_list(
1749 			int argc,
1750 			char *argv[],
1751 			struct df_request *requests_p[],
1752 			size_t *request_count)
1753 {
1754 	struct df_request	*requests;
1755 	struct df_request	*dfrp;
1756 	size_t			size;
1757 	size_t 			i;
1758 	size_t 			request_index = 0;
1759 	size_t			max_requests;
1760 	int			errors = 0;
1761 
1762 	/*
1763 	 * If no args, use the mounted file systems, otherwise use the
1764 	 * user-specified arguments.
1765 	 */
1766 	if (argc == 0) {
1767 		mtab_read_file();
1768 		max_requests = mount_table_entries;
1769 	} else
1770 		max_requests = argc;
1771 
1772 	size = max_requests * sizeof (struct df_request);
1773 	requests = xmalloc(size);
1774 	(void) memset(requests, 0, size);
1775 
1776 	if (argc == 0) {
1777 		/*
1778 		 * If -Z wasn't specified, we skip mounts in other
1779 		 * zones.  This obviously is a noop in a non-global
1780 		 * zone.
1781 		 */
1782 		boolean_t showall = (getzoneid() != GLOBAL_ZONEID) || Z_option;
1783 		struct zone_summary *zsp;
1784 
1785 		if (!showall) {
1786 			zsp = fs_get_zone_summaries();
1787 			if (zsp == NULL)
1788 				errmsg(ERR_FATAL,
1789 				    "unable to retrieve list of zones");
1790 		}
1791 
1792 		for (i = 0; i < mount_table_entries; i++) {
1793 			struct extmnttab *mtp = mount_table[i].mte_mount;
1794 
1795 			if (EQ(mtp->mnt_fstype, MNTTYPE_SWAP))
1796 				continue;
1797 
1798 			if (!showall) {
1799 				if (fs_mount_in_other_zone(zsp,
1800 				    mtp->mnt_mountp))
1801 					continue;
1802 			}
1803 			dfrp = &requests[request_index++];
1804 			dfrp->dfr_mte		= &mount_table[i];
1805 			dfrp->dfr_fstype	= mtp->mnt_fstype;
1806 			dfrp->dfr_index		= i;
1807 			dfrp->dfr_valid		= TRUE;
1808 		}
1809 	} else {
1810 		struct stat64 *arg_stat; /* array of stat structures	*/
1811 		bool_int *valid_stat;	/* which structures are valid	*/
1812 
1813 		arg_stat = xmalloc(argc * sizeof (struct stat64));
1814 		valid_stat = xmalloc(argc * sizeof (bool_int));
1815 
1816 		/*
1817 		 * Obtain stat64 information for each argument before
1818 		 * constructing the list of mounted file systems. By
1819 		 * touching all these places we force the automounter
1820 		 * to establish any mounts required to access the arguments,
1821 		 * so that the corresponding mount table entries will exist
1822 		 * when we look for them.
1823 		 * It is still possible that the automounter may timeout
1824 		 * mounts between the time we read the mount table and the
1825 		 * time we process the request. Even in that case, when
1826 		 * we issue the statvfs64(2) for the mount point, the file
1827 		 * system will be mounted again. The only problem will
1828 		 * occur if the automounter maps change in the meantime
1829 		 * and the mount point is eliminated.
1830 		 */
1831 		for (i = 0; i < argc; i++)
1832 			valid_stat[i] = (stat64(argv[i], &arg_stat[i]) == 0);
1833 
1834 		mtab_read_file();
1835 
1836 		for (i = 0; i < argc; i++) {
1837 			char *arg = argv[i];
1838 
1839 			dfrp = &requests[request_index];
1840 
1841 			dfrp->dfr_index = request_index;
1842 			dfrp->dfr_cmd_arg = arg;
1843 
1844 			if (valid_stat[i]) {
1845 				if (S_ISBLK(arg_stat[i].st_mode)) {
1846 					bdev_mount_entry(dfrp);
1847 					dfrp->dfr_valid = TRUE;
1848 				} else if (S_ISDIR(arg_stat[i].st_mode) ||
1849 				    S_ISREG(arg_stat[i].st_mode) ||
1850 				    S_ISFIFO(arg_stat[i].st_mode)) {
1851 					path_mount_entry(dfrp,
1852 					    arg_stat[i].st_dev);
1853 					if (! DFR_ISMOUNTEDFS(dfrp)) {
1854 						errors++;
1855 						continue;
1856 					}
1857 					dfrp->dfr_valid = TRUE;
1858 				}
1859 			} else {
1860 				resource_mount_entry(dfrp);
1861 				dfrp->dfr_valid = DFR_ISMOUNTEDFS(dfrp);
1862 			}
1863 
1864 			/*
1865 			 * If we haven't managed to verify that the request
1866 			 * is valid, we must have gotten a bad argument.
1867 			 */
1868 			if (!dfrp->dfr_valid) {
1869 				errmsg(ERR_NOFLAGS,
1870 				    "(%-10s) not a block device, directory or "
1871 				    "mounted resource", arg);
1872 				errors++;
1873 				continue;
1874 			}
1875 
1876 			/*
1877 			 * Determine the file system type.
1878 			 */
1879 			if (DFR_ISMOUNTEDFS(dfrp))
1880 				dfrp->dfr_fstype =
1881 				    dfrp->dfr_mte->mte_mount->mnt_fstype;
1882 			else
1883 				dfrp->dfr_fstype =
1884 				    find_fstype(dfrp->dfr_cmd_arg);
1885 
1886 			request_index++;
1887 		}
1888 	}
1889 	*requests_p = requests;
1890 	*request_count = request_index;
1891 	return (errors);
1892 }
1893 
1894 
1895 /*
1896  * Select the appropriate function and flags to use for output.
1897  * Notice that using both -e and -b options produces a different form of
1898  * output than either of those two options alone; this is the behavior of
1899  * the SVR4 df.
1900  */
1901 static struct df_output *
1902 select_output(void)
1903 {
1904 	static struct df_output dfo;
1905 
1906 	/*
1907 	 * The order of checking options follows the option precedence
1908 	 * rules as they are listed in the man page.
1909 	 */
1910 	if (use_scaling) { /* comes from the -h option */
1911 		dfo.dfo_func = k_output;
1912 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1913 	} else if (V_option) {
1914 		dfo.dfo_func = V_output;
1915 		dfo.dfo_flags = DFO_NOFLAGS;
1916 	} else if (g_option) {
1917 		dfo.dfo_func = g_output;
1918 		dfo.dfo_flags = DFO_STATVFS;
1919 	} else if (k_option || P_option || v_option) {
1920 		dfo.dfo_func = k_output;
1921 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1922 	} else if (t_option) {
1923 		dfo.dfo_func = t_output;
1924 		dfo.dfo_flags = DFO_STATVFS;
1925 	} else if (b_option && e_option) {
1926 		dfo.dfo_func = eb_output;
1927 		dfo.dfo_flags = DFO_STATVFS;
1928 	} else if (b_option) {
1929 		dfo.dfo_func = b_output;
1930 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1931 	} else if (e_option) {
1932 		dfo.dfo_func = e_output;
1933 		dfo.dfo_flags = DFO_HEADER + DFO_STATVFS;
1934 	} else if (n_option) {
1935 		dfo.dfo_func = n_output;
1936 		dfo.dfo_flags = DFO_NOFLAGS;
1937 	} else {
1938 		dfo.dfo_func = default_output;
1939 		dfo.dfo_flags = DFO_STATVFS;
1940 	}
1941 	return (&dfo);
1942 }
1943 
1944 
1945 /*
1946  * The (argc,argv) pair contains all the non-option arguments
1947  */
1948 static void
1949 do_df(int argc, char *argv[])
1950 {
1951 	size_t			i;
1952 	struct df_request	*requests;		/* array of requests */
1953 	size_t			n_requests;
1954 	struct df_request	*dfrp;
1955 	int			errors;
1956 
1957 	errors = create_request_list(argc, argv, &requests, &n_requests);
1958 
1959 	if (n_requests == 0)
1960 		exit(errors);
1961 
1962 	/*
1963 	 * If we are going to run the FSType-specific df command,
1964 	 * rearrange the requests so that we can issue a single command
1965 	 * per file system type.
1966 	 */
1967 	if (o_option) {
1968 		size_t j;
1969 
1970 		/*
1971 		 * qsort is not a stable sorting method (i.e. requests of
1972 		 * the same file system type may be swapped, and hence appear
1973 		 * in the output in a different order from the one in which
1974 		 * they were listed in the command line). In order to force
1975 		 * stability, we use the dfr_index field which is unique
1976 		 * for each request.
1977 		 */
1978 		qsort(requests,
1979 		    n_requests, sizeof (struct df_request), df_reqcomp);
1980 		for (i = 0; i < n_requests; i = j) {
1981 			char *fstype = requests[i].dfr_fstype;
1982 
1983 			for (j = i+1; j < n_requests; j++)
1984 				if (! EQ(fstype, requests[j].dfr_fstype))
1985 					break;
1986 
1987 			/*
1988 			 * At this point, requests in the range [i,j) are
1989 			 * of the same type.
1990 			 *
1991 			 * If the -F option was used, and the user specified
1992 			 * arguments, the filesystem types must match
1993 			 *
1994 			 * XXX: the alternative of doing this check here is to
1995 			 * 	invoke prune_list, but then we have to
1996 			 *	modify this code to ignore invalid requests.
1997 			 */
1998 			if (F_option && ! EQ(fstype, FSType)) {
1999 				size_t k;
2000 
2001 				for (k = i; k < j; k++) {
2002 					dfrp = &requests[k];
2003 					if (dfrp->dfr_cmd_arg != NULL) {
2004 						errmsg(ERR_NOFLAGS,
2005 						    "Warning: %s mounted as a "
2006 						    "%s file system",
2007 						    dfrp->dfr_cmd_arg,
2008 						    dfrp->dfr_fstype);
2009 						errors++;
2010 					}
2011 				}
2012 			} else
2013 				errors += run_fs_specific_df(&requests[i], j-i);
2014 		}
2015 	} else {
2016 		size_t valid_requests;
2017 
2018 		/*
2019 		 * We have to prune the request list to avoid printing a header
2020 		 * if there are no valid requests
2021 		 */
2022 		errors += prune_list(requests, n_requests, &valid_requests);
2023 
2024 		if (valid_requests) {
2025 			struct df_output *dfop = select_output();
2026 
2027 			/* indicates if we already printed out a header line */
2028 			int printed_header = 0;
2029 
2030 			for (i = 0; i < n_requests; i++) {
2031 				dfrp = &requests[i];
2032 				if (! dfrp->dfr_valid)
2033 					continue;
2034 
2035 				/*
2036 				 * If we don't have a mount point,
2037 				 * this must be a block device.
2038 				 */
2039 				if (DFR_ISMOUNTEDFS(dfrp)) {
2040 					struct statvfs64 stvfs;
2041 
2042 					if ((dfop->dfo_flags & DFO_STATVFS) &&
2043 					    statvfs64(DFR_MOUNT_POINT(dfrp),
2044 					    &stvfs) == -1) {
2045 						errmsg(ERR_PERROR,
2046 						    "cannot statvfs %s:",
2047 						    DFR_MOUNT_POINT(dfrp));
2048 						errors++;
2049 						continue;
2050 					}
2051 					if ((!printed_header) &&
2052 					    (dfop->dfo_flags & DFO_HEADER)) {
2053 						print_header();
2054 						printed_header = 1;
2055 					}
2056 
2057 					(*dfop->dfo_func)(dfrp, &stvfs);
2058 				} else {
2059 					/*
2060 					 *  -h option only works for
2061 					 *  mounted filesystems
2062 					 */
2063 					if (use_scaling) {
2064 						errmsg(ERR_NOFLAGS,
2065 		"-h option incompatible with unmounted special device (%s)",
2066 						    dfrp->dfr_cmd_arg);
2067 						errors++;
2068 						continue;
2069 					}
2070 					errors += run_fs_specific_df(dfrp, 1);
2071 				}
2072 			}
2073 		}
2074 	}
2075 	exit(errors);
2076 }
2077 
2078 
2079 /*
2080  * The rest of this file implements the devnm command
2081  */
2082 
2083 static char *
2084 find_dev_name(char *file, dev_t dev)
2085 {
2086 	struct df_request dfreq;
2087 
2088 	dfreq.dfr_cmd_arg = file;
2089 	dfreq.dfr_fstype = 0;
2090 	dfreq.dfr_mte = NULL;
2091 	path_mount_entry(&dfreq, dev);
2092 	return (DFR_ISMOUNTEDFS(&dfreq) ? DFR_SPECIAL(&dfreq) : NULL);
2093 }
2094 
2095 
2096 static void
2097 do_devnm(int argc, char *argv[])
2098 {
2099 	int arg;
2100 	int errors = 0;
2101 	char *dev_name;
2102 
2103 	if (argc == 1)
2104 		errmsg(ERR_NONAME, "Usage: %s name ...", DEVNM_CMD);
2105 
2106 	mtab_read_file();
2107 
2108 	for (arg = 1; arg < argc; arg++) {
2109 		char *file = argv[arg];
2110 		struct stat64 st;
2111 
2112 		if (stat64(file, &st) == -1) {
2113 			errmsg(ERR_PERROR, "%s: ", file);
2114 			errors++;
2115 			continue;
2116 		}
2117 
2118 		if (! is_remote_fs(st.st_fstype) &&
2119 		    ! EQ(st.st_fstype, MNTTYPE_TMPFS) &&
2120 		    (dev_name = find_dev_name(file, st.st_dev)))
2121 			(void) printf("%s %s\n", dev_name, file);
2122 		else
2123 			errmsg(ERR_NOFLAGS,
2124 			    "%s not found", file);
2125 	}
2126 	exit(errors);
2127 	/* NOTREACHED */
2128 }
2129