import less(1)
[unleashed/tickless.git] / usr / src / lib / fm / libdiskstatus / common / ds_util.c
blobfe59b128019d041441d77fd34fdd699a8a14cd9f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <ctype.h>
29 #include <libdiskstatus.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "ds_impl.h"
36 boolean_t ds_debug;
38 /*PRINTFLIKE1*/
39 void
40 dprintf(const char *fmt, ...)
42 va_list ap;
44 if (!ds_debug)
45 return;
47 va_start(ap, fmt);
48 (void) vprintf(fmt, ap);
49 va_end(ap);
52 void
53 ddump(const char *label, const void *data, size_t length)
55 int byte_count;
56 int i;
57 #define LINEBUFLEN 128
58 char linebuf[LINEBUFLEN];
59 char *linep;
60 int bufleft, len;
61 const char *start = data;
63 if (!ds_debug)
64 return;
66 if (label != NULL)
67 dprintf("%s\n", label);
69 linep = linebuf;
70 bufleft = LINEBUFLEN;
72 for (byte_count = 0; byte_count < length; byte_count += i) {
74 (void) snprintf(linep, bufleft, "0x%08x ", byte_count);
75 len = strlen(linep);
76 bufleft -= len;
77 linep += len;
80 * Inner loop processes 16 bytes at a time, or less
81 * if we have less than 16 bytes to go
83 for (i = 0; (i < 16) && ((byte_count + i) < length); i++) {
84 (void) snprintf(linep, bufleft, "%02X", (unsigned int)
85 (unsigned char) start[byte_count + i]);
87 len = strlen(linep);
88 bufleft -= len;
89 linep += len;
91 if (bufleft >= 2) {
92 if (i == 7)
93 *linep = '-';
94 else
95 *linep = ' ';
97 --bufleft;
98 ++linep;
103 * If i is less than 16, then we had less than 16 bytes
104 * written to the output. We need to fixup the alignment
105 * to allow the "text" output to be aligned
107 if (i < 16) {
108 int numspaces = (16 - i) * 3;
109 while (numspaces-- > 0) {
110 if (bufleft >= 2) {
111 *linep = ' ';
112 --bufleft;
113 linep++;
118 if (bufleft >= 2) {
119 *linep = ' ';
120 --bufleft;
121 ++linep;
124 for (i = 0; (i < 16) && ((byte_count + i) < length); i++) {
125 int subscript = byte_count + i;
126 char ch = (isprint(start[subscript]) ?
127 start[subscript] : '.');
129 if (bufleft >= 2) {
130 *linep = ch;
131 --bufleft;
132 ++linep;
136 linebuf[LINEBUFLEN - bufleft] = 0;
138 dprintf("%s\n", linebuf);
140 linep = linebuf;
141 bufleft = LINEBUFLEN;
146 const char *
147 disk_status_errmsg(int error)
149 switch (error) {
150 case EDS_NOMEM:
151 return ("memory allocation failure");
152 case EDS_CANT_OPEN:
153 return ("failed to open device");
154 case EDS_NO_TRANSPORT:
155 return ("no supported communication protocol");
156 case EDS_NOT_SUPPORTED:
157 return ("disk status information not supported");
158 case EDS_NOT_SIMULATOR:
159 return ("not a valid simulator file");
160 case EDS_IO:
161 return ("I/O error from device");
162 default:
163 return ("unknown error");
168 ds_set_errno(disk_status_t *dsp, int error)
170 dsp->ds_error = error;
171 return (-1);