10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57563SPrasad.Singamsetty@Sun.COM * Common Development and Distribution License (the "License").
67563SPrasad.Singamsetty@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12311SShengliang.Zhang@Sun.COM * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * This file contains miscellaneous routines.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate #include "global.h"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <malloc.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <sys/ioctl.h>
380Sstevel@tonic-gate #include <sys/fcntl.h>
390Sstevel@tonic-gate #include <sys/time.h>
400Sstevel@tonic-gate #include <ctype.h>
410Sstevel@tonic-gate #include <termio.h>
420Sstevel@tonic-gate #include "misc.h"
430Sstevel@tonic-gate #include "analyze.h"
440Sstevel@tonic-gate #include "label.h"
450Sstevel@tonic-gate #include "startup.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate #ifdef __STDC__
480Sstevel@tonic-gate
490Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
500Sstevel@tonic-gate static void cleanup(int sig);
510Sstevel@tonic-gate
520Sstevel@tonic-gate #else /* __STDC__ */
530Sstevel@tonic-gate
540Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
550Sstevel@tonic-gate static void cleanup();
560Sstevel@tonic-gate
570Sstevel@tonic-gate #endif /* __STDC__ */
580Sstevel@tonic-gate
590Sstevel@tonic-gate struct env *current_env = NULL; /* ptr to current environment */
600Sstevel@tonic-gate static int stop_pending = 0; /* ctrl-Z is pending */
610Sstevel@tonic-gate struct ttystate ttystate; /* tty info */
620Sstevel@tonic-gate static int aborting = 0; /* in process of aborting */
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * For 4.x, limit the choices of valid disk names to this set.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static char *disk_4x_identifiers[] = { "sd", "id"};
680Sstevel@tonic-gate #define N_DISK_4X_IDS (sizeof (disk_4x_identifiers)/sizeof (char *))
690Sstevel@tonic-gate
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * This is the list of legal inputs for all yes/no questions.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate char *confirm_list[] = {
750Sstevel@tonic-gate "yes",
760Sstevel@tonic-gate "no",
770Sstevel@tonic-gate NULL,
780Sstevel@tonic-gate };
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * This routine is a wrapper for malloc. It allocates pre-zeroed space,
820Sstevel@tonic-gate * and checks the return value so the caller doesn't have to.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate void *
zalloc(count)850Sstevel@tonic-gate zalloc(count)
860Sstevel@tonic-gate int count;
870Sstevel@tonic-gate {
880Sstevel@tonic-gate void *ptr;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if ((ptr = (void *) calloc(1, (unsigned)count)) == NULL) {
910Sstevel@tonic-gate err_print("Error: unable to calloc more space.\n");
920Sstevel@tonic-gate fullabort();
930Sstevel@tonic-gate }
940Sstevel@tonic-gate return (ptr);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * This routine is a wrapper for realloc. It reallocates the given
990Sstevel@tonic-gate * space, and checks the return value so the caller doesn't have to.
1000Sstevel@tonic-gate * Note that the any space added by this call is NOT necessarily
1010Sstevel@tonic-gate * zeroed.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate void *
rezalloc(ptr,count)1040Sstevel@tonic-gate rezalloc(ptr, count)
1050Sstevel@tonic-gate void *ptr;
1060Sstevel@tonic-gate int count;
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate void *new_ptr;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if ((new_ptr = (void *) realloc((char *)ptr,
1120Sstevel@tonic-gate (unsigned)count)) == NULL) {
1130Sstevel@tonic-gate err_print("Error: unable to realloc more space.\n");
1140Sstevel@tonic-gate fullabort();
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate return (new_ptr);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * This routine is a wrapper for free.
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate void
destroy_data(data)1230Sstevel@tonic-gate destroy_data(data)
1240Sstevel@tonic-gate char *data;
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate free((char *)data);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate #ifdef not
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * This routine takes the space number returned by an ioctl call and
1320Sstevel@tonic-gate * returns a mnemonic name for that space.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate char *
space2str(space)1350Sstevel@tonic-gate space2str(space)
1360Sstevel@tonic-gate uint_t space;
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate char *name;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate switch (space&SP_BUSMASK) {
1410Sstevel@tonic-gate case SP_VIRTUAL:
1420Sstevel@tonic-gate name = "virtual";
1430Sstevel@tonic-gate break;
1440Sstevel@tonic-gate case SP_OBMEM:
1450Sstevel@tonic-gate name = "obmem";
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate case SP_OBIO:
1480Sstevel@tonic-gate name = "obio";
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate case SP_MBMEM:
1510Sstevel@tonic-gate name = "mbmem";
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate case SP_MBIO:
1540Sstevel@tonic-gate name = "mbio";
1550Sstevel@tonic-gate break;
1560Sstevel@tonic-gate default:
1570Sstevel@tonic-gate err_print("Error: unknown address space type encountered.\n");
1580Sstevel@tonic-gate fullabort();
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate return (name);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate #endif /* not */
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * This routine asks the user the given yes/no question and returns
1660Sstevel@tonic-gate * the response.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate int
check(question)1690Sstevel@tonic-gate check(question)
1700Sstevel@tonic-gate char *question;
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate int answer;
1730Sstevel@tonic-gate u_ioparam_t ioparam;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * If we are running out of a command file, assume a yes answer.
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate if (option_f)
1790Sstevel@tonic-gate return (0);
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate * Ask the user.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate ioparam.io_charlist = confirm_list;
1840Sstevel@tonic-gate answer = input(FIO_MSTR, question, '?', &ioparam,
1850Sstevel@tonic-gate (int *)NULL, DATA_INPUT);
1860Sstevel@tonic-gate return (answer);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * This routine aborts the current command. It is called by a ctrl-C
1910Sstevel@tonic-gate * interrupt and also under certain error conditions.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate /*ARGSUSED*/
1940Sstevel@tonic-gate void
cmdabort(sig)1950Sstevel@tonic-gate cmdabort(sig)
1960Sstevel@tonic-gate int sig;
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * If there is no usable saved environment, gracefully exit. This
2010Sstevel@tonic-gate * allows the user to interrupt the program even when input is from
2020Sstevel@tonic-gate * a file, or if there is no current menu, like at the "Select disk:"
2030Sstevel@tonic-gate * prompt.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate if (current_env == NULL || !(current_env->flags & ENV_USE))
2060Sstevel@tonic-gate fullabort();
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * If we are in a critical zone, note the attempt and return.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate if (current_env->flags & ENV_CRITICAL) {
2120Sstevel@tonic-gate current_env->flags |= ENV_ABORT;
2130Sstevel@tonic-gate return;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * All interruptions when we are running out of a command file
2170Sstevel@tonic-gate * cause the program to gracefully exit.
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate if (option_f)
2200Sstevel@tonic-gate fullabort();
2210Sstevel@tonic-gate fmt_print("\n");
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * Clean up any state left by the interrupted command.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate cleanup(sig);
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * Jump to the saved environment.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate longjmp(current_env->env, 0);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * This routine implements the ctrl-Z suspend mechanism. It is called
2340Sstevel@tonic-gate * when a suspend signal is received.
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate /*ARGSUSED*/
2370Sstevel@tonic-gate void
onsusp(sig)2380Sstevel@tonic-gate onsusp(sig)
2390Sstevel@tonic-gate int sig;
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate int fix_term;
2420Sstevel@tonic-gate #ifdef NOT_DEF
2430Sstevel@tonic-gate sigset_t sigmask;
2440Sstevel@tonic-gate #endif /* NOT_DEF */
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * If we are in a critical zone, note the attempt and return.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
2500Sstevel@tonic-gate stop_pending = 1;
2510Sstevel@tonic-gate return;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * If the terminal is mucked up, note that we will need to
2550Sstevel@tonic-gate * re-muck it when we start up again.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate fix_term = ttystate.ttyflags;
2580Sstevel@tonic-gate fmt_print("\n");
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * Clean up any state left by the interrupted command.
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate cleanup(sig);
2630Sstevel@tonic-gate #ifdef NOT_DEF
2640Sstevel@tonic-gate /* Investigate whether all this is necessary */
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Stop intercepting the suspend signal, then send ourselves one
2670Sstevel@tonic-gate * to cause us to stop.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate sigmask.sigbits[0] = (ulong_t)0xffffffff;
2700Sstevel@tonic-gate if (sigprocmask(SIG_SETMASK, &sigmask, (sigset_t *)NULL) == -1)
2710Sstevel@tonic-gate err_print("sigprocmask failed %d\n", errno);
2720Sstevel@tonic-gate #endif /* NOT_DEF */
2730Sstevel@tonic-gate (void) signal(SIGTSTP, SIG_DFL);
2740Sstevel@tonic-gate (void) kill(0, SIGTSTP);
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * PC stops here
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate * We are started again. Set us up to intercept the suspend
2800Sstevel@tonic-gate * signal once again.
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate (void) signal(SIGTSTP, onsusp);
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * Re-muck the terminal if necessary.
2850Sstevel@tonic-gate */
2860Sstevel@tonic-gate if (fix_term & TTY_ECHO_OFF)
2870Sstevel@tonic-gate echo_off();
2880Sstevel@tonic-gate if (fix_term & TTY_CBREAK_ON)
2890Sstevel@tonic-gate charmode_on();
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * This routine implements the timing function used during long-term
2940Sstevel@tonic-gate * disk operations (e.g. formatting). It is called when an alarm signal
2950Sstevel@tonic-gate * is received.
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate /*ARGSUSED*/
2980Sstevel@tonic-gate void
onalarm(sig)2990Sstevel@tonic-gate onalarm(sig)
3000Sstevel@tonic-gate int sig;
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * This routine gracefully exits the program.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate void
fullabort()3090Sstevel@tonic-gate fullabort()
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate fmt_print("\n");
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Clean up any state left by an interrupted command.
3150Sstevel@tonic-gate * Avoid infinite loops caused by a clean-up
3160Sstevel@tonic-gate * routine failing again...
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate if (!aborting) {
3190Sstevel@tonic-gate aborting = 1;
3200Sstevel@tonic-gate cleanup(SIGKILL);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate exit(1);
3230Sstevel@tonic-gate /*NOTREACHED*/
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * This routine cleans up the state of the world. It is a hodge-podge
3280Sstevel@tonic-gate * of kludges to allow us to interrupt commands whenever possible.
3290Sstevel@tonic-gate *
3300Sstevel@tonic-gate * Some cleanup actions may depend on the type of signal.
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate static void
cleanup(int sig)3330Sstevel@tonic-gate cleanup(int sig)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate * Lock out interrupts to avoid recursion.
3380Sstevel@tonic-gate */
3390Sstevel@tonic-gate enter_critical();
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * Fix up the tty if necessary.
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate if (ttystate.ttyflags & TTY_CBREAK_ON) {
3440Sstevel@tonic-gate charmode_off();
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate if (ttystate.ttyflags & TTY_ECHO_OFF) {
3470Sstevel@tonic-gate echo_on();
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * If the defect list is dirty, write it out.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate if (cur_list.flags & LIST_DIRTY) {
3540Sstevel@tonic-gate cur_list.flags = 0;
3550Sstevel@tonic-gate if (!EMBEDDED_SCSI)
3560Sstevel@tonic-gate write_deflist(&cur_list);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * If the label is dirty, write it out.
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate if (cur_flags & LABEL_DIRTY) {
3620Sstevel@tonic-gate cur_flags &= ~LABEL_DIRTY;
3630Sstevel@tonic-gate (void) write_label();
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate * If we are logging and just interrupted a scan, print out
3670Sstevel@tonic-gate * some summary info to the log file.
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate if (log_file && scan_cur_block >= 0) {
3700Sstevel@tonic-gate pr_dblock(log_print, scan_cur_block);
3710Sstevel@tonic-gate log_print("\n");
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate if (scan_blocks_fixed >= 0)
3740Sstevel@tonic-gate fmt_print("Total of %lld defective blocks repaired.\n",
3750Sstevel@tonic-gate scan_blocks_fixed);
3760Sstevel@tonic-gate if (sig != SIGSTOP) { /* Don't reset on suspend (converted to stop) */
3770Sstevel@tonic-gate scan_cur_block = scan_blocks_fixed = -1;
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate exit_critical();
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * This routine causes the program to enter a critical zone. Within the
3840Sstevel@tonic-gate * critical zone, no interrupts are allowed. Note that calls to this
3850Sstevel@tonic-gate * routine for the same environment do NOT nest, so there is not
3860Sstevel@tonic-gate * necessarily pairing between calls to enter_critical() and exit_critical().
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate void
enter_critical()3890Sstevel@tonic-gate enter_critical()
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate * If there is no saved environment, interrupts will be ignored.
3940Sstevel@tonic-gate */
3950Sstevel@tonic-gate if (current_env == NULL)
3960Sstevel@tonic-gate return;
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * Mark the environment to be in a critical zone.
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate current_env->flags |= ENV_CRITICAL;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate * This routine causes the program to exit a critical zone. Note that
4050Sstevel@tonic-gate * calls to enter_critical() for the same environment do NOT nest, so
4060Sstevel@tonic-gate * one call to exit_critical() will erase any number of such calls.
4070Sstevel@tonic-gate */
4080Sstevel@tonic-gate void
exit_critical()4090Sstevel@tonic-gate exit_critical()
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate * If there is a saved environment, mark it to be non-critical.
4140Sstevel@tonic-gate */
4150Sstevel@tonic-gate if (current_env != NULL)
4160Sstevel@tonic-gate current_env->flags &= ~ENV_CRITICAL;
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate * If there is a stop pending, execute the stop.
4190Sstevel@tonic-gate */
4200Sstevel@tonic-gate if (stop_pending) {
4210Sstevel@tonic-gate stop_pending = 0;
4220Sstevel@tonic-gate onsusp(SIGSTOP);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * If there is an abort pending, execute the abort.
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate if (current_env == NULL)
4280Sstevel@tonic-gate return;
4290Sstevel@tonic-gate if (current_env->flags & ENV_ABORT) {
4300Sstevel@tonic-gate current_env->flags &= ~ENV_ABORT;
4310Sstevel@tonic-gate cmdabort(SIGINT);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * This routine turns off echoing on the controlling tty for the program.
4370Sstevel@tonic-gate */
4380Sstevel@tonic-gate void
echo_off()4390Sstevel@tonic-gate echo_off()
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate * Open the tty and store the file pointer for later.
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
4450Sstevel@tonic-gate if ((ttystate.ttyfile = open("/dev/tty",
4467563SPrasad.Singamsetty@Sun.COM O_RDWR | O_NDELAY)) < 0) {
4470Sstevel@tonic-gate err_print("Unable to open /dev/tty.\n");
4480Sstevel@tonic-gate fullabort();
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate * Get the parameters for the tty, turn off echoing and set them.
4530Sstevel@tonic-gate */
4540Sstevel@tonic-gate if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
4550Sstevel@tonic-gate err_print("Unable to get tty parameters.\n");
4560Sstevel@tonic-gate fullabort();
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate ttystate.ttystate.c_lflag &= ~ECHO;
4590Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4600Sstevel@tonic-gate err_print("Unable to set tty to echo off state.\n");
4610Sstevel@tonic-gate fullabort();
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate * Remember that we've successfully turned
4660Sstevel@tonic-gate * ECHO mode off, so we know to fix it later.
4670Sstevel@tonic-gate */
4680Sstevel@tonic-gate ttystate.ttyflags |= TTY_ECHO_OFF;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * This routine turns on echoing on the controlling tty for the program.
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate void
echo_on()4750Sstevel@tonic-gate echo_on()
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate * Using the saved parameters, turn echoing on and set them.
4800Sstevel@tonic-gate */
4810Sstevel@tonic-gate ttystate.ttystate.c_lflag |= ECHO;
4820Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4830Sstevel@tonic-gate err_print("Unable to set tty to echo on state.\n");
4840Sstevel@tonic-gate fullabort();
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * Close the tty and mark it ok again.
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate ttystate.ttyflags &= ~TTY_ECHO_OFF;
4900Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
4910Sstevel@tonic-gate (void) close(ttystate.ttyfile);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate * This routine turns off single character entry mode for tty.
4970Sstevel@tonic-gate */
4980Sstevel@tonic-gate void
charmode_on()4990Sstevel@tonic-gate charmode_on()
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate * If tty unopened, open the tty and store the file pointer for later.
5040Sstevel@tonic-gate */
5050Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
5060Sstevel@tonic-gate if ((ttystate.ttyfile = open("/dev/tty",
5077563SPrasad.Singamsetty@Sun.COM O_RDWR | O_NDELAY)) < 0) {
5080Sstevel@tonic-gate err_print("Unable to open /dev/tty.\n");
5090Sstevel@tonic-gate fullabort();
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate * Get the parameters for the tty, turn on char mode.
5140Sstevel@tonic-gate */
5150Sstevel@tonic-gate if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
5160Sstevel@tonic-gate err_print("Unable to get tty parameters.\n");
5170Sstevel@tonic-gate fullabort();
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
5200Sstevel@tonic-gate ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate ttystate.ttystate.c_lflag &= ~ICANON;
5230Sstevel@tonic-gate ttystate.ttystate.c_cc[VMIN] = 1;
5240Sstevel@tonic-gate ttystate.ttystate.c_cc[VTIME] = 0;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5270Sstevel@tonic-gate err_print("Unable to set tty to cbreak on state.\n");
5280Sstevel@tonic-gate fullabort();
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate /*
5320Sstevel@tonic-gate * Remember that we've successfully turned
5330Sstevel@tonic-gate * CBREAK mode on, so we know to fix it later.
5340Sstevel@tonic-gate */
5350Sstevel@tonic-gate ttystate.ttyflags |= TTY_CBREAK_ON;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate * This routine turns on single character entry mode for tty.
5400Sstevel@tonic-gate * Note, this routine must be called before echo_on.
5410Sstevel@tonic-gate */
5420Sstevel@tonic-gate void
charmode_off()5430Sstevel@tonic-gate charmode_off()
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate * Using the saved parameters, turn char mode on.
5480Sstevel@tonic-gate */
5490Sstevel@tonic-gate ttystate.ttystate.c_lflag |= ICANON;
5500Sstevel@tonic-gate ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
5510Sstevel@tonic-gate ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
5520Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5530Sstevel@tonic-gate err_print("Unable to set tty to cbreak off state.\n");
5540Sstevel@tonic-gate fullabort();
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate * Close the tty and mark it ok again.
5580Sstevel@tonic-gate */
5590Sstevel@tonic-gate ttystate.ttyflags &= ~TTY_CBREAK_ON;
5600Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
5610Sstevel@tonic-gate (void) close(ttystate.ttyfile);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate * Allocate space for and return a pointer to a string
5680Sstevel@tonic-gate * on the stack. If the string is null, create
5690Sstevel@tonic-gate * an empty string.
5700Sstevel@tonic-gate * Use destroy_data() to free when no longer used.
5710Sstevel@tonic-gate */
5720Sstevel@tonic-gate char *
alloc_string(s)5730Sstevel@tonic-gate alloc_string(s)
5740Sstevel@tonic-gate char *s;
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate char *ns;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate if (s == (char *)NULL) {
5790Sstevel@tonic-gate ns = (char *)zalloc(1);
5800Sstevel@tonic-gate } else {
5810Sstevel@tonic-gate ns = (char *)zalloc(strlen(s) + 1);
5820Sstevel@tonic-gate (void) strcpy(ns, s);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate return (ns);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate * This function can be used to build up an array of strings
5910Sstevel@tonic-gate * dynamically, with a trailing NULL to terminate the list.
5920Sstevel@tonic-gate *
5930Sstevel@tonic-gate * Parameters:
5940Sstevel@tonic-gate * argvlist: a pointer to the base of the current list.
5950Sstevel@tonic-gate * does not have to be initialized.
5960Sstevel@tonic-gate * size: pointer to an integer, indicating the number
5970Sstevel@tonic-gate * of string installed in the list. Must be
5980Sstevel@tonic-gate * initialized to zero.
5990Sstevel@tonic-gate * alloc: pointer to an integer, indicating the amount
6000Sstevel@tonic-gate * of space allocated. Must be initialized to
6010Sstevel@tonic-gate * zero. For efficiency, we allocate the list
6020Sstevel@tonic-gate * in chunks and use it piece-by-piece.
6030Sstevel@tonic-gate * str: the string to be inserted in the list.
6040Sstevel@tonic-gate * A copy of the string is malloc'ed, and
6050Sstevel@tonic-gate * appended at the end of the list.
6060Sstevel@tonic-gate * Returns:
6070Sstevel@tonic-gate * a pointer to the possibly-moved argvlist.
6080Sstevel@tonic-gate *
6090Sstevel@tonic-gate * No attempt to made to free unused memory when the list is
6100Sstevel@tonic-gate * completed, although this would not be hard to do. For
6110Sstevel@tonic-gate * reasonably small lists, this should suffice.
6120Sstevel@tonic-gate */
6130Sstevel@tonic-gate #define INITIAL_LISTSIZE 32
6140Sstevel@tonic-gate #define INCR_LISTSIZE 32
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate char **
build_argvlist(argvlist,size,alloc,str)6170Sstevel@tonic-gate build_argvlist(argvlist, size, alloc, str)
6180Sstevel@tonic-gate char **argvlist;
6190Sstevel@tonic-gate int *size;
6200Sstevel@tonic-gate int *alloc;
6210Sstevel@tonic-gate char *str;
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate if (*size + 2 > *alloc) {
6240Sstevel@tonic-gate if (*alloc == 0) {
6250Sstevel@tonic-gate *alloc = INITIAL_LISTSIZE;
6260Sstevel@tonic-gate argvlist = (char **)
6270Sstevel@tonic-gate zalloc(sizeof (char *) * (*alloc));
6280Sstevel@tonic-gate } else {
6290Sstevel@tonic-gate *alloc += INCR_LISTSIZE;
6300Sstevel@tonic-gate argvlist = (char **)
6310Sstevel@tonic-gate rezalloc((void *) argvlist,
6320Sstevel@tonic-gate sizeof (char *) * (*alloc));
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate argvlist[*size] = alloc_string(str);
6370Sstevel@tonic-gate *size += 1;
6380Sstevel@tonic-gate argvlist[*size] = NULL;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate return (argvlist);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate * Useful parsing macros
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate #define must_be(s, c) if (*s++ != c) return (0)
6480Sstevel@tonic-gate #define skip_digits(s) while (isdigit(*s)) s++
6490Sstevel@tonic-gate /* Parsing macro below is created to handle fabric devices which contains */
6500Sstevel@tonic-gate /* upper hex digits like c2t210000203708B8CEd0s0. */
6510Sstevel@tonic-gate /* To get the target id(tid) the digit and hex upper digit need to */
6520Sstevel@tonic-gate /* be processed. */
6530Sstevel@tonic-gate #define skip_digit_or_hexupper(s) while (isdigit(*s) || \
6540Sstevel@tonic-gate (isxdigit(*s) && isupper(*s))) s++
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate * Return true if a device name matches the conventions
6580Sstevel@tonic-gate * for the particular system.
6590Sstevel@tonic-gate */
6600Sstevel@tonic-gate int
conventional_name(char * name)6610Sstevel@tonic-gate conventional_name(char *name)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate must_be(name, 'c');
6640Sstevel@tonic-gate skip_digits(name);
6650Sstevel@tonic-gate if (*name == 't') {
6660Sstevel@tonic-gate name++;
6670Sstevel@tonic-gate skip_digit_or_hexupper(name);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate must_be(name, 'd');
6700Sstevel@tonic-gate skip_digits(name);
6710Sstevel@tonic-gate must_be(name, 's');
6720Sstevel@tonic-gate skip_digits(name);
6730Sstevel@tonic-gate return (*name == 0);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate
676*12311SShengliang.Zhang@Sun.COM #ifdef i386
677*12311SShengliang.Zhang@Sun.COM /*
678*12311SShengliang.Zhang@Sun.COM * Return true if a device name match the emc powerpath name scheme:
679*12311SShengliang.Zhang@Sun.COM * emcpowerN[a-p,p0,p1,p2,p3,p4]
680*12311SShengliang.Zhang@Sun.COM */
681*12311SShengliang.Zhang@Sun.COM int
emcpower_name(char * name)682*12311SShengliang.Zhang@Sun.COM emcpower_name(char *name)
683*12311SShengliang.Zhang@Sun.COM {
684*12311SShengliang.Zhang@Sun.COM char *emcp = "emcpower";
685*12311SShengliang.Zhang@Sun.COM char *devp = "/dev/dsk";
686*12311SShengliang.Zhang@Sun.COM char *rdevp = "/dev/rdsk";
687*12311SShengliang.Zhang@Sun.COM
688*12311SShengliang.Zhang@Sun.COM if (strncmp(devp, name, strlen(devp)) == 0) {
689*12311SShengliang.Zhang@Sun.COM name += strlen(devp) + 1;
690*12311SShengliang.Zhang@Sun.COM } else if (strncmp(rdevp, name, strlen(rdevp)) == 0) {
691*12311SShengliang.Zhang@Sun.COM name += strlen(rdevp) + 1;
692*12311SShengliang.Zhang@Sun.COM }
693*12311SShengliang.Zhang@Sun.COM if (strncmp(emcp, name, strlen(emcp)) == 0) {
694*12311SShengliang.Zhang@Sun.COM name += strlen(emcp);
695*12311SShengliang.Zhang@Sun.COM if (isdigit(*name)) {
696*12311SShengliang.Zhang@Sun.COM skip_digits(name);
697*12311SShengliang.Zhang@Sun.COM if ((*name >= 'a') && (*name <= 'p')) {
698*12311SShengliang.Zhang@Sun.COM name ++;
699*12311SShengliang.Zhang@Sun.COM if ((*name >= '0') && (*name <= '4')) {
700*12311SShengliang.Zhang@Sun.COM name++;
701*12311SShengliang.Zhang@Sun.COM }
702*12311SShengliang.Zhang@Sun.COM }
703*12311SShengliang.Zhang@Sun.COM return (*name == '\0');
704*12311SShengliang.Zhang@Sun.COM }
705*12311SShengliang.Zhang@Sun.COM }
706*12311SShengliang.Zhang@Sun.COM return (0);
707*12311SShengliang.Zhang@Sun.COM }
708*12311SShengliang.Zhang@Sun.COM #endif
709*12311SShengliang.Zhang@Sun.COM
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * Return true if a device name matches the intel physical name conventions
7120Sstevel@tonic-gate * for the particular system.
7130Sstevel@tonic-gate */
7140Sstevel@tonic-gate int
fdisk_physical_name(char * name)7150Sstevel@tonic-gate fdisk_physical_name(char *name)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate must_be(name, 'c');
7180Sstevel@tonic-gate skip_digits(name);
7190Sstevel@tonic-gate if (*name == 't') {
7200Sstevel@tonic-gate name++;
7210Sstevel@tonic-gate skip_digit_or_hexupper(name);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate must_be(name, 'd');
7240Sstevel@tonic-gate skip_digits(name);
7250Sstevel@tonic-gate must_be(name, 'p');
7260Sstevel@tonic-gate skip_digits(name);
7270Sstevel@tonic-gate return (*name == 0);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate * Return true if a device name matches the conventions
7320Sstevel@tonic-gate * for a "whole disk" name for the particular system.
7330Sstevel@tonic-gate * The name in this case must match exactly that which
7340Sstevel@tonic-gate * would appear in the device directory itself.
7350Sstevel@tonic-gate */
7360Sstevel@tonic-gate int
whole_disk_name(name)7370Sstevel@tonic-gate whole_disk_name(name)
7380Sstevel@tonic-gate char *name;
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate must_be(name, 'c');
7410Sstevel@tonic-gate skip_digits(name);
7420Sstevel@tonic-gate if (*name == 't') {
7430Sstevel@tonic-gate name++;
7440Sstevel@tonic-gate skip_digit_or_hexupper(name);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate must_be(name, 'd');
7470Sstevel@tonic-gate skip_digits(name);
7480Sstevel@tonic-gate must_be(name, 's');
7490Sstevel@tonic-gate must_be(name, '2');
7500Sstevel@tonic-gate return (*name == 0);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate * Return true if a name is in the internal canonical form
7560Sstevel@tonic-gate */
7570Sstevel@tonic-gate int
canonical_name(name)7580Sstevel@tonic-gate canonical_name(name)
7590Sstevel@tonic-gate char *name;
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate must_be(name, 'c');
7620Sstevel@tonic-gate skip_digits(name);
7630Sstevel@tonic-gate if (*name == 't') {
7640Sstevel@tonic-gate name++;
7650Sstevel@tonic-gate skip_digit_or_hexupper(name);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate must_be(name, 'd');
7680Sstevel@tonic-gate skip_digits(name);
7690Sstevel@tonic-gate return (*name == 0);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate * Return true if a name is in the internal canonical form for 4.x
7750Sstevel@tonic-gate * Used to support 4.x naming conventions under 5.0.
7760Sstevel@tonic-gate */
7770Sstevel@tonic-gate int
canonical4x_name(name)7780Sstevel@tonic-gate canonical4x_name(name)
7790Sstevel@tonic-gate char *name;
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate char **p;
7820Sstevel@tonic-gate int i;
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate p = disk_4x_identifiers;
7850Sstevel@tonic-gate for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
7860Sstevel@tonic-gate if (match_substr(name, *p)) {
7870Sstevel@tonic-gate name += strlen(*p);
7880Sstevel@tonic-gate break;
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate if (i == 0)
7920Sstevel@tonic-gate return (0);
7930Sstevel@tonic-gate skip_digits(name);
7940Sstevel@tonic-gate return (*name == 0);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate /*
7990Sstevel@tonic-gate * Map a conventional name into the internal canonical form:
8000Sstevel@tonic-gate *
8010Sstevel@tonic-gate * /dev/rdsk/c0t0d0s0 -> c0t0d0
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate void
canonicalize_name(dst,src)8040Sstevel@tonic-gate canonicalize_name(dst, src)
8050Sstevel@tonic-gate char *dst;
8060Sstevel@tonic-gate char *src;
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate char *s;
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate * Copy from the 'c' to the end to the destination string...
8120Sstevel@tonic-gate */
8130Sstevel@tonic-gate s = strchr(src, 'c');
8140Sstevel@tonic-gate if (s != NULL) {
8150Sstevel@tonic-gate (void) strcpy(dst, s);
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate * Remove the trailing slice (partition) reference
8180Sstevel@tonic-gate */
8190Sstevel@tonic-gate s = dst + strlen(dst) - 2;
8200Sstevel@tonic-gate if (*s == 's') {
8210Sstevel@tonic-gate *s = 0;
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate } else {
8240Sstevel@tonic-gate *dst = 0; /* be tolerant of garbage input */
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate * Return true if we find an occurance of s2 at the
8310Sstevel@tonic-gate * beginning of s1. We don't have to match all of
8320Sstevel@tonic-gate * s1, but we do have to match all of s2
8330Sstevel@tonic-gate */
8340Sstevel@tonic-gate int
match_substr(s1,s2)8350Sstevel@tonic-gate match_substr(s1, s2)
8360Sstevel@tonic-gate char *s1;
8370Sstevel@tonic-gate char *s2;
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate while (*s2 != 0) {
8400Sstevel@tonic-gate if (*s1++ != *s2++)
8410Sstevel@tonic-gate return (0);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate return (1);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate * Dump a structure in hexadecimal, for diagnostic purposes
8500Sstevel@tonic-gate */
8510Sstevel@tonic-gate #define BYTES_PER_LINE 16
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate void
dump(hdr,src,nbytes,format)8540Sstevel@tonic-gate dump(hdr, src, nbytes, format)
8550Sstevel@tonic-gate char *hdr;
8560Sstevel@tonic-gate caddr_t src;
8570Sstevel@tonic-gate int nbytes;
8580Sstevel@tonic-gate int format;
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate int i;
8610Sstevel@tonic-gate int n;
8620Sstevel@tonic-gate char *p;
8630Sstevel@tonic-gate char s[256];
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate assert(format == HEX_ONLY || format == HEX_ASCII);
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate (void) strcpy(s, hdr);
8680Sstevel@tonic-gate for (p = s; *p; p++) {
8690Sstevel@tonic-gate *p = ' ';
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate p = hdr;
8730Sstevel@tonic-gate while (nbytes > 0) {
8740Sstevel@tonic-gate err_print("%s", p);
8750Sstevel@tonic-gate p = s;
8760Sstevel@tonic-gate n = min(nbytes, BYTES_PER_LINE);
8770Sstevel@tonic-gate for (i = 0; i < n; i++) {
8780Sstevel@tonic-gate err_print("%02x ", src[i] & 0xff);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate if (format == HEX_ASCII) {
8810Sstevel@tonic-gate for (i = BYTES_PER_LINE-n; i > 0; i--) {
8820Sstevel@tonic-gate err_print(" ");
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate err_print(" ");
8850Sstevel@tonic-gate for (i = 0; i < n; i++) {
8860Sstevel@tonic-gate err_print("%c",
8870Sstevel@tonic-gate isprint(src[i]) ? src[i] : '.');
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate err_print("\n");
8910Sstevel@tonic-gate nbytes -= n;
8920Sstevel@tonic-gate src += n;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate float
bn2mb(uint64_t nblks)8980Sstevel@tonic-gate bn2mb(uint64_t nblks)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate float n;
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate n = (float)nblks / 1024.0;
9039889SLarry.Liu@Sun.COM return ((n / 1024.0) * cur_blksz);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate
9077563SPrasad.Singamsetty@Sun.COM diskaddr_t
mb2bn(float mb)9080Sstevel@tonic-gate mb2bn(float mb)
9090Sstevel@tonic-gate {
9107563SPrasad.Singamsetty@Sun.COM diskaddr_t n;
9110Sstevel@tonic-gate
9129889SLarry.Liu@Sun.COM n = (diskaddr_t)(mb * 1024.0 * (1024.0 / cur_blksz));
9130Sstevel@tonic-gate return (n);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate float
bn2gb(uint64_t nblks)9170Sstevel@tonic-gate bn2gb(uint64_t nblks)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate float n;
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate n = (float)nblks / (1024.0 * 1024.0);
9229889SLarry.Liu@Sun.COM return ((n/1024.0) * cur_blksz);
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate float
bn2tb(uint64_t nblks)9270Sstevel@tonic-gate bn2tb(uint64_t nblks)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate float n;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
9329889SLarry.Liu@Sun.COM return ((n/1024.0) * cur_blksz);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate
9357563SPrasad.Singamsetty@Sun.COM diskaddr_t
gb2bn(float gb)9360Sstevel@tonic-gate gb2bn(float gb)
9370Sstevel@tonic-gate {
9387563SPrasad.Singamsetty@Sun.COM diskaddr_t n;
9390Sstevel@tonic-gate
9409889SLarry.Liu@Sun.COM n = (diskaddr_t)(gb * 1024.0 * 1024.0 * (1024.0 / cur_blksz));
9410Sstevel@tonic-gate return (n);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate * This routine finds out the number of lines (rows) in a terminal
9460Sstevel@tonic-gate * window. The default value of TTY_LINES is returned on error.
9470Sstevel@tonic-gate */
9480Sstevel@tonic-gate int
get_tty_lines()9490Sstevel@tonic-gate get_tty_lines()
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate int tty_lines = TTY_LINES;
9520Sstevel@tonic-gate struct winsize winsize;
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate * We have a real terminal for std input and output
9570Sstevel@tonic-gate */
9580Sstevel@tonic-gate winsize.ws_row = 0;
9590Sstevel@tonic-gate if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
9600Sstevel@tonic-gate if (winsize.ws_row > 2) {
9610Sstevel@tonic-gate /*
9620Sstevel@tonic-gate * Should be atleast 2 lines, for division
9630Sstevel@tonic-gate * by (tty_lines - 1, tty_lines - 2) to work.
9640Sstevel@tonic-gate */
9650Sstevel@tonic-gate tty_lines = winsize.ws_row;
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate return (tty_lines);
9700Sstevel@tonic-gate }
971