blob: ecc1667db823be8af5d59c9b1c1cdec03f234480 [file] [log] [blame]
Eric Andersen24794452004-03-06 22:11:45 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen24794452004-03-06 22:11:45 +00008 */
9
10#include <stdlib.h>
11#include <errno.h>
12#include <assert.h>
13#include "libbb.h"
14
15#ifdef L_safe_strtoi
Eric Andersen24794452004-03-06 22:11:45 +000016int safe_strtoi(char *arg, int* value)
17{
18 int error;
19 long lvalue = *value;
20 error = safe_strtol(arg, &lvalue);
21 *value = (int) lvalue;
22 return error;
23}
24#endif
25
26#ifdef L_safe_strtod
Eric Andersen24794452004-03-06 22:11:45 +000027int safe_strtod(char *arg, double* value)
28{
29 char *endptr;
30 int errno_save = errno;
31
32 assert(arg!=NULL);
33 errno = 0;
34 *value = strtod(arg, &endptr);
35 if (errno != 0 || *endptr!='\0' || endptr==arg) {
36 return 1;
37 }
38 errno = errno_save;
39 return 0;
40}
41#endif
42
43#ifdef L_safe_strtol
Eric Andersen24794452004-03-06 22:11:45 +000044int safe_strtol(char *arg, long* value)
45{
46 char *endptr;
47 int errno_save = errno;
48
49 assert(arg!=NULL);
50 errno = 0;
51 *value = strtol(arg, &endptr, 0);
52 if (errno != 0 || *endptr!='\0' || endptr==arg) {
53 return 1;
54 }
55 errno = errno_save;
56 return 0;
57}
58#endif
59
60#ifdef L_safe_strtoul
Eric Andersen24794452004-03-06 22:11:45 +000061int safe_strtoul(char *arg, unsigned long* value)
62{
63 char *endptr;
64 int errno_save = errno;
65
66 assert(arg!=NULL);
67 errno = 0;
68 *value = strtoul(arg, &endptr, 0);
69 if (errno != 0 || *endptr!='\0' || endptr==arg) {
70 return 1;
71 }
72 errno = errno_save;
73 return 0;
74}
75#endif
76