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