]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/strutils.h
rtcwake: remove RTC_ALM_READ and RTC_ALM_SET compatibility
[thirdparty/util-linux.git] / include / strutils.h
CommitLineData
8abcf290
DB
1#ifndef UTIL_LINUX_STRUTILS
2#define UTIL_LINUX_STRUTILS
3
23106a29 4#include <stdlib.h>
8abcf290
DB
5#include <inttypes.h>
6#include <string.h>
ce877f2d 7#include <sys/types.h>
675de3f5 8#include <ctype.h>
8abcf290 9
a99c9130
KZ
10/* default strtoxx_or_err() exit code */
11#ifndef STRTOXX_EXIT_CODE
12# define STRTOXX_EXIT_CODE EXIT_FAILURE
13#endif
14
15
23106a29 16extern int parse_size(const char *str, uintmax_t *res, int *power);
8abcf290 17extern int strtosize(const char *str, uintmax_t *res);
551dae40
KZ
18extern uintmax_t strtosize_or_err(const char *str, const char *errmesg);
19
20extern int16_t strtos16_or_err(const char *str, const char *errmesg);
21extern uint16_t strtou16_or_err(const char *str, const char *errmesg);
22
23extern int32_t strtos32_or_err(const char *str, const char *errmesg);
24extern uint32_t strtou32_or_err(const char *str, const char *errmesg);
25
26extern int64_t strtos64_or_err(const char *str, const char *errmesg);
27extern uint64_t strtou64_or_err(const char *str, const char *errmesg);
28
a9f97001 29extern double strtod_or_err(const char *str, const char *errmesg);
551dae40 30
8abcf290 31extern long strtol_or_err(const char *str, const char *errmesg);
e53bc960 32extern unsigned long strtoul_or_err(const char *str, const char *errmesg);
8abcf290 33
477254da
KZ
34extern void strtotimeval_or_err(const char *str, struct timeval *tv,
35 const char *errmesg);
36
416c43a9
KZ
37extern int isdigit_string(const char *str);
38
30b294c4 39extern int parse_switch(const char *arg, const char *errmesg, ...);
e5cf1476 40
02887b73
DT
41#ifndef HAVE_MEMPCPY
42extern void *mempcpy(void *restrict dest, const void *restrict src, size_t n);
43#endif
8abcf290
DB
44#ifndef HAVE_STRNLEN
45extern size_t strnlen(const char *s, size_t maxlen);
46#endif
47#ifndef HAVE_STRNDUP
48extern char *strndup(const char *s, size_t n);
49#endif
50#ifndef HAVE_STRNCHR
51extern char *strnchr(const char *s, size_t maxlen, int c);
52#endif
53
54/* caller guarantees n > 0 */
55static inline void xstrncpy(char *dest, const char *src, size_t n)
56{
57 strncpy(dest, src, n-1);
58 dest[n-1] = 0;
59}
ce877f2d 60
23106a29
KZ
61static inline char *strdup_to_offset(void *stru, size_t offset, const char *str)
62{
63 char *n = NULL;
64 char **o = (char **) ((char *) stru + offset);
65
66 if (str) {
67 n = strdup(str);
68 if (!n)
69 return NULL;
70 }
71
72 free(*o);
73 *o = n;
74 return n;
75}
76
77#define strdup_to_struct_member(_s, _m, _str) \
78 strdup_to_offset((void *) _s, offsetof(__typeof__(*(_s)), _m), _str)
79
ce877f2d 80extern void strmode(mode_t mode, char *str);
5d2a9849
FC
81
82/* Options for size_to_human_string() */
83enum
84{
85 SIZE_SUFFIX_1LETTER = 0,
86 SIZE_SUFFIX_3LETTER = 1,
87 SIZE_SUFFIX_SPACE = 2
88};
89
90extern char *size_to_human_string(int options, uint64_t bytes);
ce877f2d 91
c87638ad
KZ
92extern int string_to_idarray(const char *list, int ary[], size_t arysz,
93 int (name2id)(const char *, size_t));
f5077b51 94extern int string_add_to_idarray(const char *list, int ary[],
40b17508 95 size_t arysz, size_t *ary_pos,
f5077b51
MB
96 int (name2id)(const char *, size_t));
97
c87638ad
KZ
98extern int string_to_bitarray(const char *list, char *ary,
99 int (*name2bit)(const char *, size_t));
100
5ef16771
KZ
101extern int string_to_bitmask(const char *list,
102 unsigned long *mask,
103 long (*name2flag)(const char *, size_t));
af7df9ee 104extern int parse_range(const char *str, int *lower, int *upper, int def);
a883c634 105
b106d052
PU
106extern int streq_except_trailing_slash(const char *s1, const char *s2);
107
646e159a
KZ
108/*
109 * Match string beginning.
110 */
111static inline const char *startswith(const char *s, const char *prefix)
112{
113 size_t sz = prefix ? strlen(prefix) : 0;
114
115 if (s && sz && strncmp(s, prefix, sz) == 0)
116 return s + sz;
117 return NULL;
118}
119
120/*
121 * Case insensitive match string beginning.
122 */
123static inline const char *startswith_no_case(const char *s, const char *prefix)
124{
125 size_t sz = prefix ? strlen(prefix) : 0;
126
127 if (s && sz && strncasecmp(s, prefix, sz) == 0)
128 return s + sz;
129 return NULL;
130}
131
132/*
133 * Match string ending.
134 */
135static inline const char *endswith(const char *s, const char *postfix)
136{
137 size_t sl = s ? strlen(s) : 0;
138 size_t pl = postfix ? strlen(postfix) : 0;
139
140 if (pl == 0)
141 return (char *)s + sl;
142 if (sl < pl)
143 return NULL;
144 if (memcmp(s + sl - pl, postfix, pl) != 0)
145 return NULL;
146 return (char *)s + sl - pl;
147}
199e939d 148
675de3f5
OO
149/*
150 * Skip leading white space.
151 */
152static inline const char *skip_space(const char *p)
153{
154 while (isspace(*p))
155 ++p;
156 return p;
157}
158
159static inline const char *skip_blank(const char *p)
160{
161 while (isblank(*p))
162 ++p;
163 return p;
164}
165
22e9e9c8
KZ
166
167/* Removes whitespace from the right-hand side of a string (trailing
168 * whitespace).
169 *
170 * Returns size of the new string (without \0).
171 */
172static inline size_t rtrim_whitespace(unsigned char *str)
173{
174 size_t i = strlen((char *) str);
175
2f8610ee
SK
176 while (i) {
177 i--;
178 if (!isspace(str[i])) {
179 i++;
22e9e9c8 180 break;
2f8610ee 181 }
22e9e9c8 182 }
2f8610ee 183 str[i] = '\0';
22e9e9c8
KZ
184 return i;
185}
186
187/* Removes whitespace from the left-hand side of a string.
188 *
189 * Returns size of the new string (without \0).
190 */
191static inline size_t ltrim_whitespace(unsigned char *str)
192{
193 size_t len;
194 unsigned char *p;
195
196 for (p = str; p && isspace(*p); p++);
197
198 len = strlen((char *) p);
199
200 if (len && p > str)
201 memmove(str, p, len + 1);
202
203 return len;
204}
205
548b9714
KZ
206extern char *strnappend(const char *s, const char *suffix, size_t b);
207extern char *strappend(const char *s, const char *suffix);
208extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
209
8abcf290 210#endif