Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/xstrton.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /* strtol with error checking
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <errno.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20
21 #include "common.h"
22 #include "messages.h"
23 #include "xasprintf.h"
24
25 static int
26 agemultiplier (double age, char mult, long long int *result)
27 {
28 double temp = age;
29
30 switch (mult)
31 {
32 case 0:
33 case 's':
34 case 'S':
35 break;
36
37 case 'm':
38 case 'M':
39 temp *= 60;
40 break;
41
42 case 'h':
43 case 'H':
44 temp *= 3600;
45 break;
46
47 case 'd':
48 case 'D':
49 temp *= 86400;
50 break;
51
52 case 'w':
53 case 'W':
54 temp *= 7 * 86400;
55 break;
56
57 case 'y':
58 case 'Y':
59 temp *= 31557600; /* == 365.25 * 86400 */
60 break;
61
62 default:
63 return -1;
64 }
65
66 *result = (long long int) temp;
67 return 0;
68 }
69
70 static int
71 sizemultiplier (double size, char mult, long long int *result)
72 {
73 double temp = size;
74
75 switch (mult)
76 {
77 case 0:
78 case 'b':
79 case 'B':
80 break;
81
82 case 'k':
83 case 'K':
84 temp *= 1000.0;
85 break;
86
87 case 'm':
88 case 'M':
89 temp *= 1000.0 * 1000.0;
90 break;
91
92 case 'g':
93 case 'G':
94 temp *= 1000.0 * 1000.0 * 1000.0;
95 break;
96
97 case 't':
98 case 'T':
99 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0;
100 break;
101
102 case 'p':
103 case 'P':
104 temp *= 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0;
105 break;
106
107 default:
108 return -1;
109 }
110
111 *result = (long long int) temp;
112 return 0;
113 }
114
115 static int
116 strtollint (const char *str, long long int *result,
117 int (*converter) (double, char, long long int *), char **errmesg)
118 {
119 if (str != NULL && *str != '\0')
120 {
121 char *end = NULL;
122 errno = 0;
123 double temp = strtod (str, &end);
124 if ((errno == 0) && (end != NULL) && (end != str))
125 {
126 int res = converter (temp, *end, result);
127 if (res < 0)
128 {
129 *errmesg = xasprintf ("invalid suffix `%c' in `%s'", *end, str);
130 return -1;
131 }
132
133 if (*(end + 1) != '\0')
134 {
135 *errmesg = xasprintf ("invalid trailing character `%c' in `%s'",
136 *(end + 1), str);
137 return -1;
138 }
139
140 return 0;
141 }
142
143 *errmesg = xasprintf ("converting `%s' to a number failed", str);
144 return -1;
145 }
146
147 *errmesg = xasprintf ("no number to covert (empty string)");
148 return -1;
149 }
150
151 /* convert a string with an optional prefix s (second), m (minute), h (hour),
152 * d (day), w (week), or y (year);
153 * return 0 on success, -1 otherwise (in this case errmesg will provide a
154 * human readable error message) */
155 int
156 agetollint (const char *str, long long int * age, char **errmesg)
157 {
158 return strtollint (str, age, agemultiplier, errmesg);
159 }
160
161 /* convert a string with an optional prefix b (byte), k (kilobyte),
162 * m (megabyte), g (gigabyte), t (terabyte), or p (petabyte);
163 * return 0 on success, -1 otherwise (in this case errmesg will provide a
164 * human readable error message) */
165 int
166 sizetollint (const char *str, long long int *size, char **errmesg)
167 {
168 return strtollint (str, size, sizemultiplier, errmesg);
169 }
170
171 /* same as strtol(3) but exit on failure instead of returning crap */
172 long
173 strtol_or_err (const char *str, const char *errmesg)
174 {
175 char *end = NULL;
176
177 if (str != NULL && *str != '\0')
178 {
179 errno = 0;
180 long num = strtol (str, &end, 10);
181 if (errno == 0 && str != end && end != NULL && *end == '\0')
182 return num;
183 }
184
185 plugin_error (STATE_UNKNOWN, errno, "%s: '%s'", errmesg, str);
186 return 0;
187 }