xref: /illumos-gate/usr/src/cmd/format/misc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains miscellaneous routines.
31  */
32 #include "global.h"
33 
34 #include <stdlib.h>
35 #include <signal.h>
36 #include <malloc.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/fcntl.h>
43 #include <sys/time.h>
44 #include <ctype.h>
45 #include <termio.h>
46 #include "misc.h"
47 #include "analyze.h"
48 #include "label.h"
49 #include "startup.h"
50 
51 #ifdef __STDC__
52 
53 /* Function prototypes for ANSI C Compilers */
54 static void	cleanup(int sig);
55 
56 #else	/* __STDC__ */
57 
58 /* Function prototypes for non-ANSI C Compilers */
59 static void	cleanup();
60 
61 #endif	/* __STDC__ */
62 
63 struct	env *current_env = NULL;	/* ptr to current environment */
64 static int	stop_pending = 0;	/* ctrl-Z is pending */
65 struct	ttystate ttystate;		/* tty info */
66 static int	aborting = 0;		/* in process of aborting */
67 
68 /*
69  * For 4.x, limit the choices of valid disk names to this set.
70  */
71 static char		*disk_4x_identifiers[] = { "sd", "id"};
72 #define	N_DISK_4X_IDS	(sizeof (disk_4x_identifiers)/sizeof (char *))
73 
74 
75 /*
76  * This is the list of legal inputs for all yes/no questions.
77  */
78 char	*confirm_list[] = {
79 	"yes",
80 	"no",
81 	NULL,
82 };
83 
84 /*
85  * This routine is a wrapper for malloc.  It allocates pre-zeroed space,
86  * and checks the return value so the caller doesn't have to.
87  */
88 void *
89 zalloc(count)
90 	int	count;
91 {
92 	void	*ptr;
93 
94 	if ((ptr = (void *) calloc(1, (unsigned)count)) == NULL) {
95 		err_print("Error: unable to calloc more space.\n");
96 		fullabort();
97 	}
98 	return (ptr);
99 }
100 
101 /*
102  * This routine is a wrapper for realloc.  It reallocates the given
103  * space, and checks the return value so the caller doesn't have to.
104  * Note that the any space added by this call is NOT necessarily
105  * zeroed.
106  */
107 void *
108 rezalloc(ptr, count)
109 	void	*ptr;
110 	int	count;
111 {
112 	void	*new_ptr;
113 
114 
115 	if ((new_ptr = (void *) realloc((char *)ptr,
116 				(unsigned)count)) == NULL) {
117 		err_print("Error: unable to realloc more space.\n");
118 		fullabort();
119 	}
120 	return (new_ptr);
121 }
122 
123 /*
124  * This routine is a wrapper for free.
125  */
126 void
127 destroy_data(data)
128 	char	*data;
129 {
130 	free((char *)data);
131 }
132 
133 #ifdef	not
134 /*
135  * This routine takes the space number returned by an ioctl call and
136  * returns a mnemonic name for that space.
137  */
138 char *
139 space2str(space)
140 	uint_t	space;
141 {
142 	char	*name;
143 
144 	switch (space&SP_BUSMASK) {
145 	    case SP_VIRTUAL:
146 		name = "virtual";
147 		break;
148 	    case SP_OBMEM:
149 		name = "obmem";
150 		break;
151 	    case SP_OBIO:
152 		name = "obio";
153 		break;
154 	    case SP_MBMEM:
155 		name = "mbmem";
156 		break;
157 	    case SP_MBIO:
158 		name = "mbio";
159 		break;
160 	    default:
161 		err_print("Error: unknown address space type encountered.\n");
162 		fullabort();
163 	}
164 	return (name);
165 }
166 #endif	/* not */
167 
168 /*
169  * This routine asks the user the given yes/no question and returns
170  * the response.
171  */
172 int
173 check(question)
174 	char	*question;
175 {
176 	int		answer;
177 	u_ioparam_t	ioparam;
178 
179 	/*
180 	 * If we are running out of a command file, assume a yes answer.
181 	 */
182 	if (option_f)
183 		return (0);
184 	/*
185 	 * Ask the user.
186 	 */
187 	ioparam.io_charlist = confirm_list;
188 	answer = input(FIO_MSTR, question, '?', &ioparam,
189 	    (int *)NULL, DATA_INPUT);
190 	return (answer);
191 }
192 
193 /*
194  * This routine aborts the current command.  It is called by a ctrl-C
195  * interrupt and also under certain error conditions.
196  */
197 /*ARGSUSED*/
198 void
199 cmdabort(sig)
200 	int	sig;
201 {
202 
203 	/*
204 	 * If there is no usable saved environment, gracefully exit.  This
205 	 * allows the user to interrupt the program even when input is from
206 	 * a file, or if there is no current menu, like at the "Select disk:"
207 	 * prompt.
208 	 */
209 	if (current_env == NULL || !(current_env->flags & ENV_USE))
210 		fullabort();
211 
212 	/*
213 	 * If we are in a critical zone, note the attempt and return.
214 	 */
215 	if (current_env->flags & ENV_CRITICAL) {
216 		current_env->flags |= ENV_ABORT;
217 		return;
218 	}
219 	/*
220 	 * All interruptions when we are running out of a command file
221 	 * cause the program to gracefully exit.
222 	 */
223 	if (option_f)
224 		fullabort();
225 	fmt_print("\n");
226 	/*
227 	 * Clean up any state left by the interrupted command.
228 	 */
229 	cleanup(sig);
230 	/*
231 	 * Jump to the saved environment.
232 	 */
233 	longjmp(current_env->env, 0);
234 }
235 
236 /*
237  * This routine implements the ctrl-Z suspend mechanism.  It is called
238  * when a suspend signal is received.
239  */
240 /*ARGSUSED*/
241 void
242 onsusp(sig)
243 	int	sig;
244 {
245 	int		fix_term;
246 #ifdef	NOT_DEF
247 	sigset_t	sigmask;
248 #endif	/* NOT_DEF */
249 
250 	/*
251 	 * If we are in a critical zone, note the attempt and return.
252 	 */
253 	if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
254 		stop_pending = 1;
255 		return;
256 	}
257 	/*
258 	 * If the terminal is mucked up, note that we will need to
259 	 * re-muck it when we start up again.
260 	 */
261 	fix_term = ttystate.ttyflags;
262 	fmt_print("\n");
263 	/*
264 	 * Clean up any state left by the interrupted command.
265 	 */
266 	cleanup(sig);
267 #ifdef	NOT_DEF
268 	/* Investigate whether all this is necessary */
269 	/*
270 	 * Stop intercepting the suspend signal, then send ourselves one
271 	 * to cause us to stop.
272 	 */
273 	sigmask.sigbits[0] = (ulong_t)0xffffffff;
274 	if (sigprocmask(SIG_SETMASK, &sigmask, (sigset_t *)NULL) == -1)
275 		err_print("sigprocmask failed %d\n", errno);
276 #endif	/* NOT_DEF */
277 	(void) signal(SIGTSTP, SIG_DFL);
278 	(void) kill(0, SIGTSTP);
279 	/*
280 	 * PC stops here
281 	 */
282 	/*
283 	 * We are started again.  Set us up to intercept the suspend
284 	 * signal once again.
285 	 */
286 	(void) signal(SIGTSTP, onsusp);
287 	/*
288 	 * Re-muck the terminal if necessary.
289 	 */
290 	if (fix_term & TTY_ECHO_OFF)
291 		echo_off();
292 	if (fix_term & TTY_CBREAK_ON)
293 		charmode_on();
294 }
295 
296 /*
297  * This routine implements the timing function used during long-term
298  * disk operations (e.g. formatting).  It is called when an alarm signal
299  * is received.
300  */
301 /*ARGSUSED*/
302 void
303 onalarm(sig)
304 	int	sig;
305 {
306 }
307 
308 
309 /*
310  * This routine gracefully exits the program.
311  */
312 void
313 fullabort()
314 {
315 
316 	fmt_print("\n");
317 	/*
318 	 * Clean up any state left by an interrupted command.
319 	 * Avoid infinite loops caused by a clean-up
320 	 * routine failing again...
321 	 */
322 	if (!aborting) {
323 		aborting = 1;
324 		cleanup(SIGKILL);
325 	}
326 	exit(1);
327 	/*NOTREACHED*/
328 }
329 
330 /*
331  * This routine cleans up the state of the world.  It is a hodge-podge
332  * of kludges to allow us to interrupt commands whenever possible.
333  *
334  * Some cleanup actions may depend on the type of signal.
335  */
336 static void
337 cleanup(int sig)
338 {
339 
340 	/*
341 	 * Lock out interrupts to avoid recursion.
342 	 */
343 	enter_critical();
344 	/*
345 	 * Fix up the tty if necessary.
346 	 */
347 	if (ttystate.ttyflags & TTY_CBREAK_ON) {
348 		charmode_off();
349 	}
350 	if (ttystate.ttyflags & TTY_ECHO_OFF) {
351 		echo_on();
352 	}
353 
354 	/*
355 	 * If the defect list is dirty, write it out.
356 	 */
357 	if (cur_list.flags & LIST_DIRTY) {
358 		cur_list.flags = 0;
359 		if (!EMBEDDED_SCSI)
360 			write_deflist(&cur_list);
361 	}
362 	/*
363 	 * If the label is dirty, write it out.
364 	 */
365 	if (cur_flags & LABEL_DIRTY) {
366 		cur_flags &= ~LABEL_DIRTY;
367 		(void) write_label();
368 	}
369 	/*
370 	 * If we are logging and just interrupted a scan, print out
371 	 * some summary info to the log file.
372 	 */
373 	if (log_file && scan_cur_block >= 0) {
374 		pr_dblock(log_print, scan_cur_block);
375 		log_print("\n");
376 	}
377 	if (scan_blocks_fixed >= 0)
378 		fmt_print("Total of %lld defective blocks repaired.\n",
379 		    scan_blocks_fixed);
380 	if (sig != SIGSTOP) { /* Don't reset on suspend (converted to stop) */
381 		scan_cur_block = scan_blocks_fixed = -1;
382 	}
383 	exit_critical();
384 }
385 
386 /*
387  * This routine causes the program to enter a critical zone.  Within the
388  * critical zone, no interrupts are allowed.  Note that calls to this
389  * routine for the same environment do NOT nest, so there is not
390  * necessarily pairing between calls to enter_critical() and exit_critical().
391  */
392 void
393 enter_critical()
394 {
395 
396 	/*
397 	 * If there is no saved environment, interrupts will be ignored.
398 	 */
399 	if (current_env == NULL)
400 		return;
401 	/*
402 	 * Mark the environment to be in a critical zone.
403 	 */
404 	current_env->flags |= ENV_CRITICAL;
405 }
406 
407 /*
408  * This routine causes the program to exit a critical zone.  Note that
409  * calls to enter_critical() for the same environment do NOT nest, so
410  * one call to exit_critical() will erase any number of such calls.
411  */
412 void
413 exit_critical()
414 {
415 
416 	/*
417 	 * If there is a saved environment, mark it to be non-critical.
418 	 */
419 	if (current_env != NULL)
420 		current_env->flags &= ~ENV_CRITICAL;
421 	/*
422 	 * If there is a stop pending, execute the stop.
423 	 */
424 	if (stop_pending) {
425 		stop_pending = 0;
426 		onsusp(SIGSTOP);
427 	}
428 	/*
429 	 * If there is an abort pending, execute the abort.
430 	 */
431 	if (current_env == NULL)
432 		return;
433 	if (current_env->flags & ENV_ABORT) {
434 		current_env->flags &= ~ENV_ABORT;
435 		cmdabort(SIGINT);
436 	}
437 }
438 
439 /*
440  * This routine turns off echoing on the controlling tty for the program.
441  */
442 void
443 echo_off()
444 {
445 	/*
446 	 * Open the tty and store the file pointer for later.
447 	 */
448 	if (ttystate.ttyflags == 0) {
449 		if ((ttystate.ttyfile = open("/dev/tty",
450 					O_RDWR | O_NDELAY)) < 0) {
451 			err_print("Unable to open /dev/tty.\n");
452 			fullabort();
453 		}
454 	}
455 	/*
456 	 * Get the parameters for the tty, turn off echoing and set them.
457 	 */
458 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
459 		err_print("Unable to get tty parameters.\n");
460 		fullabort();
461 	}
462 	ttystate.ttystate.c_lflag &= ~ECHO;
463 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
464 		err_print("Unable to set tty to echo off state.\n");
465 		fullabort();
466 	}
467 
468 	/*
469 	 * Remember that we've successfully turned
470 	 * ECHO mode off, so we know to fix it later.
471 	 */
472 	ttystate.ttyflags |= TTY_ECHO_OFF;
473 }
474 
475 /*
476  * This routine turns on echoing on the controlling tty for the program.
477  */
478 void
479 echo_on()
480 {
481 
482 	/*
483 	 * Using the saved parameters, turn echoing on and set them.
484 	 */
485 	ttystate.ttystate.c_lflag |= ECHO;
486 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
487 		err_print("Unable to set tty to echo on state.\n");
488 		fullabort();
489 	}
490 	/*
491 	 * Close the tty and mark it ok again.
492 	 */
493 	ttystate.ttyflags &= ~TTY_ECHO_OFF;
494 	if (ttystate.ttyflags == 0) {
495 		(void) close(ttystate.ttyfile);
496 	}
497 }
498 
499 /*
500  * This routine turns off single character entry mode for tty.
501  */
502 void
503 charmode_on()
504 {
505 
506 	/*
507 	 * If tty unopened, open the tty and store the file pointer for later.
508 	 */
509 	if (ttystate.ttyflags == 0) {
510 		if ((ttystate.ttyfile = open("/dev/tty",
511 					O_RDWR | O_NDELAY)) < 0) {
512 			err_print("Unable to open /dev/tty.\n");
513 			fullabort();
514 		}
515 	}
516 	/*
517 	 * Get the parameters for the tty, turn on char mode.
518 	 */
519 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
520 		err_print("Unable to get tty parameters.\n");
521 		fullabort();
522 	}
523 	ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
524 	ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
525 
526 	ttystate.ttystate.c_lflag &= ~ICANON;
527 	ttystate.ttystate.c_cc[VMIN] = 1;
528 	ttystate.ttystate.c_cc[VTIME] = 0;
529 
530 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
531 		err_print("Unable to set tty to cbreak on state.\n");
532 		fullabort();
533 	}
534 
535 	/*
536 	 * Remember that we've successfully turned
537 	 * CBREAK mode on, so we know to fix it later.
538 	 */
539 	ttystate.ttyflags |= TTY_CBREAK_ON;
540 }
541 
542 /*
543  * This routine turns on single character entry mode for tty.
544  * Note, this routine must be called before echo_on.
545  */
546 void
547 charmode_off()
548 {
549 
550 	/*
551 	 * Using the saved parameters, turn char mode on.
552 	 */
553 	ttystate.ttystate.c_lflag |= ICANON;
554 	ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
555 	ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
556 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
557 		err_print("Unable to set tty to cbreak off state.\n");
558 		fullabort();
559 	}
560 	/*
561 	 * Close the tty and mark it ok again.
562 	 */
563 	ttystate.ttyflags &= ~TTY_CBREAK_ON;
564 	if (ttystate.ttyflags == 0) {
565 		(void) close(ttystate.ttyfile);
566 	}
567 }
568 
569 
570 /*
571  * Allocate space for and return a pointer to a string
572  * on the stack.  If the string is null, create
573  * an empty string.
574  * Use destroy_data() to free when no longer used.
575  */
576 char *
577 alloc_string(s)
578 	char	*s;
579 {
580 	char	*ns;
581 
582 	if (s == (char *)NULL) {
583 		ns = (char *)zalloc(1);
584 	} else {
585 		ns = (char *)zalloc(strlen(s) + 1);
586 		(void) strcpy(ns, s);
587 	}
588 	return (ns);
589 }
590 
591 
592 
593 /*
594  * This function can be used to build up an array of strings
595  * dynamically, with a trailing NULL to terminate the list.
596  *
597  * Parameters:
598  *	argvlist:  a pointer to the base of the current list.
599  *		   does not have to be initialized.
600  *	size:	   pointer to an integer, indicating the number
601  *		   of string installed in the list.  Must be
602  *		   initialized to zero.
603  *	alloc:	   pointer to an integer, indicating the amount
604  *		   of space allocated.  Must be initialized to
605  *		   zero.  For efficiency, we allocate the list
606  *		   in chunks and use it piece-by-piece.
607  *	str:	   the string to be inserted in the list.
608  *		   A copy of the string is malloc'ed, and
609  *		   appended at the end of the list.
610  * Returns:
611  *	a pointer to the possibly-moved argvlist.
612  *
613  * No attempt to made to free unused memory when the list is
614  * completed, although this would not be hard to do.  For
615  * reasonably small lists, this should suffice.
616  */
617 #define	INITIAL_LISTSIZE	32
618 #define	INCR_LISTSIZE		32
619 
620 char **
621 build_argvlist(argvlist, size, alloc, str)
622 	char	**argvlist;
623 	int	*size;
624 	int	*alloc;
625 	char	*str;
626 {
627 	if (*size + 2 > *alloc) {
628 		if (*alloc == 0) {
629 			*alloc = INITIAL_LISTSIZE;
630 			argvlist = (char **)
631 				zalloc(sizeof (char *) * (*alloc));
632 		} else {
633 			*alloc += INCR_LISTSIZE;
634 			argvlist = (char **)
635 				rezalloc((void *) argvlist,
636 				sizeof (char *) * (*alloc));
637 		}
638 	}
639 
640 	argvlist[*size] = alloc_string(str);
641 	*size += 1;
642 	argvlist[*size] = NULL;
643 
644 	return (argvlist);
645 }
646 
647 
648 /*
649  * Useful parsing macros
650  */
651 #define	must_be(s, c)		if (*s++ != c) return (0)
652 #define	skip_digits(s)		while (isdigit(*s)) s++
653 /* Parsing macro below is created to handle fabric devices which contains */
654 /* upper hex digits like c2t210000203708B8CEd0s0.			  */
655 /* To get the target id(tid) the digit and hex upper digit need to	  */
656 /* be processed.							  */
657 #define	skip_digit_or_hexupper(s)	while (isdigit(*s) || \
658 					(isxdigit(*s) && isupper(*s))) s++
659 
660 /*
661  * Return true if a device name matches the conventions
662  * for the particular system.
663  */
664 int
665 conventional_name(char *name)
666 {
667 	must_be(name, 'c');
668 	skip_digits(name);
669 	if (*name == 't') {
670 		name++;
671 		skip_digit_or_hexupper(name);
672 	}
673 	must_be(name, 'd');
674 	skip_digits(name);
675 	must_be(name, 's');
676 	skip_digits(name);
677 	return (*name == 0);
678 }
679 
680 /*
681  * Return true if a device name matches the intel physical name conventions
682  * for the particular system.
683  */
684 int
685 fdisk_physical_name(char *name)
686 {
687 	must_be(name, 'c');
688 	skip_digits(name);
689 	if (*name == 't') {
690 		name++;
691 		skip_digit_or_hexupper(name);
692 	}
693 	must_be(name, 'd');
694 	skip_digits(name);
695 	must_be(name, 'p');
696 	skip_digits(name);
697 	return (*name == 0);
698 }
699 
700 /*
701  * Return true if a device name matches the conventions
702  * for a "whole disk" name for the particular system.
703  * The name in this case must match exactly that which
704  * would appear in the device directory itself.
705  */
706 int
707 whole_disk_name(name)
708 	char	*name;
709 {
710 	must_be(name, 'c');
711 	skip_digits(name);
712 	if (*name == 't') {
713 		name++;
714 		skip_digit_or_hexupper(name);
715 	}
716 	must_be(name, 'd');
717 	skip_digits(name);
718 	must_be(name, 's');
719 	must_be(name, '2');
720 	return (*name == 0);
721 }
722 
723 
724 /*
725  * Return true if a name is in the internal canonical form
726  */
727 int
728 canonical_name(name)
729 	char	*name;
730 {
731 	must_be(name, 'c');
732 	skip_digits(name);
733 	if (*name == 't') {
734 		name++;
735 		skip_digit_or_hexupper(name);
736 	}
737 	must_be(name, 'd');
738 	skip_digits(name);
739 	return (*name == 0);
740 }
741 
742 
743 /*
744  * Return true if a name is in the internal canonical form for 4.x
745  * Used to support 4.x naming conventions under 5.0.
746  */
747 int
748 canonical4x_name(name)
749 	char	*name;
750 {
751 	char    **p;
752 	int	i;
753 
754 	p = disk_4x_identifiers;
755 	for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
756 		if (match_substr(name, *p)) {
757 			name += strlen(*p);
758 			break;
759 		}
760 	}
761 	if (i == 0)
762 		return (0);
763 	skip_digits(name);
764 	return (*name == 0);
765 }
766 
767 
768 /*
769  * Map a conventional name into the internal canonical form:
770  *
771  *	/dev/rdsk/c0t0d0s0 -> c0t0d0
772  */
773 void
774 canonicalize_name(dst, src)
775 	char	*dst;
776 	char	*src;
777 {
778 	char	*s;
779 
780 	/*
781 	 * Copy from the 'c' to the end to the destination string...
782 	 */
783 	s = strchr(src, 'c');
784 	if (s != NULL) {
785 		(void) strcpy(dst, s);
786 		/*
787 		 * Remove the trailing slice (partition) reference
788 		 */
789 		s = dst + strlen(dst) - 2;
790 		if (*s == 's') {
791 			*s = 0;
792 		}
793 	} else {
794 		*dst = 0;	/* be tolerant of garbage input */
795 	}
796 }
797 
798 
799 /*
800  * Return true if we find an occurance of s2 at the
801  * beginning of s1.  We don't have to match all of
802  * s1, but we do have to match all of s2
803  */
804 int
805 match_substr(s1, s2)
806 	char    *s1;
807 	char    *s2;
808 {
809 	while (*s2 != 0) {
810 		if (*s1++ != *s2++)
811 		return (0);
812 	}
813 
814 	return (1);
815 }
816 
817 
818 /*
819  * Dump a structure in hexadecimal, for diagnostic purposes
820  */
821 #define	BYTES_PER_LINE		16
822 
823 void
824 dump(hdr, src, nbytes, format)
825 	char	*hdr;
826 	caddr_t	src;
827 	int	nbytes;
828 	int	format;
829 {
830 	int	i;
831 	int	n;
832 	char	*p;
833 	char	s[256];
834 
835 	assert(format == HEX_ONLY || format == HEX_ASCII);
836 
837 	(void) strcpy(s, hdr);
838 	for (p = s; *p; p++) {
839 		*p = ' ';
840 	}
841 
842 	p = hdr;
843 	while (nbytes > 0) {
844 		err_print("%s", p);
845 		p = s;
846 		n = min(nbytes, BYTES_PER_LINE);
847 		for (i = 0; i < n; i++) {
848 			err_print("%02x ", src[i] & 0xff);
849 		}
850 		if (format == HEX_ASCII) {
851 			for (i = BYTES_PER_LINE-n; i > 0; i--) {
852 				err_print("   ");
853 			}
854 			err_print("    ");
855 			for (i = 0; i < n; i++) {
856 				err_print("%c",
857 					isprint(src[i]) ? src[i] : '.');
858 			}
859 		}
860 		err_print("\n");
861 		nbytes -= n;
862 		src += n;
863 	}
864 }
865 
866 
867 float
868 bn2mb(uint64_t nblks)
869 {
870 	float	n;
871 
872 	n = (float)nblks / 1024.0;
873 	return ((n / 1024.0) * DEV_BSIZE);
874 }
875 
876 
877 uint_t
878 mb2bn(float mb)
879 {
880 	uint_t	n;
881 
882 	n = (uint_t)(mb * 1024.0 * (1024.0 / DEV_BSIZE));
883 	return (n);
884 }
885 
886 float
887 bn2gb(uint64_t nblks)
888 {
889 	float	n;
890 
891 	n = (float)nblks / (1024.0 * 1024.0);
892 	return ((n/1024.0) * DEV_BSIZE);
893 
894 }
895 
896 float
897 bn2tb(uint64_t nblks)
898 {
899 	float	n;
900 
901 	n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
902 	return ((n/1024.0) * DEV_BSIZE);
903 }
904 
905 uint_t
906 gb2bn(float gb)
907 {
908 	uint_t	n;
909 
910 	n = (uint_t)(gb * 1024.0 * 1024.0 * (1024.0 / DEV_BSIZE));
911 	return (n);
912 }
913 
914 /*
915  * This routine finds out the number of lines (rows) in a terminal
916  * window. The default value of TTY_LINES is returned on error.
917  */
918 int
919 get_tty_lines()
920 {
921 	int	tty_lines = TTY_LINES;
922 	struct	winsize	winsize;
923 
924 	if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
925 		/*
926 		 * We have a real terminal for std input and output
927 		 */
928 		winsize.ws_row = 0;
929 		if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
930 			if (winsize.ws_row > 2) {
931 				/*
932 				 * Should be atleast 2 lines, for division
933 				 * by (tty_lines - 1, tty_lines - 2) to work.
934 				 */
935 				tty_lines = winsize.ws_row;
936 			}
937 		}
938 	}
939 	return (tty_lines);
940 }
941