ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/error.c
Revision: 1.8
Committed: Wed Apr 7 21:19:26 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.7: +9 -4 lines
Log Message:
Add sg_get_error_arg. It uses a static buffer for storage rather than
sg_update_string because we can't do anything about a malloc failure at
that point.

Remove the default case in the switch, so GCC will give an "unhandled
enum value in switch" warning if we add a new error code without a
string.

File Contents

# User Rev Content
1 tdb 1.1 /*
2     * i-scream libstatgrab
3     * http://www.i-scream.org
4     * Copyright (C) 2000-2004 i-scream
5     *
6     * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10     *
11     * This library is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15     *
16     * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20     *
21 ats 1.8 * $Id: error.c,v 1.7 2004/04/07 21:08:40 tdb Exp $
22 tdb 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28     #include <stdlib.h>
29    
30     #include "statgrab.h"
31    
32 ats 1.2 static sg_error error = SG_ERROR_NONE;
33 ats 1.8 #define ERROR_ARG_MAX 256
34     static char error_arg[ERROR_ARG_MAX];
35 tdb 1.1
36     void sg_set_error(sg_error code, const char *arg) {
37     error = code;
38 ats 1.8 strlcpy(error_arg, arg, sizeof error_arg);
39 tdb 1.1 }
40    
41     sg_error sg_get_error() {
42     return error;
43     }
44    
45 ats 1.8 const char *sg_get_error_arg() {
46     return error_arg;
47     }
48    
49 ats 1.2 const char *sg_str_error(sg_error code) {
50     switch (code) {
51     case SG_ERROR_NONE:
52     return "no error";
53 tdb 1.7 case SG_ERROR_ASPRINTF:
54     return "asprintf failed";
55     case SG_ERROR_DEVSTAT_GETDEVS:
56     return "devstat_getdevs failed";
57     case SG_ERROR_DEVSTAT_SELECTDEVS:
58     return "devstat_selectdevs failed";
59     case SG_ERROR_ENOENT:
60     return "system call received ENOENT";
61     case SG_ERROR_GETIFADDRS:
62     return "getifaddress failed";
63     case SG_ERROR_GETMNTINFO:
64     return "getmntinfo failed";
65     case SG_ERROR_GETPAGESIZE:
66     return "getpagesize failed";
67     case SG_ERROR_KSTAT_DATA_LOOKUP:
68     return "kstat_data_lookup failed";
69     case SG_ERROR_KSTAT_LOOKUP:
70     return "kstat_lookup failed";
71     case SG_ERROR_KSTAT_OPEN:
72     return "kstat_open failed";
73     case SG_ERROR_KSTAT_READ:
74     return "kstat_read failed";
75     case SG_ERROR_KVM_GETSWAPINFO:
76     return "kvm_getswapinfo failed";
77     case SG_ERROR_KVM_OPENFILES:
78     return "kvm_openfiles failed";
79 tdb 1.4 case SG_ERROR_MALLOC:
80     return "malloc failed";
81     case SG_ERROR_OPEN:
82     return "failed to open file";
83 tdb 1.7 case SG_ERROR_OPENDIR:
84     return "failed to open directory";
85 tdb 1.4 case SG_ERROR_PARSE:
86     return "failed to parse input";
87 tdb 1.7 case SG_ERROR_SETEGID:
88     return "setegid failed";
89     case SG_ERROR_SETEUID:
90     return "seteuid failed";
91 tdb 1.6 case SG_ERROR_SETMNTENT:
92     return "setmntent failed";
93 tdb 1.7 case SG_ERROR_SOCKET:
94     return "socket failed";
95     case SG_ERROR_SWAPCTL:
96     return "swapctl failed";
97     case SG_ERROR_SYSCONF:
98     return "sysconf failed";
99     case SG_ERROR_SYSCTL:
100     return "sysctl failed";
101     case SG_ERROR_SYSCTLBYNAME:
102     return "sysctlbyname failed";
103     case SG_ERROR_SYSCTLNAMETOMIB:
104     return "sysctlnametomib failed";
105     case SG_ERROR_UNAME:
106     return "uname failed";
107     case SG_ERROR_UNSUPPORTED:
108     return "unsupported function";
109     case SG_ERROR_XSW_VER_MISMATCH:
110     return "XSW version mismatch";
111 ats 1.2 }
112 ats 1.8 return "unknown error";
113 tdb 1.1 }
114