1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "volume_error.h"
36
37 #define VOLUME_ERROR_BUFSIZE 1024
38 static char volume_error[VOLUME_ERROR_BUFSIZE];
39
40 /*
41 * Retrieve the error string for the given error code.
42 *
43 * @param error
44 * If error is less than zero, it is assumed to be a
45 * custom error code. If error is greater than zero, it
46 * is assumed to be an error defined in errno.h.
47 *
48 * @return the error string set by volume_set_error()
49 * if error < 0
50 *
51 * @return the error string returned by strerror()
52 * if error > 0
53 */
54 char *
get_error_string(int error)55 get_error_string(
56 int error)
57 {
58 if (error < 0) {
59 return (volume_error);
60 }
61
62 if (error > 0) {
63 return (strerror(error));
64 }
65
66 return (NULL);
67 }
68
69 /*
70 * Set the error string for the most recent error. This message can
71 * be retrieved with get_error_string(error), assuming error is less
72 * than zero.
73 *
74 * @param fmt
75 * printf format string
76 *
77 * @return the number of characters formatted
78 * if successful
79 *
80 * @return negative value
81 * if an error occurred
82 */
83 /*PRINTFLIKE1*/
84 int
volume_set_error(char * fmt,...)85 volume_set_error(
86 char *fmt,
87 ...)
88 {
89 int ret = 0;
90
91 va_list ap;
92 va_start(ap, fmt);
93 ret = vsnprintf(volume_error, VOLUME_ERROR_BUFSIZE, fmt, ap);
94 va_end(ap);
95
96 return (ret);
97 }
98