util-lib: unify parsing of nice level values
This adds parse_nice() that parses a nice level and ensures it is in the right range, via a new nice_is_valid() helper. It then ports over a number of users to this. No functional changes.
This commit is contained in:
+17
-1
@@ -29,6 +29,7 @@
|
||||
#include "extract-word.h"
|
||||
#include "macro.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "string-util.h"
|
||||
|
||||
int parse_boolean(const char *v) {
|
||||
@@ -551,10 +552,25 @@ int parse_percent_unbounded(const char *p) {
|
||||
}
|
||||
|
||||
int parse_percent(const char *p) {
|
||||
int v = parse_percent_unbounded(p);
|
||||
int v;
|
||||
|
||||
v = parse_percent_unbounded(p);
|
||||
if (v > 100)
|
||||
return -ERANGE;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
int parse_nice(const char *p, int *ret) {
|
||||
int n, r;
|
||||
|
||||
r = safe_atoi(p, &n);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!nice_is_valid(n))
|
||||
return -ERANGE;
|
||||
|
||||
*ret = n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user