]>
Commit | Line | Data |
---|---|---|
6bedfcbb LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | #pragma once | |
4 | ||
5 | /*** | |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2010 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
24 | #include <inttypes.h> | |
25 | #include <sys/types.h> | |
26 | ||
27 | #include "macro.h" | |
28 | ||
29 | int parse_boolean(const char *v) _pure_; | |
30 | int parse_pid(const char *s, pid_t* ret_pid); | |
31 | int parse_mode(const char *s, mode_t *ret); | |
32 | ||
33 | int parse_size(const char *t, uint64_t base, uint64_t *size); | |
34 | ||
35 | #define FORMAT_BYTES_MAX 8 | |
36 | char *format_bytes(char *buf, size_t l, uint64_t t); | |
37 | ||
38 | int safe_atou(const char *s, unsigned *ret_u); | |
39 | int safe_atoi(const char *s, int *ret_i); | |
40 | int safe_atollu(const char *s, unsigned long long *ret_u); | |
41 | int safe_atolli(const char *s, long long int *ret_i); | |
42 | ||
43 | int safe_atou8(const char *s, uint8_t *ret); | |
44 | ||
45 | int safe_atou16(const char *s, uint16_t *ret); | |
46 | int safe_atoi16(const char *s, int16_t *ret); | |
47 | ||
48 | static inline int safe_atou32(const char *s, uint32_t *ret_u) { | |
49 | assert_cc(sizeof(uint32_t) == sizeof(unsigned)); | |
50 | return safe_atou(s, (unsigned*) ret_u); | |
51 | } | |
52 | ||
53 | static inline int safe_atoi32(const char *s, int32_t *ret_i) { | |
54 | assert_cc(sizeof(int32_t) == sizeof(int)); | |
55 | return safe_atoi(s, (int*) ret_i); | |
56 | } | |
57 | ||
58 | static inline int safe_atou64(const char *s, uint64_t *ret_u) { | |
59 | assert_cc(sizeof(uint64_t) == sizeof(unsigned long long)); | |
60 | return safe_atollu(s, (unsigned long long*) ret_u); | |
61 | } | |
62 | ||
63 | static inline int safe_atoi64(const char *s, int64_t *ret_i) { | |
64 | assert_cc(sizeof(int64_t) == sizeof(long long int)); | |
65 | return safe_atolli(s, (long long int*) ret_i); | |
66 | } | |
67 | ||
68 | #if LONG_MAX == INT_MAX | |
69 | static inline int safe_atolu(const char *s, unsigned long *ret_u) { | |
70 | assert_cc(sizeof(unsigned long) == sizeof(unsigned)); | |
71 | return safe_atou(s, (unsigned*) ret_u); | |
72 | } | |
73 | static inline int safe_atoli(const char *s, long int *ret_u) { | |
74 | assert_cc(sizeof(long int) == sizeof(int)); | |
75 | return safe_atoi(s, (int*) ret_u); | |
76 | } | |
77 | #else | |
78 | static inline int safe_atolu(const char *s, unsigned long *ret_u) { | |
79 | assert_cc(sizeof(unsigned long) == sizeof(unsigned long long)); | |
80 | return safe_atollu(s, (unsigned long long*) ret_u); | |
81 | } | |
82 | static inline int safe_atoli(const char *s, long int *ret_u) { | |
83 | assert_cc(sizeof(long int) == sizeof(long long int)); | |
84 | return safe_atolli(s, (long long int*) ret_u); | |
85 | } | |
86 | #endif | |
87 | ||
88 | int safe_atod(const char *s, double *ret_d); |