]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - include/strutils.h
1 #ifndef UTIL_LINUX_STRUTILS
2 #define UTIL_LINUX_STRUTILS
12 /* initialize a custom exit code for all *_or_err functions */
13 extern void strutils_set_exitcode(int exit_code
);
15 extern int parse_size(const char *str
, uintmax_t *res
, int *power
);
16 extern int strtosize(const char *str
, uintmax_t *res
);
17 extern uintmax_t strtosize_or_err(const char *str
, const char *errmesg
);
19 extern int16_t strtos16_or_err(const char *str
, const char *errmesg
);
20 extern uint16_t strtou16_or_err(const char *str
, const char *errmesg
);
21 extern uint16_t strtox16_or_err(const char *str
, const char *errmesg
);
23 extern int32_t strtos32_or_err(const char *str
, const char *errmesg
);
24 extern uint32_t strtou32_or_err(const char *str
, const char *errmesg
);
25 extern uint32_t strtox32_or_err(const char *str
, const char *errmesg
);
27 extern int64_t strtos64_or_err(const char *str
, const char *errmesg
);
28 extern uint64_t strtou64_or_err(const char *str
, const char *errmesg
);
29 extern uint64_t strtox64_or_err(const char *str
, const char *errmesg
);
31 extern double strtod_or_err(const char *str
, const char *errmesg
);
33 extern long strtol_or_err(const char *str
, const char *errmesg
);
34 extern unsigned long strtoul_or_err(const char *str
, const char *errmesg
);
36 extern void strtotimeval_or_err(const char *str
, struct timeval
*tv
,
39 extern int isdigit_strend(const char *str
, const char **end
);
40 #define isdigit_string(_s) isdigit_strend(_s, NULL)
42 extern int isxdigit_strend(const char *str
, const char **end
);
43 #define isxdigit_string(_s) isxdigit_strend(_s, NULL)
46 extern int parse_switch(const char *arg
, const char *errmesg
, ...);
49 extern void *mempcpy(void *restrict dest
, const void *restrict src
, size_t n
);
52 extern size_t strnlen(const char *s
, size_t maxlen
);
55 extern char *strndup(const char *s
, size_t n
);
58 extern char *strnchr(const char *s
, size_t maxlen
, int c
);
61 /* caller guarantees n > 0 */
62 static inline void xstrncpy(char *dest
, const char *src
, size_t n
)
64 strncpy(dest
, src
, n
-1);
68 /* This is like strncpy(), but based on memcpy(), so compilers and static
69 * analyzers do not complain when sizeof(destination) is the same as 'n' and
70 * result is not terminated by zero.
72 * Use this function to copy string to logs with fixed sizes (wtmp/utmp. ...)
73 * where string terminator is optional.
75 static inline void *str2memcpy(void *dest
, const char *src
, size_t n
)
77 size_t bytes
= strlen(src
) + 1;
82 memcpy(dest
, src
, bytes
);
86 static inline char *mem2strcpy(char *dest
, const void *src
, size_t n
, size_t nmax
)
96 static inline int strdup_to_offset(void *stru
, size_t offset
, const char *str
)
104 o
= (char **) ((char *) stru
+ offset
);
116 #define strdup_to_struct_member(_s, _m, _str) \
117 strdup_to_offset((void *) _s, offsetof(__typeof__(*(_s)), _m), _str)
119 extern char *xstrmode(mode_t mode
, char *str
);
121 /* Options for size_to_human_string() */
124 SIZE_SUFFIX_1LETTER
= 0,
125 SIZE_SUFFIX_3LETTER
= (1 << 0),
126 SIZE_SUFFIX_SPACE
= (1 << 1),
127 SIZE_DECIMAL_2DIGITS
= (1 << 2)
130 extern char *size_to_human_string(int options
, uint64_t bytes
);
132 extern int string_to_idarray(const char *list
, int ary
[], size_t arysz
,
133 int (name2id
)(const char *, size_t));
134 extern int string_add_to_idarray(const char *list
, int ary
[],
135 size_t arysz
, size_t *ary_pos
,
136 int (name2id
)(const char *, size_t));
138 extern int string_to_bitarray(const char *list
, char *ary
,
139 int (*name2bit
)(const char *, size_t));
141 extern int string_to_bitmask(const char *list
,
143 long (*name2flag
)(const char *, size_t));
144 extern int parse_range(const char *str
, int *lower
, int *upper
, int def
);
146 extern int streq_paths(const char *a
, const char *b
);
149 * Match string beginning.
151 static inline const char *startswith(const char *s
, const char *prefix
)
153 size_t sz
= prefix
? strlen(prefix
) : 0;
155 if (s
&& sz
&& strncmp(s
, prefix
, sz
) == 0)
161 * Case insensitive match string beginning.
163 static inline const char *startswith_no_case(const char *s
, const char *prefix
)
165 size_t sz
= prefix
? strlen(prefix
) : 0;
167 if (s
&& sz
&& strncasecmp(s
, prefix
, sz
) == 0)
173 * Match string ending.
175 static inline const char *endswith(const char *s
, const char *postfix
)
177 size_t sl
= s
? strlen(s
) : 0;
178 size_t pl
= postfix
? strlen(postfix
) : 0;
184 if (memcmp(s
+ sl
- pl
, postfix
, pl
) != 0)
190 * Skip leading white space.
192 static inline const char *skip_space(const char *p
)
199 static inline const char *skip_blank(const char *p
)
207 /* Removes whitespace from the right-hand side of a string (trailing
210 * Returns size of the new string (without \0).
212 static inline size_t rtrim_whitespace(unsigned char *str
)
218 i
= strlen((char *) str
);
221 if (!isspace(str
[i
])) {
230 /* Removes whitespace from the left-hand side of a string.
232 * Returns size of the new string (without \0).
234 static inline size_t ltrim_whitespace(unsigned char *str
)
241 for (p
= str
; *p
&& isspace(*p
); p
++);
243 len
= strlen((char *) p
);
246 memmove(str
, p
, len
+ 1);
251 extern char *strnappend(const char *s
, const char *suffix
, size_t b
);
252 extern char *strappend(const char *s
, const char *suffix
);
253 extern char *strfappend(const char *s
, const char *format
, ...)
254 __attribute__ ((__format__ (__printf__
, 2, 0)));
255 extern const char *split(const char **state
, size_t *l
, const char *separator
, int quoted
);
257 extern int skip_fline(FILE *fp
);