1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E%	SMI"
7 
8 #include <strings.h>
9 #include <scsi/libscsi.h>
10 
11 static const struct {
12 	char *name;		/* error name */
13 	char *msg;		/* error message */
14 } _libscsi_errstr[] = {
15 	{ "ESCSI_NONE",	"no error" },
16 	{ "ESCSI_NOMEM",	"no memory" },
17 	{ "ESCSI_ZERO_LENGTH","zero-length allocation requested" },
18 	{ "ESCSI_VERSION",	"library version mismatch" },
19 	{ "ESCSI_BADTARGET","invalid target specification" },
20 	{ "ESCSI_BADCMD",	"invalid SCSI command" },
21 	{ "ESCSI_BADENGINE","engine library corrupt" },
22 	{ "ESCSI_NOENGINE",	"engine library not found" },
23 	{ "ESCSI_ENGINE_INIT","engine initialization failed" },
24 	{ "ESCSI_ENGINE_VER","engine version mismatch" },
25 	{ "ESCSI_ENGINE_BADPATH","engine path contains no usable components" },
26 	{ "ESCSI_BADFLAGS",	"incorrect action flags" },
27 	{ "ESCSI_BOGUSFLAGS","unknown flag value" },
28 	{ "ESCSI_BADLENGTH","buffer length overflow" },
29 	{ "ESCSI_NEEDBUF",	"missing required buffer" },
30 	{ "ESCSI_IO",	"I/O operation failed" },
31 	{ "ESCSI_SYS",	"system call failed" },
32 	{ "ESCSI_PERM",	"insufficient permissions" },
33 	{ "ESCSI_RANGE",	"parameter outside valid range" },
34 	{ "ESCSI_NOTSUP",	"operation not supported" },
35 	{ "ESCSI_UNKNOWN",	"error of unknown type" },
36 	{ "ESCSI_INQUIRY_FAILED","initial inquiry command failed" },
37 	{ "ESCSI_MAX",	"maximum libscsi errno value" },
38 };
39 
40 static int _libscsi_nerrno = sizeof (_libscsi_errstr) /
41     sizeof (_libscsi_errstr[0]);
42 
43 const char *
44 libscsi_strerror(libscsi_errno_t err)
45 {
46 	return (err < 0 || err >= _libscsi_nerrno ? "unknown error" :
47 	     _libscsi_errstr[err].msg);
48 }
49 
50 const char *
51 libscsi_errname(libscsi_errno_t err)
52 {
53 	return (err < 0 || err >= _libscsi_nerrno ? NULL :
54 	     _libscsi_errstr[err].name);
55 }
56 
57 libscsi_errno_t
58 libscsi_errcode(const char *name)
59 {
60 	libscsi_errno_t err;
61 
62 	for (err = 0; err < _libscsi_nerrno; err++) {
63 		if (strcmp(name, _libscsi_errstr[err].name) == 0)
64 			return (err);
65 	}
66 
67 	return (ESCSI_UNKNOWN);
68 }
69