xref: /titanic_44/usr/src/cmd/format/misc.c (revision 65908c77dfc02644236ba18bffe67b5ed6f23135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5342440ecSPrasad Singamsetty  * Common Development and Distribution License (the "License").
6342440ecSPrasad Singamsetty  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*65908c77Syu, larry liu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains miscellaneous routines.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include "global.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <malloc.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <termio.h>
437c478bd9Sstevel@tonic-gate #include "misc.h"
447c478bd9Sstevel@tonic-gate #include "analyze.h"
457c478bd9Sstevel@tonic-gate #include "label.h"
467c478bd9Sstevel@tonic-gate #include "startup.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifdef __STDC__
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
517c478bd9Sstevel@tonic-gate static void	cleanup(int sig);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
567c478bd9Sstevel@tonic-gate static void	cleanup();
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct	env *current_env = NULL;	/* ptr to current environment */
617c478bd9Sstevel@tonic-gate static int	stop_pending = 0;	/* ctrl-Z is pending */
627c478bd9Sstevel@tonic-gate struct	ttystate ttystate;		/* tty info */
637c478bd9Sstevel@tonic-gate static int	aborting = 0;		/* in process of aborting */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * For 4.x, limit the choices of valid disk names to this set.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static char		*disk_4x_identifiers[] = { "sd", "id"};
697c478bd9Sstevel@tonic-gate #define	N_DISK_4X_IDS	(sizeof (disk_4x_identifiers)/sizeof (char *))
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * This is the list of legal inputs for all yes/no questions.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate char	*confirm_list[] = {
767c478bd9Sstevel@tonic-gate 	"yes",
777c478bd9Sstevel@tonic-gate 	"no",
787c478bd9Sstevel@tonic-gate 	NULL,
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * This routine is a wrapper for malloc.  It allocates pre-zeroed space,
837c478bd9Sstevel@tonic-gate  * and checks the return value so the caller doesn't have to.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate void *
867c478bd9Sstevel@tonic-gate zalloc(count)
877c478bd9Sstevel@tonic-gate 	int	count;
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	void	*ptr;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((ptr = (void *) calloc(1, (unsigned)count)) == NULL) {
927c478bd9Sstevel@tonic-gate 		err_print("Error: unable to calloc more space.\n");
937c478bd9Sstevel@tonic-gate 		fullabort();
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	return (ptr);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * This routine is a wrapper for realloc.  It reallocates the given
1007c478bd9Sstevel@tonic-gate  * space, and checks the return value so the caller doesn't have to.
1017c478bd9Sstevel@tonic-gate  * Note that the any space added by this call is NOT necessarily
1027c478bd9Sstevel@tonic-gate  * zeroed.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate void *
1057c478bd9Sstevel@tonic-gate rezalloc(ptr, count)
1067c478bd9Sstevel@tonic-gate 	void	*ptr;
1077c478bd9Sstevel@tonic-gate 	int	count;
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	void	*new_ptr;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if ((new_ptr = (void *) realloc((char *)ptr,
1137c478bd9Sstevel@tonic-gate 				(unsigned)count)) == NULL) {
1147c478bd9Sstevel@tonic-gate 		err_print("Error: unable to realloc more space.\n");
1157c478bd9Sstevel@tonic-gate 		fullabort();
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	return (new_ptr);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * This routine is a wrapper for free.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate void
1247c478bd9Sstevel@tonic-gate destroy_data(data)
1257c478bd9Sstevel@tonic-gate 	char	*data;
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	free((char *)data);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #ifdef	not
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * This routine takes the space number returned by an ioctl call and
1337c478bd9Sstevel@tonic-gate  * returns a mnemonic name for that space.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate char *
1367c478bd9Sstevel@tonic-gate space2str(space)
1377c478bd9Sstevel@tonic-gate 	uint_t	space;
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char	*name;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	switch (space&SP_BUSMASK) {
1427c478bd9Sstevel@tonic-gate 	    case SP_VIRTUAL:
1437c478bd9Sstevel@tonic-gate 		name = "virtual";
1447c478bd9Sstevel@tonic-gate 		break;
1457c478bd9Sstevel@tonic-gate 	    case SP_OBMEM:
1467c478bd9Sstevel@tonic-gate 		name = "obmem";
1477c478bd9Sstevel@tonic-gate 		break;
1487c478bd9Sstevel@tonic-gate 	    case SP_OBIO:
1497c478bd9Sstevel@tonic-gate 		name = "obio";
1507c478bd9Sstevel@tonic-gate 		break;
1517c478bd9Sstevel@tonic-gate 	    case SP_MBMEM:
1527c478bd9Sstevel@tonic-gate 		name = "mbmem";
1537c478bd9Sstevel@tonic-gate 		break;
1547c478bd9Sstevel@tonic-gate 	    case SP_MBIO:
1557c478bd9Sstevel@tonic-gate 		name = "mbio";
1567c478bd9Sstevel@tonic-gate 		break;
1577c478bd9Sstevel@tonic-gate 	    default:
1587c478bd9Sstevel@tonic-gate 		err_print("Error: unknown address space type encountered.\n");
1597c478bd9Sstevel@tonic-gate 		fullabort();
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	return (name);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate #endif	/* not */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * This routine asks the user the given yes/no question and returns
1677c478bd9Sstevel@tonic-gate  * the response.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate int
1707c478bd9Sstevel@tonic-gate check(question)
1717c478bd9Sstevel@tonic-gate 	char	*question;
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	int		answer;
1747c478bd9Sstevel@tonic-gate 	u_ioparam_t	ioparam;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * If we are running out of a command file, assume a yes answer.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (option_f)
1807c478bd9Sstevel@tonic-gate 		return (0);
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Ask the user.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	ioparam.io_charlist = confirm_list;
1857c478bd9Sstevel@tonic-gate 	answer = input(FIO_MSTR, question, '?', &ioparam,
1867c478bd9Sstevel@tonic-gate 	    (int *)NULL, DATA_INPUT);
1877c478bd9Sstevel@tonic-gate 	return (answer);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * This routine aborts the current command.  It is called by a ctrl-C
1927c478bd9Sstevel@tonic-gate  * interrupt and also under certain error conditions.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1957c478bd9Sstevel@tonic-gate void
1967c478bd9Sstevel@tonic-gate cmdabort(sig)
1977c478bd9Sstevel@tonic-gate 	int	sig;
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * If there is no usable saved environment, gracefully exit.  This
2027c478bd9Sstevel@tonic-gate 	 * allows the user to interrupt the program even when input is from
2037c478bd9Sstevel@tonic-gate 	 * a file, or if there is no current menu, like at the "Select disk:"
2047c478bd9Sstevel@tonic-gate 	 * prompt.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	if (current_env == NULL || !(current_env->flags & ENV_USE))
2077c478bd9Sstevel@tonic-gate 		fullabort();
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * If we are in a critical zone, note the attempt and return.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	if (current_env->flags & ENV_CRITICAL) {
2137c478bd9Sstevel@tonic-gate 		current_env->flags |= ENV_ABORT;
2147c478bd9Sstevel@tonic-gate 		return;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	/*
2177c478bd9Sstevel@tonic-gate 	 * All interruptions when we are running out of a command file
2187c478bd9Sstevel@tonic-gate 	 * cause the program to gracefully exit.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	if (option_f)
2217c478bd9Sstevel@tonic-gate 		fullabort();
2227c478bd9Sstevel@tonic-gate 	fmt_print("\n");
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Clean up any state left by the interrupted command.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	cleanup(sig);
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Jump to the saved environment.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	longjmp(current_env->env, 0);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * This routine implements the ctrl-Z suspend mechanism.  It is called
2357c478bd9Sstevel@tonic-gate  * when a suspend signal is received.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2387c478bd9Sstevel@tonic-gate void
2397c478bd9Sstevel@tonic-gate onsusp(sig)
2407c478bd9Sstevel@tonic-gate 	int	sig;
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int		fix_term;
2437c478bd9Sstevel@tonic-gate #ifdef	NOT_DEF
2447c478bd9Sstevel@tonic-gate 	sigset_t	sigmask;
2457c478bd9Sstevel@tonic-gate #endif	/* NOT_DEF */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * If we are in a critical zone, note the attempt and return.
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
2517c478bd9Sstevel@tonic-gate 		stop_pending = 1;
2527c478bd9Sstevel@tonic-gate 		return;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * If the terminal is mucked up, note that we will need to
2567c478bd9Sstevel@tonic-gate 	 * re-muck it when we start up again.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	fix_term = ttystate.ttyflags;
2597c478bd9Sstevel@tonic-gate 	fmt_print("\n");
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * Clean up any state left by the interrupted command.
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	cleanup(sig);
2647c478bd9Sstevel@tonic-gate #ifdef	NOT_DEF
2657c478bd9Sstevel@tonic-gate 	/* Investigate whether all this is necessary */
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Stop intercepting the suspend signal, then send ourselves one
2687c478bd9Sstevel@tonic-gate 	 * to cause us to stop.
2697c478bd9Sstevel@tonic-gate 	 */
2707c478bd9Sstevel@tonic-gate 	sigmask.sigbits[0] = (ulong_t)0xffffffff;
2717c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_SETMASK, &sigmask, (sigset_t *)NULL) == -1)
2727c478bd9Sstevel@tonic-gate 		err_print("sigprocmask failed %d\n", errno);
2737c478bd9Sstevel@tonic-gate #endif	/* NOT_DEF */
2747c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_DFL);
2757c478bd9Sstevel@tonic-gate 	(void) kill(0, SIGTSTP);
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * PC stops here
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * We are started again.  Set us up to intercept the suspend
2817c478bd9Sstevel@tonic-gate 	 * signal once again.
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, onsusp);
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Re-muck the terminal if necessary.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if (fix_term & TTY_ECHO_OFF)
2887c478bd9Sstevel@tonic-gate 		echo_off();
2897c478bd9Sstevel@tonic-gate 	if (fix_term & TTY_CBREAK_ON)
2907c478bd9Sstevel@tonic-gate 		charmode_on();
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * This routine implements the timing function used during long-term
2957c478bd9Sstevel@tonic-gate  * disk operations (e.g. formatting).  It is called when an alarm signal
2967c478bd9Sstevel@tonic-gate  * is received.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2997c478bd9Sstevel@tonic-gate void
3007c478bd9Sstevel@tonic-gate onalarm(sig)
3017c478bd9Sstevel@tonic-gate 	int	sig;
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * This routine gracefully exits the program.
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate void
3107c478bd9Sstevel@tonic-gate fullabort()
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	fmt_print("\n");
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Clean up any state left by an interrupted command.
3167c478bd9Sstevel@tonic-gate 	 * Avoid infinite loops caused by a clean-up
3177c478bd9Sstevel@tonic-gate 	 * routine failing again...
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 	if (!aborting) {
3207c478bd9Sstevel@tonic-gate 		aborting = 1;
3217c478bd9Sstevel@tonic-gate 		cleanup(SIGKILL);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	exit(1);
3247c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * This routine cleans up the state of the world.  It is a hodge-podge
3297c478bd9Sstevel@tonic-gate  * of kludges to allow us to interrupt commands whenever possible.
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * Some cleanup actions may depend on the type of signal.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate static void
3347c478bd9Sstevel@tonic-gate cleanup(int sig)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * Lock out interrupts to avoid recursion.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	enter_critical();
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Fix up the tty if necessary.
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags & TTY_CBREAK_ON) {
3457c478bd9Sstevel@tonic-gate 		charmode_off();
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags & TTY_ECHO_OFF) {
3487c478bd9Sstevel@tonic-gate 		echo_on();
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * If the defect list is dirty, write it out.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	if (cur_list.flags & LIST_DIRTY) {
3557c478bd9Sstevel@tonic-gate 		cur_list.flags = 0;
3567c478bd9Sstevel@tonic-gate 		if (!EMBEDDED_SCSI)
3577c478bd9Sstevel@tonic-gate 			write_deflist(&cur_list);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * If the label is dirty, write it out.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (cur_flags & LABEL_DIRTY) {
3637c478bd9Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
3647c478bd9Sstevel@tonic-gate 		(void) write_label();
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * If we are logging and just interrupted a scan, print out
3687c478bd9Sstevel@tonic-gate 	 * some summary info to the log file.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if (log_file && scan_cur_block >= 0) {
3717c478bd9Sstevel@tonic-gate 		pr_dblock(log_print, scan_cur_block);
3727c478bd9Sstevel@tonic-gate 		log_print("\n");
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	if (scan_blocks_fixed >= 0)
3757c478bd9Sstevel@tonic-gate 		fmt_print("Total of %lld defective blocks repaired.\n",
3767c478bd9Sstevel@tonic-gate 		    scan_blocks_fixed);
3777c478bd9Sstevel@tonic-gate 	if (sig != SIGSTOP) { /* Don't reset on suspend (converted to stop) */
3787c478bd9Sstevel@tonic-gate 		scan_cur_block = scan_blocks_fixed = -1;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	exit_critical();
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * This routine causes the program to enter a critical zone.  Within the
3857c478bd9Sstevel@tonic-gate  * critical zone, no interrupts are allowed.  Note that calls to this
3867c478bd9Sstevel@tonic-gate  * routine for the same environment do NOT nest, so there is not
3877c478bd9Sstevel@tonic-gate  * necessarily pairing between calls to enter_critical() and exit_critical().
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate void
3907c478bd9Sstevel@tonic-gate enter_critical()
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * If there is no saved environment, interrupts will be ignored.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	if (current_env == NULL)
3977c478bd9Sstevel@tonic-gate 		return;
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * Mark the environment to be in a critical zone.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	current_env->flags |= ENV_CRITICAL;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * This routine causes the program to exit a critical zone.  Note that
4067c478bd9Sstevel@tonic-gate  * calls to enter_critical() for the same environment do NOT nest, so
4077c478bd9Sstevel@tonic-gate  * one call to exit_critical() will erase any number of such calls.
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate void
4107c478bd9Sstevel@tonic-gate exit_critical()
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * If there is a saved environment, mark it to be non-critical.
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	if (current_env != NULL)
4177c478bd9Sstevel@tonic-gate 		current_env->flags &= ~ENV_CRITICAL;
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * If there is a stop pending, execute the stop.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (stop_pending) {
4227c478bd9Sstevel@tonic-gate 		stop_pending = 0;
4237c478bd9Sstevel@tonic-gate 		onsusp(SIGSTOP);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * If there is an abort pending, execute the abort.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if (current_env == NULL)
4297c478bd9Sstevel@tonic-gate 		return;
4307c478bd9Sstevel@tonic-gate 	if (current_env->flags & ENV_ABORT) {
4317c478bd9Sstevel@tonic-gate 		current_env->flags &= ~ENV_ABORT;
4327c478bd9Sstevel@tonic-gate 		cmdabort(SIGINT);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * This routine turns off echoing on the controlling tty for the program.
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate void
4407c478bd9Sstevel@tonic-gate echo_off()
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Open the tty and store the file pointer for later.
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
4467c478bd9Sstevel@tonic-gate 		if ((ttystate.ttyfile = open("/dev/tty",
4477c478bd9Sstevel@tonic-gate 		    O_RDWR | O_NDELAY)) < 0) {
4487c478bd9Sstevel@tonic-gate 			err_print("Unable to open /dev/tty.\n");
4497c478bd9Sstevel@tonic-gate 			fullabort();
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * Get the parameters for the tty, turn off echoing and set them.
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
4567c478bd9Sstevel@tonic-gate 		err_print("Unable to get tty parameters.\n");
4577c478bd9Sstevel@tonic-gate 		fullabort();
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag &= ~ECHO;
4607c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4617c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to echo off state.\n");
4627c478bd9Sstevel@tonic-gate 		fullabort();
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Remember that we've successfully turned
4677c478bd9Sstevel@tonic-gate 	 * ECHO mode off, so we know to fix it later.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	ttystate.ttyflags |= TTY_ECHO_OFF;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * This routine turns on echoing on the controlling tty for the program.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate void
4767c478bd9Sstevel@tonic-gate echo_on()
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Using the saved parameters, turn echoing on and set them.
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag |= ECHO;
4837c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4847c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to echo on state.\n");
4857c478bd9Sstevel@tonic-gate 		fullabort();
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Close the tty and mark it ok again.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	ttystate.ttyflags &= ~TTY_ECHO_OFF;
4917c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
4927c478bd9Sstevel@tonic-gate 		(void) close(ttystate.ttyfile);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * This routine turns off single character entry mode for tty.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate void
5007c478bd9Sstevel@tonic-gate charmode_on()
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * If tty unopened, open the tty and store the file pointer for later.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
5077c478bd9Sstevel@tonic-gate 		if ((ttystate.ttyfile = open("/dev/tty",
5087c478bd9Sstevel@tonic-gate 		    O_RDWR | O_NDELAY)) < 0) {
5097c478bd9Sstevel@tonic-gate 			err_print("Unable to open /dev/tty.\n");
5107c478bd9Sstevel@tonic-gate 			fullabort();
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * Get the parameters for the tty, turn on char mode.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
5177c478bd9Sstevel@tonic-gate 		err_print("Unable to get tty parameters.\n");
5187c478bd9Sstevel@tonic-gate 		fullabort();
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
5217c478bd9Sstevel@tonic-gate 	ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag &= ~ICANON;
5247c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VMIN] = 1;
5257c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VTIME] = 0;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5287c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to cbreak on state.\n");
5297c478bd9Sstevel@tonic-gate 		fullabort();
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Remember that we've successfully turned
5347c478bd9Sstevel@tonic-gate 	 * CBREAK mode on, so we know to fix it later.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	ttystate.ttyflags |= TTY_CBREAK_ON;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * This routine turns on single character entry mode for tty.
5417c478bd9Sstevel@tonic-gate  * Note, this routine must be called before echo_on.
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate void
5447c478bd9Sstevel@tonic-gate charmode_off()
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Using the saved parameters, turn char mode on.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag |= ICANON;
5517c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
5527c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
5537c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5547c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to cbreak off state.\n");
5557c478bd9Sstevel@tonic-gate 		fullabort();
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Close the tty and mark it ok again.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	ttystate.ttyflags &= ~TTY_CBREAK_ON;
5617c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
5627c478bd9Sstevel@tonic-gate 		(void) close(ttystate.ttyfile);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Allocate space for and return a pointer to a string
5697c478bd9Sstevel@tonic-gate  * on the stack.  If the string is null, create
5707c478bd9Sstevel@tonic-gate  * an empty string.
5717c478bd9Sstevel@tonic-gate  * Use destroy_data() to free when no longer used.
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate char *
5747c478bd9Sstevel@tonic-gate alloc_string(s)
5757c478bd9Sstevel@tonic-gate 	char	*s;
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	char	*ns;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (s == (char *)NULL) {
5807c478bd9Sstevel@tonic-gate 		ns = (char *)zalloc(1);
5817c478bd9Sstevel@tonic-gate 	} else {
5827c478bd9Sstevel@tonic-gate 		ns = (char *)zalloc(strlen(s) + 1);
5837c478bd9Sstevel@tonic-gate 		(void) strcpy(ns, s);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	return (ns);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * This function can be used to build up an array of strings
5927c478bd9Sstevel@tonic-gate  * dynamically, with a trailing NULL to terminate the list.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * Parameters:
5957c478bd9Sstevel@tonic-gate  *	argvlist:  a pointer to the base of the current list.
5967c478bd9Sstevel@tonic-gate  *		   does not have to be initialized.
5977c478bd9Sstevel@tonic-gate  *	size:	   pointer to an integer, indicating the number
5987c478bd9Sstevel@tonic-gate  *		   of string installed in the list.  Must be
5997c478bd9Sstevel@tonic-gate  *		   initialized to zero.
6007c478bd9Sstevel@tonic-gate  *	alloc:	   pointer to an integer, indicating the amount
6017c478bd9Sstevel@tonic-gate  *		   of space allocated.  Must be initialized to
6027c478bd9Sstevel@tonic-gate  *		   zero.  For efficiency, we allocate the list
6037c478bd9Sstevel@tonic-gate  *		   in chunks and use it piece-by-piece.
6047c478bd9Sstevel@tonic-gate  *	str:	   the string to be inserted in the list.
6057c478bd9Sstevel@tonic-gate  *		   A copy of the string is malloc'ed, and
6067c478bd9Sstevel@tonic-gate  *		   appended at the end of the list.
6077c478bd9Sstevel@tonic-gate  * Returns:
6087c478bd9Sstevel@tonic-gate  *	a pointer to the possibly-moved argvlist.
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  * No attempt to made to free unused memory when the list is
6117c478bd9Sstevel@tonic-gate  * completed, although this would not be hard to do.  For
6127c478bd9Sstevel@tonic-gate  * reasonably small lists, this should suffice.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate #define	INITIAL_LISTSIZE	32
6157c478bd9Sstevel@tonic-gate #define	INCR_LISTSIZE		32
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate char **
6187c478bd9Sstevel@tonic-gate build_argvlist(argvlist, size, alloc, str)
6197c478bd9Sstevel@tonic-gate 	char	**argvlist;
6207c478bd9Sstevel@tonic-gate 	int	*size;
6217c478bd9Sstevel@tonic-gate 	int	*alloc;
6227c478bd9Sstevel@tonic-gate 	char	*str;
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	if (*size + 2 > *alloc) {
6257c478bd9Sstevel@tonic-gate 		if (*alloc == 0) {
6267c478bd9Sstevel@tonic-gate 			*alloc = INITIAL_LISTSIZE;
6277c478bd9Sstevel@tonic-gate 			argvlist = (char **)
6287c478bd9Sstevel@tonic-gate 				zalloc(sizeof (char *) * (*alloc));
6297c478bd9Sstevel@tonic-gate 		} else {
6307c478bd9Sstevel@tonic-gate 			*alloc += INCR_LISTSIZE;
6317c478bd9Sstevel@tonic-gate 			argvlist = (char **)
6327c478bd9Sstevel@tonic-gate 				rezalloc((void *) argvlist,
6337c478bd9Sstevel@tonic-gate 				sizeof (char *) * (*alloc));
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	argvlist[*size] = alloc_string(str);
6387c478bd9Sstevel@tonic-gate 	*size += 1;
6397c478bd9Sstevel@tonic-gate 	argvlist[*size] = NULL;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	return (argvlist);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /*
6467c478bd9Sstevel@tonic-gate  * Useful parsing macros
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate #define	must_be(s, c)		if (*s++ != c) return (0)
6497c478bd9Sstevel@tonic-gate #define	skip_digits(s)		while (isdigit(*s)) s++
6507c478bd9Sstevel@tonic-gate /* Parsing macro below is created to handle fabric devices which contains */
6517c478bd9Sstevel@tonic-gate /* upper hex digits like c2t210000203708B8CEd0s0.			  */
6527c478bd9Sstevel@tonic-gate /* To get the target id(tid) the digit and hex upper digit need to	  */
6537c478bd9Sstevel@tonic-gate /* be processed.							  */
6547c478bd9Sstevel@tonic-gate #define	skip_digit_or_hexupper(s)	while (isdigit(*s) || \
6557c478bd9Sstevel@tonic-gate 					(isxdigit(*s) && isupper(*s))) s++
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * Return true if a device name matches the conventions
6597c478bd9Sstevel@tonic-gate  * for the particular system.
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate int
6627c478bd9Sstevel@tonic-gate conventional_name(char *name)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
6657c478bd9Sstevel@tonic-gate 	skip_digits(name);
6667c478bd9Sstevel@tonic-gate 	if (*name == 't') {
6677c478bd9Sstevel@tonic-gate 		name++;
6687c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
6717c478bd9Sstevel@tonic-gate 	skip_digits(name);
6727c478bd9Sstevel@tonic-gate 	must_be(name, 's');
6737c478bd9Sstevel@tonic-gate 	skip_digits(name);
6747c478bd9Sstevel@tonic-gate 	return (*name == 0);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * Return true if a device name matches the intel physical name conventions
6797c478bd9Sstevel@tonic-gate  * for the particular system.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate int
6827c478bd9Sstevel@tonic-gate fdisk_physical_name(char *name)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
6857c478bd9Sstevel@tonic-gate 	skip_digits(name);
6867c478bd9Sstevel@tonic-gate 	if (*name == 't') {
6877c478bd9Sstevel@tonic-gate 		name++;
6887c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
6917c478bd9Sstevel@tonic-gate 	skip_digits(name);
6927c478bd9Sstevel@tonic-gate 	must_be(name, 'p');
6937c478bd9Sstevel@tonic-gate 	skip_digits(name);
6947c478bd9Sstevel@tonic-gate 	return (*name == 0);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /*
6987c478bd9Sstevel@tonic-gate  * Return true if a device name matches the conventions
6997c478bd9Sstevel@tonic-gate  * for a "whole disk" name for the particular system.
7007c478bd9Sstevel@tonic-gate  * The name in this case must match exactly that which
7017c478bd9Sstevel@tonic-gate  * would appear in the device directory itself.
7027c478bd9Sstevel@tonic-gate  */
7037c478bd9Sstevel@tonic-gate int
7047c478bd9Sstevel@tonic-gate whole_disk_name(name)
7057c478bd9Sstevel@tonic-gate 	char	*name;
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
7087c478bd9Sstevel@tonic-gate 	skip_digits(name);
7097c478bd9Sstevel@tonic-gate 	if (*name == 't') {
7107c478bd9Sstevel@tonic-gate 		name++;
7117c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
7147c478bd9Sstevel@tonic-gate 	skip_digits(name);
7157c478bd9Sstevel@tonic-gate 	must_be(name, 's');
7167c478bd9Sstevel@tonic-gate 	must_be(name, '2');
7177c478bd9Sstevel@tonic-gate 	return (*name == 0);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate  * Return true if a name is in the internal canonical form
7237c478bd9Sstevel@tonic-gate  */
7247c478bd9Sstevel@tonic-gate int
7257c478bd9Sstevel@tonic-gate canonical_name(name)
7267c478bd9Sstevel@tonic-gate 	char	*name;
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
7297c478bd9Sstevel@tonic-gate 	skip_digits(name);
7307c478bd9Sstevel@tonic-gate 	if (*name == 't') {
7317c478bd9Sstevel@tonic-gate 		name++;
7327c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
7357c478bd9Sstevel@tonic-gate 	skip_digits(name);
7367c478bd9Sstevel@tonic-gate 	return (*name == 0);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * Return true if a name is in the internal canonical form for 4.x
7427c478bd9Sstevel@tonic-gate  * Used to support 4.x naming conventions under 5.0.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate int
7457c478bd9Sstevel@tonic-gate canonical4x_name(name)
7467c478bd9Sstevel@tonic-gate 	char	*name;
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	char    **p;
7497c478bd9Sstevel@tonic-gate 	int	i;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	p = disk_4x_identifiers;
7527c478bd9Sstevel@tonic-gate 	for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
7537c478bd9Sstevel@tonic-gate 		if (match_substr(name, *p)) {
7547c478bd9Sstevel@tonic-gate 			name += strlen(*p);
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	if (i == 0)
7597c478bd9Sstevel@tonic-gate 		return (0);
7607c478bd9Sstevel@tonic-gate 	skip_digits(name);
7617c478bd9Sstevel@tonic-gate 	return (*name == 0);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * Map a conventional name into the internal canonical form:
7677c478bd9Sstevel@tonic-gate  *
7687c478bd9Sstevel@tonic-gate  *	/dev/rdsk/c0t0d0s0 -> c0t0d0
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate void
7717c478bd9Sstevel@tonic-gate canonicalize_name(dst, src)
7727c478bd9Sstevel@tonic-gate 	char	*dst;
7737c478bd9Sstevel@tonic-gate 	char	*src;
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	char	*s;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * Copy from the 'c' to the end to the destination string...
7797c478bd9Sstevel@tonic-gate 	 */
7807c478bd9Sstevel@tonic-gate 	s = strchr(src, 'c');
7817c478bd9Sstevel@tonic-gate 	if (s != NULL) {
7827c478bd9Sstevel@tonic-gate 		(void) strcpy(dst, s);
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * Remove the trailing slice (partition) reference
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 		s = dst + strlen(dst) - 2;
7877c478bd9Sstevel@tonic-gate 		if (*s == 's') {
7887c478bd9Sstevel@tonic-gate 			*s = 0;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	} else {
7917c478bd9Sstevel@tonic-gate 		*dst = 0;	/* be tolerant of garbage input */
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate  * Return true if we find an occurance of s2 at the
7987c478bd9Sstevel@tonic-gate  * beginning of s1.  We don't have to match all of
7997c478bd9Sstevel@tonic-gate  * s1, but we do have to match all of s2
8007c478bd9Sstevel@tonic-gate  */
8017c478bd9Sstevel@tonic-gate int
8027c478bd9Sstevel@tonic-gate match_substr(s1, s2)
8037c478bd9Sstevel@tonic-gate 	char    *s1;
8047c478bd9Sstevel@tonic-gate 	char    *s2;
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	while (*s2 != 0) {
8077c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
8087c478bd9Sstevel@tonic-gate 		return (0);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	return (1);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Dump a structure in hexadecimal, for diagnostic purposes
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate #define	BYTES_PER_LINE		16
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate void
8217c478bd9Sstevel@tonic-gate dump(hdr, src, nbytes, format)
8227c478bd9Sstevel@tonic-gate 	char	*hdr;
8237c478bd9Sstevel@tonic-gate 	caddr_t	src;
8247c478bd9Sstevel@tonic-gate 	int	nbytes;
8257c478bd9Sstevel@tonic-gate 	int	format;
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	int	i;
8287c478bd9Sstevel@tonic-gate 	int	n;
8297c478bd9Sstevel@tonic-gate 	char	*p;
8307c478bd9Sstevel@tonic-gate 	char	s[256];
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	assert(format == HEX_ONLY || format == HEX_ASCII);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	(void) strcpy(s, hdr);
8357c478bd9Sstevel@tonic-gate 	for (p = s; *p; p++) {
8367c478bd9Sstevel@tonic-gate 		*p = ' ';
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	p = hdr;
8407c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
8417c478bd9Sstevel@tonic-gate 		err_print("%s", p);
8427c478bd9Sstevel@tonic-gate 		p = s;
8437c478bd9Sstevel@tonic-gate 		n = min(nbytes, BYTES_PER_LINE);
8447c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
8457c478bd9Sstevel@tonic-gate 			err_print("%02x ", src[i] & 0xff);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		if (format == HEX_ASCII) {
8487c478bd9Sstevel@tonic-gate 			for (i = BYTES_PER_LINE-n; i > 0; i--) {
8497c478bd9Sstevel@tonic-gate 				err_print("   ");
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 			err_print("    ");
8527c478bd9Sstevel@tonic-gate 			for (i = 0; i < n; i++) {
8537c478bd9Sstevel@tonic-gate 				err_print("%c",
8547c478bd9Sstevel@tonic-gate 					isprint(src[i]) ? src[i] : '.');
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate 		}
8577c478bd9Sstevel@tonic-gate 		err_print("\n");
8587c478bd9Sstevel@tonic-gate 		nbytes -= n;
8597c478bd9Sstevel@tonic-gate 		src += n;
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate float
8657c478bd9Sstevel@tonic-gate bn2mb(uint64_t nblks)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	float	n;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	n = (float)nblks / 1024.0;
870*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	return ((n / 1024.0) * cur_blksz);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 
874342440ecSPrasad Singamsetty diskaddr_t
8757c478bd9Sstevel@tonic-gate mb2bn(float mb)
8767c478bd9Sstevel@tonic-gate {
877342440ecSPrasad Singamsetty 	diskaddr_t	n;
8787c478bd9Sstevel@tonic-gate 
879*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	n = (diskaddr_t)(mb * 1024.0 * (1024.0 / cur_blksz));
8807c478bd9Sstevel@tonic-gate 	return (n);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate float
8847c478bd9Sstevel@tonic-gate bn2gb(uint64_t nblks)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	float	n;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	n = (float)nblks / (1024.0 * 1024.0);
889*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	return ((n/1024.0) * cur_blksz);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate float
8947c478bd9Sstevel@tonic-gate bn2tb(uint64_t nblks)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	float	n;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
899*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	return ((n/1024.0) * cur_blksz);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
902342440ecSPrasad Singamsetty diskaddr_t
9037c478bd9Sstevel@tonic-gate gb2bn(float gb)
9047c478bd9Sstevel@tonic-gate {
905342440ecSPrasad Singamsetty 	diskaddr_t	n;
9067c478bd9Sstevel@tonic-gate 
907*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	n = (diskaddr_t)(gb * 1024.0 * 1024.0 * (1024.0 / cur_blksz));
9087c478bd9Sstevel@tonic-gate 	return (n);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate  * This routine finds out the number of lines (rows) in a terminal
9137c478bd9Sstevel@tonic-gate  * window. The default value of TTY_LINES is returned on error.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate int
9167c478bd9Sstevel@tonic-gate get_tty_lines()
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	int	tty_lines = TTY_LINES;
9197c478bd9Sstevel@tonic-gate 	struct	winsize	winsize;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
9227c478bd9Sstevel@tonic-gate 		/*
9237c478bd9Sstevel@tonic-gate 		 * We have a real terminal for std input and output
9247c478bd9Sstevel@tonic-gate 		 */
9257c478bd9Sstevel@tonic-gate 		winsize.ws_row = 0;
9267c478bd9Sstevel@tonic-gate 		if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
9277c478bd9Sstevel@tonic-gate 			if (winsize.ws_row > 2) {
9287c478bd9Sstevel@tonic-gate 				/*
9297c478bd9Sstevel@tonic-gate 				 * Should be atleast 2 lines, for division
9307c478bd9Sstevel@tonic-gate 				 * by (tty_lines - 1, tty_lines - 2) to work.
9317c478bd9Sstevel@tonic-gate 				 */
9327c478bd9Sstevel@tonic-gate 				tty_lines = winsize.ws_row;
9337c478bd9Sstevel@tonic-gate 			}
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	return (tty_lines);
9377c478bd9Sstevel@tonic-gate }
938