本文整理汇总了C++中safe_atou函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_atou函数的具体用法?C++ safe_atou怎么用?C++ safe_atou使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_atou函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: routes_max
static unsigned routes_max(void) {
static thread_local unsigned cached = 0;
_cleanup_free_ char *s4 = NULL, *s6 = NULL;
unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
if (cached > 0)
return cached;
if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) {
truncate_nl(s4);
if (safe_atou(s4, &val4) >= 0 &&
val4 == 2147483647U)
/* This is the default "no limit" value in the kernel */
val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
}
if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) {
truncate_nl(s6);
(void) safe_atou(s6, &val6);
}
cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
return cached;
}
示例2: config_parse_unsigned
int config_parse_unsigned(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
unsigned *u = data;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
r = safe_atou(rvalue, u);
if (r < 0) {
log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
return r;
}
return 0;
}
示例3: client_run
static int client_run(int ifindex, const char *seed_str, const struct ether_addr *ha, sd_event *e) {
sd_ipv4ll *ll;
assert_se(sd_ipv4ll_new(&ll) >= 0);
assert_se(sd_ipv4ll_attach_event(ll, e, 0) >= 0);
assert_se(sd_ipv4ll_set_index(ll, ifindex) >= 0);
assert_se(sd_ipv4ll_set_mac(ll, ha) >= 0);
assert_se(sd_ipv4ll_set_callback(ll, ll_handler, NULL) >= 0);
if (seed_str) {
unsigned seed;
assert_se(safe_atou(seed_str, &seed) >= 0);
assert_se(sd_ipv4ll_set_address_seed(ll, seed) >= 0);
}
log_info("starting IPv4LL client");
assert_se(sd_ipv4ll_start(ll) >= 0);
assert_se(sd_event_loop(e) >= 0);
assert_se(!sd_ipv4ll_unref(ll));
return EXIT_SUCCESS;
}
示例4: socket_address_parse
int socket_address_parse(SocketAddress *a, const char *s) {
char *e, *n;
unsigned u;
int r;
assert(a);
assert(s);
zero(*a);
a->type = SOCK_STREAM;
if (*s == '[') {
/* IPv6 in [x:.....:z]:p notation */
if (!socket_ipv6_is_supported()) {
log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
return -EAFNOSUPPORT;
}
e = strchr(s+1, ']');
if (!e)
return -EINVAL;
n = strndupa(s+1, e-s-1);
errno = 0;
if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
return errno > 0 ? -errno : -EINVAL;
e++;
if (*e != ':')
return -EINVAL;
e++;
r = safe_atou(e, &u);
if (r < 0)
return r;
if (u <= 0 || u > 0xFFFF)
return -EINVAL;
a->sockaddr.in6.sin6_family = AF_INET6;
a->sockaddr.in6.sin6_port = htons((uint16_t) u);
a->size = sizeof(struct sockaddr_in6);
} else if (*s == '/') {
/* AF_UNIX socket */
size_t l;
l = strlen(s);
if (l >= sizeof(a->sockaddr.un.sun_path))
return -EINVAL;
a->sockaddr.un.sun_family = AF_UNIX;
memcpy(a->sockaddr.un.sun_path, s, l);
a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
} else if (*s == '@') {
示例5: prompt_loop
static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) {
int r;
assert(text);
assert(is_valid);
assert(ret);
for (;;) {
_cleanup_free_ char *p = NULL;
unsigned u;
r = ask_string(&p, "%s %s (empty to skip): ", draw_special_char(DRAW_TRIANGULAR_BULLET), text);
if (r < 0) {
log_error("Failed to query user: %s", strerror(-r));
return r;
}
if (isempty(p)) {
log_warning("No data entered, skipping.");
return 0;
}
r = safe_atou(p, &u);
if (r >= 0) {
char *c;
if (u <= 0 || u > strv_length(l)) {
log_error("Specified entry number out of range.");
continue;
}
log_info("Selected '%s'.", l[u-1]);
c = strdup(l[u-1]);
if (!c)
return log_oom();
free(*ret);
*ret = c;
return 0;
}
if (!is_valid(p)) {
log_error("Entered data invalid.");
continue;
}
free(*ret);
*ret = p;
p = 0;
return 0;
}
}
示例6: test_ioprio_class_from_to_string_one
static void test_ioprio_class_from_to_string_one(const char *val, int expected) {
assert_se(ioprio_class_from_string(val) == expected);
if (expected >= 0) {
_cleanup_free_ char *s = NULL;
unsigned ret;
assert_se(ioprio_class_to_string_alloc(expected, &s) == 0);
/* We sometimes get a class number and sometimes a number back */
assert_se(streq(s, val) ||
safe_atou(val, &ret) == 0);
}
}
示例7: parse_cpu_set_and_warn
int parse_cpu_set_and_warn(
const char *rvalue,
cpu_set_t **cpu_set,
const char *unit,
const char *filename,
unsigned line,
const char *lvalue) {
const char *whole_rvalue = rvalue;
_cleanup_cpu_free_ cpu_set_t *c = NULL;
unsigned ncpus = 0;
assert(lvalue);
assert(rvalue);
for (;;) {
_cleanup_free_ char *word = NULL;
unsigned cpu;
int r;
r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
return r;
}
if (r == 0)
break;
if (!c) {
c = cpu_set_malloc(&ncpus);
if (!c)
return log_oom();
}
r = safe_atou(word, &cpu);
if (r < 0 || cpu >= ncpus) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", rvalue);
return -EINVAL;
}
CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
}
/* On success, sets *cpu_set and returns ncpus for the system. */
if (c) {
*cpu_set = c;
c = NULL;
}
return (int) ncpus;
}
示例8: strspn
/* Ignore failure. E.g. 10.M is valid */
frac = l2;
for (; e < e2; e++)
frac /= 10;
}
}
e += strspn(e, WHITESPACE);
for (i = start_pos; i < n_entries; i++)
if (startswith(e, table[i].suffix))
break;
if (i >= n_entries)
return -EINVAL;
if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
return -ERANGE;
tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
if (tmp > ULLONG_MAX - r)
return -ERANGE;
r += tmp;
if ((unsigned long long) (uint64_t) r != r)
return -ERANGE;
p = e + strlen(table[i].suffix);
start_pos = i + 1;
} while (*p);
*size = r;
return 0;
}
#if 0 /* NM_IGNORED */
int parse_range(const char *t, unsigned *lower, unsigned *upper) {
_cleanup_free_ char *word = NULL;
unsigned l, u;
int r;
assert(lower);
assert(upper);
/* Extract the lower bound. */
r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
r = safe_atou(word, &l);
if (r < 0)
return r;
/* Check for the upper bound and extract it if needed */
if (!t)
/* Single number with no dashes. */
u = l;
else if (!*t)
/* Trailing dash is an error. */
return -EINVAL;
else {
r = safe_atou(t, &u);
if (r < 0)
return r;
}
*lower = l;
*upper = u;
return 0;
}
示例9: sd_session_get_vt
_public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
_cleanup_free_ char *vtnr_string = NULL;
unsigned u;
int r;
r = session_get_string(session, "VTNR", &vtnr_string);
if (r < 0)
return r;
r = safe_atou(vtnr_string, &u);
if (r < 0)
return r;
*vtnr = u;
return 0;
}
示例10: parse_percent_unbounded
int parse_percent_unbounded(const char *p) {
const char *pc, *n;
unsigned v;
int r;
pc = endswith(p, "%");
if (!pc)
return -EINVAL;
n = strndupa(p, pc - p);
r = safe_atou(n, &v);
if (r < 0)
return r;
return (int) v;
}
示例11: device_set_devuid
static int device_set_devuid(sd_device *device, const char *uid) {
unsigned u;
int r;
assert(device);
assert(uid);
r = safe_atou(uid, &u);
if (r < 0)
return r;
r = device_add_property_internal(device, "DEVUID", uid);
if (r < 0)
return r;
device->devuid = u;
return 0;
}
示例12: device_set_devgid
static int device_set_devgid(sd_device *device, const char *gid) {
unsigned g;
int r;
assert(device);
assert(gid);
r = safe_atou(gid, &g);
if (r < 0)
return r;
r = device_add_property_internal(device, "DEVGID", gid);
if (r < 0)
return r;
device->devgid = g;
return 0;
}
示例13: signal_from_string
int signal_from_string(const char *s) {
int signo;
int offset = 0;
unsigned u;
signo = __signal_from_string(s);
if (signo > 0)
return signo;
if (startswith(s, "RTMIN+")) {
s += 6;
offset = SIGRTMIN;
}
if (safe_atou(s, &u) >= 0) {
signo = (int) u + offset;
if (signo > 0 && signo < _NSIG)
return signo;
}
return -EINVAL;
}
示例14: dns_type_from_string
int dns_type_from_string(const char *s) {
const struct dns_type_name *sc;
assert(s);
sc = lookup_dns_type(s, strlen(s));
if (sc)
return sc->id;
s = startswith_no_case(s, "TYPE");
if (s) {
unsigned x;
if (safe_atou(s, &x) >= 0 &&
x <= UINT16_MAX)
return (int) x;
}
return _DNS_TYPE_INVALID;
}
示例15: test_cap_list
/* verify the capability parser */
static void test_cap_list(void) {
int i;
assert_se(!capability_to_name(-1));
assert_se(!capability_to_name(capability_list_length()));
for (i = 0; i < capability_list_length(); i++) {
const char *n;
assert_se(n = capability_to_name(i));
assert_se(capability_from_name(n) == i);
printf("%s = %i\n", n, i);
}
assert_se(capability_from_name("asdfbsd") == -EINVAL);
assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ);
assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ);
assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ);
assert_se(capability_from_name("0") == 0);
assert_se(capability_from_name("15") == 15);
assert_se(capability_from_name("-1") == -EINVAL);
for (i = 0; i < capability_list_length(); i++) {
_cleanup_cap_free_charp_ char *a = NULL;
const char *b;
unsigned u;
assert_se(a = cap_to_name(i));
/* quit the loop as soon as libcap starts returning
* numeric ids, formatted as strings */
if (safe_atou(a, &u) >= 0)
break;
assert_se(b = capability_to_name(i));
printf("%s vs. %s\n", a, b);
assert_se(strcasecmp(a, b) == 0);
}
}