summaryrefslogtreecommitdiffstats
path: root/lib/getrange.c
blob: 8f7acee0ceb0e37588e1c00dc5a196e1af71ae4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * SPDX-FileCopyrightText: 2008       , Nicolas François
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */


#include <config.h>

#ident "$Id: $"

#include <ctype.h>
#include <stdlib.h>

#include "atoi/strtou_noneg.h"
#include "defines.h"
#include "prototypes.h"


/*
 * Parse a range and indicate if the range is valid.
 * Valid ranges are in the form:
 *     <long>          -> min=max=long         has_min  has_max
 *     -<long>         -> max=long            !has_min  has_max
 *     <long>-         -> min=long             has_min !has_max
 *     <long1>-<long2> -> min=long1 max=long2  has_min  has_max
 */
int
getrange(const char *range,
         unsigned long *min, bool *has_min,
         unsigned long *max, bool *has_max)
{
	char  *endptr;

	if (NULL == range)
		return -1;

	*has_min = false;
	*has_max = false;

	if ('-' == range[0]) {
		endptr = range + 1;
		goto parse_max;
	}

	errno = 0;
	*min = strtoul_noneg(range, &endptr, 10);
	if (endptr == range || 0 != errno)
		return -1;
	*has_min = true;

	switch (*endptr++) {
	case '\0':
		*has_max = true;
		*max = *min;
		return 0;  /* <long> */

	case '-':
		if ('\0' == *endptr)
			return 0;  /* <long>- */
parse_max:
		if (!isdigit(*endptr))
			return -1;

		errno = 0;
		*max = strtoul_noneg(endptr, &endptr, 10);
		if ('\0' != *endptr || 0 != errno)
			return -1;
		*has_max = true;

		return 0;  /* <long>-<long>, or -<long> */

	default:
		return -1;
	}
}