summaryrefslogtreecommitdiffstats
path: root/plugins/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/utils.c')
-rw-r--r--plugins/utils.c180
1 files changed, 94 insertions, 86 deletions
diff --git a/plugins/utils.c b/plugins/utils.c
index b4214c6..77d6a6f 100644
--- a/plugins/utils.c
+++ b/plugins/utils.c
@@ -23,13 +23,15 @@
23*****************************************************************************/ 23*****************************************************************************/
24 24
25#include "common.h" 25#include "common.h"
26#include "utils.h" 26#include "./utils.h"
27#include "utils_base.h" 27#include "utils_base.h"
28#include <stdarg.h> 28#include <stdarg.h>
29#include <limits.h> 29#include <limits.h>
30#include <string.h> 30#include <string.h>
31#include <errno.h> 31#include <errno.h>
32 32
33#include <stdbool.h>
34
33#include <arpa/inet.h> 35#include <arpa/inet.h>
34 36
35extern void print_usage (void); 37extern void print_usage (void);
@@ -147,98 +149,107 @@ print_revision (const char *command_name, const char *revision)
147 command_name, revision, PACKAGE, VERSION); 149 command_name, revision, PACKAGE, VERSION);
148} 150}
149 151
150int 152bool is_numeric (char *number) {
151is_numeric (char *number)
152{
153 char tmp[1]; 153 char tmp[1];
154 float x; 154 float x;
155 155
156 if (!number) 156 if (!number)
157 return FALSE; 157 return false;
158 else if (sscanf (number, "%f%c", &x, tmp) == 1) 158 else if (sscanf (number, "%f%c", &x, tmp) == 1)
159 return TRUE; 159 return true;
160 else 160 else
161 return FALSE; 161 return false;
162} 162}
163 163
164int 164bool is_positive (char *number) {
165is_positive (char *number)
166{
167 if (is_numeric (number) && atof (number) > 0.0) 165 if (is_numeric (number) && atof (number) > 0.0)
168 return TRUE; 166 return true;
169 else 167 else
170 return FALSE; 168 return false;
171} 169}
172 170
173int 171bool is_negative (char *number) {
174is_negative (char *number)
175{
176 if (is_numeric (number) && atof (number) < 0.0) 172 if (is_numeric (number) && atof (number) < 0.0)
177 return TRUE; 173 return true;
178 else 174 else
179 return FALSE; 175 return false;
180} 176}
181 177
182int 178bool is_nonnegative (char *number) {
183is_nonnegative (char *number)
184{
185 if (is_numeric (number) && atof (number) >= 0.0) 179 if (is_numeric (number) && atof (number) >= 0.0)
186 return TRUE; 180 return true;
187 else 181 else
188 return FALSE; 182 return false;
189} 183}
190 184
191int 185bool is_percentage (char *number) {
192is_percentage (char *number)
193{
194 int x; 186 int x;
195 if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) 187 if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
196 return TRUE; 188 return true;
197 else 189 else
198 return FALSE; 190 return false;
199} 191}
200 192
201int 193bool is_percentage_expression (const char str[]) {
202is_integer (char *number) 194 if (!str) {
203{ 195 return false;
196 }
197
198 size_t len = strlen(str);
199
200 if (str[len-1] != '%') {
201 return false;
202 }
203
204 char *foo = calloc(sizeof(char), len + 1);
205
206 if (!foo) {
207 die (STATE_UNKNOWN, _("calloc failed \n"));
208 }
209
210 strcpy(foo, str);
211 foo[len-1] = '\0';
212
213 bool result = is_numeric(foo);
214
215 free(foo);
216
217 return result;
218}
219
220bool is_integer (char *number) {
204 long int n; 221 long int n;
205 222
206 if (!number || (strspn (number, "-0123456789 ") != strlen (number))) 223 if (!number || (strspn (number, "-0123456789 ") != strlen (number)))
207 return FALSE; 224 return false;
208 225
209 n = strtol (number, NULL, 10); 226 n = strtol (number, NULL, 10);
210 227
211 if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) 228 if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
212 return TRUE; 229 return true;
213 else 230 else
214 return FALSE; 231 return false;
215} 232}
216 233
217int 234bool is_intpos (char *number) {
218is_intpos (char *number)
219{
220 if (is_integer (number) && atoi (number) > 0) 235 if (is_integer (number) && atoi (number) > 0)
221 return TRUE; 236 return true;
222 else 237 else
223 return FALSE; 238 return false;
224} 239}
225 240
226int 241bool is_intneg (char *number) {
227is_intneg (char *number)
228{
229 if (is_integer (number) && atoi (number) < 0) 242 if (is_integer (number) && atoi (number) < 0)
230 return TRUE; 243 return true;
231 else 244 else
232 return FALSE; 245 return false;
233} 246}
234 247
235int 248bool is_intnonneg (char *number) {
236is_intnonneg (char *number)
237{
238 if (is_integer (number) && atoi (number) >= 0) 249 if (is_integer (number) && atoi (number) >= 0)
239 return TRUE; 250 return true;
240 else 251 else
241 return FALSE; 252 return false;
242} 253}
243 254
244/* 255/*
@@ -246,19 +257,27 @@ is_intnonneg (char *number)
246 * On success the number will be written to the _target_ address, if _target_ is not set 257 * On success the number will be written to the _target_ address, if _target_ is not set
247 * to NULL. 258 * to NULL.
248 */ 259 */
249int is_int64(char *number, int64_t *target) { 260bool is_int64(char *number, int64_t *target) {
250 errno = 0; 261 errno = 0;
251 uint64_t tmp = strtoll(number, NULL, 10); 262 char *endptr = { 0 };
263
264 int64_t tmp = strtoll(number, &endptr, 10);
252 if (errno != 0) { 265 if (errno != 0) {
266 return false;
267 }
268
269 if (*endptr == '\0') {
253 return 0; 270 return 0;
254 } 271 }
272
255 if (tmp < INT64_MIN || tmp > INT64_MAX) { 273 if (tmp < INT64_MIN || tmp > INT64_MAX) {
256 return 0; 274 return false;
257 } 275 }
276
258 if (target != NULL) { 277 if (target != NULL) {
259 *target = tmp; 278 *target = tmp;
260 } 279 }
261 return 1; 280 return true;
262} 281}
263 282
264/* 283/*
@@ -266,40 +285,45 @@ int is_int64(char *number, int64_t *target) {
266 * On success the number will be written to the _target_ address, if _target_ is not set 285 * On success the number will be written to the _target_ address, if _target_ is not set
267 * to NULL. 286 * to NULL.
268 */ 287 */
269int is_uint64(char *number, uint64_t *target) { 288bool is_uint64(char *number, uint64_t *target) {
270 errno = 0; 289 errno = 0;
271 uint64_t tmp = strtoll(number, NULL, 10); 290 char *endptr = { 0 };
291 unsigned long long tmp = strtoull(number, &endptr, 10);
292
272 if (errno != 0) { 293 if (errno != 0) {
273 return 0; 294 return false;
274 } 295 }
275 if (tmp < 0 || tmp > UINT64_MAX) { 296
276 return 0; 297 if (*endptr != '\0') {
298 return false;
299 }
300
301 if (tmp > UINT64_MAX) {
302 return false;
277 } 303 }
304
278 if (target != NULL) { 305 if (target != NULL) {
279 *target = tmp; 306 *target = (uint64_t)tmp;
280 } 307 }
281 return 1; 308
309 return true;
282} 310}
283 311
284int 312bool is_intpercent (char *number) {
285is_intpercent (char *number)
286{
287 int i; 313 int i;
288 if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) 314 if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
289 return TRUE; 315 return true;
290 else 316 else
291 return FALSE; 317 return false;
292} 318}
293 319
294int 320bool is_option (char *str) {
295is_option (char *str)
296{
297 if (!str) 321 if (!str)
298 return FALSE; 322 return false;
299 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) 323 else if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
300 return TRUE; 324 return true;
301 else 325 else
302 return FALSE; 326 return false;
303} 327}
304 328
305#ifdef NEED_GETTIMEOFDAY 329#ifdef NEED_GETTIMEOFDAY
@@ -804,19 +828,3 @@ char *sperfdata_int (const char *label,
804 828
805 return data; 829 return data;
806} 830}
807
808int
809open_max (void)
810{
811 errno = 0;
812 if (maxfd > 0)
813 return(maxfd);
814
815 if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) {
816 if (errno == 0)
817 maxfd = DEFAULT_MAXFD; /* it's indeterminate */
818 else
819 die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n"));
820 }
821 return(maxfd);
822}