* Safer wrapper of strtoll. Return 0 on success, otherwise -1. * Idea from corosync-qdevice project */
| 8 | * Idea from corosync-qdevice project |
| 9 | */ |
| 10 | int |
| 11 | util_strtonum(const char *str, long long int min_val, long long int max_val, |
| 12 | long long int *res) |
| 13 | { |
| 14 | long long int tmp_ll; |
| 15 | char *ep; |
| 16 | |
| 17 | if (min_val > max_val) { |
| 18 | return (-1); |
| 19 | } |
| 20 | |
| 21 | errno = 0; |
| 22 | |
| 23 | tmp_ll = strtoll(str, &ep, 10); |
| 24 | if (ep == str || *ep != '\0' || errno != 0) { |
| 25 | return (-1); |
| 26 | } |
| 27 | |
| 28 | if (tmp_ll < min_val || tmp_ll > max_val) { |
| 29 | return (-1); |
| 30 | } |
| 31 | |
| 32 | *res = tmp_ll; |
| 33 | |
| 34 | return (0); |
| 35 | } |