blob: 00598cf622d97424bb61c7efe774d91b49c96d23 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001#ifndef UTIL_LINUX_STRUTILS
2#define UTIL_LINUX_STRUTILS
3
4#include <inttypes.h>
5#include <string.h>
6#include <sys/types.h>
7
8/* default strtoxx_or_err() exit code */
9#ifndef STRTOXX_EXIT_CODE
10# define STRTOXX_EXIT_CODE EXIT_FAILURE
11#endif
12
13
14extern int strtosize(const char *str, uintmax_t *res);
15extern uintmax_t strtosize_or_err(const char *str, const char *errmesg);
16
17extern int16_t strtos16_or_err(const char *str, const char *errmesg);
18extern uint16_t strtou16_or_err(const char *str, const char *errmesg);
19
20extern int32_t strtos32_or_err(const char *str, const char *errmesg);
21extern uint32_t strtou32_or_err(const char *str, const char *errmesg);
22
23extern int64_t strtos64_or_err(const char *str, const char *errmesg);
24extern uint64_t strtou64_or_err(const char *str, const char *errmesg);
25
26extern double strtod_or_err(const char *str, const char *errmesg);
27
28extern long strtol_or_err(const char *str, const char *errmesg);
29extern unsigned long strtoul_or_err(const char *str, const char *errmesg);
30
31#ifndef HAVE_MEMPCPY
32extern void *mempcpy(void *restrict dest, const void *restrict src, size_t n);
33#endif
34#ifndef HAVE_STRNLEN
35extern size_t strnlen(const char *s, size_t maxlen);
36#endif
37#ifndef HAVE_STRNDUP
38extern char *strndup(const char *s, size_t n);
39#endif
40#ifndef HAVE_STRNCHR
41extern char *strnchr(const char *s, size_t maxlen, int c);
42#endif
43
44/* caller guarantees n > 0 */
45static inline void xstrncpy(char *dest, const char *src, size_t n)
46{
47 strncpy(dest, src, n-1);
48 dest[n-1] = 0;
49}
50
51extern void strmode(mode_t mode, char *str);
52
53/* Options for size_to_human_string() */
54enum
55{
56 SIZE_SUFFIX_1LETTER = 0,
57 SIZE_SUFFIX_3LETTER = 1,
58 SIZE_SUFFIX_SPACE = 2
59};
60
61extern char *size_to_human_string(int options, uint64_t bytes);
62
63extern int string_to_idarray(const char *list, int ary[], size_t arysz,
64 int (name2id)(const char *, size_t));
65extern int string_add_to_idarray(const char *list, int ary[],
66 size_t arysz, int *ary_pos,
67 int (name2id)(const char *, size_t));
68
69extern int string_to_bitarray(const char *list, char *ary,
70 int (*name2bit)(const char *, size_t));
71
72extern int string_to_bitmask(const char *list,
73 unsigned long *mask,
74 long (*name2flag)(const char *, size_t));
75extern int parse_range(const char *str, int *lower, int *upper, int def);
76
77extern int streq_except_trailing_slash(const char *s1, const char *s2);
78
79#endif